src/ColorSelector.cc

changeset 690
9e9c52ca955e
parent 644
93dcd1a0e4bd
equal deleted inserted replaced
689:397870c6ed38 690:9e9c52ca955e
37 37
38 extern_cfg (String, gl_maincolor); 38 extern_cfg (String, gl_maincolor);
39 extern_cfg (Float, gl_maincolor_alpha); 39 extern_cfg (Float, gl_maincolor_alpha);
40 40
41 // ============================================================================= 41 // =============================================================================
42 // ----------------------------------------------------------------------------- 42 // =============================================================================
43 ColorSelector::ColorSelector (int defval, QWidget* parent) : QDialog (parent) 43 ColorSelector::ColorSelector (int defval, QWidget* parent) : QDialog (parent)
44 { 44 {
45 // Remove the default color if it's invalid 45 // Remove the default color if it's invalid
46 if (!getColor (defval)) 46 if (!getColor (defval))
47 defval = -1; 47 defval = -1;
64 64
65 drawColorInfo(); 65 drawColorInfo();
66 } 66 }
67 67
68 // ============================================================================= 68 // =============================================================================
69 // ----------------------------------------------------------------------------- 69 // =============================================================================
70 ColorSelector::~ColorSelector() 70 ColorSelector::~ColorSelector()
71 { 71 {
72 delete ui; 72 delete ui;
73 } 73 }
74 74
75 // ============================================================================= 75 // =============================================================================
76 // ----------------------------------------------------------------------------- 76 // =============================================================================
77 void ColorSelector::drawScene() 77 void ColorSelector::drawScene()
78 { 78 {
79 const int numCols = g_numColumns; 79 const int numCols = g_numColumns;
80 const int square = g_squareSize; 80 const int square = g_squareSize;
81 const int g_maxHeight = (numRows() * square); 81 const int g_maxHeight = (numRows() * square);
109 col.setAlpha (gl_maincolor_alpha * 255.0f); 109 col.setAlpha (gl_maincolor_alpha * 255.0f);
110 } 110 }
111 111
112 QPen pen (info->edgeColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin); 112 QPen pen (info->edgeColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
113 m_scene->addRect (x, y, w, w, pen, col); 113 m_scene->addRect (x, y, w, w, pen, col);
114 QGraphicsTextItem* numtext = m_scene->addText (fmt ("%1", i)); 114 QGraphicsTextItem* numtext = m_scene->addText (format ("%1", i));
115 numtext->setDefaultTextColor ( (luma (col) < 80) ? Qt::white : Qt::black); 115 numtext->setDefaultTextColor ( (luma (col) < 80) ? Qt::white : Qt::black);
116 numtext->setPos (x, y); 116 numtext->setPos (x, y);
117 117
118 if (getSelection() && i == getSelection()->index) 118 if (selection() && i == selection()->index)
119 { 119 {
120 auto curspic = m_scene->addPixmap (getIcon ("colorcursor")); 120 auto curspic = m_scene->addPixmap (getIcon ("colorcursor"));
121 curspic->setPos (x, y); 121 curspic->setPos (x, y);
122 } 122 }
123 } 123 }
124 } 124 }
125 125
126 // ============================================================================= 126 // =============================================================================
127 // ----------------------------------------------------------------------------- 127 // =============================================================================
128 int ColorSelector::numRows() const 128 int ColorSelector::numRows() const
129 { 129 {
130 return (MAX_COLORS / g_numColumns); 130 return (MAX_COLORS / g_numColumns);
131 } 131 }
132 132
133 // ============================================================================= 133 // =============================================================================
134 // ----------------------------------------------------------------------------- 134 // =============================================================================
135 int ColorSelector::viewportWidth() const 135 int ColorSelector::viewportWidth() const
136 { 136 {
137 return g_numColumns * g_squareSize + 21; 137 return g_numColumns * g_squareSize + 21;
138 } 138 }
139 139
140 // ============================================================================= 140 // =============================================================================
141 // ----------------------------------------------------------------------------- 141 // =============================================================================
142 void ColorSelector::drawColorInfo() 142 void ColorSelector::drawColorInfo()
143 { 143 {
144 if (!getSelection()) 144 if (!selection())
145 { 145 {
146 ui->colorLabel->setText ("---"); 146 ui->colorLabel->setText ("---");
147 return; 147 return;
148 } 148 }
149 149
150 ui->colorLabel->setText (fmt ("%1 - %2", getSelection()->index, getSelection()->name)); 150 ui->colorLabel->setText (format ("%1 - %2", selection()->index, selection()->name));
151 } 151 }
152 152
153 // ============================================================================= 153 // =============================================================================
154 // ----------------------------------------------------------------------------- 154 // =============================================================================
155 void ColorSelector::resizeEvent (QResizeEvent* ev) 155 void ColorSelector::resizeEvent (QResizeEvent* ev)
156 { 156 {
157 // If this is the first resize, check if we need to scroll down to see the 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 158 // currently selected color. We cannot do this in the constructor because the
159 // height is not set properly there. 159 // height is not set properly there.
160 if (m_firstResize) 160 if (m_firstResize)
161 { 161 {
162 int visibleColors = (ui->viewport->height() / g_squareSize) * g_numColumns; 162 int visibleColors = (ui->viewport->height() / g_squareSize) * g_numColumns;
163 163
164 if (getSelection() && getSelection()->index >= visibleColors) 164 if (selection() && selection()->index >= visibleColors)
165 { 165 {
166 int y = (getSelection()->index / g_numColumns) * g_squareSize; 166 int y = (selection()->index / g_numColumns) * g_squareSize;
167 ui->viewport->verticalScrollBar()->setValue (y); 167 ui->viewport->verticalScrollBar()->setValue (y);
168 } 168 }
169 169
170 m_firstResize = false; 170 m_firstResize = false;
171 } 171 }
173 (void) ev; 173 (void) ev;
174 drawScene(); 174 drawScene();
175 } 175 }
176 176
177 // ============================================================================= 177 // =============================================================================
178 // ----------------------------------------------------------------------------- 178 // =============================================================================
179 void ColorSelector::mousePressEvent (QMouseEvent* event) 179 void ColorSelector::mousePressEvent (QMouseEvent* event)
180 { 180 {
181 QPointF scenepos = ui->viewport->mapToScene (event->pos()); 181 QPointF scenepos = ui->viewport->mapToScene (event->pos());
182 182
183 int x = (scenepos.x() - (g_squareSize / 2)) / g_squareSize; 183 int x = (scenepos.x() - (g_squareSize / 2)) / g_squareSize;
193 drawScene(); 193 drawScene();
194 drawColorInfo(); 194 drawColorInfo();
195 } 195 }
196 196
197 // ============================================================================= 197 // =============================================================================
198 // ----------------------------------------------------------------------------- 198 // =============================================================================
199 bool ColorSelector::selectColor (int& val, int defval, QWidget* parent) 199 bool ColorSelector::selectColor (int& val, int defval, QWidget* parent)
200 { 200 {
201 ColorSelector dlg (defval, parent); 201 ColorSelector dlg (defval, parent);
202 202
203 if (dlg.exec() && dlg.getSelection() != null) 203 if (dlg.exec() && dlg.selection() != null)
204 { 204 {
205 val = dlg.getSelection()->index; 205 val = dlg.selection()->index;
206 return true; 206 return true;
207 } 207 }
208 208
209 return false; 209 return false;
210 } 210 }

mercurial