|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013, 2014 Teemu Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include <QGraphicsScene> |
|
20 #include <QGraphicsItem> |
|
21 #include <QMouseEvent> |
|
22 #include <QScrollBar> |
|
23 #include <QColorDialog> |
|
24 #include "../main.h" |
|
25 #include "../mainWindow.h" |
|
26 #include "../colors.h" |
|
27 #include "../configuration.h" |
|
28 #include "../miscallenous.h" |
|
29 #include "colorselector.h" |
|
30 #include "ui_colorselector.h" |
|
31 |
|
32 enum { NUM_COLUMNS = 16 }; |
|
33 |
|
34 EXTERN_CFGENTRY (String, MainColor) |
|
35 EXTERN_CFGENTRY (Float, MainColorAlpha) |
|
36 |
|
37 ColorSelector::ColorSelector (LDColor defaultvalue, QWidget* parent) : |
|
38 QDialog (parent), |
|
39 ui (*new Ui_ColorSelUi) |
|
40 { |
|
41 m_firstResize = true; |
|
42 ui.setupUi (this); |
|
43 setSelection (defaultvalue); |
|
44 |
|
45 QGridLayout* layout = new QGridLayout (this); |
|
46 |
|
47 // Spawn color selector buttons |
|
48 for (LDColor ldcolor; ldcolor.isLDConfigColor(); ++ldcolor) |
|
49 { |
|
50 QPushButton* button = new QPushButton (this); |
|
51 button->setMinimumSize (QSize (32, 32)); |
|
52 button->setMaximumSize (button->minimumSize()); |
|
53 |
|
54 if (ldcolor.isValid ()) |
|
55 { |
|
56 QColor color (ldcolor.faceColor()); |
|
57 |
|
58 if (ldcolor == MainColor) |
|
59 { |
|
60 color = QColor (cfg::MainColor); |
|
61 color.setAlphaF (cfg::MainColorAlpha); |
|
62 } |
|
63 |
|
64 QString color2name (Luma (color) < 80 ? "white" : "black"); |
|
65 button->setAutoFillBackground (true); |
|
66 button->setStyleSheet (format ("background-color: rgba(%1, %2, %3, %4); color: %5", |
|
67 color.red(), color.green(), color.blue(), color.alpha(), color2name)); |
|
68 button->setCheckable (true); |
|
69 button->setText (QString::number (ldcolor.index())); |
|
70 button->setToolTip (format ("%1: %2", ldcolor.index(), ldcolor.name())); |
|
71 m_buttons[ldcolor.index()] = button; |
|
72 m_buttonsReversed[button] = ldcolor.index(); |
|
73 connect (button, SIGNAL (clicked(bool)), this, SLOT (colorButtonClicked())); |
|
74 |
|
75 if (ldcolor == selection()) |
|
76 button->setChecked (true); |
|
77 } |
|
78 else |
|
79 { |
|
80 button->setEnabled (false); |
|
81 } |
|
82 |
|
83 layout->addWidget (button, ldcolor.index() / NUM_COLUMNS, ldcolor.index() % NUM_COLUMNS); |
|
84 } |
|
85 |
|
86 QWidget* widget = new QWidget(); |
|
87 widget->setLayout (layout); |
|
88 ui.definedColors->setWidget (widget); |
|
89 connect (ui.directColor, SIGNAL (clicked (bool)), this, SLOT (chooseDirectColor())); |
|
90 |
|
91 ui.definedColors->setMinimumWidth (ui.definedColors->widget()->width() + 16); |
|
92 |
|
93 #ifdef TRANSPARENT_DIRECT_COLORS |
|
94 connect (ui.transparentDirectColor, SIGNAL (clicked (bool)), this, SLOT (transparentCheckboxClicked())); |
|
95 #else |
|
96 ui.transparentDirectColor->hide(); |
|
97 #endif |
|
98 |
|
99 drawColorInfo(); |
|
100 } |
|
101 |
|
102 ColorSelector::~ColorSelector() |
|
103 { |
|
104 delete &ui; |
|
105 } |
|
106 |
|
107 void ColorSelector::colorButtonClicked() |
|
108 { |
|
109 QPushButton* button = qobject_cast<QPushButton*> (sender()); |
|
110 auto it = m_buttonsReversed.find (button); |
|
111 LDColor color; |
|
112 |
|
113 if (Q_UNLIKELY (button == null or it == m_buttonsReversed.end() |
|
114 or not (color = *it).isValid())) |
|
115 { |
|
116 print ("colorButtonClicked() called with invalid sender"); |
|
117 return; |
|
118 } |
|
119 |
|
120 if (selection().isValid()) |
|
121 { |
|
122 auto button = m_buttons.find (selection().index()); |
|
123 |
|
124 if (button != m_buttons.end()) |
|
125 (*button)->setChecked (false); |
|
126 } |
|
127 |
|
128 setSelection (color); |
|
129 button->setChecked (true); |
|
130 drawColorInfo(); |
|
131 } |
|
132 |
|
133 void ColorSelector::drawColorInfo() |
|
134 { |
|
135 if (not selection().isValid()) |
|
136 { |
|
137 ui.colorLabel->setText ("---"); |
|
138 ui.iconLabel->setPixmap (QPixmap()); |
|
139 ui.transparentDirectColor->setChecked (false); |
|
140 return; |
|
141 } |
|
142 |
|
143 ui.colorLabel->setText (format ("%1 - %2", selection().indexString(), |
|
144 (selection().isDirect() ? "<direct color>" : selection().name()))); |
|
145 ui.iconLabel->setPixmap (MakeColorIcon (selection(), 16).pixmap (16, 16)); |
|
146 |
|
147 #ifdef TRANSPARENT_DIRECT_COLORS |
|
148 ui.transparentDirectColor->setEnabled (selection().isDirect()); |
|
149 ui.transparentDirectColor->setChecked (selection().isDirect() and selection().faceColor().alphaF() < 1.0); |
|
150 #else |
|
151 ui.transparentDirectColor->setChecked (false); |
|
152 ui.transparentDirectColor->setEnabled (false); |
|
153 #endif |
|
154 } |
|
155 |
|
156 void ColorSelector::selectDirectColor (QColor color) |
|
157 { |
|
158 qint32 colorIndex = (ui.transparentDirectColor->isChecked() ? 0x03000000 : 0x02000000); |
|
159 colorIndex |= (color.red() << 16) | (color.green() << 8) | (color.blue()); |
|
160 setSelection (colorIndex); |
|
161 drawColorInfo(); |
|
162 } |
|
163 |
|
164 void ColorSelector::chooseDirectColor() |
|
165 { |
|
166 QColor defcolor = selection() != -1 ? selection().faceColor() : Qt::white; |
|
167 QColor newcolor = QColorDialog::getColor (defcolor); |
|
168 |
|
169 if (not newcolor.isValid()) |
|
170 return; // canceled |
|
171 |
|
172 selectDirectColor (newcolor); |
|
173 } |
|
174 |
|
175 void ColorSelector::transparentCheckboxClicked() |
|
176 { |
|
177 if (selection().isDirect()) |
|
178 selectDirectColor (selection().faceColor()); |
|
179 } |
|
180 |
|
181 bool ColorSelector::selectColor (LDColor& val, LDColor defval, QWidget* parent) |
|
182 { |
|
183 ColorSelector dlg (defval, parent); |
|
184 |
|
185 if (dlg.exec() and dlg.selection().isValid()) |
|
186 { |
|
187 val = dlg.selection(); |
|
188 return true; |
|
189 } |
|
190 |
|
191 return false; |
|
192 } |