src/widgets/colorselectdialog.cpp

Mon, 20 Jun 2022 21:14:17 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 20 Jun 2022 21:14:17 +0300
changeset 238
b8ad4c12d937
parent 205
1a4342d80de7
child 264
76a025db4948
permissions
-rw-r--r--

Fix pick scene rendering: we cannot use glReadPixels when using multisampling, so we need to render the pick scene to a separate frame buffer

94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include <QColorDialog>
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include <QTableView>
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
3 #include <QStyleFactory>
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 #include "colorselectdialog.h"
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 #include "ui_colorselectdialog.h"
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
6 #include "uiutilities.h"
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
8 ColorSelectDialog::ColorSelectDialog(const ColorTable& colorTable, QWidget *parent) :
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 QDialog{parent},
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 ui{*new Ui_ColorSelectDialog},
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 colorTable{colorTable}
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 this->ui.setupUi(this);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 this->makeColorButtons();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 connect(this->ui.filter, &QLineEdit::textEdited, this, &ColorSelectDialog::populateColors);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 this->updateSelectedColorTexts();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 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
18 connect(this->ui.directColorButton, &QPushButton::clicked, this, &ColorSelectDialog::chooseDirectColor);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 connect(this->ui.buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 connect(this->ui.buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 this->populateColors();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 }
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 ColorSelectDialog::~ColorSelectDialog()
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 delete &this->ui;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 }
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 ldraw::Color colorFromButton(QAbstractButton* button)
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 return {button->property("_colorIndex").value<qint32>()};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 }
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 void ColorSelectDialog::makeColorButtons()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 this->buttons.reserve(this->colorTable.size());
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 for (
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 auto iterator = std::begin(this->colorTable);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 iterator != std::end(this->colorTable);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 ++iterator
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 ) {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
42 const qint32 index = iterator->first.index;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
43 const ColorDefinition& colordef = iterator->second;
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 QPushButton* const button = new QPushButton{QString::number(index), this};
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
45 button->setMinimumSize({40, 40});
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 button->setToolTip(colordef.displayName);
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
47 const QColor face = colorFace({index}, colorTable).value_or(QColor{});
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
48 uiutilities::colorizeWidget(button, face);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 button->setProperty("_colorIndex", index);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 button->setCheckable(true);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 connect(button, &QAbstractButton::clicked, this, &ColorSelectDialog::handleButtonClick);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 this->buttons.push_back(button);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 std::sort(
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 std::begin(this->buttons),
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 std::end(this->buttons),
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 [](QAbstractButton* button_1, QAbstractButton* 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 return colorFromButton(button_1) < colorFromButton(button_2);
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 }
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 void ColorSelectDialog::populateColors()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 delete this->ui.colorFrame->layout();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 if (this->buttons.size() > 0)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 QGridLayout* gridLayout = new QGridLayout{};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 this->ui.colorFrame->setLayout(gridLayout);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 const int columnsPerRow = 8;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 int i = 0;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 for (
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 auto iterator = std::begin(this->buttons);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 iterator != std::end(this->buttons);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 ++iterator
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 ) {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 QPushButton* const button = *iterator;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 const bool filtered = this->filterColor(colorFromButton(button));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 button->setVisible(filtered);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 if (filtered)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 const int row = i / columnsPerRow;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 const int column = i % columnsPerRow;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 gridLayout->addWidget(button, row, column);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 i += 1;
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 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 void ColorSelectDialog::updateSelectedColorTexts()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
94 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
95 this->ui.selectedColorName->setText(displayName);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
96 const QColor face = colorFace(this->selectedColor, colorTable).value_or(QColor{});
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
97 uiutilities::colorizeWidget(this->ui.selectedColorName, face);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 this->ui.colorIndex->setValue(this->selectedColor.index);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 for (QPushButton* button : this->buttons)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 ldraw::Color buttonColor = colorFromButton(button);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 button->setChecked(buttonColor == this->selectedColor);
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 }
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 void ColorSelectDialog::handleButtonClick()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 QAbstractButton* button = qobject_cast<QAbstractButton*>(this->sender());
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 if (button != nullptr)
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 this->setCurrentColor(colorFromButton(button));
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 }
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 void ColorSelectDialog::spinboxEdited()
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 this->setCurrentColor(ldraw::Color{this->ui.colorIndex->value()});
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 }
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 void ColorSelectDialog::chooseDirectColor()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 QColorDialog dialog;
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
123 const std::optional<QColor> face = colorFace(this->selectedColor, this->colorTable);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
124 if (face.has_value()) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
125 dialog.setCurrentColor(*face);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
126 }
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 if (dialog.exec())
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
129 this->setCurrentColor(directColor(dialog.selectedColor()));
94
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 }
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 bool ColorSelectDialog::filterColor(ldraw::Color color) const
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 const QString& filterText = this->ui.filter->text();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 if (filterText.isEmpty())
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 return true;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
140 else
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
141 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
142 const std::optional<QString> name = colorDisplayName(color, colorTable);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
143 return name.value_or(QString{}).contains(filterText, Qt::CaseInsensitive);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
147 void ColorSelectDialog::setCurrentColor(ColorIndex color)
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 this->selectedColor = color;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
150 this->updateSelectedColorTexts();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151 }
178
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 ldraw::Color ColorSelectDialog::currentColor() const
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
154 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
155 return this->selectedColor;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
156 }

mercurial