src/colorSelector.cc

changeset 795
195fa1fff9c3
parent 757
8ab9fa53142b
child 796
f23f4e4ed279
equal deleted inserted replaced
794:c254ddc6618b 795:195fa1fff9c3
21 21
22 #include <QGraphicsScene> 22 #include <QGraphicsScene>
23 #include <QGraphicsItem> 23 #include <QGraphicsItem>
24 #include <QMouseEvent> 24 #include <QMouseEvent>
25 #include <QScrollBar> 25 #include <QScrollBar>
26 26 #include <QColorDialog>
27 #include "main.h" 27 #include "main.h"
28 #include "mainWindow.h" 28 #include "mainWindow.h"
29 #include "colorSelector.h" 29 #include "colorSelector.h"
30 #include "colors.h" 30 #include "colors.h"
31 #include "configuration.h" 31 #include "configuration.h"
37 37
38 EXTERN_CFGENTRY (String, mainColor); 38 EXTERN_CFGENTRY (String, mainColor);
39 EXTERN_CFGENTRY (Float, mainColorAlpha); 39 EXTERN_CFGENTRY (Float, mainColorAlpha);
40 40
41 // ============================================================================= 41 // =============================================================================
42 // ============================================================================= 42 //
43 ColorSelector::ColorSelector (int defval, QWidget* parent) : QDialog (parent) 43 ColorSelector::ColorSelector (LDColor defval, QWidget* parent) : QDialog (parent)
44 { 44 {
45 // Remove the default color if it's invalid
46 if (not getColor (defval))
47 defval = -1;
48
49 m_firstResize = true; 45 m_firstResize = true;
50 ui = new Ui_ColorSelUI; 46 ui = new Ui_ColorSelUI;
51 ui->setupUi (this); 47 ui->setupUi (this);
52 48
53 m_scene = new QGraphicsScene; 49 m_scene = new QGraphicsScene;
54 ui->viewport->setScene (m_scene); 50 ui->viewport->setScene (m_scene);
55 setSelection (getColor (defval)); 51 setSelection (defval);
56 52
57 // not really an icon but eh 53 // not really an icon but eh
58 m_scene->setBackgroundBrush (getIcon ("checkerboard")); 54 m_scene->setBackgroundBrush (getIcon ("checkerboard"));
59 drawScene(); 55 drawScene();
60 56
61 int width = viewportWidth(); 57 int width = viewportWidth();
62 ui->viewport->setMinimumWidth (width); 58 ui->viewport->setMinimumWidth (width);
63 ui->viewport->setMaximumWidth (width); 59 ui->viewport->setMaximumWidth (width);
64 60
61 connect (ui->directColor, SIGNAL (clicked (bool)), this, SLOT (chooseDirectColor()));
62 connect (ui->transparentDirectColor, SIGNAL (clicked (bool)), this, SLOT (transparentCheckboxClicked()));
63
65 drawColorInfo(); 64 drawColorInfo();
66 } 65 }
67 66
68 // ============================================================================= 67 // =============================================================================
69 // ============================================================================= 68 //
70 ColorSelector::~ColorSelector() 69 ColorSelector::~ColorSelector()
71 { 70 {
72 delete ui; 71 delete ui;
73 } 72 }
74 73
75 // ============================================================================= 74 // =============================================================================
76 // ============================================================================= 75 //
77 void ColorSelector::drawScene() 76 void ColorSelector::drawScene()
78 { 77 {
79 const int numCols = g_numColumns; 78 const int numCols = g_numColumns;
80 const int square = g_squareSize; 79 const int square = g_squareSize;
81 const int g_maxHeight = (numRows() * square); 80 const int g_maxHeight = (numRows() * square);
87 const double penWidth = 1.0f; 86 const double penWidth = 1.0f;
88 87
89 // Draw the color rectangles. 88 // Draw the color rectangles.
90 m_scene->clear(); 89 m_scene->clear();
91 90
92 for (int i = 0; i < MAX_COLORS; ++i) 91 for (int i = 0; i < numLDConfigColors(); ++i)
93 { 92 {
94 LDColor* info = ::getColor (i); 93 LDColor info = LDColor::fromIndex (i);
95 94
96 if (not info) 95 if (info == null)
97 continue; 96 continue;
98 97
99 const double x = (i % numCols) * square; 98 const double x = (i % numCols) * square;
100 const double y = (i / numCols) * square; 99 const double y = (i / numCols) * square;
101 const double w = square - (penWidth / 2); 100 const double w = square - (penWidth / 2);
102 101
103 QColor col = info->faceColor; 102 QColor col (info->faceColor());
104 103
105 if (i == maincolor) 104 if (i == mainColorIndex)
106 { 105 {
107 // Use the user preferences for main color here 106 // Use the user preferences for main color here
108 col = QColor (cfg::mainColor); 107 col = QColor (cfg::mainColor);
109 col.setAlpha (cfg::mainColorAlpha * 255.0f); 108 col.setAlpha (cfg::mainColorAlpha * 255.0f);
110 } 109 }
111 110
112 QPen pen (info->edgeColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin); 111 QPen pen (info->edgeColor(), penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
113 m_scene->addRect (x, y, w, w, pen, col); 112 m_scene->addRect (x, y, w, w, pen, col);
114 QGraphicsTextItem* numtext = m_scene->addText (format ("%1", i)); 113 QGraphicsTextItem* numtext = m_scene->addText (format ("%1", i));
115 numtext->setDefaultTextColor ( (luma (col) < 80) ? Qt::white : Qt::black); 114 numtext->setDefaultTextColor ((luma (col) < 80) ? Qt::white : Qt::black);
116 numtext->setPos (x, y); 115 numtext->setPos (x, y);
117 116
118 if (selection() && i == selection()->index) 117 if (selection() && i == selection()->index())
119 { 118 {
120 auto curspic = m_scene->addPixmap (getIcon ("colorcursor")); 119 auto curspic = m_scene->addPixmap (getIcon ("colorcursor"));
121 curspic->setPos (x, y); 120 curspic->setPos (x, y);
122 } 121 }
123 } 122 }
124 } 123 }
125 124
126 // ============================================================================= 125 // =============================================================================
127 // ============================================================================= 126 //
128 int ColorSelector::numRows() const 127 int ColorSelector::numRows() const
129 { 128 {
130 return (MAX_COLORS / g_numColumns); 129 return (numLDConfigColors() / g_numColumns);
131 } 130 }
132 131
133 // ============================================================================= 132 // =============================================================================
134 // ============================================================================= 133 //
135 int ColorSelector::viewportWidth() const 134 int ColorSelector::viewportWidth() const
136 { 135 {
137 return g_numColumns * g_squareSize + 21; 136 return g_numColumns * g_squareSize + 21;
138 } 137 }
139 138
140 // ============================================================================= 139 // =============================================================================
141 // ============================================================================= 140 //
142 void ColorSelector::drawColorInfo() 141 void ColorSelector::drawColorInfo()
143 { 142 {
144 if (not selection()) 143 if (selection() == null)
145 { 144 {
146 ui->colorLabel->setText ("---"); 145 ui->colorLabel->setText ("---");
146 ui->iconLabel->setPixmap (null);
147 ui->transparentDirectColor->setChecked (false);
147 return; 148 return;
148 } 149 }
149 150
150 ui->colorLabel->setText (format ("%1 - %2", selection()->index, selection()->name)); 151 ui->colorLabel->setText (format ("%1 - %2", selection()->indexString(),
151 } 152 (selection()->isDirect() ? "<direct color>" : selection()->name())));
152 153 ui->iconLabel->setPixmap (makeColorIcon (selection(), 16).pixmap (16, 16));
153 // ============================================================================= 154 ui->transparentDirectColor->setChecked (selection()->isDirect() && selection()->faceColor().alphaF() < 1.0);
154 // ============================================================================= 155 }
155 void ColorSelector::resizeEvent (QResizeEvent* ev) 156
157 // =============================================================================
158 //
159 void ColorSelector::resizeEvent (QResizeEvent*)
156 { 160 {
157 // If this is the first resize, check if we need to scroll down to see the 161 // 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 162 // currently selected color. We cannot do this in the constructor because the
159 // height is not set properly there. 163 // height is not set properly there. Though don't do this if we selected a
160 if (m_firstResize) 164 // direct color.
165 if (m_firstResize && selection()->index() >= numLDConfigColors())
161 { 166 {
162 int visibleColors = (ui->viewport->height() / g_squareSize) * g_numColumns; 167 int visibleColors = (ui->viewport->height() / g_squareSize) * g_numColumns;
163 168
164 if (selection() && selection()->index >= visibleColors) 169 if (selection() && selection()->index() >= visibleColors)
165 { 170 {
166 int y = (selection()->index / g_numColumns) * g_squareSize; 171 int y = (selection()->index() / g_numColumns) * g_squareSize;
167 ui->viewport->verticalScrollBar()->setValue (y); 172 ui->viewport->verticalScrollBar()->setValue (y);
168 } 173 }
169 174 }
170 m_firstResize = false; 175
171 } 176 m_firstResize = false;
172
173 (void) ev;
174 drawScene(); 177 drawScene();
175 } 178 }
176 179
177 // ============================================================================= 180 // =============================================================================
178 // ============================================================================= 181 //
179 void ColorSelector::mousePressEvent (QMouseEvent* event) 182 void ColorSelector::mousePressEvent (QMouseEvent* event)
180 { 183 {
181 QPointF scenepos = ui->viewport->mapToScene (event->pos()); 184 QPointF scenepos = ui->viewport->mapToScene (event->pos());
182 185
183 int x = (scenepos.x() - (g_squareSize / 2)) / g_squareSize; 186 int x = (scenepos.x() - (g_squareSize / 2)) / g_squareSize;
184 int y = (scenepos.y() - (g_squareSize / 2)) / g_squareSize; 187 int y = (scenepos.y() - (g_squareSize / 2)) / g_squareSize;
185 int idx = (y * g_numColumns) + x; 188 int idx = (y * g_numColumns) + x;
186 189
187 LDColor* col = ::getColor (idx); 190 LDColor col = LDColor::fromIndex (idx);
188 191
189 if (not col) 192 if (not col)
190 return; 193 return;
191 194
192 setSelection (col); 195 setSelection (col);
193 drawScene(); 196 drawScene();
194 drawColorInfo(); 197 drawColorInfo();
195 } 198 }
196 199
197 // ============================================================================= 200 // =============================================================================
198 // ============================================================================= 201 //
199 bool ColorSelector::selectColor (int& val, int defval, QWidget* parent) 202 void ColorSelector::selectDirectColor (QColor col)
203 {
204 int32 idx = (ui->transparentDirectColor->isChecked() ? 0x03000000 : 0x02000000);
205 idx |= (col.red() << 16) | (col.green() << 8) | (col.blue());
206 setSelection (LDColor::fromIndex (idx));
207 drawColorInfo();
208 }
209
210 // =============================================================================
211 //
212 void ColorSelector::chooseDirectColor()
213 {
214 QColor defcolor = selection() != null ? selection()->faceColor() : Qt::white;
215 QColor newcolor = QColorDialog::getColor (defcolor);
216
217 if (not newcolor.isValid())
218 return; // canceled
219
220 selectDirectColor (newcolor);
221 }
222
223 // =============================================================================
224 //
225 void ColorSelector::transparentCheckboxClicked()
226 {
227 if (selection() == null || not selection()->isDirect())
228 return;
229
230 selectDirectColor (selection()->faceColor());
231 }
232
233 // =============================================================================
234 //
235 bool ColorSelector::selectColor (LDColor& val, LDColor defval, QWidget* parent)
200 { 236 {
201 ColorSelector dlg (defval, parent); 237 ColorSelector dlg (defval, parent);
202 238
203 if (dlg.exec() && dlg.selection() != null) 239 if (dlg.exec() && dlg.selection() != null)
204 { 240 {
205 val = dlg.selection()->index; 241 val = dlg.selection();
206 return true; 242 return true;
207 } 243 }
208 244
209 return false; 245 return false;
210 } 246 }

mercurial