Changeset View
Changeset View
Standalone View
Standalone View
widgets/kpDualColorButton.cpp
Show First 20 Lines • Show All 84 Lines • ▼ Show 20 Line(s) | |||||
85 | } | 85 | } | ||
86 | 86 | | |||
87 | //--------------------------------------------------------------------- | 87 | //--------------------------------------------------------------------- | ||
88 | 88 | | |||
89 | void kpDualColorButton::setColor (int which, const kpColor &color) | 89 | void kpDualColorButton::setColor (int which, const kpColor &color) | ||
90 | { | 90 | { | ||
91 | Q_ASSERT (which == 0 || which == 1); | 91 | Q_ASSERT (which == 0 || which == 1); | ||
92 | 92 | | |||
93 | if (m_color [which] == color) | 93 | if (m_color [which] == color) { | ||
94 | return; | 94 | return; | ||
95 | } | ||||
95 | 96 | | |||
96 | m_oldColor [which] = m_color [which]; | 97 | m_oldColor [which] = m_color [which]; | ||
97 | m_color [which] = color; | 98 | m_color [which] = color; | ||
98 | update (); | 99 | update (); | ||
99 | 100 | | |||
100 | if (which == 0) | 101 | if (which == 0) { | ||
101 | emit foregroundColorChanged (color); | 102 | emit foregroundColorChanged (color); | ||
102 | else | 103 | } | ||
104 | else { | ||||
103 | emit backgroundColorChanged (color); | 105 | emit backgroundColorChanged (color); | ||
104 | } | 106 | } | ||
107 | } | ||||
105 | 108 | | |||
106 | //--------------------------------------------------------------------- | 109 | //--------------------------------------------------------------------- | ||
107 | 110 | | |||
108 | void kpDualColorButton::setForegroundColor (const kpColor &color) | 111 | void kpDualColorButton::setForegroundColor (const kpColor &color) | ||
109 | { | 112 | { | ||
110 | setColor (0, color); | 113 | setColor (0, color); | ||
111 | } | 114 | } | ||
112 | 115 | | |||
Show All 22 Lines | |||||
135 | } | 138 | } | ||
136 | 139 | | |||
137 | //--------------------------------------------------------------------- | 140 | //--------------------------------------------------------------------- | ||
138 | 141 | | |||
139 | 142 | | |||
140 | // public virtual [base QWidget] | 143 | // public virtual [base QWidget] | ||
141 | QSize kpDualColorButton::sizeHint () const | 144 | QSize kpDualColorButton::sizeHint () const | ||
142 | { | 145 | { | ||
143 | return QSize (52, 52); | 146 | return {52, 52}; | ||
144 | } | 147 | } | ||
145 | 148 | | |||
146 | //--------------------------------------------------------------------- | 149 | //--------------------------------------------------------------------- | ||
147 | 150 | | |||
148 | 151 | | |||
149 | // protected | 152 | // protected | ||
150 | QRect kpDualColorButton::swapPixmapRect () const | 153 | QRect kpDualColorButton::swapPixmapRect () const | ||
151 | { | 154 | { | ||
152 | QPixmap swapPixmap = UserIcon ("colorbutton_swap_16x16"); | 155 | QPixmap swapPixmap = UserIcon ("colorbutton_swap_16x16"); | ||
153 | 156 | | |||
154 | return QRect (contentsRect ().width () - swapPixmap.width (), | 157 | return {contentsRect ().width () - swapPixmap.width (), 0, | ||
155 | 0, | 158 | swapPixmap.width (), swapPixmap.height ()}; | ||
156 | swapPixmap.width (), | | |||
157 | swapPixmap.height ()); | | |||
158 | } | 159 | } | ||
159 | 160 | | |||
160 | //--------------------------------------------------------------------- | 161 | //--------------------------------------------------------------------- | ||
161 | 162 | | |||
162 | // protected | 163 | // protected | ||
163 | QRect kpDualColorButton::foregroundBackgroundRect () const | 164 | QRect kpDualColorButton::foregroundBackgroundRect () const | ||
164 | { | 165 | { | ||
165 | QRect cr (contentsRect ()); | 166 | QRect cr (contentsRect ()); | ||
166 | return QRect (cr.width () / 8, | 167 | return {cr.width () / 8, cr.height () / 8, | ||
167 | cr.height () / 8, | 168 | cr.width () * 6 / 8, cr.height () * 6 / 8}; | ||
168 | cr.width () * 6 / 8, | | |||
169 | cr.height () * 6 / 8); | | |||
170 | } | 169 | } | ||
171 | 170 | | |||
172 | //--------------------------------------------------------------------- | 171 | //--------------------------------------------------------------------- | ||
173 | 172 | | |||
174 | // protected | 173 | // protected | ||
175 | QRect kpDualColorButton::foregroundRect () const | 174 | QRect kpDualColorButton::foregroundRect () const | ||
176 | { | 175 | { | ||
177 | QRect fbr (foregroundBackgroundRect ()); | 176 | QRect fbr (foregroundBackgroundRect ()); | ||
178 | return QRect (fbr.x (), | 177 | return {fbr.x (), fbr.y (), | ||
179 | fbr.y (), | 178 | fbr.width () * 3 / 4, fbr.height () * 3 / 4}; | ||
180 | fbr.width () * 3 / 4, | | |||
181 | fbr.height () * 3 / 4); | | |||
182 | } | 179 | } | ||
183 | 180 | | |||
184 | //--------------------------------------------------------------------- | 181 | //--------------------------------------------------------------------- | ||
185 | 182 | | |||
186 | // protected | 183 | // protected | ||
187 | QRect kpDualColorButton::backgroundRect () const | 184 | QRect kpDualColorButton::backgroundRect () const | ||
188 | { | 185 | { | ||
189 | QRect fbr (foregroundBackgroundRect ()); | 186 | QRect fbr (foregroundBackgroundRect ()); | ||
190 | return QRect (fbr.x () + fbr.width () / 4, | 187 | return {fbr.x () + fbr.width () / 4, fbr.y () + fbr.height () / 4, | ||
191 | fbr.y () + fbr.height () / 4, | 188 | fbr.width () * 3 / 4, fbr.height () * 3 / 4}; | ||
192 | fbr.width () * 3 / 4, | | |||
193 | fbr.height () * 3 / 4); | | |||
194 | } | 189 | } | ||
195 | 190 | | |||
196 | //--------------------------------------------------------------------- | 191 | //--------------------------------------------------------------------- | ||
197 | 192 | | |||
198 | 193 | | |||
199 | // protected virtual | 194 | // protected virtual | ||
200 | void kpDualColorButton::dragEnterEvent (QDragEnterEvent *e) | 195 | void kpDualColorButton::dragEnterEvent (QDragEnterEvent *e) | ||
201 | { | 196 | { | ||
202 | qCDebug(kpLogWidgets) << "kpDualColorButton::dragEnterEvent() canDecode=" | 197 | qCDebug(kpLogWidgets) << "kpDualColorButton::dragEnterEvent() canDecode=" | ||
203 | << KColorMimeData::canDecode (e->mimeData ()) | 198 | << KColorMimeData::canDecode (e->mimeData ()); | ||
204 | << endl; | | |||
205 | e->accept (); | 199 | e->accept (); | ||
206 | } | 200 | } | ||
207 | 201 | | |||
208 | //--------------------------------------------------------------------- | 202 | //--------------------------------------------------------------------- | ||
209 | 203 | | |||
210 | // protected virtual [base QWidget] | 204 | // protected virtual [base QWidget] | ||
211 | void kpDualColorButton::dragMoveEvent (QDragMoveEvent *e) | 205 | void kpDualColorButton::dragMoveEvent (QDragMoveEvent *e) | ||
212 | { | 206 | { | ||
213 | qCDebug(kpLogWidgets) << "kpDualColorButton::dragMoveEvent() canDecode=" | 207 | qCDebug(kpLogWidgets) << "kpDualColorButton::dragMoveEvent() canDecode=" | ||
214 | << KColorMimeData::canDecode (e->mimeData ()) | 208 | << KColorMimeData::canDecode (e->mimeData ()); | ||
215 | << endl; | | |||
216 | e->setAccepted ( | 209 | e->setAccepted ( | ||
217 | (foregroundRect ().contains (e->pos ()) || | 210 | (foregroundRect ().contains (e->pos ()) || | ||
218 | backgroundRect ().contains (e->pos ())) && | 211 | backgroundRect ().contains (e->pos ())) && | ||
219 | KColorMimeData::canDecode (e->mimeData ())); | 212 | KColorMimeData::canDecode (e->mimeData ())); | ||
220 | } | 213 | } | ||
221 | 214 | | |||
222 | //--------------------------------------------------------------------- | 215 | //--------------------------------------------------------------------- | ||
223 | 216 | | |||
224 | // protected virtual [base QWidget] | 217 | // protected virtual [base QWidget] | ||
225 | void kpDualColorButton::dropEvent (QDropEvent *e) | 218 | void kpDualColorButton::dropEvent (QDropEvent *e) | ||
226 | { | 219 | { | ||
227 | QColor col = KColorMimeData::fromMimeData (e->mimeData ()); | 220 | QColor col = KColorMimeData::fromMimeData (e->mimeData ()); | ||
228 | qCDebug(kpLogWidgets) << "kpDualColorButton::dropEvent() col=" | 221 | qCDebug(kpLogWidgets) << "kpDualColorButton::dropEvent() col=" | ||
229 | << (int *) col.rgba() | 222 | << (int *) col.rgba() | ||
230 | << " (with alpha=" << (int *) col.rgba () << ")" << endl; | 223 | << " (with alpha=" << (int *) col.rgba () << ")"; | ||
231 | 224 | | |||
232 | if (col.isValid ()) | 225 | if (col.isValid ()) | ||
233 | { | 226 | { | ||
234 | if (foregroundRect ().contains (e->pos ())) | 227 | if (foregroundRect ().contains (e->pos ())) { | ||
235 | setForegroundColor (kpColor (col.rgba())); | 228 | setForegroundColor (kpColor (col.rgba())); | ||
236 | else if (backgroundRect ().contains (e->pos ())) | 229 | } | ||
230 | else if (backgroundRect ().contains (e->pos ())) { | ||||
237 | setBackgroundColor (kpColor (col.rgba())); | 231 | setBackgroundColor (kpColor (col.rgba())); | ||
238 | } | 232 | } | ||
239 | } | 233 | } | ||
234 | } | ||||
240 | 235 | | |||
241 | //--------------------------------------------------------------------- | 236 | //--------------------------------------------------------------------- | ||
242 | 237 | | |||
243 | 238 | | |||
244 | // protected virtual [base QWidget] | 239 | // protected virtual [base QWidget] | ||
245 | void kpDualColorButton::mousePressEvent (QMouseEvent *e) | 240 | void kpDualColorButton::mousePressEvent (QMouseEvent *e) | ||
246 | { | 241 | { | ||
247 | qCDebug(kpLogWidgets) << "kpDualColorButton::mousePressEvent() pos=" << e->pos (); | 242 | qCDebug(kpLogWidgets) << "kpDualColorButton::mousePressEvent() pos=" << e->pos (); | ||
248 | 243 | | |||
249 | m_dragStartPoint = KP_INVALID_POINT; | 244 | m_dragStartPoint = KP_INVALID_POINT; | ||
250 | 245 | | |||
251 | if (e->button () == Qt::LeftButton) | 246 | if (e->button () == Qt::LeftButton) { | ||
252 | m_dragStartPoint = e->pos (); | 247 | m_dragStartPoint = e->pos (); | ||
253 | } | 248 | } | ||
249 | } | ||||
254 | 250 | | |||
255 | //--------------------------------------------------------------------- | 251 | //--------------------------------------------------------------------- | ||
256 | 252 | | |||
257 | void kpDualColorButton::mouseMoveEvent (QMouseEvent *e) | 253 | void kpDualColorButton::mouseMoveEvent (QMouseEvent *e) | ||
258 | { | 254 | { | ||
259 | qCDebug(kpLogWidgets) << "kpDualColorButton::mouseMoveEvent() pos=" << e->pos () | 255 | qCDebug(kpLogWidgets) << "kpDualColorButton::mouseMoveEvent() pos=" << e->pos () | ||
260 | << " buttons=" << e->buttons () | 256 | << " buttons=" << e->buttons () | ||
261 | << " dragStartPoint=" << m_dragStartPoint; | 257 | << " dragStartPoint=" << m_dragStartPoint; | ||
262 | 258 | | |||
263 | if (m_dragStartPoint == KP_INVALID_POINT) | 259 | if (m_dragStartPoint == KP_INVALID_POINT) { | ||
264 | return; | 260 | return; | ||
261 | } | ||||
265 | 262 | | |||
266 | if (!(e->buttons () & Qt::LeftButton)) | 263 | if (!(e->buttons () & Qt::LeftButton)) | ||
267 | { | 264 | { | ||
268 | m_dragStartPoint = KP_INVALID_POINT; | 265 | m_dragStartPoint = KP_INVALID_POINT; | ||
269 | return; | 266 | return; | ||
270 | } | 267 | } | ||
271 | 268 | | |||
272 | const int delay = QApplication::startDragDistance (); | 269 | const int delay = QApplication::startDragDistance (); | ||
273 | if (e->x () < m_dragStartPoint.x () - delay || | 270 | if (e->x () < m_dragStartPoint.x () - delay || | ||
274 | e->x () > m_dragStartPoint.x () + delay || | 271 | e->x () > m_dragStartPoint.x () + delay || | ||
275 | e->y () < m_dragStartPoint.y () - delay || | 272 | e->y () < m_dragStartPoint.y () - delay || | ||
276 | e->y () > m_dragStartPoint.y () + delay) | 273 | e->y () > m_dragStartPoint.y () + delay) | ||
277 | { | 274 | { | ||
278 | qCDebug(kpLogWidgets) << "\tstarting drag as long as it's in a rectangle"; | 275 | qCDebug(kpLogWidgets) << "\tstarting drag as long as it's in a rectangle"; | ||
279 | 276 | | |||
280 | kpColor color; | 277 | kpColor color; | ||
281 | 278 | | |||
282 | if (foregroundRect ().contains (m_dragStartPoint)) | 279 | if (foregroundRect ().contains (m_dragStartPoint)) { | ||
283 | color = foregroundColor (); | 280 | color = foregroundColor (); | ||
284 | else if (backgroundRect ().contains (m_dragStartPoint)) | 281 | } | ||
282 | else if (backgroundRect ().contains (m_dragStartPoint)) { | ||||
285 | color = backgroundColor (); | 283 | color = backgroundColor (); | ||
286 | else | | |||
287 | { | | |||
288 | // "color" is left as invalid. | | |||
289 | } | 284 | } | ||
290 | 285 | | |||
291 | qCDebug(kpLogWidgets) << "\tcolor.isValid=" << color.isValid () | 286 | qCDebug(kpLogWidgets) << "\tcolor.isValid=" << color.isValid () | ||
292 | << " rgb=" << (color.isValid () ? (int *) color.toQRgb () : 0); | 287 | << " rgb=" << (color.isValid () ? (int *) color.toQRgb () : 0); | ||
293 | 288 | | |||
294 | if (color.isValid ()) | 289 | if (color.isValid ()) | ||
295 | { | 290 | { | ||
296 | if (!color.isTransparent ()) | 291 | if (!color.isTransparent ()) { | ||
297 | KColorMimeData::createDrag (color.toQColor (), this)->exec (); | 292 | KColorMimeData::createDrag (color.toQColor (), this)->exec (); | ||
298 | } | 293 | } | ||
294 | } | ||||
299 | 295 | | |||
300 | m_dragStartPoint = KP_INVALID_POINT; | 296 | m_dragStartPoint = KP_INVALID_POINT; | ||
301 | } | 297 | } | ||
302 | } | 298 | } | ||
303 | 299 | | |||
304 | //--------------------------------------------------------------------- | 300 | //--------------------------------------------------------------------- | ||
305 | 301 | | |||
306 | // protected virtual [base QWidget] | 302 | // protected virtual [base QWidget] | ||
Show All 22 Lines | |||||
329 | 325 | | |||
330 | //--------------------------------------------------------------------- | 326 | //--------------------------------------------------------------------- | ||
331 | 327 | | |||
332 | // protected virtual [base QWidget] | 328 | // protected virtual [base QWidget] | ||
333 | void kpDualColorButton::mouseDoubleClickEvent (QMouseEvent *e) | 329 | void kpDualColorButton::mouseDoubleClickEvent (QMouseEvent *e) | ||
334 | { | 330 | { | ||
335 | int whichColor = -1; | 331 | int whichColor = -1; | ||
336 | 332 | | |||
337 | if (foregroundRect ().contains (e->pos ())) | 333 | if (foregroundRect ().contains (e->pos ())) { | ||
338 | whichColor = 0; | 334 | whichColor = 0; | ||
339 | else if (backgroundRect ().contains (e->pos ())) | 335 | } | ||
336 | else if (backgroundRect ().contains (e->pos ())) { | ||||
340 | whichColor = 1; | 337 | whichColor = 1; | ||
338 | } | ||||
341 | 339 | | |||
342 | if (whichColor == 0 || whichColor == 1) | 340 | if (whichColor == 0 || whichColor == 1) | ||
343 | { | 341 | { | ||
344 | QColorDialog dialog(this); | 342 | QColorDialog dialog(this); | ||
345 | dialog.setCurrentColor(color(whichColor).toQColor()); | 343 | dialog.setCurrentColor(color(whichColor).toQColor()); | ||
346 | dialog.setOptions(QColorDialog::ShowAlphaChannel); | 344 | dialog.setOptions(QColorDialog::ShowAlphaChannel); | ||
347 | if ( dialog.exec() == QDialog::Accepted ) | 345 | if ( dialog.exec() == QDialog::Accepted ) { | ||
348 | setColor(whichColor, kpColor(dialog.currentColor().rgba())); | 346 | setColor(whichColor, kpColor(dialog.currentColor().rgba())); | ||
349 | } | 347 | } | ||
350 | } | 348 | } | ||
349 | } | ||||
351 | 350 | | |||
352 | //--------------------------------------------------------------------- | 351 | //--------------------------------------------------------------------- | ||
353 | 352 | | |||
354 | // protected virtual [base QWidget] | 353 | // protected virtual [base QWidget] | ||
355 | void kpDualColorButton::paintEvent (QPaintEvent *e) | 354 | void kpDualColorButton::paintEvent (QPaintEvent *e) | ||
356 | { | 355 | { | ||
357 | qCDebug(kpLogWidgets) << "kpDualColorButton::draw() rect=" << rect () | 356 | qCDebug(kpLogWidgets) << "kpDualColorButton::draw() rect=" << rect () | ||
358 | << " contentsRect=" << contentsRect (); | 357 | << " contentsRect=" << contentsRect (); | ||
Show All 38 Lines | |||||
397 | 396 | | |||
398 | // Draw background colour patch. | 397 | // Draw background colour patch. | ||
399 | QRect bgRect = backgroundRect (); | 398 | QRect bgRect = backgroundRect (); | ||
400 | QRect bgRectInside = QRect (bgRect.x () + 2, bgRect.y () + 2, | 399 | QRect bgRectInside = QRect (bgRect.x () + 2, bgRect.y () + 2, | ||
401 | bgRect.width () - 4, bgRect.height () - 4); | 400 | bgRect.width () - 4, bgRect.height () - 4); | ||
402 | if (isEnabled ()) | 401 | if (isEnabled ()) | ||
403 | { | 402 | { | ||
404 | qCDebug(kpLogWidgets) << "\tbackgroundColor=" << (int *) m_color [1].toQRgb (); | 403 | qCDebug(kpLogWidgets) << "\tbackgroundColor=" << (int *) m_color [1].toQRgb (); | ||
405 | if (m_color [1].isTransparent ()) // only if fully transparent | 404 | if (m_color [1].isTransparent ()) { // only if fully transparent | ||
406 | painter.drawPixmap (bgRectInside, UserIcon ("color_transparent_26x26")); | 405 | painter.drawPixmap (bgRectInside, UserIcon ("color_transparent_26x26")); | ||
407 | else | 406 | } | ||
407 | else { | ||||
408 | painter.fillRect (bgRectInside, m_color [1].toQColor ()); | 408 | painter.fillRect (bgRectInside, m_color [1].toQColor ()); | ||
409 | } | 409 | } | ||
410 | else | 410 | } | ||
411 | else { | ||||
411 | painter.fillRect (bgRectInside, palette().color (QPalette::Button)); | 412 | painter.fillRect (bgRectInside, palette().color (QPalette::Button)); | ||
413 | } | ||||
412 | qDrawShadePanel (&painter, bgRect, palette(), | 414 | qDrawShadePanel (&painter, bgRect, palette(), | ||
413 | false/*not sunken*/, 2/*lineWidth*/, | 415 | false/*not sunken*/, 2/*lineWidth*/, | ||
414 | nullptr/*never fill*/); | 416 | nullptr/*never fill*/); | ||
415 | 417 | | |||
416 | 418 | | |||
419 | | ||||
417 | // Draw foreground colour patch. | 420 | // Draw foreground colour patch. | ||
418 | // Must be drawn after background patch since we're on top. | 421 | // Must be drawn after background patch since we're on top. | ||
419 | QRect fgRect = foregroundRect (); | 422 | QRect fgRect = foregroundRect (); | ||
420 | QRect fgRectInside = QRect (fgRect.x () + 2, fgRect.y () + 2, | 423 | QRect fgRectInside = QRect (fgRect.x () + 2, fgRect.y () + 2, | ||
421 | fgRect.width () - 4, fgRect.height () - 4); | 424 | fgRect.width () - 4, fgRect.height () - 4); | ||
422 | if (isEnabled ()) | 425 | if (isEnabled ()) | ||
423 | { | 426 | { | ||
424 | qCDebug(kpLogWidgets) << "\tforegroundColor=" << (int *) m_color [0].toQRgb (); | 427 | qCDebug(kpLogWidgets) << "\tforegroundColor=" << (int *) m_color [0].toQRgb (); | ||
425 | if (m_color [0].isTransparent ()) // only if fully transparent | 428 | if (m_color [0].isTransparent ()) { // only if fully transparent | ||
426 | painter.drawPixmap (fgRectInside, UserIcon ("color_transparent_26x26")); | 429 | painter.drawPixmap (fgRectInside, UserIcon ("color_transparent_26x26")); | ||
427 | else | 430 | } | ||
431 | else { | ||||
428 | painter.fillRect (fgRectInside, m_color [0].toQColor ()); | 432 | painter.fillRect (fgRectInside, m_color [0].toQColor ()); | ||
429 | } | 433 | } | ||
430 | else | 434 | } | ||
435 | else { | ||||
431 | painter.fillRect (fgRectInside, palette ().color (QPalette::Button)); | 436 | painter.fillRect (fgRectInside, palette ().color (QPalette::Button)); | ||
437 | } | ||||
432 | 438 | | |||
433 | qDrawShadePanel (&painter, fgRect, palette (), | 439 | qDrawShadePanel (&painter, fgRect, palette (), | ||
434 | false/*not sunken*/, 2/*lineWidth*/, | 440 | false/*not sunken*/, 2/*lineWidth*/, | ||
435 | nullptr/*never fill*/); | 441 | nullptr/*never fill*/); | ||
436 | } | 442 | } | ||
437 | 443 | | |||
438 | 444 | |