src/gui/dialogs/qcolordialog.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the QtGui module of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "qcolordialog.h"
00025 
00026 #ifndef QT_NO_COLORDIALOG
00027 
00028 #include "qdialog_p.h"
00029 #include "qapplication.h"
00030 #include "qdesktopwidget.h"
00031 #include "qdrawutil.h"
00032 #include "qevent.h"
00033 #include "qimage.h"
00034 #include "qlabel.h"
00035 #include "qlayout.h"
00036 #include "qlineedit.h"
00037 #include "qmenu.h"
00038 #include "qpainter.h"
00039 #include "qpixmap.h"
00040 #include "qpushbutton.h"
00041 #include "qsettings.h"
00042 #include "qstyle.h"
00043 #include "qstyleoption.h"
00044 #include "qvalidator.h"
00045 #include "qmime.h"
00046 #include "qspinbox.h"
00047 
00048 #ifdef Q_WS_MAC
00049 QRgb macGetRgba(QRgb initial, bool *ok, QWidget *parent);
00050 QColor macGetColor(const QColor& initial, QWidget *parent);
00051 #endif
00052 
00054 
00055 struct QWellArrayData;
00056 
00057 class QWellArray : public QWidget
00058 {
00059     Q_OBJECT
00060     Q_PROPERTY(int selectedColumn READ selectedColumn)
00061     Q_PROPERTY(int selectedRow READ selectedRow)
00062 
00063 public:
00064     QWellArray(int rows, int cols, QWidget* parent=0);
00065     ~QWellArray() {}
00066     QString cellContent(int row, int col) const;
00067 
00068     int selectedColumn() const { return selCol; }
00069     int selectedRow() const { return selRow; }
00070 
00071     virtual void setCurrent(int row, int col);
00072     virtual void setSelected(int row, int col);
00073 
00074     QSize sizeHint() const;
00075 
00076     virtual void setCellBrush(int row, int col, const QBrush &);
00077     QBrush cellBrush(int row, int col);
00078 
00079     inline int cellWidth() const
00080         { return cellw; }
00081 
00082     inline int cellHeight() const
00083         { return cellh; }
00084 
00085     inline int rowAt(int y) const
00086         { return y / cellh; }
00087 
00088     inline int columnAt(int x) const
00089         { if (isRightToLeft()) return ncols - (x / cellw) - 1; return x / cellw; }
00090 
00091     inline int rowY(int row) const
00092         { return cellh * row; }
00093 
00094     inline int columnX(int column) const
00095         { if (isRightToLeft()) return cellw * (ncols - column - 1); return cellw * column; }
00096 
00097     inline int numRows() const
00098         { return nrows; }
00099 
00100     inline int numCols() const
00101         {return ncols; }
00102 
00103     inline QRect cellRect() const
00104         { return QRect(0, 0, cellw, cellh); }
00105 
00106     inline QSize gridSize() const
00107         { return QSize(ncols * cellw, nrows * cellh); }
00108 
00109     QRect cellGeometry(int row, int column)
00110         {
00111             QRect r;
00112             if (row >= 0 && row < nrows && column >= 0 && column < ncols)
00113                 r.setRect(columnX(column), rowY(row), cellw, cellh);
00114             return r;
00115         }
00116 
00117 
00118     inline void updateCell(int row, int column) { update(cellGeometry(row, column)); }
00119 
00120 
00121 signals:
00122     void selected(int row, int col);
00123 
00124 protected:
00125     virtual void paintCell(QPainter *, int row, int col, const QRect&);
00126     virtual void paintCellContents(QPainter *, int row, int col, const QRect&);
00127 
00128     void mousePressEvent(QMouseEvent*);
00129     void mouseReleaseEvent(QMouseEvent*);
00130     void keyPressEvent(QKeyEvent*);
00131     void focusInEvent(QFocusEvent*);
00132     void focusOutEvent(QFocusEvent*);
00133     void paintEvent(QPaintEvent *);
00134 
00135 private:
00136     Q_DISABLE_COPY(QWellArray)
00137 
00138     int nrows;
00139     int ncols;
00140     int cellw;
00141     int cellh;
00142     int curRow;
00143     int curCol;
00144     int selRow;
00145     int selCol;
00146     QWellArrayData *d;
00147 };
00148 
00149 void QWellArray::paintEvent(QPaintEvent *e)
00150 {
00151     QRect r = e->rect();
00152     int cx = r.x();
00153     int cy = r.y();
00154     int ch = r.height();
00155     int cw = r.width();
00156     int colfirst = columnAt(cx);
00157     int collast = columnAt(cx + cw);
00158     int rowfirst = rowAt(cy);
00159     int rowlast = rowAt(cy + ch);
00160 
00161     if (isRightToLeft()) {
00162         int t = colfirst;
00163         colfirst = collast;
00164         collast = t;
00165     }
00166 
00167     QPainter painter(this);
00168     QPainter *p = &painter;
00169     QRect rect(0, 0, cellWidth(), cellHeight());
00170 
00171 
00172     if (collast < 0 || collast >= ncols)
00173         collast = ncols-1;
00174     if (rowlast < 0 || rowlast >= nrows)
00175         rowlast = nrows-1;
00176 
00177     // Go through the rows
00178     for (int r = rowfirst; r <= rowlast; ++r) {
00179         // get row position and height
00180         int rowp = rowY(r);
00181 
00182         // Go through the columns in the row r
00183         // if we know from where to where, go through [colfirst, collast],
00184         // else go through all of them
00185         for (int c = colfirst; c <= collast; ++c) {
00186             // get position and width of column c
00187             int colp = columnX(c);
00188             // Translate painter and draw the cell
00189             rect.translate(colp, rowp);
00190             paintCell(p, r, c, rect);
00191             rect.translate(-colp, -rowp);
00192         }
00193     }
00194 
00195 }
00196 
00197 struct QWellArrayData {
00198     QBrush *brush;
00199 };
00200 
00201 QWellArray::QWellArray(int rows, int cols, QWidget *parent)
00202     : QWidget(parent)
00203         ,nrows(rows), ncols(cols)
00204 {
00205     d = 0;
00206     setFocusPolicy(Qt::StrongFocus);
00207     cellw = 28;
00208     cellh = 24;
00209     curCol = 0;
00210     curRow = 0;
00211     selCol = -1;
00212     selRow = -1;
00213 
00214 }
00215 
00216 
00217 QSize QWellArray::sizeHint() const
00218 {
00219     ensurePolished();
00220     return gridSize().boundedTo(QSize(640, 480));
00221 }
00222 
00223 
00224 void QWellArray::paintCell(QPainter* p, int row, int col, const QRect &rect)
00225 {
00226     int b = 3; //margin
00227 
00228     const QPalette & g = palette();
00229     QStyleOptionFrame opt;
00230     int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
00231     opt.lineWidth = dfw;
00232     opt.midLineWidth = 1;
00233     opt.rect = rect.adjusted(b, b, -b, -b);
00234     opt.palette = g;
00235     opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
00236     style()->drawPrimitive(QStyle::PE_Frame, &opt, p, this);
00237     b += dfw;
00238 
00239     if ((row == curRow) && (col == curCol)) {
00240         if (hasFocus()) {
00241             QStyleOptionFocusRect opt;
00242             opt.palette = g;
00243             opt.rect = rect;
00244             opt.state = QStyle::State_None | QStyle::State_KeyboardFocusChange;
00245             style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p, this);
00246         }
00247     }
00248     paintCellContents(p, row, col, opt.rect.adjusted(dfw, dfw, -dfw, -dfw));
00249 }
00250 
00254 void QWellArray::paintCellContents(QPainter *p, int row, int col, const QRect &r)
00255 {
00256 
00257     if (d) {
00258         p->fillRect(r, d->brush[row*numCols()+col]);
00259     } else {
00260         p->fillRect(r, Qt::white);
00261         p->setPen(Qt::black);
00262         p->drawLine(r.topLeft(), r.bottomRight());
00263         p->drawLine(r.topRight(), r.bottomLeft());
00264     }
00265 }
00266 
00267 
00268 /*\reimp
00269 */
00270 void QWellArray::mousePressEvent(QMouseEvent* e)
00271 {
00272     // The current cell marker is set to the cell the mouse is pressed
00273     // in.
00274     QPoint pos = e->pos();
00275     setCurrent(rowAt(pos.y()), columnAt(pos.x()));
00276 }
00277 
00278 /*\reimp
00279 */
00280 void QWellArray::mouseReleaseEvent(QMouseEvent*)
00281 {
00282     // The current cell marker is set to the cell the mouse is clicked
00283     // in.
00284     setSelected(curRow, curCol);
00285 }
00286 
00287 
00288 /*
00289   Sets the cell currently having the focus. This is not necessarily
00290   the same as the currently selected cell.
00291 */
00292 
00293 void QWellArray::setCurrent(int row, int col)
00294 {
00295 
00296     if ((curRow == row) && (curCol == col))
00297         return;
00298 
00299     if (row < 0 || col < 0)
00300         row = col = -1;
00301 
00302     int oldRow = curRow;
00303     int oldCol = curCol;
00304 
00305     curRow = row;
00306     curCol = col;
00307 
00308     updateCell(oldRow, oldCol);
00309     updateCell(curRow, curCol);
00310 }
00311 
00312 
00313 /*
00314   Sets the currently selected cell to \a row, \a column. If \a row or
00315   \a column are less than zero, the current cell is unselected.
00316 
00317   Does not set the position of the focus indicator.
00318 */
00319 
00320 void QWellArray::setSelected(int row, int col)
00321 {
00322     int oldRow = selRow;
00323     int oldCol = selCol;
00324 
00325     if (row < 0 || col < 0)
00326         row = col = -1;
00327 
00328     selCol = col;
00329     selRow = row;
00330 
00331     updateCell(oldRow, oldCol);
00332     updateCell(selRow, selCol);
00333     if (row >= 0)
00334         emit selected(row, col);
00335 
00336 #ifndef QT_NO_MENU
00337     if (isVisible() && qobject_cast<QMenu*>(parentWidget()))
00338         parentWidget()->close();
00339 #endif
00340 }
00341 
00342 
00343 
00346 void QWellArray::focusInEvent(QFocusEvent*)
00347 {
00348     updateCell(curRow, curCol);
00349 }
00350 
00351 
00352 void QWellArray::setCellBrush(int row, int col, const QBrush &b)
00353 {
00354     if (!d) {
00355         d = new QWellArrayData;
00356         int i = numRows()*numCols();
00357         d->brush = new QBrush[i];
00358     }
00359     if (row >= 0 && row < numRows() && col >= 0 && col < numCols())
00360         d->brush[row*numCols()+col] = b;
00361 }
00362 
00363 
00364 
00365 /*
00366   Returns the brush set for the cell at \a row, \a column. If no brush is
00367   set, Qt::NoBrush is returned.
00368 */
00369 
00370 QBrush QWellArray::cellBrush(int row, int col)
00371 {
00372     if (d && row >= 0 && row < numRows() && col >= 0 && col < numCols())
00373         return d->brush[row*numCols()+col];
00374     return Qt::NoBrush;
00375 }
00376 
00377 
00378 
00382 void QWellArray::focusOutEvent(QFocusEvent*)
00383 {
00384     updateCell(curRow, curCol);
00385 }
00386 
00387 /*\reimp
00388 */
00389 void QWellArray::keyPressEvent(QKeyEvent* e)
00390 {
00391     switch(e->key()) {                        // Look at the key code
00392     case Qt::Key_Left:                                // If 'left arrow'-key,
00393         if(curCol > 0)                        // and cr't not in leftmost col
00394             setCurrent(curRow, curCol - 1);        // set cr't to next left column
00395         break;
00396     case Qt::Key_Right:                                // Correspondingly...
00397         if(curCol < numCols()-1)
00398             setCurrent(curRow, curCol + 1);
00399         break;
00400     case Qt::Key_Up:
00401         if(curRow > 0)
00402             setCurrent(curRow - 1, curCol);
00403         break;
00404     case Qt::Key_Down:
00405         if(curRow < numRows()-1)
00406             setCurrent(curRow + 1, curCol);
00407         break;
00408     case Qt::Key_Return:
00409     case Qt::Key_Enter:
00410         /*
00411           ignore the key, so that the dialog get it, but still select
00412           the current row/col
00413         */
00414         e->ignore();
00415         // fallthrough intended
00416     case Qt::Key_Space:
00417         setSelected(curRow, curCol);
00418         break;
00419     default:                                // If not an interesting key,
00420         e->ignore();                        // we don't accept the event
00421         return;
00422     }
00423 
00424 }
00425 
00427 
00428 static bool initrgb = false;
00429 static QRgb stdrgb[6*8];
00430 static QRgb cusrgb[2*8];
00431 static bool customSet = false;
00432 
00433 
00434 static void initRGB()
00435 {
00436     if (initrgb)
00437         return;
00438     initrgb = true;
00439     int i = 0;
00440     for (int g = 0; g < 4; g++)
00441         for (int r = 0;  r < 4; r++)
00442             for (int b = 0; b < 3; b++)
00443                 stdrgb[i++] = qRgb(r*255/3, g*255/3, b*255/2);
00444 
00445     for (i = 0; i < 2*8; i++)
00446         cusrgb[i] = 0xffffffff;
00447 }
00448 
00453 int QColorDialog::customCount()
00454 {
00455     return 2*8;
00456 }
00457 
00461 QRgb QColorDialog::customColor(int i)
00462 {
00463     initRGB();
00464     Q_ASSERT(i >= 0 && i < customCount());
00465     return cusrgb[i];
00466 }
00467 
00473 void QColorDialog::setCustomColor(int i, QRgb c)
00474 {
00475     initRGB();
00476     Q_ASSERT(i >= 0 && i < customCount());
00477     customSet = true;
00478     cusrgb[i] = c;
00479 }
00480 
00487 void QColorDialog::setStandardColor(int i, QRgb c)
00488 {
00489     initRGB();
00490     Q_ASSERT(i >= 0 && i < 6*8);
00491     stdrgb[i] = c;
00492 }
00493 
00494 static inline void rgb2hsv(QRgb rgb, int&h, int&s, int&v)
00495 {
00496     QColor c;
00497     c.setRgb(rgb);
00498     c.getHsv(&h, &s, &v);
00499 }
00500 
00501 class QColorWell : public QWellArray
00502 {
00503 public:
00504     QColorWell(QWidget *parent, int r, int c, QRgb *vals)
00505         :QWellArray(r, c, parent), values(vals), mousePressed(false), oldCurrent(-1, -1)
00506     { setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); }
00507 
00508 protected:
00509     void paintCellContents(QPainter *, int row, int col, const QRect&);
00510     void mousePressEvent(QMouseEvent *e);
00511     void mouseMoveEvent(QMouseEvent *e);
00512     void mouseReleaseEvent(QMouseEvent *e);
00513 #ifndef QT_NO_DRAGANDDROP
00514     void dragEnterEvent(QDragEnterEvent *e);
00515     void dragLeaveEvent(QDragLeaveEvent *e);
00516     void dragMoveEvent(QDragMoveEvent *e);
00517     void dropEvent(QDropEvent *e);
00518 #endif
00519 
00520 private:
00521     QRgb *values;
00522     bool mousePressed;
00523     QPoint pressPos;
00524     QPoint oldCurrent;
00525 
00526 };
00527 
00528 void QColorWell::paintCellContents(QPainter *p, int row, int col, const QRect &r)
00529 {
00530     int i = row + col*numRows();
00531     p->fillRect(r, QColor(values[i]));
00532 }
00533 
00534 void QColorWell::mousePressEvent(QMouseEvent *e)
00535 {
00536     oldCurrent = QPoint(selectedRow(), selectedColumn());
00537     QWellArray::mousePressEvent(e);
00538     mousePressed = true;
00539     pressPos = e->pos();
00540 }
00541 
00542 void QColorWell::mouseMoveEvent(QMouseEvent *e)
00543 {
00544     QWellArray::mouseMoveEvent(e);
00545 #ifndef QT_NO_DRAGANDDROP
00546     if (!mousePressed)
00547         return;
00548     if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
00549         setCurrent(oldCurrent.x(), oldCurrent.y());
00550         int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
00551         QColor col(values[i]);
00552         QMimeData *mime = new QMimeData;
00553         mime->setColorData(col);
00554         QPixmap pix(cellWidth(), cellHeight());
00555         pix.fill(col);
00556         QPainter p(&pix);
00557         p.drawRect(0, 0, pix.width(), pix.height());
00558         p.end();
00559         QDrag *drg = new QDrag(this);
00560         drg->setMimeData(mime);
00561         drg->setPixmap(pix);
00562         mousePressed = false;
00563         drg->start();
00564     }
00565 #endif
00566 }
00567 
00568 #ifndef QT_NO_DRAGANDDROP
00569 void QColorWell::dragEnterEvent(QDragEnterEvent *e)
00570 {
00571     if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
00572         e->accept();
00573     else
00574         e->ignore();
00575 }
00576 
00577 void QColorWell::dragLeaveEvent(QDragLeaveEvent *)
00578 {
00579     if (hasFocus())
00580         parentWidget()->setFocus();
00581 }
00582 
00583 void QColorWell::dragMoveEvent(QDragMoveEvent *e)
00584 {
00585     if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) {
00586         setCurrent(rowAt(e->pos().y()), columnAt(e->pos().x()));
00587         e->accept();
00588     } else {
00589         e->ignore();
00590     }
00591 }
00592 
00593 void QColorWell::dropEvent(QDropEvent *e)
00594 {
00595     QColor col = qvariant_cast<QColor>(e->mimeData()->colorData());
00596     if (col.isValid()) {
00597         int i = rowAt(e->pos().y()) + columnAt(e->pos().x()) * numRows();
00598         values[i] = col.rgb();
00599         update();
00600         e->accept();
00601     } else {
00602         e->ignore();
00603     }
00604 }
00605 
00606 #endif // QT_NO_DRAGANDDROP
00607 
00608 void QColorWell::mouseReleaseEvent(QMouseEvent *e)
00609 {
00610     if (!mousePressed)
00611         return;
00612     QWellArray::mouseReleaseEvent(e);
00613     mousePressed = false;
00614 }
00615 
00616 class QColorPicker : public QFrame
00617 {
00618     Q_OBJECT
00619 public:
00620     QColorPicker(QWidget* parent);
00621     ~QColorPicker();
00622 
00623 public slots:
00624     void setCol(int h, int s);
00625 
00626 signals:
00627     void newCol(int h, int s);
00628 
00629 protected:
00630     QSize sizeHint() const;
00631     void paintEvent(QPaintEvent*);
00632     void mouseMoveEvent(QMouseEvent *);
00633     void mousePressEvent(QMouseEvent *);
00634 
00635 private:
00636     int hue;
00637     int sat;
00638 
00639     QPoint colPt();
00640     int huePt(const QPoint &pt);
00641     int satPt(const QPoint &pt);
00642     void setCol(const QPoint &pt);
00643 
00644     QPixmap *pix;
00645 };
00646 
00647 static int pWidth = 220;
00648 static int pHeight = 200;
00649 
00650 class QColorLuminancePicker : public QWidget
00651 {
00652     Q_OBJECT
00653 public:
00654     QColorLuminancePicker(QWidget* parent=0);
00655     ~QColorLuminancePicker();
00656 
00657 public slots:
00658     void setCol(int h, int s, int v);
00659     void setCol(int h, int s);
00660 
00661 signals:
00662     void newHsv(int h, int s, int v);
00663 
00664 protected:
00665     void paintEvent(QPaintEvent*);
00666     void mouseMoveEvent(QMouseEvent *);
00667     void mousePressEvent(QMouseEvent *);
00668 
00669 private:
00670     enum { foff = 3, coff = 4 }; //frame and contents offset
00671     int val;
00672     int hue;
00673     int sat;
00674 
00675     int y2val(int y);
00676     int val2y(int val);
00677     void setVal(int v);
00678 
00679     QPixmap *pix;
00680 };
00681 
00682 
00683 int QColorLuminancePicker::y2val(int y)
00684 {
00685     int d = height() - 2*coff - 1;
00686     return 255 - (y - coff)*255/d;
00687 }
00688 
00689 int QColorLuminancePicker::val2y(int v)
00690 {
00691     int d = height() - 2*coff - 1;
00692     return coff + (255-v)*d/255;
00693 }
00694 
00695 QColorLuminancePicker::QColorLuminancePicker(QWidget* parent)
00696     :QWidget(parent)
00697 {
00698     hue = 100; val = 100; sat = 100;
00699     pix = 0;
00700     //    setAttribute(WA_NoErase, true);
00701 }
00702 
00703 QColorLuminancePicker::~QColorLuminancePicker()
00704 {
00705     delete pix;
00706 }
00707 
00708 void QColorLuminancePicker::mouseMoveEvent(QMouseEvent *m)
00709 {
00710     setVal(y2val(m->y()));
00711 }
00712 void QColorLuminancePicker::mousePressEvent(QMouseEvent *m)
00713 {
00714     setVal(y2val(m->y()));
00715 }
00716 
00717 void QColorLuminancePicker::setVal(int v)
00718 {
00719     if (val == v)
00720         return;
00721     val = qMax(0, qMin(v,255));
00722     delete pix; pix=0;
00723     repaint();
00724     emit newHsv(hue, sat, val);
00725 }
00726 
00727 //receives from a hue,sat chooser and relays.
00728 void QColorLuminancePicker::setCol(int h, int s)
00729 {
00730     setCol(h, s, val);
00731     emit newHsv(h, s, val);
00732 }
00733 
00734 void QColorLuminancePicker::paintEvent(QPaintEvent *)
00735 {
00736     int w = width() - 5;
00737 
00738     QRect r(0, foff, w, height() - 2*foff);
00739     int wi = r.width() - 2;
00740     int hi = r.height() - 2;
00741     if (!pix || pix->height() != hi || pix->width() != wi) {
00742         delete pix;
00743         QImage img(wi, hi, QImage::Format_RGB32);
00744         int y;
00745         for (y = 0; y < hi; y++) {
00746             QColor c;
00747             c.setHsv(hue, sat, y2val(y+coff));
00748             QRgb r = c.rgb();
00749             int x;
00750             for (x = 0; x < wi; x++)
00751                 img.setPixel(x, y, r);
00752         }
00753         pix = new QPixmap(QPixmap::fromImage(img));
00754     }
00755     QPainter p(this);
00756     p.drawPixmap(1, coff, *pix);
00757     const QPalette &g = palette();
00758     qDrawShadePanel(&p, r, g, true);
00759     p.setPen(g.foreground().color());
00760     p.setBrush(g.foreground());
00761     QPolygon a;
00762     int y = val2y(val);
00763     a.setPoints(3, w, y, w+5, y+5, w+5, y-5);
00764     p.eraseRect(w, 0, 5, height());
00765     p.drawPolygon(a);
00766 }
00767 
00768 void QColorLuminancePicker::setCol(int h, int s , int v)
00769 {
00770     val = v;
00771     hue = h;
00772     sat = s;
00773     delete pix; pix=0;
00774     repaint();
00775 }
00776 
00777 QPoint QColorPicker::colPt()
00778 { return QPoint((360-hue)*(pWidth-1)/360, (255-sat)*(pHeight-1)/255); }
00779 int QColorPicker::huePt(const QPoint &pt)
00780 { return 360 - pt.x()*360/(pWidth-1); }
00781 int QColorPicker::satPt(const QPoint &pt)
00782 { return 255 - pt.y()*255/(pHeight-1) ; }
00783 void QColorPicker::setCol(const QPoint &pt)
00784 { setCol(huePt(pt), satPt(pt)); }
00785 
00786 QColorPicker::QColorPicker(QWidget* parent)
00787     : QFrame(parent)
00788 {
00789     hue = 0; sat = 0;
00790     setCol(150, 255);
00791 
00792     QImage img(pWidth, pHeight, QImage::Format_RGB32);
00793     int x,y;
00794     for (y = 0; y < pHeight; y++)
00795         for (x = 0; x < pWidth; x++) {
00796             QPoint p(x, y);
00797             QColor c;
00798             c.setHsv(huePt(p), satPt(p), 200);
00799             img.setPixel(x, y, c.rgb());
00800         }
00801     pix = new QPixmap(QPixmap::fromImage(img));
00802     setAttribute(Qt::WA_NoSystemBackground);
00803     setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
00804 }
00805 
00806 QColorPicker::~QColorPicker()
00807 {
00808     delete pix;
00809 }
00810 
00811 QSize QColorPicker::sizeHint() const
00812 {
00813     return QSize(pWidth + 2*frameWidth(), pHeight + 2*frameWidth());
00814 }
00815 
00816 void QColorPicker::setCol(int h, int s)
00817 {
00818     int nhue = qMin(qMax(0,h), 359);
00819     int nsat = qMin(qMax(0,s), 255);
00820     if (nhue == hue && nsat == sat)
00821         return;
00822     QRect r(colPt(), QSize(20,20));
00823     hue = nhue; sat = nsat;
00824     r = r.united(QRect(colPt(), QSize(20,20)));
00825     r.translate(contentsRect().x()-9, contentsRect().y()-9);
00826     //    update(r);
00827     repaint(r);
00828 }
00829 
00830 void QColorPicker::mouseMoveEvent(QMouseEvent *m)
00831 {
00832     QPoint p = m->pos() - contentsRect().topLeft();
00833     setCol(p);
00834     emit newCol(hue, sat);
00835 }
00836 
00837 void QColorPicker::mousePressEvent(QMouseEvent *m)
00838 {
00839     QPoint p = m->pos() - contentsRect().topLeft();
00840     setCol(p);
00841     emit newCol(hue, sat);
00842 }
00843 
00844 void QColorPicker::paintEvent(QPaintEvent* )
00845 {
00846     QPainter p(this);
00847     drawFrame(&p);
00848     QRect r = contentsRect();
00849 
00850     p.drawPixmap(r.topLeft(), *pix);
00851     QPoint pt = colPt() + r.topLeft();
00852     p.setPen(Qt::black);
00853 
00854     p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
00855     p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
00856 
00857 }
00858 
00859 class QColSpinBox : public QSpinBox
00860 {
00861 public:
00862     QColSpinBox(QWidget *parent)
00863         : QSpinBox(parent) { setRange(0, 255); }
00864     void setValue(int i) {
00865         bool block = signalsBlocked();
00866         blockSignals(true);
00867         QSpinBox::setValue(i);
00868         blockSignals(block);
00869     }
00870 };
00871 
00872 class QColorShowLabel;
00873 
00874 class QColorShower : public QWidget
00875 {
00876     Q_OBJECT
00877 public:
00878     QColorShower(QWidget *parent);
00879 
00880     //things that don't emit signals
00881     void setHsv(int h, int s, int v);
00882 
00883     int currentAlpha() const { return alphaEd->value(); }
00884     void setCurrentAlpha(int a) { alphaEd->setValue(a); }
00885     void showAlpha(bool b);
00886 
00887 
00888     QRgb currentColor() const { return curCol; }
00889 
00890 public slots:
00891     void setRgb(QRgb rgb);
00892 
00893 signals:
00894     void newCol(QRgb rgb);
00895 private slots:
00896     void rgbEd();
00897     void hsvEd();
00898 private:
00899     void showCurrentColor();
00900     int hue, sat, val;
00901     QRgb curCol;
00902     QColSpinBox *hEd;
00903     QColSpinBox *sEd;
00904     QColSpinBox *vEd;
00905     QColSpinBox *rEd;
00906     QColSpinBox *gEd;
00907     QColSpinBox *bEd;
00908     QColSpinBox *alphaEd;
00909     QLabel *alphaLab;
00910     QColorShowLabel *lab;
00911     bool rgbOriginal;
00912 };
00913 
00914 class QColorShowLabel : public QFrame
00915 {
00916     Q_OBJECT
00917 
00918 public:
00919     QColorShowLabel(QWidget *parent) : QFrame(parent) {
00920         setFrameStyle(QFrame::Panel|QFrame::Sunken);
00921         setAcceptDrops(true);
00922         mousePressed = false;
00923     }
00924     void setColor(QColor c) { col = c; }
00925 
00926 signals:
00927     void colorDropped(QRgb);
00928 
00929 protected:
00930     void paintEvent(QPaintEvent *);
00931     void mousePressEvent(QMouseEvent *e);
00932     void mouseMoveEvent(QMouseEvent *e);
00933     void mouseReleaseEvent(QMouseEvent *e);
00934 #ifndef QT_NO_DRAGANDDROP
00935     void dragEnterEvent(QDragEnterEvent *e);
00936     void dragLeaveEvent(QDragLeaveEvent *e);
00937     void dropEvent(QDropEvent *e);
00938 #endif
00939 
00940 private:
00941     QColor col;
00942     bool mousePressed;
00943     QPoint pressPos;
00944 
00945 };
00946 
00947 void QColorShowLabel::paintEvent(QPaintEvent *e)
00948 {
00949     QPainter p(this);
00950     drawFrame(&p);
00951     p.fillRect(contentsRect()&e->rect(), col);
00952 }
00953 
00954 void QColorShower::showAlpha(bool b)
00955 {
00956     if (b) {
00957         alphaLab->show();
00958         alphaEd->show();
00959     } else {
00960         alphaLab->hide();
00961         alphaEd->hide();
00962     }
00963 }
00964 
00965 void QColorShowLabel::mousePressEvent(QMouseEvent *e)
00966 {
00967     mousePressed = true;
00968     pressPos = e->pos();
00969 }
00970 
00971 void QColorShowLabel::mouseMoveEvent(QMouseEvent *e)
00972 {
00973 #ifdef QT_NO_DRAGANDDROP
00974     Q_UNUSED(e);
00975 #else
00976     if (!mousePressed)
00977         return;
00978     if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
00979         QMimeData *mime = new QMimeData;
00980         mime->setColorData(col);
00981         QPixmap pix(30, 20);
00982         pix.fill(col);
00983         QPainter p(&pix);
00984         p.drawRect(0, 0, pix.width(), pix.height());
00985         p.end();
00986         QDrag *drg = new QDrag(this);
00987         drg->setMimeData(mime);
00988         drg->setPixmap(pix);
00989         mousePressed = false;
00990         drg->start();
00991     }
00992 #endif
00993 }
00994 
00995 #ifndef QT_NO_DRAGANDDROP
00996 void QColorShowLabel::dragEnterEvent(QDragEnterEvent *e)
00997 {
00998     if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
00999         e->accept();
01000     else
01001         e->ignore();
01002 }
01003 
01004 void QColorShowLabel::dragLeaveEvent(QDragLeaveEvent *)
01005 {
01006 }
01007 
01008 void QColorShowLabel::dropEvent(QDropEvent *e)
01009 {
01010     QColor color = qvariant_cast<QColor>(e->mimeData()->colorData());
01011     if (color.isValid()) {
01012         col = color;
01013         repaint();
01014         emit colorDropped(col.rgb());
01015         e->accept();
01016     } else {
01017         e->ignore();
01018     }
01019 }
01020 #endif // QT_NO_DRAGANDDROP
01021 
01022 void QColorShowLabel::mouseReleaseEvent(QMouseEvent *)
01023 {
01024     if (!mousePressed)
01025         return;
01026     mousePressed = false;
01027 }
01028 
01029 QColorShower::QColorShower(QWidget *parent)
01030     :QWidget(parent)
01031 {
01032     curCol = qRgb(-1, -1, -1);
01033 
01034     QGridLayout *gl = new QGridLayout(this);
01035     gl->setMargin(gl->spacing());
01036     lab = new QColorShowLabel(this);
01037     lab->setMinimumWidth(60);
01038     gl->addWidget(lab, 0, 0, -1, 1);
01039     connect(lab, SIGNAL(colorDropped(QRgb)),
01040              this, SIGNAL(newCol(QRgb)));
01041     connect(lab, SIGNAL(colorDropped(QRgb)),
01042              this, SLOT(setRgb(QRgb)));
01043 
01044     hEd = new QColSpinBox(this);
01045     hEd->setRange(0, 359);
01046     QLabel *l = new QLabel(QColorDialog::tr("Hu&e:"), this);
01047 #ifndef QT_NO_SHORTCUT
01048     l->setBuddy(hEd);
01049 #endif
01050     l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
01051     gl->addWidget(l, 0, 1);
01052     gl->addWidget(hEd, 0, 2);
01053 
01054     sEd = new QColSpinBox(this);
01055     l = new QLabel(QColorDialog::tr("&Sat:"), this);
01056 #ifndef QT_NO_SHORTCUT
01057     l->setBuddy(sEd);
01058 #endif
01059     l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
01060     gl->addWidget(l, 1, 1);
01061     gl->addWidget(sEd, 1, 2);
01062 
01063     vEd = new QColSpinBox(this);
01064     l = new QLabel(QColorDialog::tr("&Val:"), this);
01065 #ifndef QT_NO_SHORTCUT
01066     l->setBuddy(vEd);
01067 #endif
01068     l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
01069     gl->addWidget(l, 2, 1);
01070     gl->addWidget(vEd, 2, 2);
01071 
01072     rEd = new QColSpinBox(this);
01073     l = new QLabel(QColorDialog::tr("&Red:"), this);
01074 #ifndef QT_NO_SHORTCUT
01075     l->setBuddy(rEd);
01076 #endif
01077     l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
01078     gl->addWidget(l, 0, 3);
01079     gl->addWidget(rEd, 0, 4);
01080 
01081     gEd = new QColSpinBox(this);
01082     l = new QLabel(QColorDialog::tr("&Green:"), this);
01083 #ifndef QT_NO_SHORTCUT
01084     l->setBuddy(gEd);
01085 #endif
01086     l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
01087     gl->addWidget(l, 1, 3);
01088     gl->addWidget(gEd, 1, 4);
01089 
01090     bEd = new QColSpinBox(this);
01091     l = new QLabel(QColorDialog::tr("Bl&ue:"), this);
01092 #ifndef QT_NO_SHORTCUT
01093     l->setBuddy(bEd);
01094 #endif
01095     l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
01096     gl->addWidget(l, 2, 3);
01097     gl->addWidget(bEd, 2, 4);
01098 
01099     alphaEd = new QColSpinBox(this);
01100     alphaLab = new QLabel(QColorDialog::tr("A&lpha channel:"), this);
01101 #ifndef QT_NO_SHORTCUT
01102     alphaLab->setBuddy(alphaEd);
01103 #endif
01104     alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
01105     gl->addWidget(alphaLab, 3, 1, 1, 3);
01106     gl->addWidget(alphaEd, 3, 4);
01107     alphaEd->hide();
01108     alphaLab->hide();
01109 
01110     connect(hEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
01111     connect(sEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
01112     connect(vEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
01113 
01114     connect(rEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
01115     connect(gEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
01116     connect(bEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
01117     connect(alphaEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
01118 }
01119 
01120 void QColorShower::showCurrentColor()
01121 {
01122     lab->setColor(currentColor());
01123     lab->repaint();
01124 }
01125 
01126 void QColorShower::rgbEd()
01127 {
01128     rgbOriginal = true;
01129     if (alphaEd->isVisible())
01130         curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
01131     else
01132         curCol = qRgb(rEd->value(), gEd->value(), bEd->value());
01133 
01134     rgb2hsv(currentColor(), hue, sat, val);
01135 
01136     hEd->setValue(hue);
01137     sEd->setValue(sat);
01138     vEd->setValue(val);
01139 
01140     showCurrentColor();
01141     emit newCol(currentColor());
01142 }
01143 
01144 void QColorShower::hsvEd()
01145 {
01146     rgbOriginal = false;
01147     hue = hEd->value();
01148     sat = sEd->value();
01149     val = vEd->value();
01150 
01151     QColor c;
01152     c.setHsv(hue, sat, val);
01153     curCol = c.rgb();
01154 
01155     rEd->setValue(qRed(currentColor()));
01156     gEd->setValue(qGreen(currentColor()));
01157     bEd->setValue(qBlue(currentColor()));
01158 
01159     showCurrentColor();
01160     emit newCol(currentColor());
01161 }
01162 
01163 void QColorShower::setRgb(QRgb rgb)
01164 {
01165     rgbOriginal = true;
01166     curCol = rgb;
01167 
01168     rgb2hsv(currentColor(), hue, sat, val);
01169 
01170     hEd->setValue(hue);
01171     sEd->setValue(sat);
01172     vEd->setValue(val);
01173 
01174     rEd->setValue(qRed(currentColor()));
01175     gEd->setValue(qGreen(currentColor()));
01176     bEd->setValue(qBlue(currentColor()));
01177 
01178     showCurrentColor();
01179 }
01180 
01181 void QColorShower::setHsv(int h, int s, int v)
01182 {
01183     if (h < -1 || (uint)s > 255 || (uint)v > 255)
01184   return;
01185 
01186     rgbOriginal = false;
01187     hue = h; val = v; sat = s;
01188     QColor c;
01189     c.setHsv(hue, sat, val);
01190     curCol = c.rgb();
01191 
01192     hEd->setValue(hue);
01193     sEd->setValue(sat);
01194     vEd->setValue(val);
01195 
01196     rEd->setValue(qRed(currentColor()));
01197     gEd->setValue(qGreen(currentColor()));
01198     bEd->setValue(qBlue(currentColor()));
01199 
01200     showCurrentColor();
01201 }
01202 
01203 class QColorDialogPrivate : public QDialogPrivate
01204 {
01205     Q_DECLARE_PUBLIC(QColorDialog)
01206 public:
01207     void init();
01208     QRgb currentColor() const { return cs->currentColor(); }
01209     void setCurrentColor(QRgb rgb);
01210 
01211     int currentAlpha() const { return cs->currentAlpha(); }
01212     void setCurrentAlpha(int a) { cs->setCurrentAlpha(a); }
01213     void showAlpha(bool b) { cs->showAlpha(b); }
01214 
01215     void _q_addCustom();
01216 
01217     void _q_newHsv(int h, int s, int v);
01218     void _q_newColorTypedIn(QRgb rgb);
01219     void _q_newCustom(int, int);
01220     void _q_newStandard(int, int);
01221 
01222     QWellArray *custom;
01223     QWellArray *standard;
01224 
01225     QColorPicker *cp;
01226     QColorLuminancePicker *lp;
01227     QColorShower *cs;
01228     int nextCust;
01229     bool compact;
01230 };
01231 
01232 //sets all widgets to display h,s,v
01233 void QColorDialogPrivate::_q_newHsv(int h, int s, int v)
01234 {
01235     cs->setHsv(h, s, v);
01236     cp->setCol(h, s);
01237     lp->setCol(h, s, v);
01238 }
01239 
01240 //sets all widgets to display rgb
01241 void QColorDialogPrivate::setCurrentColor(QRgb rgb)
01242 {
01243     cs->setRgb(rgb);
01244     _q_newColorTypedIn(rgb);
01245 }
01246 
01247 //sets all widgets except cs to display rgb
01248 void QColorDialogPrivate::_q_newColorTypedIn(QRgb rgb)
01249 {
01250     int h, s, v;
01251     rgb2hsv(rgb, h, s, v);
01252     cp->setCol(h, s);
01253     lp->setCol(h, s, v);
01254 }
01255 
01256 void QColorDialogPrivate::_q_newCustom(int r, int c)
01257 {
01258     int i = r+2*c;
01259     setCurrentColor(cusrgb[i]);
01260     nextCust = i;
01261     if (standard)
01262         standard->setSelected(-1,-1);
01263 }
01264 
01265 void QColorDialogPrivate::_q_newStandard(int r, int c)
01266 {
01267     setCurrentColor(stdrgb[r+c*6]);
01268     if (custom)
01269         custom->setSelected(-1,-1);
01270 }
01271 
01272 void QColorDialogPrivate::init()
01273 {
01274     Q_Q(QColorDialog);
01275     compact = false;
01276     // small displays (e.g. PDAs cannot fit the full color dialog,
01277     // so just use the color picker.
01278     if (qApp->desktop()->width() < 480 || qApp->desktop()->height() < 350)
01279         compact = true;
01280 
01281     nextCust = 0;
01282     QHBoxLayout *topLay = new QHBoxLayout(q);
01283     const int lumSpace = topLay->spacing() / 2;
01284     QVBoxLayout *leftLay = 0;
01285 
01286     if (!compact) {
01287         leftLay = new QVBoxLayout;
01288         topLay->addLayout(leftLay);
01289     }
01290 
01291     initRGB();
01292 
01293     if (!compact) {
01294         standard = new QColorWell(q, 6, 8, stdrgb);
01295         QLabel *lab = new QLabel(QColorDialog::tr("&Basic colors"), q);
01296 #ifndef QT_NO_SHORTCUT
01297         lab->setBuddy(standard);
01298 #endif
01299         q->connect(standard, SIGNAL(selected(int,int)), SLOT(_q_newStandard(int,int)));
01300         leftLay->addWidget(lab);
01301         leftLay->addWidget(standard);
01302 
01303 
01304         leftLay->addStretch();
01305 
01306         custom = new QColorWell(q, 2, 8, cusrgb);
01307         custom->setAcceptDrops(true);
01308 
01309         q->connect(custom, SIGNAL(selected(int,int)), SLOT(_q_newCustom(int,int)));
01310         lab = new QLabel(QColorDialog::tr("&Custom colors") , q);
01311 #ifndef QT_NO_SHORTCUT
01312         lab->setBuddy(custom);
01313 #endif
01314         leftLay->addWidget(lab);
01315         leftLay->addWidget(custom);
01316 
01317         QPushButton *custbut = new QPushButton(QColorDialog::tr("&Define Custom Colors >>"), q);
01318         custbut->setEnabled(false);
01319         leftLay->addWidget(custbut);
01320     } else {
01321         // better color picker size for small displays
01322         pWidth = 150;
01323         pHeight = 100;
01324         custom = 0;
01325         standard = 0;
01326     }
01327 
01328     QVBoxLayout *rightLay = new QVBoxLayout;
01329     topLay->addLayout(rightLay);
01330 
01331     QHBoxLayout *pickLay = new QHBoxLayout;
01332     rightLay->addLayout(pickLay);
01333 
01334 
01335     QVBoxLayout *cLay = new QVBoxLayout;
01336     pickLay->addLayout(cLay);
01337     cp = new QColorPicker(q);
01338     cp->setFrameStyle(QFrame::Panel + QFrame::Sunken);
01339     cLay->addSpacing(lumSpace);
01340     cLay->addWidget(cp);
01341     cLay->addSpacing(lumSpace);
01342 
01343     lp = new QColorLuminancePicker(q);
01344     lp->setFixedWidth(20);
01345     pickLay->addWidget(lp);
01346 
01347     QObject::connect(cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int)));
01348     QObject::connect(lp, SIGNAL(newHsv(int,int,int)), q, SLOT(_q_newHsv(int,int,int)));
01349 
01350     rightLay->addStretch();
01351 
01352     cs = new QColorShower(q);
01353     QObject::connect(cs, SIGNAL(newCol(QRgb)), q, SLOT(_q_newColorTypedIn(QRgb)));
01354     rightLay->addWidget(cs);
01355 
01356     QHBoxLayout *buttons;
01357     if (compact) {
01358         buttons = new QHBoxLayout;
01359         rightLay->addLayout(buttons);
01360     } else {
01361         buttons = new QHBoxLayout;
01362         leftLay->addLayout(buttons);
01363     }
01364 
01365     QPushButton *ok, *cancel;
01366     ok = new QPushButton(QColorDialog::tr("OK"), q);
01367     QObject::connect(ok, SIGNAL(clicked()), q, SLOT(accept()));
01368     ok->setDefault(true);
01369     cancel = new QPushButton(QColorDialog::tr("Cancel"), q);
01370     QObject::connect(cancel, SIGNAL(clicked()), q, SLOT(reject()));
01371     buttons->addWidget(ok);
01372     buttons->addWidget(cancel);
01373     buttons->addStretch();
01374 
01375     if (!compact) {
01376         QPushButton *addCusBt = new QPushButton(QColorDialog::tr("&Add to Custom Colors"), q);
01377         rightLay->addWidget(addCusBt);
01378         QObject::connect(addCusBt, SIGNAL(clicked()), q, SLOT(_q_addCustom()));
01379     }
01380 }
01381 
01382 void QColorDialogPrivate::_q_addCustom()
01383 {
01384     cusrgb[nextCust] = cs->currentColor();
01385     if (custom)
01386         custom->update();
01387     nextCust = (nextCust+1) % 16;
01388 }
01389 
01390 
01437 QColorDialog::QColorDialog(QWidget* parent, bool modal) :
01438     QDialog(*new QColorDialogPrivate, parent, (Qt::Dialog | Qt::WindowTitleHint |
01439                      Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint))
01440 {
01441     Q_D(QColorDialog);
01442     setModal(modal);
01443     setSizeGripEnabled(false);
01444     d->init();
01445 
01446 #ifndef QT_NO_SETTINGS
01447     if (!customSet) {
01448         QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
01449         settings.beginGroup(QLatin1String("Qt"));
01450         for (int i = 0; i < 2*8; ++i) {
01451             QVariant v = settings.value(QLatin1String("customColors/") + QString::number(i));
01452             if (v.isValid()) {
01453                 QRgb rgb = v.toUInt();
01454                 cusrgb[i] = rgb;
01455             }
01456         }
01457         settings.endGroup(); // Qt
01458     }
01459 #endif
01460 }
01461 
01471 QColor QColorDialog::getColor(const QColor& initial, QWidget *parent)
01472 {
01473 #if defined(Q_WS_MAC)
01474     return macGetColor(initial, parent);
01475 #endif
01476 
01477     QColorDialog *dlg = new QColorDialog(parent, true);  //modal
01478     dlg->setWindowTitle(QColorDialog::tr("Select color"));
01479     dlg->setColor(initial);
01480     dlg->selectColor(initial);
01481     int resultCode = dlg->exec();
01482     QColor result;
01483     if (resultCode == QDialog::Accepted)
01484         result = dlg->color();
01485     delete dlg;
01486     return result;
01487 }
01488 
01489 
01501 QRgb QColorDialog::getRgba(QRgb initial, bool *ok, QWidget *parent)
01502 {
01503 #if defined(Q_WS_MAC)
01504     return macGetRgba(initial, ok, parent);
01505 #endif
01506 
01507     QColorDialog *dlg = new QColorDialog(parent, true);  //modal
01508 
01509     dlg->setWindowTitle(QColorDialog::tr("Select color"));
01510     dlg->setColor(initial);
01511     dlg->selectColor(initial);
01512     dlg->setSelectedAlpha(qAlpha(initial));
01513     int resultCode = dlg->exec();
01514     QRgb result = initial;
01515     if (resultCode == QDialog::Accepted) {
01516         QRgb c = dlg->color().rgb();
01517         int alpha = dlg->selectedAlpha();
01518         result = qRgba(qRed(c), qGreen(c), qBlue(c), alpha);
01519     }
01520     if (ok)
01521         *ok = resultCode == QDialog::Accepted;
01522 
01523     delete dlg;
01524     return result;
01525 }
01526 
01527 
01528 
01529 
01530 
01537 QColor QColorDialog::color() const
01538 {
01539     Q_D(const QColorDialog);
01540     return QColor(d->currentColor());
01541 }
01542 
01543 
01548 QColorDialog::~QColorDialog()
01549 {
01550 #ifndef QT_NO_SETTINGS
01551     if (!customSet) {
01552         QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
01553         settings.beginGroup(QLatin1String("Qt"));
01554         for (int i = 0; i < 2*8; ++i)
01555             settings.setValue(QLatin1String("customColors/") + QString::number(i), cusrgb[i]);
01556         settings.endGroup();
01557     }
01558 #endif
01559 }
01560 
01561 
01570 void QColorDialog::setColor(const QColor& c)
01571 {
01572     Q_D(QColorDialog);
01573     d->setCurrentColor(c.rgb());
01574 }
01575 
01576 
01577 
01578 
01586 void QColorDialog::setSelectedAlpha(int a)
01587 {
01588     Q_D(QColorDialog);
01589     d->showAlpha(true);
01590     d->setCurrentAlpha(a);
01591 }
01592 
01593 
01598 int QColorDialog::selectedAlpha() const
01599 {
01600     Q_D(const QColorDialog);
01601     return d->currentAlpha();
01602 }
01603 
01607 bool QColorDialog::selectColor(const QColor& col)
01608 {
01609     Q_D(QColorDialog);
01610     QRgb color = col.rgb();
01611     int i = 0, j = 0;
01612     // Check standard colors
01613     if (d->standard) {
01614         for (i = 0; i < 6; i++) {
01615             for (j = 0; j < 8; j++) {
01616                 if (color == stdrgb[i + j*6]) {
01617                     d->_q_newStandard(i, j);
01618                     d->standard->setCurrent(i, j);
01619                     d->standard->setSelected(i, j);
01620                     d->standard->setFocus();
01621                     return true;
01622                 }
01623             }
01624         }
01625     }
01626     // Check custom colors
01627     if (d->custom) {
01628         for (i = 0; i < 2; i++) {
01629             for (j = 0; j < 8; j++) {
01630                 if (color == cusrgb[i + j*2]) {
01631                     d->_q_newCustom(i, j);
01632                     d->custom->setCurrent(i, j);
01633                     d->custom->setSelected(i, j);
01634                     d->custom->setFocus();
01635                     return true;
01636                 }
01637             }
01638         }
01639     }
01640     return false;
01641 }
01642 
01643 #include "qcolordialog.moc"
01644 #include "moc_qcolordialog.cpp"
01645 
01646 #endif
01647 

Generated on Thu Mar 15 11:54:21 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1