Changeset View
Changeset View
Standalone View
Standalone View
widgets/kpColorCells.cpp
Show First 20 Lines • Show All 49 Lines • ▼ Show 20 Line(s) | |||||
50 | 50 | | |||
51 | const int TableDefaultWidth = ::TableDefaultNumColumns * 26; | 51 | const int TableDefaultWidth = ::TableDefaultNumColumns * 26; | ||
52 | 52 | | |||
53 | const int TableDefaultHeight = 52; | 53 | const int TableDefaultHeight = 52; | ||
54 | 54 | | |||
55 | 55 | | |||
56 | static int TableNumColumns (const kpColorCollection &colorCol) | 56 | static int TableNumColumns (const kpColorCollection &colorCol) | ||
57 | { | 57 | { | ||
58 | if (colorCol.count () == 0) | 58 | if (colorCol.count () == 0) { | ||
59 | return 0; | 59 | return 0; | ||
60 | } | ||||
60 | 61 | | |||
61 | return ::TableDefaultNumColumns; | 62 | return ::TableDefaultNumColumns; | ||
62 | } | 63 | } | ||
63 | 64 | | |||
64 | static int TableNumRows (const kpColorCollection &colorCol) | 65 | static int TableNumRows (const kpColorCollection &colorCol) | ||
65 | { | 66 | { | ||
66 | const int cols = ::TableNumColumns (colorCol); | 67 | const int cols = ::TableNumColumns (colorCol); | ||
67 | if (cols == 0) | 68 | if (cols == 0) { | ||
68 | return 0; | 69 | return 0; | ||
70 | } | ||||
69 | 71 | | |||
70 | return (colorCol.count () + (cols - 1)) / cols; | 72 | return (colorCol.count () + (cols - 1)) / cols; | ||
71 | } | 73 | } | ||
72 | 74 | | |||
73 | 75 | | |||
74 | static int TableCellWidth (const kpColorCollection &colorCol) | 76 | static int TableCellWidth (const kpColorCollection &colorCol) | ||
75 | { | 77 | { | ||
76 | Q_UNUSED (colorCol); | 78 | Q_UNUSED (colorCol); | ||
77 | 79 | | |||
78 | return ::TableDefaultWidth / ::TableDefaultNumColumns; | 80 | return ::TableDefaultWidth / ::TableDefaultNumColumns; | ||
79 | } | 81 | } | ||
80 | 82 | | |||
81 | static int TableCellHeight (const kpColorCollection &colorCol) | 83 | static int TableCellHeight (const kpColorCollection &colorCol) | ||
82 | { | 84 | { | ||
83 | if (::TableNumRows (colorCol) <= 2) | 85 | if (::TableNumRows (colorCol) <= 2) { | ||
84 | return ::TableDefaultHeight / 2; | 86 | return ::TableDefaultHeight / 2; | ||
85 | else | 87 | } | ||
88 | | ||||
86 | return ::TableDefaultHeight / 3; | 89 | return ::TableDefaultHeight / 3; | ||
87 | } | 90 | } | ||
88 | 91 | | |||
89 | 92 | | |||
90 | // | 93 | // | ||
91 | // kpColorCells | 94 | // kpColorCells | ||
92 | // | 95 | // | ||
93 | 96 | | |||
94 | 97 | | |||
95 | struct kpColorCellsPrivate | 98 | struct kpColorCellsPrivate | ||
96 | { | 99 | { | ||
97 | Qt::Orientation orientation; | 100 | Qt::Orientation orientation{}; | ||
98 | 101 | | |||
99 | // REFACTOR: This is data duplication with kpColorCellsBase::color[]. | 102 | // REFACTOR: This is data duplication with kpColorCellsBase::color[]. | ||
100 | // We've probably forgotten to synchronize them in some points. | 103 | // We've probably forgotten to synchronize them in some points. | ||
101 | // | 104 | // | ||
102 | // Calls to kpColorCellsBase::setColor() (which also come from | 105 | // Calls to kpColorCellsBase::setColor() (which also come from | ||
103 | // kpColorCellsBase itself) will automatically update both | 106 | // kpColorCellsBase itself) will automatically update both | ||
104 | // kpColorCellsBase::d->color[] and the table cells. setColor() emits | 107 | // kpColorCellsBase::d->color[] and the table cells. setColor() emits | ||
105 | // colorChanged(), which is caught by our slotColorChanged(), | 108 | // colorChanged(), which is caught by our slotColorChanged(), | ||
106 | // which synchronizes this color collection and updates the modified flag. | 109 | // which synchronizes this color collection and updates the modified flag. | ||
107 | // | 110 | // | ||
108 | // Avoid calling our grandparent's, QTableWidget's, mutating methods as we | 111 | // Avoid calling our grandparent's, QTableWidget's, mutating methods as we | ||
109 | // don't override enough of them, to fire signals that we can catch to update | 112 | // don't override enough of them, to fire signals that we can catch to update | ||
110 | // this color collection. | 113 | // this color collection. | ||
111 | // | 114 | // | ||
112 | // If you modify this color collection directly (e.g. in setColorCollection(), | 115 | // If you modify this color collection directly (e.g. in setColorCollection(), | ||
113 | // openColorCollection(), appendRow(), deleteLastRow(), ...), you must work | 116 | // openColorCollection(), appendRow(), deleteLastRow(), ...), you must work | ||
114 | // the other way and call makeCellsMatchColorCollection() to synchronize | 117 | // the other way and call makeCellsMatchColorCollection() to synchronize | ||
115 | // kpColorCellsBase::d->color[] and the table cells. You still need to update | 118 | // kpColorCellsBase::d->color[] and the table cells. You still need to update | ||
116 | // the modified flag. | 119 | // the modified flag. | ||
117 | kpColorCollection colorCol; | 120 | kpColorCollection colorCol; | ||
118 | 121 | | |||
119 | QUrl url; | 122 | QUrl url; | ||
120 | bool isModified; | 123 | bool isModified{}; | ||
121 | 124 | | |||
122 | bool blockColorChangedSig; | 125 | bool blockColorChangedSig{}; | ||
123 | }; | 126 | }; | ||
124 | 127 | | |||
125 | //--------------------------------------------------------------------- | 128 | //--------------------------------------------------------------------- | ||
126 | 129 | | |||
127 | kpColorCells::kpColorCells (QWidget *parent, | 130 | kpColorCells::kpColorCells (QWidget *parent, | ||
128 | Qt::Orientation o) | 131 | Qt::Orientation o) | ||
129 | : kpColorCellsBase (parent, 0/*rows for now*/, 0/*cols for now*/), | 132 | : kpColorCellsBase (parent, 0/*rows for now*/, 0/*cols for now*/), | ||
130 | d (new kpColorCellsPrivate ()) | 133 | d (new kpColorCellsPrivate ()) | ||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Line(s) | 250 | { | |||
250 | if (orientation () == Qt::Horizontal) | 253 | if (orientation () == Qt::Horizontal) | ||
251 | { | 254 | { | ||
252 | c = ::TableNumColumns (d->colorCol); | 255 | c = ::TableNumColumns (d->colorCol); | ||
253 | r = ::TableNumRows (d->colorCol); | 256 | r = ::TableNumRows (d->colorCol); | ||
254 | } | 257 | } | ||
255 | else | 258 | else | ||
256 | { | 259 | { | ||
257 | c = ::TableNumRows (d->colorCol); | 260 | c = ::TableNumRows (d->colorCol); | ||
258 | r = ::TableNumColumns (d->colorCol);; | 261 | r = ::TableNumColumns (d->colorCol); | ||
259 | } | 262 | } | ||
260 | 263 | | |||
261 | qCDebug(kpLogWidgets) << "kpColorCells::makeCellsMatchColorCollection():" | 264 | qCDebug(kpLogWidgets) << "kpColorCells::makeCellsMatchColorCollection():" | ||
262 | << "r=" << r << "c=" << c; | 265 | << "r=" << r << "c=" << c; | ||
263 | qCDebug(kpLogWidgets) << "verticalScrollBar=" << verticalScrollBar () | 266 | qCDebug(kpLogWidgets) << "verticalScrollBar=" << verticalScrollBar () | ||
264 | << " sizeHint=" | 267 | << " sizeHint=" | ||
265 | << (verticalScrollBar () ? | 268 | << (verticalScrollBar () ? | ||
266 | verticalScrollBar ()->sizeHint () : | 269 | verticalScrollBar ()->sizeHint () : | ||
Show All 10 Lines | |||||
277 | 280 | | |||
278 | int CellWidth = ::TableCellWidth (d->colorCol), | 281 | int CellWidth = ::TableCellWidth (d->colorCol), | ||
279 | CellHeight = ::TableCellHeight (d->colorCol); | 282 | CellHeight = ::TableCellHeight (d->colorCol); | ||
280 | 283 | | |||
281 | // TODO: Take a screenshot of KolourPaint, magnify it and you'll find the | 284 | // TODO: Take a screenshot of KolourPaint, magnify it and you'll find the | ||
282 | // cells don't have exactly the sizes requested here. e.g. the | 285 | // cells don't have exactly the sizes requested here. e.g. the | ||
283 | // top row of cells is 1 pixel shorter than the bottom row. There | 286 | // top row of cells is 1 pixel shorter than the bottom row. There | ||
284 | // are probably other glitches. | 287 | // are probably other glitches. | ||
285 | for (int y = 0; y < r; y++) | 288 | for (int y = 0; y < r; y++) { | ||
286 | setRowHeight (y, CellHeight); | 289 | setRowHeight (y, CellHeight); | ||
287 | for (int x = 0; x < c; x++) | 290 | } | ||
291 | for (int x = 0; x < c; x++) { | ||||
288 | setColumnWidth (x, CellWidth); | 292 | setColumnWidth (x, CellWidth); | ||
293 | } | ||||
289 | 294 | | |||
290 | 295 | | |||
291 | const bool oldBlockColorChangedSig = d->blockColorChangedSig; | 296 | const bool oldBlockColorChangedSig = d->blockColorChangedSig; | ||
292 | d->blockColorChangedSig = true; | 297 | d->blockColorChangedSig = true; | ||
293 | // The last "(rowCount() * columnCount()) - d->colorCol.count()" cells | 298 | // The last "(rowCount() * columnCount()) - d->colorCol.count()" cells | ||
294 | // will be empty because we did not initialize them. | 299 | // will be empty because we did not initialize them. | ||
295 | for (int i = 0; i < d->colorCol.count (); i++) | 300 | for (int i = 0; i < d->colorCol.count (); i++) | ||
296 | { | 301 | { | ||
Show All 9 Lines | |||||
306 | else | 311 | else | ||
307 | { | 312 | { | ||
308 | y = i % r; | 313 | y = i % r; | ||
309 | x = i / r; | 314 | x = i / r; | ||
310 | // int x = c - 1 - i / r; | 315 | // int x = c - 1 - i / r; | ||
311 | pos = y * c + x; | 316 | pos = y * c + x; | ||
312 | } | 317 | } | ||
313 | qCDebug(kpLogWidgets) << "\tSetting cell " << i << ": y=" << y << " x=" << x | 318 | qCDebug(kpLogWidgets) << "\tSetting cell " << i << ": y=" << y << " x=" << x | ||
314 | << " pos=" << pos << endl; | 319 | << " pos=" << pos; | ||
315 | qCDebug(kpLogWidgets) << "\t\tcolor=" << (int *) d->colorCol.color (i).rgba() | 320 | qCDebug(kpLogWidgets) << "\t\tcolor=" << (int *) d->colorCol.color (i).rgba() | ||
316 | << "isValid=" << d->colorCol.color (i).isValid (); | 321 | << "isValid=" << d->colorCol.color (i).isValid (); | ||
317 | 322 | | |||
318 | // (color may be invalid resulting in a hole in the middle of the table) | 323 | // (color may be invalid resulting in a hole in the middle of the table) | ||
319 | setColor (pos, d->colorCol.color (i)); | 324 | setColor (pos, d->colorCol.color (i)); | ||
320 | //this->setToolTip( cellGeometry (y, x), colors [i].name ()); | 325 | //this->setToolTip( cellGeometry (y, x), colors [i].name ()); | ||
321 | } | 326 | } | ||
322 | d->blockColorChangedSig = oldBlockColorChangedSig; | 327 | d->blockColorChangedSig = oldBlockColorChangedSig; | ||
323 | } | 328 | } | ||
324 | 329 | | |||
325 | //--------------------------------------------------------------------- | 330 | //--------------------------------------------------------------------- | ||
326 | 331 | | |||
327 | 332 | | |||
328 | bool kpColorCells::isModified () const | 333 | bool kpColorCells::isModified () const | ||
329 | { | 334 | { | ||
330 | return d->isModified; | 335 | return d->isModified; | ||
331 | } | 336 | } | ||
332 | 337 | | |||
333 | //--------------------------------------------------------------------- | 338 | //--------------------------------------------------------------------- | ||
334 | 339 | | |||
335 | void kpColorCells::setModified (bool yes) | 340 | void kpColorCells::setModified (bool yes) | ||
336 | { | 341 | { | ||
337 | qCDebug(kpLogWidgets) << "kpColorCells::setModified(" << yes << ")"; | 342 | qCDebug(kpLogWidgets) << "kpColorCells::setModified(" << yes << ")"; | ||
338 | 343 | | |||
339 | if (yes == d->isModified) | 344 | if (yes == d->isModified) { | ||
340 | return; | 345 | return; | ||
346 | } | ||||
341 | 347 | | |||
342 | d->isModified = yes; | 348 | d->isModified = yes; | ||
343 | 349 | | |||
344 | emit isModifiedChanged (yes); | 350 | emit isModifiedChanged (yes); | ||
345 | } | 351 | } | ||
346 | 352 | | |||
347 | //--------------------------------------------------------------------- | 353 | //--------------------------------------------------------------------- | ||
348 | 354 | | |||
Show All 25 Lines | 379 | { | |||
374 | return &d->colorCol; | 380 | return &d->colorCol; | ||
375 | } | 381 | } | ||
376 | 382 | | |||
377 | //--------------------------------------------------------------------- | 383 | //--------------------------------------------------------------------- | ||
378 | 384 | | |||
379 | 385 | | |||
380 | void kpColorCells::ensureHaveAtLeastOneRow () | 386 | void kpColorCells::ensureHaveAtLeastOneRow () | ||
381 | { | 387 | { | ||
382 | if (d->colorCol.count () == 0) | 388 | if (d->colorCol.count () == 0) { | ||
383 | d->colorCol.resize (::TableDefaultNumColumns); | 389 | d->colorCol.resize (::TableDefaultNumColumns); | ||
384 | } | 390 | } | ||
391 | } | ||||
385 | 392 | | |||
386 | //--------------------------------------------------------------------- | 393 | //--------------------------------------------------------------------- | ||
387 | 394 | | |||
388 | void kpColorCells::setColorCollection (const kpColorCollection &colorCol, const QUrl &url) | 395 | void kpColorCells::setColorCollection (const kpColorCollection &colorCol, const QUrl &url) | ||
389 | { | 396 | { | ||
390 | d->colorCol = colorCol; | 397 | d->colorCol = colorCol; | ||
391 | ensureHaveAtLeastOneRow (); | 398 | ensureHaveAtLeastOneRow (); | ||
392 | 399 | | |||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Line(s) | |||||
512 | //--------------------------------------------------------------------- | 519 | //--------------------------------------------------------------------- | ||
513 | 520 | | |||
514 | // protected slot | 521 | // protected slot | ||
515 | void kpColorCells::slotColorSelected (int cell, const QColor &color, | 522 | void kpColorCells::slotColorSelected (int cell, const QColor &color, | ||
516 | Qt::MouseButton button) | 523 | Qt::MouseButton button) | ||
517 | { | 524 | { | ||
518 | qCDebug(kpLogWidgets) << "kpColorCells::slotColorSelected(cell=" << cell | 525 | qCDebug(kpLogWidgets) << "kpColorCells::slotColorSelected(cell=" << cell | ||
519 | << ") mouseButton = " << button | 526 | << ") mouseButton = " << button | ||
520 | << " rgb=" << (int *) color.rgba() | 527 | << " rgb=" << (int *) color.rgba(); | ||
521 | << endl; | | |||
522 | 528 | | |||
523 | if (button == Qt::LeftButton) | 529 | if (button == Qt::LeftButton) | ||
524 | { | 530 | { | ||
525 | emit foregroundColorChanged (kpColor (color.rgba())); | 531 | emit foregroundColorChanged (kpColor (color.rgba())); | ||
526 | } | 532 | } | ||
527 | else if (button == Qt::RightButton) | 533 | else if (button == Qt::RightButton) | ||
528 | { | 534 | { | ||
529 | emit backgroundColorChanged (kpColor (color.rgba())); | 535 | emit backgroundColorChanged (kpColor (color.rgba())); | ||
Show All 28 Lines | |||||
558 | //--------------------------------------------------------------------- | 564 | //--------------------------------------------------------------------- | ||
559 | 565 | | |||
560 | // protected slot | 566 | // protected slot | ||
561 | void kpColorCells::slotColorChanged (int cell, const QColor &color) | 567 | void kpColorCells::slotColorChanged (int cell, const QColor &color) | ||
562 | { | 568 | { | ||
563 | qCDebug(kpLogWidgets) << "cell=" << cell << "color=" << (const int *) color.rgba() | 569 | qCDebug(kpLogWidgets) << "cell=" << cell << "color=" << (const int *) color.rgba() | ||
564 | << "d->colorCol.count()=" << d->colorCol.count (); | 570 | << "d->colorCol.count()=" << d->colorCol.count (); | ||
565 | 571 | | |||
566 | if (d->blockColorChangedSig) | 572 | if (d->blockColorChangedSig) { | ||
567 | return; | 573 | return; | ||
574 | } | ||||
568 | 575 | | |||
569 | // Cater for adding new colors to the end. | 576 | // Cater for adding new colors to the end. | ||
570 | if (cell >= d->colorCol.count ()) | 577 | if (cell >= d->colorCol.count ()) { | ||
571 | d->colorCol.resize (cell + 1); | 578 | d->colorCol.resize (cell + 1); | ||
579 | } | ||||
572 | 580 | | |||
573 | // TODO: We lose color names on a color swap (during drag-and-drop). | 581 | // TODO: We lose color names on a color swap (during drag-and-drop). | ||
574 | const int ret = d->colorCol.changeColor (cell, color, | 582 | const int ret = d->colorCol.changeColor (cell, color, | ||
575 | QString ()/*color name*/); | 583 | QString ()/*color name*/); | ||
576 | Q_ASSERT (ret == cell); | 584 | Q_ASSERT (ret == cell); | ||
577 | 585 | | |||
578 | setModified (true); | 586 | setModified (true); | ||
579 | } | 587 | } | ||
580 | 588 | |