src/widgets/colorselectdialog.cpp

Sat, 05 Mar 2022 13:18:40 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:18:40 +0200
changeset 161
6fe485a84d06
parent 139
72098474d362
child 178
a23024fc98e0
permissions
-rw-r--r--

Icons update

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>
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #include "colorselectdialog.h"
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 #include "ui_colorselectdialog.h"
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 ColorSelectDialog::ColorSelectDialog(const ldraw::ColorTable& colorTable, QWidget *parent) :
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 QString styleSheetForColor(const QColor& color)
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 QColor const textColor = (luma(color) < 0.4) ? Qt::white : Qt::black;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 return QString{"background-color: %1; color: %2;"}
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 .arg(color.name())
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 .arg(textColor.name());
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 void ColorSelectDialog::makeColorButtons()
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 this->buttons.reserve(this->colorTable.size());
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 for (
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 auto iterator = std::begin(this->colorTable);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 iterator != std::end(this->colorTable);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 ++iterator
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 ) {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 const qint32 index = iterator->first;
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
49 const ldraw::ColorDefinition& colordef = iterator->second;
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 QPushButton* const button = new QPushButton{QString::number(index), this};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 button->setToolTip(colordef.displayName);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 button->setStyleSheet(styleSheetForColor(colordef.faceColor));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 button->setProperty("_colorIndex", index);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 button->setCheckable(true);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 connect(button, &QAbstractButton::clicked, this, &ColorSelectDialog::handleButtonClick);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 this->buttons.push_back(button);
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 std::sort(
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 std::begin(this->buttons),
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 std::end(this->buttons),
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 [](QAbstractButton* button_1, QAbstractButton* button_2)
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 return colorFromButton(button_1) < colorFromButton(button_2);
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 );
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
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 void ColorSelectDialog::populateColors()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 delete this->ui.colorFrame->layout();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 if (this->buttons.size() > 0)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 QGridLayout* gridLayout = new QGridLayout{};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 this->ui.colorFrame->setLayout(gridLayout);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 const int columnsPerRow = 8;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 int i = 0;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 for (
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 auto iterator = std::begin(this->buttons);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 iterator != std::end(this->buttons);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 ++iterator
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 QPushButton* const button = *iterator;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 const bool filtered = this->filterColor(colorFromButton(button));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 button->setVisible(filtered);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 if (filtered)
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 const int row = i / columnsPerRow;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 const int column = i % columnsPerRow;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 gridLayout->addWidget(button, row, column);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 i += 1;
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 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 void ColorSelectDialog::updateSelectedColorTexts()
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 if (ldraw::isDirectColor(this->selectedColor))
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 this->ui.selectedColorName->setText(ldraw::directColorFace(this->selectedColor).name());
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 else
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 {
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
104 const ldraw::ColorDefinition& colordef = this->colorTable[this->selectedColor];
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 this->ui.selectedColorName->setText(colordef.displayName);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 }
115
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
107 const QColor colorFace = ldraw::colorFace(this->selectedColor, this->colorTable);
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
108 this->ui.selectedColorName->setStyleSheet(styleSheetForColor(colorFace));
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 this->ui.colorIndex->setValue(this->selectedColor.index);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 for (QPushButton* button : this->buttons)
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 ldraw::Color buttonColor = colorFromButton(button);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 button->setChecked(buttonColor == this->selectedColor);
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 }
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::handleButtonClick()
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 QAbstractButton* button = qobject_cast<QAbstractButton*>(this->sender());
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 if (button != nullptr)
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 this->setCurrentColor(colorFromButton(button));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 }
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 void ColorSelectDialog::spinboxEdited()
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 this->setCurrentColor(ldraw::Color{this->ui.colorIndex->value()});
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 void ColorSelectDialog::chooseDirectColor()
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 QColorDialog dialog;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 dialog.setCurrentColor(ldraw::colorFace(this->selectedColor, this->colorTable));
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 if (dialog.exec())
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 this->setCurrentColor(ldraw::directColor(dialog.selectedColor()));
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 bool ColorSelectDialog::filterColor(ldraw::Color color) const
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 const QString& filterText = this->ui.filter->text();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 if (filterText.isEmpty())
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 return true;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 else
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 {
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
150 const ldraw::ColorDefinition& colordef = this->colorTable[color];
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151 return colordef.displayName.contains(filterText, Qt::CaseInsensitive);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
152 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
153 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155 void ColorSelectDialog::setCurrentColor(ldraw::Color color)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 this->selectedColor = color;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 this->updateSelectedColorTexts();
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 }

mercurial