src/colorSelector.cc

changeset 655
b376645315ab
child 662
2f1bd9112408
equal deleted inserted replaced
654:a74f2ff353b8 655:b376645315ab
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.cxx: Color selector box.
20 */
21
22 #include <QGraphicsScene>
23 #include <QGraphicsItem>
24 #include <QMouseEvent>
25 #include <QScrollBar>
26
27 #include "main.h"
28 #include "mainWindow.h"
29 #include "colorSelector.h"
30 #include "colors.h"
31 #include "configuration.h"
32 #include "miscallenous.h"
33 #include "ui_colorsel.h"
34
35 static const int g_numColumns = 16;
36 static const int g_squareSize = 32;
37
38 extern_cfg (String, gl_maincolor);
39 extern_cfg (Float, gl_maincolor_alpha);
40
41 // =============================================================================
42 // =============================================================================
43 ColorSelector::ColorSelector (int defval, QWidget* parent) : QDialog (parent)
44 {
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 {
72 delete ui;
73 }
74
75 // =============================================================================
76 // =============================================================================
77 void ColorSelector::drawScene()
78 {
79 const int numCols = g_numColumns;
80 const int square = g_squareSize;
81 const int g_maxHeight = (numRows() * square);
82 QRect sceneRect (0, 0, viewportWidth(), g_maxHeight);
83
84 m_scene->setSceneRect (sceneRect);
85 ui->viewport->setSceneRect (sceneRect);
86
87 const double penWidth = 1.0f;
88
89 // Draw the color rectangles.
90 m_scene->clear();
91
92 for (int i = 0; i < MAX_COLORS; ++i)
93 {
94 LDColor* info = ::getColor (i);
95
96 if (!info)
97 continue;
98
99 const double x = (i % numCols) * square;
100 const double y = (i / numCols) * square;
101 const double w = square - (penWidth / 2);
102
103 QColor col = info->faceColor;
104
105 if (i == maincolor)
106 {
107 // Use the user preferences for main color here
108 col = QColor (gl_maincolor);
109 col.setAlpha (gl_maincolor_alpha * 255.0f);
110 }
111
112 QPen pen (info->edgeColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
113 m_scene->addRect (x, y, w, w, pen, col);
114 QGraphicsTextItem* numtext = m_scene->addText (format ("%1", i));
115 numtext->setDefaultTextColor ( (luma (col) < 80) ? Qt::white : Qt::black);
116 numtext->setPos (x, y);
117
118 if (selection() && i == selection()->index)
119 {
120 auto curspic = m_scene->addPixmap (getIcon ("colorcursor"));
121 curspic->setPos (x, y);
122 }
123 }
124 }
125
126 // =============================================================================
127 // =============================================================================
128 int ColorSelector::numRows() const
129 {
130 return (MAX_COLORS / g_numColumns);
131 }
132
133 // =============================================================================
134 // =============================================================================
135 int ColorSelector::viewportWidth() const
136 {
137 return g_numColumns * g_squareSize + 21;
138 }
139
140 // =============================================================================
141 // =============================================================================
142 void ColorSelector::drawColorInfo()
143 {
144 if (!selection())
145 {
146 ui->colorLabel->setText ("---");
147 return;
148 }
149
150 ui->colorLabel->setText (format ("%1 - %2", selection()->index, selection()->name));
151 }
152
153 // =============================================================================
154 // =============================================================================
155 void ColorSelector::resizeEvent (QResizeEvent* ev)
156 {
157 // If this is the first resize, check if we need to scroll down to see the
158 // currently selected color. We cannot do this in the constructor because the
159 // height is not set properly there.
160 if (m_firstResize)
161 {
162 int visibleColors = (ui->viewport->height() / g_squareSize) * g_numColumns;
163
164 if (selection() && selection()->index >= visibleColors)
165 {
166 int y = (selection()->index / g_numColumns) * g_squareSize;
167 ui->viewport->verticalScrollBar()->setValue (y);
168 }
169
170 m_firstResize = false;
171 }
172
173 (void) ev;
174 drawScene();
175 }
176
177 // =============================================================================
178 // =============================================================================
179 void ColorSelector::mousePressEvent (QMouseEvent* event)
180 {
181 QPointF scenepos = ui->viewport->mapToScene (event->pos());
182
183 int x = (scenepos.x() - (g_squareSize / 2)) / g_squareSize;
184 int y = (scenepos.y() - (g_squareSize / 2)) / g_squareSize;
185 int idx = (y * g_numColumns) + x;
186
187 LDColor* col = ::getColor (idx);
188
189 if (!col)
190 return;
191
192 setSelection (col);
193 drawScene();
194 drawColorInfo();
195 }
196
197 // =============================================================================
198 // =============================================================================
199 bool ColorSelector::selectColor (int& val, int defval, QWidget* parent)
200 {
201 ColorSelector dlg (defval, parent);
202
203 if (dlg.exec() && dlg.selection() != null)
204 {
205 val = dlg.selection()->index;
206 return true;
207 }
208
209 return false;
210 }

mercurial