src/widgets/colorselectdialog.cpp

Tue, 28 Jun 2022 14:53:22 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Tue, 28 Jun 2022 14:53:22 +0300
changeset 289
a0ddbc9a4e77
parent 264
76a025db4948
permissions
-rw-r--r--

Work around a Qt bug involving the rendering behavior of the first created sub window

94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include <QColorDialog>
264
76a025db4948 Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
2 #include <ui_colorselectdialog.h>
76a025db4948 Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
3 #include "src/widgets/colorselectdialog.h"
76a025db4948 Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
4 #include "src/uiutilities.h"
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
6 ColorSelectDialog::ColorSelectDialog(const ColorTable& colorTable, QWidget *parent) :
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 QDialog{parent},
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 ui{*new Ui_ColorSelectDialog},
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 colorTable{colorTable}
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 this->ui.setupUi(this);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 this->makeColorButtons();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 connect(this->ui.filter, &QLineEdit::textEdited, this, &ColorSelectDialog::populateColors);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 this->updateSelectedColorTexts();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 connect(this->ui.colorIndex, qOverload<int>(&QSpinBox::valueChanged), this, &ColorSelectDialog::spinboxEdited);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 connect(this->ui.directColorButton, &QPushButton::clicked, this, &ColorSelectDialog::chooseDirectColor);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 connect(this->ui.buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 connect(this->ui.buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 this->populateColors();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 ColorSelectDialog::~ColorSelectDialog()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 delete &this->ui;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 ldraw::Color colorFromButton(QAbstractButton* button)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 return {button->property("_colorIndex").value<qint32>()};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 void ColorSelectDialog::makeColorButtons()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 this->buttons.reserve(this->colorTable.size());
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 for (
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 auto iterator = std::begin(this->colorTable);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 iterator != std::end(this->colorTable);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 ++iterator
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 ) {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
40 const qint32 index = iterator->first.index;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
41 const ColorDefinition& colordef = iterator->second;
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 QPushButton* const button = new QPushButton{QString::number(index), this};
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
43 button->setMinimumSize({40, 40});
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 button->setToolTip(colordef.displayName);
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
45 const QColor face = colorFace({index}, colorTable).value_or(QColor{});
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
46 uiutilities::colorizeWidget(button, face);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 button->setProperty("_colorIndex", index);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 button->setCheckable(true);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 connect(button, &QAbstractButton::clicked, this, &ColorSelectDialog::handleButtonClick);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 this->buttons.push_back(button);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 std::sort(
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 std::begin(this->buttons),
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 std::end(this->buttons),
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 [](QAbstractButton* button_1, QAbstractButton* button_2)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 return colorFromButton(button_1) < colorFromButton(button_2);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 );
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 void ColorSelectDialog::populateColors()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 delete this->ui.colorFrame->layout();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 if (this->buttons.size() > 0)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 QGridLayout* gridLayout = new QGridLayout{};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 this->ui.colorFrame->setLayout(gridLayout);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 const int columnsPerRow = 8;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 int i = 0;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 for (
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 auto iterator = std::begin(this->buttons);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 iterator != std::end(this->buttons);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 ++iterator
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 ) {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 QPushButton* const button = *iterator;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 const bool filtered = this->filterColor(colorFromButton(button));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 button->setVisible(filtered);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 if (filtered)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 const int row = i / columnsPerRow;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 const int column = i % columnsPerRow;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 gridLayout->addWidget(button, row, column);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 i += 1;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 void ColorSelectDialog::updateSelectedColorTexts()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
92 const QString displayName = colorDisplayName(this->selectedColor, this->colorTable).value_or(QStringLiteral("???"));
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
93 this->ui.selectedColorName->setText(displayName);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
94 const QColor face = colorFace(this->selectedColor, colorTable).value_or(QColor{});
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
95 uiutilities::colorizeWidget(this->ui.selectedColorName, face);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 this->ui.colorIndex->setValue(this->selectedColor.index);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 for (QPushButton* button : this->buttons)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 ldraw::Color buttonColor = colorFromButton(button);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 button->setChecked(buttonColor == this->selectedColor);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 void ColorSelectDialog::handleButtonClick()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 QAbstractButton* button = qobject_cast<QAbstractButton*>(this->sender());
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 if (button != nullptr)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 this->setCurrentColor(colorFromButton(button));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 void ColorSelectDialog::spinboxEdited()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 this->setCurrentColor(ldraw::Color{this->ui.colorIndex->value()});
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 void ColorSelectDialog::chooseDirectColor()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 QColorDialog dialog;
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
121 const std::optional<QColor> face = colorFace(this->selectedColor, this->colorTable);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
122 if (face.has_value()) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
123 dialog.setCurrentColor(*face);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
124 }
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 if (dialog.exec())
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
127 this->setCurrentColor(directColor(dialog.selectedColor()));
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131 bool ColorSelectDialog::filterColor(ldraw::Color color) const
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133 const QString& filterText = this->ui.filter->text();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 if (filterText.isEmpty())
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 return true;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 else
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
140 const std::optional<QString> name = colorDisplayName(color, colorTable);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
141 return name.value_or(QString{}).contains(filterText, Qt::CaseInsensitive);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
142 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
143 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
145 void ColorSelectDialog::setCurrentColor(ColorIndex color)
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147 this->selectedColor = color;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 this->updateSelectedColorTexts();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
150
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
151 ldraw::Color ColorSelectDialog::currentColor() const
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
152 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
153 return this->selectedColor;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
154 }

mercurial