src/widgets/colorselectdialog.cpp

Wed, 25 May 2022 20:36:34 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 20:36:34 +0300
changeset 199
6988973515d2
parent 178
a23024fc98e0
child 205
1a4342d80de7
permissions
-rw-r--r--

Fix pick() picking from weird places on the screen with high DPI scaling

glReadPixels reads data from the frame buffer, which contains data after
high DPI scaling, so any reads to that need to take this scaling into account

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
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 ColorSelectDialog::ColorSelectDialog(const ldraw::ColorTable& colorTable, QWidget *parent) :
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 ) {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 const qint32 index = iterator->first;
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
43 const ldraw::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);
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
47 uiutilities::colorizeWidget(button, ldraw::colorFace({index}, colorTable));
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 button->setProperty("_colorIndex", index);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 button->setCheckable(true);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 connect(button, &QAbstractButton::clicked, this, &ColorSelectDialog::handleButtonClick);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 this->buttons.push_back(button);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 std::sort(
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 std::begin(this->buttons),
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 std::end(this->buttons),
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 [](QAbstractButton* button_1, QAbstractButton* button_2)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 return colorFromButton(button_1) < colorFromButton(button_2);
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
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 void ColorSelectDialog::populateColors()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 delete this->ui.colorFrame->layout();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 if (this->buttons.size() > 0)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 QGridLayout* gridLayout = new QGridLayout{};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 this->ui.colorFrame->setLayout(gridLayout);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 const int columnsPerRow = 8;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 int i = 0;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 for (
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 auto iterator = std::begin(this->buttons);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 iterator != std::end(this->buttons);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 ++iterator
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 ) {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 QPushButton* const button = *iterator;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 const bool filtered = this->filterColor(colorFromButton(button));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 button->setVisible(filtered);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 if (filtered)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 const int row = i / columnsPerRow;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 const int column = i % columnsPerRow;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 gridLayout->addWidget(button, row, column);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 i += 1;
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
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 void ColorSelectDialog::updateSelectedColorTexts()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 {
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
93 this->ui.selectedColorName->setText(ldraw::colorDisplayName(this->selectedColor, this->colorTable));
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
94 uiutilities::colorizeWidget(this->ui.selectedColorName, ldraw::colorFace(this->selectedColor, colorTable));
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 this->ui.colorIndex->setValue(this->selectedColor.index);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 for (QPushButton* button : this->buttons)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 ldraw::Color buttonColor = colorFromButton(button);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 button->setChecked(buttonColor == this->selectedColor);
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 }
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 void ColorSelectDialog::handleButtonClick()
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 QAbstractButton* button = qobject_cast<QAbstractButton*>(this->sender());
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 if (button != nullptr)
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 this->setCurrentColor(colorFromButton(button));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 }
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 void ColorSelectDialog::spinboxEdited()
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 this->setCurrentColor(ldraw::Color{this->ui.colorIndex->value()});
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 }
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 void ColorSelectDialog::chooseDirectColor()
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 QColorDialog dialog;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 dialog.setCurrentColor(ldraw::colorFace(this->selectedColor, this->colorTable));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121 if (dialog.exec())
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 this->setCurrentColor(ldraw::directColor(dialog.selectedColor()));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 bool ColorSelectDialog::filterColor(ldraw::Color color) const
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 const QString& filterText = this->ui.filter->text();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130 if (filterText.isEmpty())
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 return true;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 else
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 {
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
136 const ldraw::ColorDefinition& colordef = this->colorTable[color];
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 return colordef.displayName.contains(filterText, Qt::CaseInsensitive);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 }
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
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
141 void ColorSelectDialog::setCurrentColor(ldraw::Color color)
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 this->selectedColor = color;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 this->updateSelectedColorTexts();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
146
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
147 ldraw::Color ColorSelectDialog::currentColor() const
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
148 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
149 return this->selectedColor;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
150 }

mercurial