summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@digia.com>2014-07-02 09:14:28 +0200
committerFriedemann Kleint <Friedemann.Kleint@digia.com>2014-07-08 12:05:59 +0200
commitea09c9961a36f3acd576ab9e761e53cce4cf4514 (patch)
tree34dc6fd175a945290af98b55aa393886a287a58e
parentcb7dd74bb52a24cc5de8109b60b676c3a9b1c83d (diff)
Refactor custom/standard color selectors in QColorDialog.
Introduce an enumeration for the rows/columns and use std::find() to find the matching colors. Change-Id: If8b7f76d48beab470f6cac0bfdeaf56058237e94 Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
-rw-r--r--src/widgets/dialogs/qcolordialog.cpp58
1 files changed, 33 insertions, 25 deletions
diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp
index 3f1773c88e..14c1ce081f 100644
--- a/src/widgets/dialogs/qcolordialog.cpp
+++ b/src/widgets/dialogs/qcolordialog.cpp
@@ -1468,38 +1468,46 @@ void QColorDialogPrivate::setCurrentQColor(const QColor &color)
}
}
+// size of standard and custom color selector
+enum {
+ colorColumns = 8,
+ standardColorRows = 6,
+ customColorRows = 2
+};
+
bool QColorDialogPrivate::selectColor(const QColor &col)
{
QRgb color = col.rgb();
- int i = 0, j = 0;
// Check standard colors
if (standard) {
const QRgb *standardColors = QColorDialogOptions::standardColors();
- for (i = 0; i < 6; i++) {
- for (j = 0; j < 8; j++) {
- if (color == standardColors[i + j*6]) {
- _q_newStandard(i, j);
- standard->setCurrent(i, j);
- standard->setSelected(i, j);
- standard->setFocus();
- return true;
- }
- }
+ const QRgb *standardColorsEnd = standardColors + standardColorRows * colorColumns;
+ const QRgb *match = std::find(standardColors, standardColorsEnd, color);
+ if (match != standardColorsEnd) {
+ const int index = int(match - standardColors);
+ const int row = index / standardColorRows;
+ const int column = index % standardColorRows;
+ _q_newStandard(row, column);
+ standard->setCurrent(row, column);
+ standard->setSelected(row, column);
+ standard->setFocus();
+ return true;
}
}
// Check custom colors
if (custom) {
const QRgb *customColors = QColorDialogOptions::customColors();
- for (i = 0; i < 2; i++) {
- for (j = 0; j < 8; j++) {
- if (color == customColors[i + j*2]) {
- _q_newCustom(i, j);
- custom->setCurrent(i, j);
- custom->setSelected(i, j);
- custom->setFocus();
- return true;
- }
- }
+ const QRgb *customColorsEnd = customColors + customColorRows * colorColumns;
+ const QRgb *match = std::find(customColors, customColorsEnd, color);
+ if (match != customColorsEnd) {
+ const int index = int(match - customColors);
+ const int row = index / customColorRows;
+ const int column = index % customColorRows;
+ _q_newCustom(row, column);
+ custom->setCurrent(row, column);
+ custom->setSelected(row, column);
+ custom->setFocus();
+ return true;
}
}
return false;
@@ -1527,12 +1535,12 @@ void QColorDialogPrivate::_q_newColorTypedIn(QRgb rgb)
void QColorDialogPrivate::_q_nextCustom(int r, int c)
{
- nextCust = r + 2 * c;
+ nextCust = r + customColorRows * c;
}
void QColorDialogPrivate::_q_newCustom(int r, int c)
{
- const int i = r + 2 * c;
+ const int i = r + customColorRows * c;
setCurrentRgbColor(QColorDialogOptions::customColor(i));
if (standard)
standard->setSelected(-1,-1);
@@ -1638,7 +1646,7 @@ void QColorDialogPrivate::initWidgets()
}
if (!smallDisplay) {
- standard = new QColorWell(q, 6, 8, QColorDialogOptions::standardColors());
+ standard = new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
lblBasicColors = new QLabel(q);
#ifndef QT_NO_SHORTCUT
lblBasicColors->setBuddy(standard);
@@ -1660,7 +1668,7 @@ void QColorDialogPrivate::initWidgets()
leftLay->addStretch();
#endif
- custom = new QColorWell(q, 2, 8, QColorDialogOptions::customColors());
+ custom = new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
custom->setAcceptDrops(true);
q->connect(custom, SIGNAL(selected(int,int)), SLOT(_q_newCustom(int,int)));