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>
This commit is contained in:
Friedemann Kleint 2014-07-02 09:14:28 +02:00
parent cb7dd74bb5
commit ea09c9961a

View File

@ -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) bool QColorDialogPrivate::selectColor(const QColor &col)
{ {
QRgb color = col.rgb(); QRgb color = col.rgb();
int i = 0, j = 0;
// Check standard colors // Check standard colors
if (standard) { if (standard) {
const QRgb *standardColors = QColorDialogOptions::standardColors(); const QRgb *standardColors = QColorDialogOptions::standardColors();
for (i = 0; i < 6; i++) { const QRgb *standardColorsEnd = standardColors + standardColorRows * colorColumns;
for (j = 0; j < 8; j++) { const QRgb *match = std::find(standardColors, standardColorsEnd, color);
if (color == standardColors[i + j*6]) { if (match != standardColorsEnd) {
_q_newStandard(i, j); const int index = int(match - standardColors);
standard->setCurrent(i, j); const int row = index / standardColorRows;
standard->setSelected(i, j); const int column = index % standardColorRows;
standard->setFocus(); _q_newStandard(row, column);
return true; standard->setCurrent(row, column);
} standard->setSelected(row, column);
} standard->setFocus();
return true;
} }
} }
// Check custom colors // Check custom colors
if (custom) { if (custom) {
const QRgb *customColors = QColorDialogOptions::customColors(); const QRgb *customColors = QColorDialogOptions::customColors();
for (i = 0; i < 2; i++) { const QRgb *customColorsEnd = customColors + customColorRows * colorColumns;
for (j = 0; j < 8; j++) { const QRgb *match = std::find(customColors, customColorsEnd, color);
if (color == customColors[i + j*2]) { if (match != customColorsEnd) {
_q_newCustom(i, j); const int index = int(match - customColors);
custom->setCurrent(i, j); const int row = index / customColorRows;
custom->setSelected(i, j); const int column = index % customColorRows;
custom->setFocus(); _q_newCustom(row, column);
return true; custom->setCurrent(row, column);
} custom->setSelected(row, column);
} custom->setFocus();
return true;
} }
} }
return false; return false;
@ -1527,12 +1535,12 @@ void QColorDialogPrivate::_q_newColorTypedIn(QRgb rgb)
void QColorDialogPrivate::_q_nextCustom(int r, int c) void QColorDialogPrivate::_q_nextCustom(int r, int c)
{ {
nextCust = r + 2 * c; nextCust = r + customColorRows * c;
} }
void QColorDialogPrivate::_q_newCustom(int r, int 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)); setCurrentRgbColor(QColorDialogOptions::customColor(i));
if (standard) if (standard)
standard->setSelected(-1,-1); standard->setSelected(-1,-1);
@ -1638,7 +1646,7 @@ void QColorDialogPrivate::initWidgets()
} }
if (!smallDisplay) { if (!smallDisplay) {
standard = new QColorWell(q, 6, 8, QColorDialogOptions::standardColors()); standard = new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
lblBasicColors = new QLabel(q); lblBasicColors = new QLabel(q);
#ifndef QT_NO_SHORTCUT #ifndef QT_NO_SHORTCUT
lblBasicColors->setBuddy(standard); lblBasicColors->setBuddy(standard);
@ -1660,7 +1668,7 @@ void QColorDialogPrivate::initWidgets()
leftLay->addStretch(); leftLay->addStretch();
#endif #endif
custom = new QColorWell(q, 2, 8, QColorDialogOptions::customColors()); custom = new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
custom->setAcceptDrops(true); custom->setAcceptDrops(true);
q->connect(custom, SIGNAL(selected(int,int)), SLOT(_q_newCustom(int,int))); q->connect(custom, SIGNAL(selected(int,int)), SLOT(_q_newCustom(int,int)));