1 #include <QColorDialog> |
1 #include <QColorDialog> |
2 #include <QTableView> |
2 #include <QTableView> |
|
3 #include <QStyleFactory> |
3 #include "colorselectdialog.h" |
4 #include "colorselectdialog.h" |
4 #include "ui_colorselectdialog.h" |
5 #include "ui_colorselectdialog.h" |
|
6 #include "uiutilities.h" |
5 |
7 |
6 ColorSelectDialog::ColorSelectDialog(const ldraw::ColorTable& colorTable, QWidget *parent) : |
8 ColorSelectDialog::ColorSelectDialog(const ldraw::ColorTable& colorTable, QWidget *parent) : |
7 QDialog{parent}, |
9 QDialog{parent}, |
8 ui{*new Ui_ColorSelectDialog}, |
10 ui{*new Ui_ColorSelectDialog}, |
9 colorTable{colorTable} |
11 colorTable{colorTable} |
27 ldraw::Color colorFromButton(QAbstractButton* button) |
29 ldraw::Color colorFromButton(QAbstractButton* button) |
28 { |
30 { |
29 return {button->property("_colorIndex").value<qint32>()}; |
31 return {button->property("_colorIndex").value<qint32>()}; |
30 } |
32 } |
31 |
33 |
32 QString styleSheetForColor(const QColor& color) |
|
33 { |
|
34 QColor const textColor = (luma(color) < 0.4) ? Qt::white : Qt::black; |
|
35 return QString{"background-color: %1; color: %2;"} |
|
36 .arg(color.name()) |
|
37 .arg(textColor.name()); |
|
38 } |
|
39 |
|
40 void ColorSelectDialog::makeColorButtons() |
34 void ColorSelectDialog::makeColorButtons() |
41 { |
35 { |
42 this->buttons.reserve(this->colorTable.size()); |
36 this->buttons.reserve(this->colorTable.size()); |
43 for ( |
37 for ( |
44 auto iterator = std::begin(this->colorTable); |
38 auto iterator = std::begin(this->colorTable); |
46 ++iterator |
40 ++iterator |
47 ) { |
41 ) { |
48 const qint32 index = iterator->first; |
42 const qint32 index = iterator->first; |
49 const ldraw::ColorDefinition& colordef = iterator->second; |
43 const ldraw::ColorDefinition& colordef = iterator->second; |
50 QPushButton* const button = new QPushButton{QString::number(index), this}; |
44 QPushButton* const button = new QPushButton{QString::number(index), this}; |
|
45 button->setMinimumSize({40, 40}); |
51 button->setToolTip(colordef.displayName); |
46 button->setToolTip(colordef.displayName); |
52 button->setStyleSheet(styleSheetForColor(colordef.faceColor)); |
47 uiutilities::colorizeWidget(button, ldraw::colorFace({index}, colorTable)); |
53 button->setProperty("_colorIndex", index); |
48 button->setProperty("_colorIndex", index); |
54 button->setCheckable(true); |
49 button->setCheckable(true); |
55 connect(button, &QAbstractButton::clicked, this, &ColorSelectDialog::handleButtonClick); |
50 connect(button, &QAbstractButton::clicked, this, &ColorSelectDialog::handleButtonClick); |
56 this->buttons.push_back(button); |
51 this->buttons.push_back(button); |
57 } |
52 } |
93 } |
88 } |
94 } |
89 } |
95 |
90 |
96 void ColorSelectDialog::updateSelectedColorTexts() |
91 void ColorSelectDialog::updateSelectedColorTexts() |
97 { |
92 { |
98 if (ldraw::isDirectColor(this->selectedColor)) |
93 this->ui.selectedColorName->setText(ldraw::colorDisplayName(this->selectedColor, this->colorTable)); |
99 { |
94 uiutilities::colorizeWidget(this->ui.selectedColorName, ldraw::colorFace(this->selectedColor, colorTable)); |
100 this->ui.selectedColorName->setText(ldraw::directColorFace(this->selectedColor).name()); |
|
101 } |
|
102 else |
|
103 { |
|
104 const ldraw::ColorDefinition& colordef = this->colorTable[this->selectedColor]; |
|
105 this->ui.selectedColorName->setText(colordef.displayName); |
|
106 } |
|
107 const QColor colorFace = ldraw::colorFace(this->selectedColor, this->colorTable); |
|
108 this->ui.selectedColorName->setStyleSheet(styleSheetForColor(colorFace)); |
|
109 this->ui.colorIndex->setValue(this->selectedColor.index); |
95 this->ui.colorIndex->setValue(this->selectedColor.index); |
110 for (QPushButton* button : this->buttons) |
96 for (QPushButton* button : this->buttons) |
111 { |
97 { |
112 ldraw::Color buttonColor = colorFromButton(button); |
98 ldraw::Color buttonColor = colorFromButton(button); |
113 button->setChecked(buttonColor == this->selectedColor); |
99 button->setChecked(buttonColor == this->selectedColor); |