LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
colorselect.cpp
Go to the documentation of this file.
1 #include "colorselect.h"
2 
3 using namespace lc;
4 using namespace ui;
5 
6 ColorSelect::ColorSelect(lc::ui::MetaInfoManager_SPtr metaInfoManager, QWidget *parent, bool showByLayer, bool showByBlock) :
7  QComboBox(parent) {
8 
9  qIconSize = QSize(32, 32);
10  setIconSize(qIconSize);
11 
12  setMaximumHeight(32);
13 
14  if(showByLayer) {
15  addItem(BY_LAYER);
16  }
17  if(showByBlock) {
18  addItem(BY_BLOCK);
19  }
20 
21  addItem(CUSTOM);
22  setColor(Color(255, 255, 255));
23 
24  insertSeparator(count());
25 
26  for(auto color : QColor::colorNames()){
27  QPixmap pixmap(qIconSize);
28  pixmap.fill(color);
29  addItem(QIcon(pixmap), color);
30  }
31 
32  connect(this, SIGNAL(activated(const QString&)), this, SLOT(onActivated(const QString&)));
33 
34  setMetaInfoManager(metaInfoManager);
35 }
36 
37 void ColorSelect::onActivated(const QString& text) {
38  if(text == CUSTOM) {
39  auto colorDialog = new QColorDialog(this);
40 
41  if(_customColor.isValid()) {
42  colorDialog->setCurrentColor(_customColor);
43  }
44  colorDialog->show();
45 
46  connect(colorDialog, &QColorDialog::colorSelected, this, &ColorSelect::on_customColorChanged);
47  }
48  else {
50  }
51 }
52 
53 void ColorSelect::on_customColorChanged(const QColor &color) {
54  QPixmap pixmap(qIconSize);
55 
56  auto index = findText(CUSTOM);
57  pixmap.fill(color);
58  setItemIcon(index, QIcon(pixmap));
59 
61 
63 }
64 
65 void ColorSelect::onLayerChanged(lc::Layer_CSPtr layer) {
66  auto index = findText(BY_LAYER);
67 
68  if(index != -1) {
69  QColor color(layer->color().redI(), layer->color().greenI(), layer->color().blueI(), layer->color().alphaI());
70  QPixmap pixmap(qIconSize);
71  pixmap.fill(color);
72  setItemIcon(index, QIcon(pixmap));
73 
75  }
76 }
77 
78 void ColorSelect::setColor(lc::Color color) {
79  QColor qColor(color.redI(), color.greenI(), color.blueI(), color.alphaI());
80  setCurrentText(CUSTOM);
81 
82  on_customColorChanged(qColor);
84 }
85 
86 void ColorSelect::setMetaInfoManager(lc::ui::MetaInfoManager_SPtr metaInfoManager) {
87  _metaInfoManager = metaInfoManager;
88 
89  if(metaInfoManager != nullptr) {
90  if(metaInfoManager->color() == nullptr) {
91  setCurrentText(BY_LAYER);
92  }
93  else if(std::dynamic_pointer_cast<const lc::MetaColorByBlock>(metaInfoManager->color())) {
94  setCurrentText(BY_BLOCK);
95  }
96  else {
97  auto colorByValue = std::dynamic_pointer_cast<const lc::MetaColorByValue>(metaInfoManager->color());
98  if(colorByValue != nullptr) {
99  setColor(colorByValue->color());
100  }
101  }
102  }
103 }
104 
106  if(!_metaInfoManager) {
107  return;
108  }
109 
110  _metaInfoManager->setColor(metaColor());
111 }
112 
113 lc::MetaColor_CSPtr ColorSelect::metaColor() {
114  if(currentText() == BY_LAYER) {
115  return nullptr;
116  }
117  if(currentText() == BY_BLOCK) {
118  return std::make_shared<lc::MetaColorByBlock>();
119  }
120 
121  return std::make_shared<const lc::MetaColorByValue>(color());
122 }
123 
124 lc::Color ColorSelect::color() {
125  if(currentText() == BY_LAYER || currentText() == BY_BLOCK) {
126  throw "Color can't be returned if ByLayer or ByBlock is selected.";
127  }
128 
129  QColor color;
130 
131  if(currentText() == CUSTOM) {
132  color = _customColor;
133  }
134  else {
135  color = QColor(currentText());
136  }
137 
138  if(!color.isValid()) {
139  throw "The selected color is invalid.";
140  }
141 
142  return lc::Color(color.red(), color.green(), color.blue(), color.alpha());
143 }
lc::Color color()
Returns selected color.
void onActivated(const QString &text)
Definition: colorselect.cpp:37
void onLayerChanged(lc::Layer_CSPtr layer)
Event when a new layer is selected.
Definition: colorselect.cpp:65
#define BY_BLOCK
Definition: colorselect.h:12
#define BY_LAYER
Definition: colorselect.h:13
lc::ui::MetaInfoManager_SPtr _metaInfoManager
Definition: colorselect.h:78
void setColor(lc::Color color)
Set selected color.
Definition: colorselect.cpp:78
void on_customColorChanged(const QColor &color)
Definition: colorselect.cpp:53
lc::MetaColor_CSPtr metaColor()
Returns selected color.
void setMetaInfoManager(lc::ui::MetaInfoManager_SPtr metaInfoManager=nullptr)
Set the MetaInfo manager.
Definition: colorselect.cpp:86
#define CUSTOM
Definition: colorselect.h:14
ColorSelect(lc::ui::MetaInfoManager_SPtr metaInfoManager=nullptr, QWidget *parent=0, bool showByLayer=false, bool showByBlock=false)
Create widget.
Definition: colorselect.cpp:6