LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
linewidthselect.cpp
Go to the documentation of this file.
1 #include <cad/meta/layer.h>
2 #include "linewidthselect.h"
3 
4 using namespace lc;
5 using namespace ui;
6 
7 LineWidthSelect::LineWidthSelect(lc::ui::MetaInfoManager_SPtr metaInfoManager, QWidget* parent, bool showByLayer, bool showByBlock) :
8  QComboBox(parent) {
9 
10  qIconSize = QSize(128, 32);
11  setIconSize(qIconSize);
12 
13  setMaximumHeight(32);
14 
15  if(showByLayer) {
16  addItem(BY_LAYER);
17  }
18  if(showByBlock) {
19  addItem(BY_BLOCK);
20  }
21 
22  values["0.00mm"] = 0.00;
23  values["0.05mm"] = 0.05;
24  values["0.09mm"] = 0.09;
25  values["0.13mm (ISO)"] = 0.13;
26  values["0.15mm"] = 0.15;
27  values["0.18mm (ISO)"] = 0.18;
28  values["0.20mm"] = 0.20;
29  values["0.25mm (ISO)"] = 0.25;
30  values["0.30mm"] = 0.30;
31  values["0.35mm (ISO)"] = 0.35;
32  values["0.40mm"] = 0.40;
33  values["0.50mm (ISO)"] = 0.50;
34  values["0.53mm"] = 0.53;
35  values["0.60mm"] = 0.60;
36  values["0.70mm (ISO)"] = 0.70;
37  values["0.80mm"] = 0.80;
38  values["0.90mm"] = 0.90;
39  values["1.00mm (ISO)"] = 1.00;
40  values["1.06mm"] = 1.06;
41  values["1.20mm"] = 1.20;
42  values["1.40mm (ISO)"] = 1.40;
43  values["1.58mm"] = 1.58;
44  values["2.00mm (ISO)"] = 2.00;
45  values["2.11mm"] = 2.11;
46 
47  createEntries();
48 
49  setMetaInfoManager(metaInfoManager);
50 
51  connect(this, SIGNAL(activated(const QString&)), this, SLOT(onActivated(const QString&)));
52 }
53 
55  for(auto v : values) {
56  QPixmap pixmap(qIconSize);
57 
58  LinePatternPainter painter(&pixmap, v.second);
59  painter.drawLinePattern();
60 
61  addItem(QIcon(pixmap), v.first);
62  }
63 }
64 
65 void LineWidthSelect::onLayerChanged(lc::Layer_CSPtr layer) {
66  auto index = findText(BY_LAYER);
67 
68  if(index != -1) {
69  QPixmap pixmap(qIconSize);
70 
71  LinePatternPainter painter(&pixmap, layer->lineWidth().width());
72  painter.drawLinePattern();
73 
74  setItemIcon(index, QIcon(pixmap));
76  }
77 }
78 
79 void LineWidthSelect::setWidth(lc::MetaLineWidth_CSPtr lineWidth) {
80  if(lineWidth == nullptr) {
81  setCurrentText(BY_LAYER);
83  return;
84  }
85 
86  auto byValue = std::dynamic_pointer_cast<const lc::MetaLineWidthByValue>(lineWidth);
87  if(byValue != nullptr) {
88  for (auto v : values) {
89  if (v.second == byValue->width()) {
90  setCurrentText(v.first);
92  break;
93  }
94  }
95 
96  return;
97  }
98 
99  auto byBlock = std::dynamic_pointer_cast<const lc::MetaLineWidthByBlock>(lineWidth);
100  if(byBlock != nullptr) {
101  setCurrentText(BY_BLOCK);
103  return;
104  }
105 }
106 
107 void LineWidthSelect::setMetaInfoManager(lc::ui::MetaInfoManager_SPtr metaInfoManager) {
108  _metaInfoManager = metaInfoManager;
109 
110  if(metaInfoManager != nullptr && metaInfoManager->lineWidth() != nullptr) {
111  setWidth(metaInfoManager->lineWidth());
112  }
113 }
114 
116  if(!_metaInfoManager) {
117  return;
118  }
119 
120  if(currentText() == BY_LAYER) {
121  _metaInfoManager->setLineWidth(nullptr);
122  return;
123  }
124 
125  if(currentText() == BY_BLOCK) {
126  _metaInfoManager->setLineWidth(std::make_shared<const lc::MetaLineWidthByBlock>());
127  return;
128  }
129 
130  try {
131  _metaInfoManager->setLineWidth(std::make_shared<lc::MetaLineWidthByValue>(values.at(currentText())));
132  }
133  catch (std::out_of_range& e) {
134  _metaInfoManager->setLineWidth(nullptr);
135  }
136 }
137 
138 lc::MetaLineWidth_CSPtr LineWidthSelect::lineWidth() {
139  if(currentText() == BY_LAYER) {
140  return nullptr;
141  }
142 
143  if(currentText() == BY_BLOCK) {
144  return std::make_shared<const lc::MetaLineWidthByBlock>();
145  }
146 
147  try {
148  return std::make_shared<lc::MetaLineWidthByValue>(values.at(currentText()));
149  }
150  catch (std::out_of_range& e) {
151  return nullptr;
152  }
153 }
154 
155 void LineWidthSelect::onActivated(const QString& text) {
157 }
lc::ui::MetaInfoManager_SPtr _metaInfoManager
lc::MetaLineWidth_CSPtr lineWidth()
Return selected line width.
#define BY_BLOCK
Definition: colorselect.h:12
void onActivated(const QString &text)
#define BY_LAYER
Definition: colorselect.h:13
void setWidth(lc::MetaLineWidth_CSPtr lineWidth)
Select a new width.
Painting line patterns on a QPaintDevice.
void onLayerChanged(lc::Layer_CSPtr layer)
Event when a new layer is selected.
std::map< QString, double > values
LineWidthSelect(lc::ui::MetaInfoManager_SPtr metaInfoManager, QWidget *parent=0, bool showByLayer=false, bool showByBlock=false)
Create widget.
void setMetaInfoManager(lc::ui::MetaInfoManager_SPtr metaInfoManager)
Set the MetaInfo manager.
void drawLinePattern()
Paint line.