1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 Santeri 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 * colorSelectDialog.cpp: Color selector box. |
|
20 */ |
|
21 |
|
22 #include <QGraphicsScene> |
|
23 #include <QGraphicsItem> |
|
24 #include <QMouseEvent> |
|
25 #include <QScrollBar> |
|
26 |
|
27 #include "main.h" |
|
28 #include "gui.h" |
|
29 #include "colorSelectDialog.h" |
|
30 #include "colors.h" |
|
31 #include "config.h" |
|
32 #include "misc.h" |
|
33 #include "ui_colorsel.h" |
|
34 #include "moc_colorSelectDialog.cpp" |
|
35 |
|
36 static const int g_numColumns = 16; |
|
37 static const int g_squareSize = 32; |
|
38 |
|
39 extern_cfg (String, gl_maincolor); |
|
40 extern_cfg (Float, gl_maincolor_alpha); |
|
41 |
|
42 // ============================================================================= |
|
43 // ----------------------------------------------------------------------------- |
|
44 ColorSelector::ColorSelector (int defval, QWidget* parent) : QDialog (parent) |
|
45 { // Remove the default color if it's invalid |
|
46 if (!getColor (defval)) |
|
47 defval = -1; |
|
48 |
|
49 m_firstResize = true; |
|
50 ui = new Ui_ColorSelUI; |
|
51 ui->setupUi (this); |
|
52 |
|
53 m_scene = new QGraphicsScene; |
|
54 ui->viewport->setScene (m_scene); |
|
55 setSelection (getColor (defval)); |
|
56 |
|
57 // not really an icon but eh |
|
58 m_scene->setBackgroundBrush (getIcon ("checkerboard")); |
|
59 drawScene(); |
|
60 |
|
61 int width = viewportWidth(); |
|
62 ui->viewport->setMinimumWidth (width); |
|
63 ui->viewport->setMaximumWidth (width); |
|
64 |
|
65 drawColorInfo(); |
|
66 } |
|
67 |
|
68 // ============================================================================= |
|
69 // ----------------------------------------------------------------------------- |
|
70 ColorSelector::~ColorSelector() |
|
71 { delete ui; |
|
72 } |
|
73 |
|
74 // ============================================================================= |
|
75 // ----------------------------------------------------------------------------- |
|
76 void ColorSelector::drawScene() |
|
77 { const int numCols = g_numColumns; |
|
78 const int square = g_squareSize; |
|
79 const int g_maxHeight = (numRows() * square); |
|
80 QRect sceneRect (0, 0, viewportWidth(), g_maxHeight); |
|
81 |
|
82 m_scene->setSceneRect (sceneRect); |
|
83 ui->viewport->setSceneRect (sceneRect); |
|
84 |
|
85 const double penWidth = 1.0f; |
|
86 |
|
87 // Draw the color rectangles. |
|
88 m_scene->clear(); |
|
89 |
|
90 for (int i = 0; i < MAX_COLORS; ++i) |
|
91 { LDColor* info = ::getColor (i); |
|
92 |
|
93 if (!info) |
|
94 continue; |
|
95 |
|
96 const double x = (i % numCols) * square; |
|
97 const double y = (i / numCols) * square; |
|
98 const double w = square - (penWidth / 2); |
|
99 |
|
100 QColor col = info->faceColor; |
|
101 |
|
102 if (i == maincolor) |
|
103 { // Use the user preferences for main color here |
|
104 col = QColor (gl_maincolor); |
|
105 col.setAlpha (gl_maincolor_alpha * 255.0f); |
|
106 } |
|
107 |
|
108 QPen pen (info->edgeColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin); |
|
109 m_scene->addRect (x, y, w, w, pen, col); |
|
110 QGraphicsTextItem* numtext = m_scene->addText (fmt ("%1", i)); |
|
111 numtext->setDefaultTextColor ( (luma (col) < 80) ? Qt::white : Qt::black); |
|
112 numtext->setPos (x, y); |
|
113 |
|
114 if (getSelection() && i == getSelection()->index) |
|
115 { auto curspic = m_scene->addPixmap (getIcon ("colorcursor")); |
|
116 curspic->setPos (x, y); |
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 // ============================================================================= |
|
122 // ----------------------------------------------------------------------------- |
|
123 int ColorSelector::numRows() const |
|
124 { return (MAX_COLORS / g_numColumns); |
|
125 } |
|
126 |
|
127 // ============================================================================= |
|
128 // ----------------------------------------------------------------------------- |
|
129 int ColorSelector::viewportWidth() const |
|
130 { return g_numColumns * g_squareSize + 21; |
|
131 } |
|
132 |
|
133 // ============================================================================= |
|
134 // ----------------------------------------------------------------------------- |
|
135 void ColorSelector::drawColorInfo() |
|
136 { if (!getSelection()) |
|
137 { ui->colorLabel->setText ("---"); |
|
138 return; |
|
139 } |
|
140 |
|
141 ui->colorLabel->setText (fmt ("%1 - %2", getSelection()->index, getSelection()->name)); |
|
142 } |
|
143 |
|
144 // ============================================================================= |
|
145 // ----------------------------------------------------------------------------- |
|
146 void ColorSelector::resizeEvent (QResizeEvent* ev) |
|
147 { // If this is the first resize, check if we need to scroll down to see the |
|
148 // currently selected color. We cannot do this in the constructor because the |
|
149 // height is not set properly there. |
|
150 if (m_firstResize) |
|
151 { int visibleColors = (ui->viewport->height() / g_squareSize) * g_numColumns; |
|
152 |
|
153 if (getSelection() && getSelection()->index >= visibleColors) |
|
154 { int y = (getSelection()->index / g_numColumns) * g_squareSize; |
|
155 ui->viewport->verticalScrollBar()->setValue (y); |
|
156 } |
|
157 |
|
158 m_firstResize = false; |
|
159 } |
|
160 |
|
161 (void) ev; |
|
162 drawScene(); |
|
163 } |
|
164 |
|
165 // ============================================================================= |
|
166 // ----------------------------------------------------------------------------- |
|
167 void ColorSelector::mousePressEvent (QMouseEvent* event) |
|
168 { QPointF scenepos = ui->viewport->mapToScene (event->pos()); |
|
169 |
|
170 int x = (scenepos.x() - (g_squareSize / 2)) / g_squareSize; |
|
171 int y = (scenepos.y() - (g_squareSize / 2)) / g_squareSize; |
|
172 int idx = (y * g_numColumns) + x; |
|
173 |
|
174 LDColor* col = ::getColor (idx); |
|
175 |
|
176 if (!col) |
|
177 return; |
|
178 |
|
179 setSelection (col); |
|
180 drawScene(); |
|
181 drawColorInfo(); |
|
182 } |
|
183 |
|
184 // ============================================================================= |
|
185 // ----------------------------------------------------------------------------- |
|
186 bool ColorSelector::selectColor (int& val, int defval, QWidget* parent) |
|
187 { ColorSelector dlg (defval, parent); |
|
188 |
|
189 if (dlg.exec() && dlg.getSelection() != null) |
|
190 { val = dlg.getSelection()->index; |
|
191 return true; |
|
192 } |
|
193 |
|
194 return false; |
|
195 } |
|