src/widgets/colorselectdialog.cpp

changeset 205
1a4342d80de7
parent 178
a23024fc98e0
child 264
76a025db4948
equal deleted inserted replaced
204:52e10e8d88cc 205:1a4342d80de7
3 #include <QStyleFactory> 3 #include <QStyleFactory>
4 #include "colorselectdialog.h" 4 #include "colorselectdialog.h"
5 #include "ui_colorselectdialog.h" 5 #include "ui_colorselectdialog.h"
6 #include "uiutilities.h" 6 #include "uiutilities.h"
7 7
8 ColorSelectDialog::ColorSelectDialog(const ldraw::ColorTable& colorTable, QWidget *parent) : 8 ColorSelectDialog::ColorSelectDialog(const ColorTable& colorTable, QWidget *parent) :
9 QDialog{parent}, 9 QDialog{parent},
10 ui{*new Ui_ColorSelectDialog}, 10 ui{*new Ui_ColorSelectDialog},
11 colorTable{colorTable} 11 colorTable{colorTable}
12 { 12 {
13 this->ui.setupUi(this); 13 this->ui.setupUi(this);
37 for ( 37 for (
38 auto iterator = std::begin(this->colorTable); 38 auto iterator = std::begin(this->colorTable);
39 iterator != std::end(this->colorTable); 39 iterator != std::end(this->colorTable);
40 ++iterator 40 ++iterator
41 ) { 41 ) {
42 const qint32 index = iterator->first; 42 const qint32 index = iterator->first.index;
43 const ldraw::ColorDefinition& colordef = iterator->second; 43 const ColorDefinition& colordef = iterator->second;
44 QPushButton* const button = new QPushButton{QString::number(index), this}; 44 QPushButton* const button = new QPushButton{QString::number(index), this};
45 button->setMinimumSize({40, 40}); 45 button->setMinimumSize({40, 40});
46 button->setToolTip(colordef.displayName); 46 button->setToolTip(colordef.displayName);
47 uiutilities::colorizeWidget(button, ldraw::colorFace({index}, colorTable)); 47 const QColor face = colorFace({index}, colorTable).value_or(QColor{});
48 uiutilities::colorizeWidget(button, face);
48 button->setProperty("_colorIndex", index); 49 button->setProperty("_colorIndex", index);
49 button->setCheckable(true); 50 button->setCheckable(true);
50 connect(button, &QAbstractButton::clicked, this, &ColorSelectDialog::handleButtonClick); 51 connect(button, &QAbstractButton::clicked, this, &ColorSelectDialog::handleButtonClick);
51 this->buttons.push_back(button); 52 this->buttons.push_back(button);
52 } 53 }
88 } 89 }
89 } 90 }
90 91
91 void ColorSelectDialog::updateSelectedColorTexts() 92 void ColorSelectDialog::updateSelectedColorTexts()
92 { 93 {
93 this->ui.selectedColorName->setText(ldraw::colorDisplayName(this->selectedColor, this->colorTable)); 94 const QString displayName = colorDisplayName(this->selectedColor, this->colorTable).value_or(QStringLiteral("???"));
94 uiutilities::colorizeWidget(this->ui.selectedColorName, ldraw::colorFace(this->selectedColor, colorTable)); 95 this->ui.selectedColorName->setText(displayName);
96 const QColor face = colorFace(this->selectedColor, colorTable).value_or(QColor{});
97 uiutilities::colorizeWidget(this->ui.selectedColorName, face);
95 this->ui.colorIndex->setValue(this->selectedColor.index); 98 this->ui.colorIndex->setValue(this->selectedColor.index);
96 for (QPushButton* button : this->buttons) 99 for (QPushButton* button : this->buttons)
97 { 100 {
98 ldraw::Color buttonColor = colorFromButton(button); 101 ldraw::Color buttonColor = colorFromButton(button);
99 button->setChecked(buttonColor == this->selectedColor); 102 button->setChecked(buttonColor == this->selectedColor);
115 } 118 }
116 119
117 void ColorSelectDialog::chooseDirectColor() 120 void ColorSelectDialog::chooseDirectColor()
118 { 121 {
119 QColorDialog dialog; 122 QColorDialog dialog;
120 dialog.setCurrentColor(ldraw::colorFace(this->selectedColor, this->colorTable)); 123 const std::optional<QColor> face = colorFace(this->selectedColor, this->colorTable);
124 if (face.has_value()) {
125 dialog.setCurrentColor(*face);
126 }
121 if (dialog.exec()) 127 if (dialog.exec())
122 { 128 {
123 this->setCurrentColor(ldraw::directColor(dialog.selectedColor())); 129 this->setCurrentColor(directColor(dialog.selectedColor()));
124 } 130 }
125 } 131 }
126 132
127 bool ColorSelectDialog::filterColor(ldraw::Color color) const 133 bool ColorSelectDialog::filterColor(ldraw::Color color) const
128 { 134 {
131 { 137 {
132 return true; 138 return true;
133 } 139 }
134 else 140 else
135 { 141 {
136 const ldraw::ColorDefinition& colordef = this->colorTable[color]; 142 const std::optional<QString> name = colorDisplayName(color, colorTable);
137 return colordef.displayName.contains(filterText, Qt::CaseInsensitive); 143 return name.value_or(QString{}).contains(filterText, Qt::CaseInsensitive);
138 } 144 }
139 } 145 }
140 146
141 void ColorSelectDialog::setCurrentColor(ldraw::Color color) 147 void ColorSelectDialog::setCurrentColor(ColorIndex color)
142 { 148 {
143 this->selectedColor = color; 149 this->selectedColor = color;
144 this->updateSelectedColorTexts(); 150 this->updateSelectedColorTexts();
145 } 151 }
146 152

mercurial