src/colorSelectDialog.cc

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

mercurial