LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
linepatternselect.cpp
Go to the documentation of this file.
1 #include "linepatternselect.h"
2 
3 using namespace lc;
4 using namespace ui;
5 
6 LinePatternSelect::LinePatternSelect(lc::Document_SPtr document, QWidget *parent, bool showByLayer, bool showByBlock) :
7  QComboBox(parent),
8  _showByLayer(showByLayer),
9  _showByBlock(showByBlock) {
10 
11  qIconSize = QSize(128, 32);
12  setIconSize(qIconSize);
13 
14  setMaximumHeight(32);
15 
16  setDocument(document);
17 
18  connect(this, SIGNAL(activated(const QString&)), this, SLOT(onActivated(const QString&)));
19 }
20 
21 LinePatternSelect::LinePatternSelect(CadMdiChild* mdiChild, QWidget* parent, bool showByLayer, bool showByBlock) :
22  LinePatternSelect((lc::Document_SPtr) nullptr, parent, showByLayer, showByBlock) {
23 
24  setMdiChild(mdiChild);
25 }
26 
28  if(mdiChild == nullptr) {
29  setDocument(nullptr);
30  }
31  else {
32  setDocument(mdiChild->document());
33 
34  _metaInfoManager = mdiChild->metaInfoManager();
35  }
36 }
37 
38 void LinePatternSelect::setDocument(lc::Document_SPtr document) {
39  if(_document != nullptr) {
40  _document->addLinePatternEvent().disconnect<LinePatternSelect, &LinePatternSelect::on_addLinePatternEvent>(this);
41  _document->removeLinePatternEvent().disconnect<LinePatternSelect, &LinePatternSelect::on_removeLinePatternEvent>(this);
42  _document->replaceLinePatternEvent().disconnect<LinePatternSelect, &LinePatternSelect::on_replaceLinePatternEvent>(this);
43  }
44 
45  _document = document;
46 
47  if(_document != nullptr) {
48  _document->addLinePatternEvent().connect<LinePatternSelect, &LinePatternSelect::on_addLinePatternEvent>(this);
49  _document->removeLinePatternEvent().connect<LinePatternSelect, &LinePatternSelect::on_removeLinePatternEvent>(this);
50  _document->replaceLinePatternEvent().connect<LinePatternSelect, &LinePatternSelect::on_replaceLinePatternEvent>(this);
51  }
52 
53  createEntries();
54 }
55 
56 lc::DxfLinePattern_CSPtr LinePatternSelect::linePattern() {
57  if(currentText() == BY_LAYER) {
58  return nullptr;
59  }
60 
61  if(currentText() == BY_BLOCK) {
62  return std::make_shared<const lc::DxfLinePatternByBlock>();
63  }
64 
65  auto linePatterns = _document->linePatterns();
66  auto position = std::find_if(linePatterns.begin(), linePatterns.end(), [&](const lc::DxfLinePattern_CSPtr& item) {
67  return item->name() == currentText().toStdString();
68  });
69 
70  if(position != linePatterns.end()) {
71  return *position;
72  }
73  else {
74  return nullptr;
75  }
76 }
77 
78 QIcon LinePatternSelect::generateQIcon(lc::DxfLinePatternByValue_CSPtr linePattern) {
79  QPixmap pixmap(qIconSize);
80 
81  LinePatternPainter painter(&pixmap, linePattern);
82  painter.drawLinePattern();
83 
84  return QIcon(pixmap);
85 }
86 
87 void LinePatternSelect::onActivated(const QString& text) {
88  if(text.toStdString() == NEW_LP) {
89  auto dialog = new AddLinePatternDialog(_document, this);
90  dialog->show();
91 
92  if(_showByLayer) {
93  setCurrentText(BY_LAYER);
94  }
95  }
96  else if(text == MANAGE_LP) {
97  auto dialog = new LinePatternManager(_document, this);
98  dialog->show();
99 
100  if(_showByLayer) {
101  setCurrentText(BY_LAYER);
102  }
103  }
104  else if(_metaInfoManager != nullptr) {
105  _metaInfoManager->setLinePattern(linePattern());
106  }
107 }
108 
110  clear();
111 
112  if(_document != nullptr) {
113  addItem(NEW_LP);
114  addItem(MANAGE_LP);
115  insertSeparator(2);
116 
117  if(_showByLayer) {
118  addItem(BY_LAYER);
119  setCurrentText(BY_LAYER);
120  }
121  if(_showByBlock) {
122  addItem(BY_BLOCK);
123  }
124 
125  auto linePatterns = _document->linePatterns();
126  for (auto linePattern : linePatterns) {
127  auto lp = std::dynamic_pointer_cast<const lc::DxfLinePatternByValue>(linePattern);
128 
129  if(lp == nullptr) {
130  continue;
131  }
132 
133  auto icon = generateQIcon(lp);
134  addItem(icon, linePattern->name().c_str());
135  }
136  }
137 }
138 
139 void LinePatternSelect::on_addLinePatternEvent(const lc::AddLinePatternEvent& event) {
140  createEntries();
141 
142  setCurrentText(event.linePattern()->name().c_str());
143 }
144 
145 void LinePatternSelect::on_removeLinePatternEvent(const lc::RemoveLinePatternEvent& event) {
146  createEntries();
147 }
148 
149 void LinePatternSelect::on_replaceLinePatternEvent(const lc::ReplaceLinePatternEvent& event) {
150  createEntries();
151 
152  setCurrentText(event.newLinePattern()->name().c_str());
153 }
154 
155 void LinePatternSelect::onLayerChanged(lc::Layer_CSPtr layer) {
156  auto index = findText(BY_LAYER);
157 
158  if(index != -1) {
159  auto icon = generateQIcon(layer->linePattern());
160  setItemIcon(index, icon);
161  }
162 }
void on_removeLinePatternEvent(const lc::RemoveLinePatternEvent &)
lc::ui::MetaInfoManager_SPtr metaInfoManager() const
Get the MetaInfo manager.
#define MANAGE_LP
void onLayerChanged(lc::Layer_CSPtr layer)
Event when a new layer is selected.
void setMdiChild(CadMdiChild *mdiChild=nullptr)
Set the window on which line pattern is applied.
void setDocument(lc::Document_SPtr document=nullptr)
Change document.
#define BY_BLOCK
Definition: colorselect.h:12
void onActivated(const QString &text)
#define BY_LAYER
Definition: colorselect.h:13
Create a new window which shows a list of line patterns.
LinePatternSelect(lc::Document_SPtr document=nullptr, QWidget *parent=0, bool showByLayer=false, bool showByBlock=false)
Create widget.
Create a dialog with all required fields to create a new line pattern.
void on_replaceLinePatternEvent(const lc::ReplaceLinePatternEvent &)
lc::Document_SPtr _document
Painting line patterns on a QPaintDevice.
lc::ui::MetaInfoManager_SPtr _metaInfoManager
QIcon generateQIcon(lc::DxfLinePatternByValue_CSPtr linePattern)
#define NEW_LP
lc::DxfLinePattern_CSPtr linePattern()
Return selected line pattern.
Dropdown select for DxfLinePatterns.
std::shared_ptr< lc::Document > document() const
void on_addLinePatternEvent(const lc::AddLinePatternEvent &)
void drawLinePattern()
Paint line.