1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the dialog module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qcolordialog.h"
31
#ifndef QT_NO_COLORDIALOG
33
#include "qdialog_p.h"
34
#include "qapplication.h"
35
#include "qdesktopwidget.h"
36
#include "qdrawutil.h"
41
#include "qlineedit.h"
45
#include "qpushbutton.h"
46
#include "qsettings.h"
48
#include "qstyleoption.h"
49
#include "qvalidator.h"
54
QRgb macGetRgba(QRgb initial, bool *ok, QWidget *parent);
55
QColor macGetColor(const QColor& initial, QWidget *parent);
58
//////////// QWellArray BEGIN
60
struct QWellArrayData;
62
class QWellArray : public QWidget
65
Q_PROPERTY(int selectedColumn READ selectedColumn)
66
Q_PROPERTY(int selectedRow READ selectedRow)
69
QWellArray(int rows, int cols, QWidget* parent=0);
71
QString cellContent(int row, int col) const;
73
int selectedColumn() const { return selCol; }
74
int selectedRow() const { return selRow; }
76
virtual void setCurrent(int row, int col);
77
virtual void setSelected(int row, int col);
79
QSize sizeHint() const;
81
virtual void setCellBrush(int row, int col, const QBrush &);
82
QBrush cellBrush(int row, int col);
84
inline int cellWidth() const
87
inline int cellHeight() const
90
inline int rowAt(int y) const
93
inline int columnAt(int x) const
94
{ if (isRightToLeft()) return ncols - (x / cellw) - 1; return x / cellw; }
96
inline int rowY(int row) const
97
{ return cellh * row; }
99
inline int columnX(int column) const
100
{ if (isRightToLeft()) return cellw * (ncols - column - 1); return cellw * column; }
102
inline int numRows() const
105
inline int numCols() const
108
inline QRect cellRect() const
109
{ return QRect(0, 0, cellw, cellh); }
111
inline QSize gridSize() const
112
{ return QSize(ncols * cellw, nrows * cellh); }
114
QRect cellGeometry(int row, int column)
117
if (row >= 0 && row < nrows && column >= 0 && column < ncols)
118
r.setRect(columnX(column), rowY(row), cellw, cellh);
123
inline void updateCell(int row, int column) { update(cellGeometry(row, column)); }
127
void selected(int row, int col);
130
virtual void paintCell(QPainter *, int row, int col);
131
virtual void paintCellContents(QPainter *, int row, int col, const QRect&);
133
void mousePressEvent(QMouseEvent*);
134
void mouseReleaseEvent(QMouseEvent*);
135
void keyPressEvent(QKeyEvent*);
136
void focusInEvent(QFocusEvent*);
137
void focusOutEvent(QFocusEvent*);
138
void paintEvent(QPaintEvent *);
141
Q_DISABLE_COPY(QWellArray)
154
void QWellArray::paintEvent(QPaintEvent *e)
161
int colfirst = columnAt(cx);
162
int collast = columnAt(cx + cw);
163
int rowfirst = rowAt(cy);
164
int rowlast = rowAt(cy + ch);
166
if (isRightToLeft()) {
172
QPainter painter(this);
173
QPainter *p = &painter;
176
if (collast < 0 || collast >= ncols)
178
if (rowlast < 0 || rowlast >= nrows)
181
// Go through the rows
182
for (int r = rowfirst; r <= rowlast; ++r) {
183
// get row position and height
186
// Go through the columns in the row r
187
// if we know from where to where, go through [colfirst, collast],
188
// else go through all of them
189
for (int c = colfirst; c <= collast; ++c) {
190
// get position and width of column c
191
int colp = columnX(c);
192
// Translate painter and draw the cell
193
p->translate(colp, rowp);
195
p->translate(-colp, -rowp);
201
struct QWellArrayData {
205
QWellArray::QWellArray(int rows, int cols, QWidget *parent)
207
,nrows(rows), ncols(cols)
210
setFocusPolicy(Qt::StrongFocus);
221
QSize QWellArray::sizeHint() const
224
return gridSize().boundedTo(QSize(640, 480));
228
void QWellArray::paintCell(QPainter* p, int row, int col)
230
int w = cellWidth(); // width of cell in pixels
231
int h = cellHeight(); // height of cell in pixels
234
const QPalette & g = palette();
235
QStyleOptionFrame opt;
236
int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
238
opt.midLineWidth = 1;
239
opt.rect.setRect(b, b, w - 2 * b, h - 2 * b);
241
opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
242
style()->drawPrimitive(QStyle::PE_Frame, &opt, p, this);
245
if ((row == curRow) && (col == curCol)) {
247
QStyleOptionFocusRect opt;
249
opt.rect.setRect(0, 0, w, h);
250
opt.state = QStyle::State_None;
251
style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p, this);
254
QRect cr(b, b, w - 2*b, h - 2*b);
255
paintCellContents(p, row, col, cr);
259
Reimplement this function to change the contents of the well array.
261
void QWellArray::paintCellContents(QPainter *p, int row, int col, const QRect &r)
265
p->fillRect(r, d->brush[row*numCols()+col]);
267
p->fillRect(r, Qt::white);
268
p->setPen(Qt::black);
269
p->drawLine(r.topLeft(), r.bottomRight());
270
p->drawLine(r.topRight(), r.bottomLeft());
277
void QWellArray::mousePressEvent(QMouseEvent* e)
279
// The current cell marker is set to the cell the mouse is pressed
281
QPoint pos = e->pos();
282
setCurrent(rowAt(pos.y()), columnAt(pos.x()));
287
void QWellArray::mouseReleaseEvent(QMouseEvent*)
289
// The current cell marker is set to the cell the mouse is clicked
291
setSelected(curRow, curCol);
296
Sets the cell currently having the focus. This is not necessarily
297
the same as the currently selected cell.
300
void QWellArray::setCurrent(int row, int col)
303
if ((curRow == row) && (curCol == col))
306
if (row < 0 || col < 0)
315
updateCell(oldRow, oldCol);
316
updateCell(curRow, curCol);
321
Sets the currently selected cell to \a row, \a column. If \a row or
322
\a column are less than zero, the current cell is unselected.
324
Does not set the position of the focus indicator.
327
void QWellArray::setSelected(int row, int col)
329
if ((selRow == row) && (selCol == col))
335
if (row < 0 || col < 0)
341
updateCell(oldRow, oldCol);
342
updateCell(selRow, selCol);
344
emit selected(row, col);
346
if (isVisible() && qobject_cast<QMenu*>(parentWidget()))
347
parentWidget()->close();
354
void QWellArray::focusInEvent(QFocusEvent*)
356
updateCell(curRow, curCol);
360
void QWellArray::setCellBrush(int row, int col, const QBrush &b)
363
d = new QWellArrayData;
364
int i = numRows()*numCols();
365
d->brush = new QBrush[i];
367
if (row >= 0 && row < numRows() && col >= 0 && col < numCols())
368
d->brush[row*numCols()+col] = b;
374
Returns the brush set for the cell at \a row, \a column. If no brush is
375
set, \c Qt::NoBrush is returned.
378
QBrush QWellArray::cellBrush(int row, int col)
380
if (d && row >= 0 && row < numRows() && col >= 0 && col < numCols())
381
return d->brush[row*numCols()+col];
390
void QWellArray::focusOutEvent(QFocusEvent*)
392
updateCell(curRow, curCol);
397
void QWellArray::keyPressEvent(QKeyEvent* e)
399
switch(e->key()) { // Look at the key code
400
case Qt::Key_Left: // If 'left arrow'-key,
401
if(curCol > 0) // and cr't not in leftmost col
402
setCurrent(curRow, curCol - 1); // set cr't to next left column
404
case Qt::Key_Right: // Correspondingly...
405
if(curCol < numCols()-1)
406
setCurrent(curRow, curCol + 1);
410
setCurrent(curRow - 1, curCol);
413
if(curRow < numRows()-1)
414
setCurrent(curRow + 1, curCol);
419
setSelected(curRow, curCol);
421
default: // If not an interesting key,
422
e->ignore(); // we don't accept the event
428
//////////// QWellArray END
430
static bool initrgb = false;
431
static QRgb stdrgb[6*8];
432
static QRgb cusrgb[2*8];
433
static bool customSet = false;
436
static void initRGB()
442
for (int g = 0; g < 4; g++)
443
for (int r = 0; r < 4; r++)
444
for (int b = 0; b < 3; b++)
445
stdrgb[i++] = qRgb(r*255/3, g*255/3, b*255/2);
447
for (i = 0; i < 2*8; i++)
448
cusrgb[i] = 0xffffffff;
452
Returns the number of custom colors supported by QColorDialog. All
453
color dialogs share the same custom colors.
455
int QColorDialog::customCount()
461
Returns custom color number \a i as a QRgb value.
463
QRgb QColorDialog::customColor(int i)
466
Q_ASSERT(i >= 0 && i < customCount());
471
\fn void QColorDialog::setCustomColor(int number, QRgb color)
473
Sets the custom color \a number to the QRgb \a color value.
475
void QColorDialog::setCustomColor(int i, QRgb c)
478
Q_ASSERT(i >= 0 && i < customCount());
484
\fn void QColorDialog::setStandardColor(int number, QRgb color)
486
Sets the standard color \a number to the QRgb \a color value given.
489
void QColorDialog::setStandardColor(int i, QRgb c)
492
Q_ASSERT(i >= 0 && i < 6*8);
496
static inline void rgb2hsv(QRgb rgb, int&h, int&s, int&v)
500
c.getHsv(&h, &s, &v);
503
class QColorWell : public QWellArray
506
QColorWell(QWidget *parent, int r, int c, QRgb *vals)
507
:QWellArray(r, c, parent), values(vals), mousePressed(false), oldCurrent(-1, -1)
508
{ setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); }
511
void paintCellContents(QPainter *, int row, int col, const QRect&);
512
void mousePressEvent(QMouseEvent *e);
513
void mouseMoveEvent(QMouseEvent *e);
514
void mouseReleaseEvent(QMouseEvent *e);
515
#ifndef QT_NO_DRAGANDDROP
516
void dragEnterEvent(QDragEnterEvent *e);
517
void dragLeaveEvent(QDragLeaveEvent *e);
518
void dragMoveEvent(QDragMoveEvent *e);
519
void dropEvent(QDropEvent *e);
530
void QColorWell::paintCellContents(QPainter *p, int row, int col, const QRect &r)
532
int i = row + col*numRows();
533
p->fillRect(r, QColor(values[i]));
536
void QColorWell::mousePressEvent(QMouseEvent *e)
538
oldCurrent = QPoint(selectedRow(), selectedColumn());
539
QWellArray::mousePressEvent(e);
544
void QColorWell::mouseMoveEvent(QMouseEvent *e)
546
QWellArray::mouseMoveEvent(e);
547
#ifndef QT_NO_DRAGANDDROP
550
if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
551
setCurrent(oldCurrent.x(), oldCurrent.y());
552
int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
553
QColor col(values[i]);
554
QMimeData *mime = new QMimeData;
555
mime->setColorData(col);
556
QPixmap pix(cellWidth(), cellHeight());
559
p.drawRect(0, 0, pix.width(), pix.height());
561
QDrag *drg = new QDrag(this);
562
drg->setMimeData(mime);
564
mousePressed = false;
570
#ifndef QT_NO_DRAGANDDROP
571
void QColorWell::dragEnterEvent(QDragEnterEvent *e)
573
if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
579
void QColorWell::dragLeaveEvent(QDragLeaveEvent *)
582
parentWidget()->setFocus();
585
void QColorWell::dragMoveEvent(QDragMoveEvent *e)
587
if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) {
588
setCurrent(rowAt(e->pos().y()), columnAt(e->pos().x()));
595
void QColorWell::dropEvent(QDropEvent *e)
597
QColor col = qvariant_cast<QColor>(e->mimeData()->colorData());
599
int i = rowAt(e->pos().y()) + columnAt(e->pos().x()) * numRows();
600
values[i] = col.rgb();
608
#endif // QT_NO_DRAGANDDROP
610
void QColorWell::mouseReleaseEvent(QMouseEvent *e)
614
QWellArray::mouseReleaseEvent(e);
615
mousePressed = false;
618
class QColorPicker : public QFrame
622
QColorPicker(QWidget* parent);
626
void setCol(int h, int s);
629
void newCol(int h, int s);
632
QSize sizeHint() const;
633
void paintEvent(QPaintEvent*);
634
void mouseMoveEvent(QMouseEvent *);
635
void mousePressEvent(QMouseEvent *);
642
int huePt(const QPoint &pt);
643
int satPt(const QPoint &pt);
644
void setCol(const QPoint &pt);
649
static int pWidth = 220;
650
static int pHeight = 200;
652
class QColorLuminancePicker : public QWidget
656
QColorLuminancePicker(QWidget* parent=0);
657
~QColorLuminancePicker();
660
void setCol(int h, int s, int v);
661
void setCol(int h, int s);
664
void newHsv(int h, int s, int v);
667
void paintEvent(QPaintEvent*);
668
void mouseMoveEvent(QMouseEvent *);
669
void mousePressEvent(QMouseEvent *);
672
enum { foff = 3, coff = 4 }; //frame and contents offset
685
int QColorLuminancePicker::y2val(int y)
687
int d = height() - 2*coff - 1;
688
return 255 - (y - coff)*255/d;
691
int QColorLuminancePicker::val2y(int v)
693
int d = height() - 2*coff - 1;
694
return coff + (255-v)*d/255;
697
QColorLuminancePicker::QColorLuminancePicker(QWidget* parent)
700
hue = 100; val = 100; sat = 100;
702
// setAttribute(WA_NoErase, true);
705
QColorLuminancePicker::~QColorLuminancePicker()
710
void QColorLuminancePicker::mouseMoveEvent(QMouseEvent *m)
712
setVal(y2val(m->y()));
714
void QColorLuminancePicker::mousePressEvent(QMouseEvent *m)
716
setVal(y2val(m->y()));
719
void QColorLuminancePicker::setVal(int v)
723
val = qMax(0, qMin(v,255));
726
emit newHsv(hue, sat, val);
729
//receives from a hue,sat chooser and relays.
730
void QColorLuminancePicker::setCol(int h, int s)
733
emit newHsv(h, s, val);
736
void QColorLuminancePicker::paintEvent(QPaintEvent *)
740
QRect r(0, foff, w, height() - 2*foff);
741
int wi = r.width() - 2;
742
int hi = r.height() - 2;
743
if (!pix || pix->height() != hi || pix->width() != wi) {
745
QImage img(wi, hi, QImage::Format_RGB32);
747
for (y = 0; y < hi; y++) {
749
c.setHsv(hue, sat, y2val(y+coff));
752
for (x = 0; x < wi; x++)
753
img.setPixel(x, y, r);
755
pix = new QPixmap(QPixmap::fromImage(img));
758
p.drawPixmap(1, coff, *pix);
759
const QPalette &g = palette();
760
qDrawShadePanel(&p, r, g, true);
761
p.setPen(g.foreground().color());
762
p.setBrush(g.foreground());
765
a.setPoints(3, w, y, w+5, y+5, w+5, y-5);
766
p.eraseRect(w, 0, 5, height());
770
void QColorLuminancePicker::setCol(int h, int s , int v)
779
QPoint QColorPicker::colPt()
780
{ return QPoint((360-hue)*(pWidth-1)/360, (255-sat)*(pHeight-1)/255); }
781
int QColorPicker::huePt(const QPoint &pt)
782
{ return 360 - pt.x()*360/(pWidth-1); }
783
int QColorPicker::satPt(const QPoint &pt)
784
{ return 255 - pt.y()*255/(pHeight-1) ; }
785
void QColorPicker::setCol(const QPoint &pt)
786
{ setCol(huePt(pt), satPt(pt)); }
788
QColorPicker::QColorPicker(QWidget* parent)
794
QImage img(pWidth, pHeight, QImage::Format_RGB32);
796
for (y = 0; y < pHeight; y++)
797
for (x = 0; x < pWidth; x++) {
800
c.setHsv(huePt(p), satPt(p), 200);
801
img.setPixel(x, y, c.rgb());
803
pix = new QPixmap(QPixmap::fromImage(img));
804
setAttribute(Qt::WA_NoSystemBackground);
805
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
808
QColorPicker::~QColorPicker()
813
QSize QColorPicker::sizeHint() const
815
return QSize(pWidth + 2*frameWidth(), pHeight + 2*frameWidth());
818
void QColorPicker::setCol(int h, int s)
820
int nhue = qMin(qMax(0,h), 359);
821
int nsat = qMin(qMax(0,s), 255);
822
if (nhue == hue && nsat == sat)
824
QRect r(colPt(), QSize(20,20));
825
hue = nhue; sat = nsat;
826
r = r.unite(QRect(colPt(), QSize(20,20)));
827
r.translate(contentsRect().x()-9, contentsRect().y()-9);
832
void QColorPicker::mouseMoveEvent(QMouseEvent *m)
834
QPoint p = m->pos() - contentsRect().topLeft();
836
emit newCol(hue, sat);
839
void QColorPicker::mousePressEvent(QMouseEvent *m)
841
QPoint p = m->pos() - contentsRect().topLeft();
843
emit newCol(hue, sat);
846
void QColorPicker::paintEvent(QPaintEvent* )
850
QRect r = contentsRect();
852
p.drawPixmap(r.topLeft(), *pix);
853
QPoint pt = colPt() + r.topLeft();
856
p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
857
p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
861
class QColSpinBox : public QSpinBox
864
QColSpinBox(QWidget *parent)
865
: QSpinBox(parent) { setRange(0, 255); }
866
void setValue(int i) {
867
bool block = signalsBlocked();
869
QSpinBox::setValue(i);
874
class QColorShowLabel;
876
class QColorShower : public QWidget
880
QColorShower(QWidget *parent);
882
//things that don't emit signals
883
void setHsv(int h, int s, int v);
885
int currentAlpha() const { return alphaEd->value(); }
886
void setCurrentAlpha(int a) { alphaEd->setValue(a); }
887
void showAlpha(bool b);
890
QRgb currentColor() const { return curCol; }
893
void setRgb(QRgb rgb);
896
void newCol(QRgb rgb);
901
void showCurrentColor();
910
QColSpinBox *alphaEd;
912
QColorShowLabel *lab;
916
class QColorShowLabel : public QFrame
921
QColorShowLabel(QWidget *parent) : QFrame(parent) {
922
setFrameStyle(QFrame::Panel|QFrame::Sunken);
923
setAcceptDrops(true);
924
mousePressed = false;
926
void setColor(QColor c) { col = c; }
929
void colorDropped(QRgb);
932
void paintEvent(QPaintEvent *);
933
void mousePressEvent(QMouseEvent *e);
934
void mouseMoveEvent(QMouseEvent *e);
935
void mouseReleaseEvent(QMouseEvent *e);
936
#ifndef QT_NO_DRAGANDDROP
937
void dragEnterEvent(QDragEnterEvent *e);
938
void dragLeaveEvent(QDragLeaveEvent *e);
939
void dropEvent(QDropEvent *e);
949
void QColorShowLabel::paintEvent(QPaintEvent *e)
953
p.fillRect(contentsRect()&e->rect(), col);
956
void QColorShower::showAlpha(bool b)
967
void QColorShowLabel::mousePressEvent(QMouseEvent *e)
973
void QColorShowLabel::mouseMoveEvent(QMouseEvent *e)
975
#ifndef QT_NO_DRAGANDDROP
978
if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
979
QMimeData *mime = new QMimeData;
980
mime->setColorData(col);
984
p.drawRect(0, 0, pix.width(), pix.height());
986
QDrag *drg = new QDrag(this);
987
drg->setMimeData(mime);
989
mousePressed = false;
995
#ifndef QT_NO_DRAGANDDROP
996
void QColorShowLabel::dragEnterEvent(QDragEnterEvent *e)
998
if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
1004
void QColorShowLabel::dragLeaveEvent(QDragLeaveEvent *)
1008
void QColorShowLabel::dropEvent(QDropEvent *e)
1010
QColor color = qvariant_cast<QColor>(e->mimeData()->colorData());
1011
if (color.isValid()) {
1014
emit colorDropped(col.rgb());
1020
#endif // QT_NO_DRAGANDDROP
1022
void QColorShowLabel::mouseReleaseEvent(QMouseEvent *)
1026
mousePressed = false;
1029
QColorShower::QColorShower(QWidget *parent)
1032
curCol = qRgb(-1, -1, -1);
1034
QGridLayout *gl = new QGridLayout(this);
1036
lab = new QColorShowLabel(this);
1037
lab->setMinimumWidth(60);
1038
gl->addWidget(lab, 0, 0, -1, 1);
1039
connect(lab, SIGNAL(colorDropped(QRgb)),
1040
this, SIGNAL(newCol(QRgb)));
1041
connect(lab, SIGNAL(colorDropped(QRgb)),
1042
this, SLOT(setRgb(QRgb)));
1044
hEd = new QColSpinBox(this);
1045
hEd->setRange(0, 359);
1046
QLabel *l = new QLabel(QColorDialog::tr("Hu&e:"), this);
1048
l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1049
gl->addWidget(l, 0, 1);
1050
gl->addWidget(hEd, 0, 2);
1052
sEd = new QColSpinBox(this);
1053
l = new QLabel(QColorDialog::tr("&Sat:"), this);
1055
l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1056
gl->addWidget(l, 1, 1);
1057
gl->addWidget(sEd, 1, 2);
1059
vEd = new QColSpinBox(this);
1060
l = new QLabel(QColorDialog::tr("&Val:"), this);
1062
l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1063
gl->addWidget(l, 2, 1);
1064
gl->addWidget(vEd, 2, 2);
1066
rEd = new QColSpinBox(this);
1067
l = new QLabel(QColorDialog::tr("&Red:"), this);
1069
l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1070
gl->addWidget(l, 0, 3);
1071
gl->addWidget(rEd, 0, 4);
1073
gEd = new QColSpinBox(this);
1074
l = new QLabel(QColorDialog::tr("&Green:"), this);
1076
l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1077
gl->addWidget(l, 1, 3);
1078
gl->addWidget(gEd, 1, 4);
1080
bEd = new QColSpinBox(this);
1081
l = new QLabel(QColorDialog::tr("Bl&ue:"), this);
1083
l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1084
gl->addWidget(l, 2, 3);
1085
gl->addWidget(bEd, 2, 4);
1087
alphaEd = new QColSpinBox(this);
1088
alphaLab = new QLabel(QColorDialog::tr("A&lpha channel:"), this);
1089
l->setBuddy(alphaEd);
1090
alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1091
gl->addWidget(alphaLab, 3, 1, 1, 3);
1092
gl->addWidget(alphaEd, 3, 4);
1096
connect(hEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
1097
connect(sEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
1098
connect(vEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
1100
connect(rEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
1101
connect(gEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
1102
connect(bEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
1103
connect(alphaEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
1106
void QColorShower::showCurrentColor()
1108
lab->setColor(currentColor());
1112
void QColorShower::rgbEd()
1115
if (alphaEd->isVisible())
1116
curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
1118
curCol = qRgb(rEd->value(), gEd->value(), bEd->value());
1120
rgb2hsv(currentColor(), hue, sat, val);
1127
emit newCol(currentColor());
1130
void QColorShower::hsvEd()
1132
rgbOriginal = false;
1138
c.setHsv(hue, sat, val);
1141
rEd->setValue(qRed(currentColor()));
1142
gEd->setValue(qGreen(currentColor()));
1143
bEd->setValue(qBlue(currentColor()));
1146
emit newCol(currentColor());
1149
void QColorShower::setRgb(QRgb rgb)
1154
rgb2hsv(currentColor(), hue, sat, val);
1160
rEd->setValue(qRed(currentColor()));
1161
gEd->setValue(qGreen(currentColor()));
1162
bEd->setValue(qBlue(currentColor()));
1167
void QColorShower::setHsv(int h, int s, int v)
1169
if (h < -1 || (uint)s > 255 || (uint)v > 255)
1172
rgbOriginal = false;
1173
hue = h; val = v; sat = s;
1175
c.setHsv(hue, sat, val);
1182
rEd->setValue(qRed(currentColor()));
1183
gEd->setValue(qGreen(currentColor()));
1184
bEd->setValue(qBlue(currentColor()));
1189
class QColorDialogPrivate : public QDialogPrivate
1191
Q_DECLARE_PUBLIC(QColorDialog)
1194
QRgb currentColor() const { return cs->currentColor(); }
1195
void setCurrentColor(QRgb rgb);
1197
int currentAlpha() const { return cs->currentAlpha(); }
1198
void setCurrentAlpha(int a) { cs->setCurrentAlpha(a); }
1199
void showAlpha(bool b) { cs->showAlpha(b); }
1203
void newHsv(int h, int s, int v);
1204
void newColorTypedIn(QRgb rgb);
1205
void newCustom(int, int);
1206
void newStandard(int, int);
1209
QWellArray *standard;
1212
QColorLuminancePicker *lp;
1218
//sets all widgets to display h,s,v
1219
void QColorDialogPrivate::newHsv(int h, int s, int v)
1221
cs->setHsv(h, s, v);
1223
lp->setCol(h, s, v);
1226
//sets all widgets to display rgb
1227
void QColorDialogPrivate::setCurrentColor(QRgb rgb)
1230
newColorTypedIn(rgb);
1233
//sets all widgets exept cs to display rgb
1234
void QColorDialogPrivate::newColorTypedIn(QRgb rgb)
1237
rgb2hsv(rgb, h, s, v);
1239
lp->setCol(h, s, v);
1242
void QColorDialogPrivate::newCustom(int r, int c)
1245
setCurrentColor(cusrgb[i]);
1248
standard->setSelected(-1,-1);
1251
void QColorDialogPrivate::newStandard(int r, int c)
1253
setCurrentColor(stdrgb[r+c*6]);
1255
custom->setSelected(-1,-1);
1258
void QColorDialogPrivate::init()
1262
// small displays (e.g. PDAs cannot fit the full color dialog,
1263
// so just use the color picker.
1264
if (qApp->desktop()->width() < 480 || qApp->desktop()->height() < 350)
1268
const int lumSpace = 3;
1272
QHBoxLayout *topLay = new QHBoxLayout(q);
1273
topLay->setMargin(border);
1274
topLay->setSpacing(6);
1275
QVBoxLayout *leftLay = 0;
1278
leftLay = new QVBoxLayout;
1279
topLay->addLayout(leftLay);
1285
standard = new QColorWell(q, 6, 8, stdrgb);
1286
QLabel *lab = new QLabel(QColorDialog::tr("&Basic colors"), q);
1287
lab->setBuddy(standard);
1288
q->connect(standard, SIGNAL(selected(int,int)), SLOT(newStandard(int,int)));
1289
leftLay->addWidget(lab);
1290
leftLay->addWidget(standard);
1293
leftLay->addStretch();
1295
custom = new QColorWell(q, 2, 8, cusrgb);
1296
custom->setAcceptDrops(true);
1298
q->connect(custom, SIGNAL(selected(int,int)), SLOT(newCustom(int,int)));
1299
lab = new QLabel(QColorDialog::tr("&Custom colors") , q);
1300
lab->setBuddy(custom);
1301
leftLay->addWidget(lab);
1302
leftLay->addWidget(custom);
1304
QPushButton *custbut = new QPushButton(QColorDialog::tr("&Define Custom Colors >>"), q);
1305
custbut->setEnabled(false);
1306
leftLay->addWidget(custbut);
1308
// better color picker size for small displays
1315
QVBoxLayout *rightLay = new QVBoxLayout;
1316
topLay->addLayout(rightLay);
1318
QHBoxLayout *pickLay = new QHBoxLayout;
1319
rightLay->addLayout(pickLay);
1322
QVBoxLayout *cLay = new QVBoxLayout;
1323
pickLay->addLayout(cLay);
1324
cp = new QColorPicker(q);
1325
cp->setFrameStyle(QFrame::Panel + QFrame::Sunken);
1326
cLay->addSpacing(lumSpace);
1327
cLay->addWidget(cp);
1328
cLay->addSpacing(lumSpace);
1330
lp = new QColorLuminancePicker(q);
1331
lp->setFixedWidth(20);
1332
pickLay->addWidget(lp);
1334
QObject::connect(cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int)));
1335
QObject::connect(lp, SIGNAL(newHsv(int,int,int)), q, SLOT(newHsv(int,int,int)));
1337
rightLay->addStretch();
1339
cs = new QColorShower(q);
1340
QObject::connect(cs, SIGNAL(newCol(QRgb)), q, SLOT(newColorTypedIn(QRgb)));
1341
rightLay->addWidget(cs);
1343
QHBoxLayout *buttons;
1345
buttons = new QHBoxLayout;
1346
rightLay->addLayout(buttons);
1348
buttons = new QHBoxLayout;
1349
leftLay->addLayout(buttons);
1352
QPushButton *ok, *cancel;
1353
ok = new QPushButton(QColorDialog::tr("OK"), q);
1354
QObject::connect(ok, SIGNAL(clicked()), q, SLOT(accept()));
1355
ok->setDefault(true);
1356
cancel = new QPushButton(QColorDialog::tr("Cancel"), q);
1357
QObject::connect(cancel, SIGNAL(clicked()), q, SLOT(reject()));
1358
buttons->addWidget(ok);
1359
buttons->addWidget(cancel);
1360
buttons->addStretch();
1363
QPushButton *addCusBt = new QPushButton(QColorDialog::tr("&Add to Custom Colors"), q);
1364
rightLay->addWidget(addCusBt);
1365
QObject::connect(addCusBt, SIGNAL(clicked()), q, SLOT(addCustom()));
1369
void QColorDialogPrivate::addCustom()
1371
cusrgb[nextCust] = cs->currentColor();
1374
nextCust = (nextCust+1) % 16;
1379
\class QColorDialog qcolordialog.h
1380
\brief The QColorDialog class provides a dialog widget for specifying colors.
1386
The color dialog's function is to allow users to choose colors.
1387
For example, you might use this in a drawing program to allow the
1388
user to set the brush color.
1390
The static functions provide modal color dialogs.
1392
If you require a modeless dialog, use the QColorDialog constructor.
1395
The static getColor() function shows the dialog, and allows the
1396
user to specify a color. The getRgba() function does the same, but
1397
also allows the user to specify a color with an alpha channel
1398
(transparency) value.
1400
The user can store customCount() different custom colors. The
1401
custom colors are shared by all color dialogs, and remembered
1402
during the execution of the program. Use setCustomColor() to set
1403
the custom colors, and use customColor() to get them.
1405
Additional widgets that allow users to pick colors are available
1407
http://www.trolltech.com/products/solutions/index.html Qt
1410
The \l{dialogs/standarddialogs}{Standard Dialogs} example shows
1411
how to use QColorDialog as well as other built-in Qt dialogs.
1413
\img qcolordlg-w.png
1417
Constructs a default color dialog called \a name with the given \a parent.
1418
If \a modal is true the dialog will be modal. Use setColor() to set an
1424
QColorDialog::QColorDialog(QWidget* parent, bool modal) :
1425
QDialog(*new QColorDialogPrivate, parent, (Qt::Dialog | Qt::WindowTitleHint |
1426
Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint))
1430
setSizeGripEnabled(false);
1433
#ifndef QT_NO_SETTINGS
1435
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
1436
settings.beginGroup(QLatin1String("Qt"));
1437
for (int i = 0; i < 2*8; ++i) {
1438
QVariant v = settings.value(QLatin1String("customColors/") + QString::number(i));
1440
QRgb rgb = v.toUInt();
1444
settings.endGroup(); // Qt
1450
Pops up a modal color dialog, lets the user choose a color, and
1451
returns that color. The color is initially set to \a initial. The
1452
dialog is a child of \a parent. It returns an invalid (see
1453
QColor::isValid()) color if the user cancels the dialog. All
1454
colors allocated by the dialog will be deallocated before this
1458
QColor QColorDialog::getColor(const QColor& initial, QWidget *parent)
1460
#if defined(Q_WS_MAC)
1461
return macGetColor(initial, parent);
1464
QColorDialog *dlg = new QColorDialog(parent, true); //modal
1465
dlg->setWindowTitle(QColorDialog::tr("Select color"));
1466
dlg->setColor(initial);
1467
dlg->selectColor(initial);
1468
int resultCode = dlg->exec();
1470
if (resultCode == QDialog::Accepted)
1471
result = dlg->color();
1478
Pops up a modal color dialog to allow the user to choose a color
1479
and an alpha channel (transparency) value. The color+alpha is
1480
initially set to \a initial. The dialog is a child of \a parent.
1482
If \a ok is non-null, \e *\a ok is set to true if the user clicked
1483
OK, and to false if the user clicked Cancel.
1485
If the user clicks Cancel, the \a initial value is returned.
1488
QRgb QColorDialog::getRgba(QRgb initial, bool *ok, QWidget *parent)
1490
#if defined(Q_WS_MAC)
1491
return macGetRgba(initial, ok, parent);
1494
QColorDialog *dlg = new QColorDialog(parent, true); //modal
1496
dlg->setWindowTitle(QColorDialog::tr("Select color"));
1497
dlg->setColor(initial);
1498
dlg->selectColor(initial);
1499
dlg->setSelectedAlpha(qAlpha(initial));
1500
int resultCode = dlg->exec();
1501
QRgb result = initial;
1502
if (resultCode == QDialog::Accepted) {
1503
QRgb c = dlg->color().rgb();
1504
int alpha = dlg->selectedAlpha();
1505
result = qRgba(qRed(c), qGreen(c), qBlue(c), alpha);
1508
*ok = resultCode == QDialog::Accepted;
1519
Returns the currently selected color in the dialog.
1524
QColor QColorDialog::color() const
1526
Q_D(const QColorDialog);
1527
return QColor(d->currentColor());
1532
Destroys the color dialog.
1535
QColorDialog::~QColorDialog()
1537
#ifndef QT_NO_SETTINGS
1539
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
1540
settings.beginGroup(QLatin1String("Qt"));
1541
for (int i = 0; i < 2*8; ++i)
1542
settings.setValue(QLatin1String("customColors/") + QString::number(i), cusrgb[i]);
1543
settings.endGroup();
1550
\fn void QColorDialog::setColor(const QColor &color)
1552
Sets the color shown in the dialog to the \a color given.
1557
void QColorDialog::setColor(const QColor& c)
1560
d->setCurrentColor(c.rgb());
1567
\fn void QColorDialog::setSelectedAlpha(int alpha)
1569
Sets the initial alpha channel value to the \a alpha value given, and
1570
shows the alpha channel entry box.
1573
void QColorDialog::setSelectedAlpha(int a)
1577
d->setCurrentAlpha(a);
1582
Returns the value selected for the alpha channel.
1585
int QColorDialog::selectedAlpha() const
1587
Q_D(const QColorDialog);
1588
return d->currentAlpha();
1592
Sets focus to the corresponding button, if any.
1594
bool QColorDialog::selectColor(const QColor& col)
1597
QRgb color = col.rgb();
1599
// Check standard colors
1601
for (i = 0; i < 6; i++) {
1602
for (j = 0; j < 8; j++) {
1603
if (color == stdrgb[i + j*6]) {
1604
d->newStandard(i, j);
1605
d->standard->setCurrent(i, j);
1606
d->standard->setSelected(i, j);
1607
d->standard->setFocus();
1613
// Check custom colors
1615
for (i = 0; i < 2; i++) {
1616
for (j = 0; j < 8; j++) {
1617
if (color == cusrgb[i + j*2]) {
1619
d->custom->setCurrent(i, j);
1620
d->custom->setSelected(i, j);
1621
d->custom->setFocus();
1630
#include "qcolordialog.moc"
1631
#include "moc_qcolordialog.cpp"
1636
\fn QColor QColorDialog::getColor(const QColor &init, QWidget *parent, const char *name)
1641
\fn QRgb QColorDialog::getRgba(QRgb rgba, bool *ok, QWidget *parent, const char *name)