~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/widgets/dialogs/qcolordialog.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtGui module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "qcolordialog_p.h"
 
43
 
 
44
#ifndef QT_NO_COLORDIALOG
 
45
 
 
46
#include "qapplication.h"
 
47
#include "qdesktopwidget.h"
 
48
#include "qdrawutil.h"
 
49
#include "qevent.h"
 
50
#include "qimage.h"
 
51
#include "qdrag.h"
 
52
#include "qlabel.h"
 
53
#include "qlayout.h"
 
54
#include "qlineedit.h"
 
55
#include "qmenu.h"
 
56
#include "qpainter.h"
 
57
#include "qpixmap.h"
 
58
#include "qpushbutton.h"
 
59
#include "qsettings.h"
 
60
#include "qstyle.h"
 
61
#include "qstyleoption.h"
 
62
#include "qvalidator.h"
 
63
#include "qmimedata.h"
 
64
#include "qspinbox.h"
 
65
#include "qdialogbuttonbox.h"
 
66
 
 
67
QT_BEGIN_NAMESPACE
 
68
 
 
69
//////////// QWellArray BEGIN
 
70
 
 
71
struct QWellArrayData;
 
72
 
 
73
class QWellArray : public QWidget
 
74
{
 
75
    Q_OBJECT
 
76
    Q_PROPERTY(int selectedColumn READ selectedColumn)
 
77
    Q_PROPERTY(int selectedRow READ selectedRow)
 
78
 
 
79
public:
 
80
    QWellArray(int rows, int cols, QWidget* parent=0);
 
81
    ~QWellArray() {}
 
82
    QString cellContent(int row, int col) const;
 
83
 
 
84
    int selectedColumn() const { return selCol; }
 
85
    int selectedRow() const { return selRow; }
 
86
 
 
87
    virtual void setCurrent(int row, int col);
 
88
    virtual void setSelected(int row, int col);
 
89
 
 
90
    QSize sizeHint() const;
 
91
 
 
92
    virtual void setCellBrush(int row, int col, const QBrush &);
 
93
    QBrush cellBrush(int row, int col);
 
94
 
 
95
    inline int cellWidth() const
 
96
        { return cellw; }
 
97
 
 
98
    inline int cellHeight() const
 
99
        { return cellh; }
 
100
 
 
101
    inline int rowAt(int y) const
 
102
        { return y / cellh; }
 
103
 
 
104
    inline int columnAt(int x) const
 
105
        { if (isRightToLeft()) return ncols - (x / cellw) - 1; return x / cellw; }
 
106
 
 
107
    inline int rowY(int row) const
 
108
        { return cellh * row; }
 
109
 
 
110
    inline int columnX(int column) const
 
111
        { if (isRightToLeft()) return cellw * (ncols - column - 1); return cellw * column; }
 
112
 
 
113
    inline int numRows() const
 
114
        { return nrows; }
 
115
 
 
116
    inline int numCols() const
 
117
        {return ncols; }
 
118
 
 
119
    inline QRect cellRect() const
 
120
        { return QRect(0, 0, cellw, cellh); }
 
121
 
 
122
    inline QSize gridSize() const
 
123
        { return QSize(ncols * cellw, nrows * cellh); }
 
124
 
 
125
    QRect cellGeometry(int row, int column)
 
126
        {
 
127
            QRect r;
 
128
            if (row >= 0 && row < nrows && column >= 0 && column < ncols)
 
129
                r.setRect(columnX(column), rowY(row), cellw, cellh);
 
130
            return r;
 
131
        }
 
132
 
 
133
    inline void updateCell(int row, int column) { update(cellGeometry(row, column)); }
 
134
 
 
135
signals:
 
136
    void selected(int row, int col);
 
137
 
 
138
protected:
 
139
    virtual void paintCell(QPainter *, int row, int col, const QRect&);
 
140
    virtual void paintCellContents(QPainter *, int row, int col, const QRect&);
 
141
 
 
142
    void mousePressEvent(QMouseEvent*);
 
143
    void mouseReleaseEvent(QMouseEvent*);
 
144
    void keyPressEvent(QKeyEvent*);
 
145
    void focusInEvent(QFocusEvent*);
 
146
    void focusOutEvent(QFocusEvent*);
 
147
    void paintEvent(QPaintEvent *);
 
148
 
 
149
private:
 
150
    Q_DISABLE_COPY(QWellArray)
 
151
 
 
152
    int nrows;
 
153
    int ncols;
 
154
    int cellw;
 
155
    int cellh;
 
156
    int curRow;
 
157
    int curCol;
 
158
    int selRow;
 
159
    int selCol;
 
160
    QWellArrayData *d;
 
161
};
 
162
 
 
163
void QWellArray::paintEvent(QPaintEvent *e)
 
164
{
 
165
    QRect r = e->rect();
 
166
    int cx = r.x();
 
167
    int cy = r.y();
 
168
    int ch = r.height();
 
169
    int cw = r.width();
 
170
    int colfirst = columnAt(cx);
 
171
    int collast = columnAt(cx + cw);
 
172
    int rowfirst = rowAt(cy);
 
173
    int rowlast = rowAt(cy + ch);
 
174
 
 
175
    if (isRightToLeft()) {
 
176
        int t = colfirst;
 
177
        colfirst = collast;
 
178
        collast = t;
 
179
    }
 
180
 
 
181
    QPainter painter(this);
 
182
    QPainter *p = &painter;
 
183
    QRect rect(0, 0, cellWidth(), cellHeight());
 
184
 
 
185
 
 
186
    if (collast < 0 || collast >= ncols)
 
187
        collast = ncols-1;
 
188
    if (rowlast < 0 || rowlast >= nrows)
 
189
        rowlast = nrows-1;
 
190
 
 
191
    // Go through the rows
 
192
    for (int r = rowfirst; r <= rowlast; ++r) {
 
193
        // get row position and height
 
194
        int rowp = rowY(r);
 
195
 
 
196
        // Go through the columns in the row r
 
197
        // if we know from where to where, go through [colfirst, collast],
 
198
        // else go through all of them
 
199
        for (int c = colfirst; c <= collast; ++c) {
 
200
            // get position and width of column c
 
201
            int colp = columnX(c);
 
202
            // Translate painter and draw the cell
 
203
            rect.translate(colp, rowp);
 
204
            paintCell(p, r, c, rect);
 
205
            rect.translate(-colp, -rowp);
 
206
        }
 
207
    }
 
208
}
 
209
 
 
210
struct QWellArrayData {
 
211
    QBrush *brush;
 
212
};
 
213
 
 
214
QWellArray::QWellArray(int rows, int cols, QWidget *parent)
 
215
    : QWidget(parent)
 
216
        ,nrows(rows), ncols(cols)
 
217
{
 
218
    d = 0;
 
219
    setFocusPolicy(Qt::StrongFocus);
 
220
    cellw = 28;
 
221
    cellh = 24;
 
222
    curCol = 0;
 
223
    curRow = 0;
 
224
    selCol = -1;
 
225
    selRow = -1;
 
226
}
 
227
 
 
228
QSize QWellArray::sizeHint() const
 
229
{
 
230
    ensurePolished();
 
231
    return gridSize().boundedTo(QSize(640, 480));
 
232
}
 
233
 
 
234
 
 
235
void QWellArray::paintCell(QPainter* p, int row, int col, const QRect &rect)
 
236
{
 
237
    int b = 3; //margin
 
238
 
 
239
    const QPalette & g = palette();
 
240
    QStyleOptionFrame opt;
 
241
    int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
 
242
    opt.lineWidth = dfw;
 
243
    opt.midLineWidth = 1;
 
244
    opt.rect = rect.adjusted(b, b, -b, -b);
 
245
    opt.palette = g;
 
246
    opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
 
247
    style()->drawPrimitive(QStyle::PE_Frame, &opt, p, this);
 
248
    b += dfw;
 
249
 
 
250
    if ((row == curRow) && (col == curCol)) {
 
251
        if (hasFocus()) {
 
252
            QStyleOptionFocusRect opt;
 
253
            opt.palette = g;
 
254
            opt.rect = rect;
 
255
            opt.state = QStyle::State_None | QStyle::State_KeyboardFocusChange;
 
256
            style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p, this);
 
257
        }
 
258
    }
 
259
    paintCellContents(p, row, col, opt.rect.adjusted(dfw, dfw, -dfw, -dfw));
 
260
}
 
261
 
 
262
/*!
 
263
  Reimplement this function to change the contents of the well array.
 
264
 */
 
265
void QWellArray::paintCellContents(QPainter *p, int row, int col, const QRect &r)
 
266
{
 
267
    if (d) {
 
268
        p->fillRect(r, d->brush[row*numCols()+col]);
 
269
    } else {
 
270
        p->fillRect(r, Qt::white);
 
271
        p->setPen(Qt::black);
 
272
        p->drawLine(r.topLeft(), r.bottomRight());
 
273
        p->drawLine(r.topRight(), r.bottomLeft());
 
274
    }
 
275
}
 
276
 
 
277
void QWellArray::mousePressEvent(QMouseEvent *e)
 
278
{
 
279
    // The current cell marker is set to the cell the mouse is pressed in
 
280
    QPoint pos = e->pos();
 
281
    setCurrent(rowAt(pos.y()), columnAt(pos.x()));
 
282
}
 
283
 
 
284
void QWellArray::mouseReleaseEvent(QMouseEvent * /* event */)
 
285
{
 
286
    // The current cell marker is set to the cell the mouse is clicked in
 
287
    setSelected(curRow, curCol);
 
288
}
 
289
 
 
290
 
 
291
/*
 
292
  Sets the cell currently having the focus. This is not necessarily
 
293
  the same as the currently selected cell.
 
294
*/
 
295
 
 
296
void QWellArray::setCurrent(int row, int col)
 
297
{
 
298
    if ((curRow == row) && (curCol == col))
 
299
        return;
 
300
 
 
301
    if (row < 0 || col < 0)
 
302
        row = col = -1;
 
303
 
 
304
    int oldRow = curRow;
 
305
    int oldCol = curCol;
 
306
 
 
307
    curRow = row;
 
308
    curCol = col;
 
309
 
 
310
    updateCell(oldRow, oldCol);
 
311
    updateCell(curRow, curCol);
 
312
}
 
313
 
 
314
/*
 
315
  Sets the currently selected cell to \a row, \a column. If \a row or
 
316
  \a column are less than zero, the current cell is unselected.
 
317
 
 
318
  Does not set the position of the focus indicator.
 
319
*/
 
320
void QWellArray::setSelected(int row, int col)
 
321
{
 
322
    int oldRow = selRow;
 
323
    int oldCol = selCol;
 
324
 
 
325
    if (row < 0 || col < 0)
 
326
        row = col = -1;
 
327
 
 
328
    selCol = col;
 
329
    selRow = row;
 
330
 
 
331
    updateCell(oldRow, oldCol);
 
332
    updateCell(selRow, selCol);
 
333
    if (row >= 0)
 
334
        emit selected(row, col);
 
335
 
 
336
#ifndef QT_NO_MENU
 
337
    if (isVisible() && qobject_cast<QMenu*>(parentWidget()))
 
338
        parentWidget()->close();
 
339
#endif
 
340
}
 
341
 
 
342
void QWellArray::focusInEvent(QFocusEvent*)
 
343
{
 
344
    updateCell(curRow, curCol);
 
345
}
 
346
 
 
347
void QWellArray::setCellBrush(int row, int col, const QBrush &b)
 
348
{
 
349
    if (!d) {
 
350
        d = new QWellArrayData;
 
351
        int i = numRows()*numCols();
 
352
        d->brush = new QBrush[i];
 
353
    }
 
354
    if (row >= 0 && row < numRows() && col >= 0 && col < numCols())
 
355
        d->brush[row*numCols()+col] = b;
 
356
}
 
357
 
 
358
/*
 
359
  Returns the brush set for the cell at \a row, \a column. If no brush is
 
360
  set, Qt::NoBrush is returned.
 
361
*/
 
362
 
 
363
QBrush QWellArray::cellBrush(int row, int col)
 
364
{
 
365
    if (d && row >= 0 && row < numRows() && col >= 0 && col < numCols())
 
366
        return d->brush[row*numCols()+col];
 
367
    return Qt::NoBrush;
 
368
}
 
369
 
 
370
 
 
371
 
 
372
/*!\reimp
 
373
*/
 
374
 
 
375
void QWellArray::focusOutEvent(QFocusEvent*)
 
376
{
 
377
    updateCell(curRow, curCol);
 
378
}
 
379
 
 
380
/*\reimp
 
381
*/
 
382
void QWellArray::keyPressEvent(QKeyEvent* e)
 
383
{
 
384
    switch(e->key()) {                        // Look at the key code
 
385
    case Qt::Key_Left:                                // If 'left arrow'-key,
 
386
        if(curCol > 0)                        // and cr't not in leftmost col
 
387
            setCurrent(curRow, curCol - 1);        // set cr't to next left column
 
388
        break;
 
389
    case Qt::Key_Right:                                // Correspondingly...
 
390
        if(curCol < numCols()-1)
 
391
            setCurrent(curRow, curCol + 1);
 
392
        break;
 
393
    case Qt::Key_Up:
 
394
        if(curRow > 0)
 
395
            setCurrent(curRow - 1, curCol);
 
396
        break;
 
397
    case Qt::Key_Down:
 
398
        if(curRow < numRows()-1)
 
399
            setCurrent(curRow + 1, curCol);
 
400
        break;
 
401
#if 0
 
402
    // bad idea that shouldn't have been implemented; very counterintuitive
 
403
    case Qt::Key_Return:
 
404
    case Qt::Key_Enter:
 
405
        /*
 
406
          ignore the key, so that the dialog get it, but still select
 
407
          the current row/col
 
408
        */
 
409
        e->ignore();
 
410
        // fallthrough intended
 
411
#endif
 
412
    case Qt::Key_Space:
 
413
        setSelected(curRow, curCol);
 
414
        break;
 
415
    default:                                // If not an interesting key,
 
416
        e->ignore();                        // we don't accept the event
 
417
        return;
 
418
    }
 
419
 
 
420
}
 
421
 
 
422
//////////// QWellArray END
 
423
 
 
424
/*!
 
425
    Returns the number of custom colors supported by QColorDialog. All
 
426
    color dialogs share the same custom colors.
 
427
*/
 
428
int QColorDialog::customCount()
 
429
{
 
430
    return QColorDialogOptions::customColorCount();
 
431
}
 
432
 
 
433
/*!
 
434
    \since 4.5
 
435
 
 
436
    Returns the custom color at the given \a index as a QColor value.
 
437
*/
 
438
QColor QColorDialog::customColor(int index)
 
439
{
 
440
    return QColor(QColorDialogOptions::customColor(index));
 
441
}
 
442
 
 
443
/*!
 
444
    Sets the custom color at \a index to the QColor \a color value.
 
445
 
 
446
    \note This function does not apply to the Native Color Dialog on the Mac
 
447
    OS X platform. If you still require this function, use the
 
448
    QColorDialog::DontUseNativeDialog option.
 
449
*/
 
450
void QColorDialog::setCustomColor(int index, QColor color)
 
451
{
 
452
    QColorDialogOptions::setCustomColor(index, color.rgba());
 
453
}
 
454
 
 
455
/*!
 
456
    \since 5.0
 
457
 
 
458
    Returns the standard color at the given \a index as a QColor value.
 
459
*/
 
460
QColor QColorDialog::standardColor(int index)
 
461
{
 
462
    return QColor(QColorDialogOptions::standardColor(index));
 
463
}
 
464
 
 
465
/*!
 
466
    Sets the standard color at \a index to the QColor \a color value.
 
467
 
 
468
    \note This function does not apply to the Native Color Dialog on the Mac
 
469
    OS X platform. If you still require this function, use the
 
470
    QColorDialog::DontUseNativeDialog option.
 
471
*/
 
472
void QColorDialog::setStandardColor(int index, QColor color)
 
473
{
 
474
    QColorDialogOptions::setStandardColor(index, color.rgba());
 
475
}
 
476
 
 
477
static inline void rgb2hsv(QRgb rgb, int &h, int &s, int &v)
 
478
{
 
479
    QColor c;
 
480
    c.setRgb(rgb);
 
481
    c.getHsv(&h, &s, &v);
 
482
}
 
483
 
 
484
class QColorWell : public QWellArray
 
485
{
 
486
public:
 
487
    QColorWell(QWidget *parent, int r, int c, QRgb *vals)
 
488
        :QWellArray(r, c, parent), values(vals), mousePressed(false), oldCurrent(-1, -1)
 
489
    { setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); }
 
490
 
 
491
protected:
 
492
    void paintCellContents(QPainter *, int row, int col, const QRect&);
 
493
    void mousePressEvent(QMouseEvent *e);
 
494
    void mouseMoveEvent(QMouseEvent *e);
 
495
    void mouseReleaseEvent(QMouseEvent *e);
 
496
#ifndef QT_NO_DRAGANDDROP
 
497
    void dragEnterEvent(QDragEnterEvent *e);
 
498
    void dragLeaveEvent(QDragLeaveEvent *e);
 
499
    void dragMoveEvent(QDragMoveEvent *e);
 
500
    void dropEvent(QDropEvent *e);
 
501
#endif
 
502
 
 
503
private:
 
504
    QRgb *values;
 
505
    bool mousePressed;
 
506
    QPoint pressPos;
 
507
    QPoint oldCurrent;
 
508
 
 
509
};
 
510
 
 
511
void QColorWell::paintCellContents(QPainter *p, int row, int col, const QRect &r)
 
512
{
 
513
    int i = row + col*numRows();
 
514
    p->fillRect(r, QColor(values[i]));
 
515
}
 
516
 
 
517
void QColorWell::mousePressEvent(QMouseEvent *e)
 
518
{
 
519
    oldCurrent = QPoint(selectedRow(), selectedColumn());
 
520
    QWellArray::mousePressEvent(e);
 
521
    mousePressed = true;
 
522
    pressPos = e->pos();
 
523
}
 
524
 
 
525
void QColorWell::mouseMoveEvent(QMouseEvent *e)
 
526
{
 
527
    QWellArray::mouseMoveEvent(e);
 
528
#ifndef QT_NO_DRAGANDDROP
 
529
    if (!mousePressed)
 
530
        return;
 
531
    if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
 
532
        setCurrent(oldCurrent.x(), oldCurrent.y());
 
533
        int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
 
534
        QColor col(values[i]);
 
535
        QMimeData *mime = new QMimeData;
 
536
        mime->setColorData(col);
 
537
        QPixmap pix(cellWidth(), cellHeight());
 
538
        pix.fill(col);
 
539
        QPainter p(&pix);
 
540
        p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
 
541
        p.end();
 
542
        QDrag *drg = new QDrag(this);
 
543
        drg->setMimeData(mime);
 
544
        drg->setPixmap(pix);
 
545
        mousePressed = false;
 
546
        drg->start();
 
547
    }
 
548
#endif
 
549
}
 
550
 
 
551
#ifndef QT_NO_DRAGANDDROP
 
552
void QColorWell::dragEnterEvent(QDragEnterEvent *e)
 
553
{
 
554
    if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
 
555
        e->accept();
 
556
    else
 
557
        e->ignore();
 
558
}
 
559
 
 
560
void QColorWell::dragLeaveEvent(QDragLeaveEvent *)
 
561
{
 
562
    if (hasFocus())
 
563
        parentWidget()->setFocus();
 
564
}
 
565
 
 
566
void QColorWell::dragMoveEvent(QDragMoveEvent *e)
 
567
{
 
568
    if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) {
 
569
        setCurrent(rowAt(e->pos().y()), columnAt(e->pos().x()));
 
570
        e->accept();
 
571
    } else {
 
572
        e->ignore();
 
573
    }
 
574
}
 
575
 
 
576
void QColorWell::dropEvent(QDropEvent *e)
 
577
{
 
578
    QColor col = qvariant_cast<QColor>(e->mimeData()->colorData());
 
579
    if (col.isValid()) {
 
580
        int i = rowAt(e->pos().y()) + columnAt(e->pos().x()) * numRows();
 
581
        values[i] = col.rgb();
 
582
        update();
 
583
        e->accept();
 
584
    } else {
 
585
        e->ignore();
 
586
    }
 
587
}
 
588
 
 
589
#endif // QT_NO_DRAGANDDROP
 
590
 
 
591
void QColorWell::mouseReleaseEvent(QMouseEvent *e)
 
592
{
 
593
    if (!mousePressed)
 
594
        return;
 
595
    QWellArray::mouseReleaseEvent(e);
 
596
    mousePressed = false;
 
597
}
 
598
 
 
599
class QColorPicker : public QFrame
 
600
{
 
601
    Q_OBJECT
 
602
public:
 
603
    QColorPicker(QWidget* parent);
 
604
    ~QColorPicker();
 
605
 
 
606
public slots:
 
607
    void setCol(int h, int s);
 
608
 
 
609
signals:
 
610
    void newCol(int h, int s);
 
611
 
 
612
protected:
 
613
    QSize sizeHint() const;
 
614
    void paintEvent(QPaintEvent*);
 
615
    void mouseMoveEvent(QMouseEvent *);
 
616
    void mousePressEvent(QMouseEvent *);
 
617
    void resizeEvent(QResizeEvent *);
 
618
 
 
619
private:
 
620
    int hue;
 
621
    int sat;
 
622
 
 
623
    QPoint colPt();
 
624
    int huePt(const QPoint &pt);
 
625
    int satPt(const QPoint &pt);
 
626
    void setCol(const QPoint &pt);
 
627
 
 
628
    QPixmap pix;
 
629
};
 
630
 
 
631
static int pWidth = 220;
 
632
static int pHeight = 200;
 
633
 
 
634
class QColorLuminancePicker : public QWidget
 
635
{
 
636
    Q_OBJECT
 
637
public:
 
638
    QColorLuminancePicker(QWidget* parent=0);
 
639
    ~QColorLuminancePicker();
 
640
 
 
641
public slots:
 
642
    void setCol(int h, int s, int v);
 
643
    void setCol(int h, int s);
 
644
 
 
645
signals:
 
646
    void newHsv(int h, int s, int v);
 
647
 
 
648
protected:
 
649
    void paintEvent(QPaintEvent*);
 
650
    void mouseMoveEvent(QMouseEvent *);
 
651
    void mousePressEvent(QMouseEvent *);
 
652
 
 
653
private:
 
654
    enum { foff = 3, coff = 4 }; //frame and contents offset
 
655
    int val;
 
656
    int hue;
 
657
    int sat;
 
658
 
 
659
    int y2val(int y);
 
660
    int val2y(int val);
 
661
    void setVal(int v);
 
662
 
 
663
    QPixmap *pix;
 
664
};
 
665
 
 
666
 
 
667
int QColorLuminancePicker::y2val(int y)
 
668
{
 
669
    int d = height() - 2*coff - 1;
 
670
    return 255 - (y - coff)*255/d;
 
671
}
 
672
 
 
673
int QColorLuminancePicker::val2y(int v)
 
674
{
 
675
    int d = height() - 2*coff - 1;
 
676
    return coff + (255-v)*d/255;
 
677
}
 
678
 
 
679
QColorLuminancePicker::QColorLuminancePicker(QWidget* parent)
 
680
    :QWidget(parent)
 
681
{
 
682
    hue = 100; val = 100; sat = 100;
 
683
    pix = 0;
 
684
    //    setAttribute(WA_NoErase, true);
 
685
}
 
686
 
 
687
QColorLuminancePicker::~QColorLuminancePicker()
 
688
{
 
689
    delete pix;
 
690
}
 
691
 
 
692
void QColorLuminancePicker::mouseMoveEvent(QMouseEvent *m)
 
693
{
 
694
    setVal(y2val(m->y()));
 
695
}
 
696
void QColorLuminancePicker::mousePressEvent(QMouseEvent *m)
 
697
{
 
698
    setVal(y2val(m->y()));
 
699
}
 
700
 
 
701
void QColorLuminancePicker::setVal(int v)
 
702
{
 
703
    if (val == v)
 
704
        return;
 
705
    val = qMax(0, qMin(v,255));
 
706
    delete pix; pix=0;
 
707
    repaint();
 
708
    emit newHsv(hue, sat, val);
 
709
}
 
710
 
 
711
//receives from a hue,sat chooser and relays.
 
712
void QColorLuminancePicker::setCol(int h, int s)
 
713
{
 
714
    setCol(h, s, val);
 
715
    emit newHsv(h, s, val);
 
716
}
 
717
 
 
718
void QColorLuminancePicker::paintEvent(QPaintEvent *)
 
719
{
 
720
    int w = width() - 5;
 
721
 
 
722
    QRect r(0, foff, w, height() - 2*foff);
 
723
    int wi = r.width() - 2;
 
724
    int hi = r.height() - 2;
 
725
    if (!pix || pix->height() != hi || pix->width() != wi) {
 
726
        delete pix;
 
727
        QImage img(wi, hi, QImage::Format_RGB32);
 
728
        int y;
 
729
        uint *pixel = (uint *) img.scanLine(0);
 
730
        for (y = 0; y < hi; y++) {
 
731
            const uint *end = pixel + wi;
 
732
            while (pixel < end) {
 
733
                QColor c;
 
734
                c.setHsv(hue, sat, y2val(y+coff));
 
735
                *pixel = c.rgb();
 
736
                ++pixel;
 
737
            }
 
738
        }
 
739
        pix = new QPixmap(QPixmap::fromImage(img));
 
740
    }
 
741
    QPainter p(this);
 
742
    p.drawPixmap(1, coff, *pix);
 
743
    const QPalette &g = palette();
 
744
    qDrawShadePanel(&p, r, g, true);
 
745
    p.setPen(g.foreground().color());
 
746
    p.setBrush(g.foreground());
 
747
    QPolygon a;
 
748
    int y = val2y(val);
 
749
    a.setPoints(3, w, y, w+5, y+5, w+5, y-5);
 
750
    p.eraseRect(w, 0, 5, height());
 
751
    p.drawPolygon(a);
 
752
}
 
753
 
 
754
void QColorLuminancePicker::setCol(int h, int s , int v)
 
755
{
 
756
    val = v;
 
757
    hue = h;
 
758
    sat = s;
 
759
    delete pix; pix=0;
 
760
    repaint();
 
761
}
 
762
 
 
763
QPoint QColorPicker::colPt()
 
764
{
 
765
    QRect r = contentsRect();
 
766
    return QPoint((360 - hue) * (r.width() - 1) / 360, (255 - sat) * (r.height() - 1) / 255);
 
767
}
 
768
 
 
769
int QColorPicker::huePt(const QPoint &pt)
 
770
{
 
771
    QRect r = contentsRect();
 
772
    return 360 - pt.x() * 360 / (r.width() - 1);
 
773
}
 
774
 
 
775
int QColorPicker::satPt(const QPoint &pt)
 
776
{
 
777
    QRect r = contentsRect();
 
778
    return 255 - pt.y() * 255 / (r.height() - 1);
 
779
}
 
780
 
 
781
void QColorPicker::setCol(const QPoint &pt)
 
782
{
 
783
    setCol(huePt(pt), satPt(pt));
 
784
}
 
785
 
 
786
QColorPicker::QColorPicker(QWidget* parent)
 
787
    : QFrame(parent)
 
788
{
 
789
    hue = 0; sat = 0;
 
790
    setCol(150, 255);
 
791
 
 
792
    setAttribute(Qt::WA_NoSystemBackground);
 
793
    setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
 
794
}
 
795
 
 
796
QColorPicker::~QColorPicker()
 
797
{
 
798
}
 
799
 
 
800
QSize QColorPicker::sizeHint() const
 
801
{
 
802
    return QSize(pWidth + 2*frameWidth(), pHeight + 2*frameWidth());
 
803
}
 
804
 
 
805
void QColorPicker::setCol(int h, int s)
 
806
{
 
807
    int nhue = qMin(qMax(0,h), 359);
 
808
    int nsat = qMin(qMax(0,s), 255);
 
809
    if (nhue == hue && nsat == sat)
 
810
        return;
 
811
 
 
812
    QRect r(colPt(), QSize(20,20));
 
813
    hue = nhue; sat = nsat;
 
814
    r = r.united(QRect(colPt(), QSize(20,20)));
 
815
    r.translate(contentsRect().x()-9, contentsRect().y()-9);
 
816
    //    update(r);
 
817
    repaint(r);
 
818
}
 
819
 
 
820
void QColorPicker::mouseMoveEvent(QMouseEvent *m)
 
821
{
 
822
    QPoint p = m->pos() - contentsRect().topLeft();
 
823
    setCol(p);
 
824
    emit newCol(hue, sat);
 
825
}
 
826
 
 
827
void QColorPicker::mousePressEvent(QMouseEvent *m)
 
828
{
 
829
    QPoint p = m->pos() - contentsRect().topLeft();
 
830
    setCol(p);
 
831
    emit newCol(hue, sat);
 
832
}
 
833
 
 
834
void QColorPicker::paintEvent(QPaintEvent* )
 
835
{
 
836
    QPainter p(this);
 
837
    drawFrame(&p);
 
838
    QRect r = contentsRect();
 
839
 
 
840
    p.drawPixmap(r.topLeft(), pix);
 
841
    QPoint pt = colPt() + r.topLeft();
 
842
    p.setPen(Qt::black);
 
843
 
 
844
    p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
 
845
    p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
 
846
 
 
847
}
 
848
 
 
849
void QColorPicker::resizeEvent(QResizeEvent *ev)
 
850
{
 
851
    QFrame::resizeEvent(ev);
 
852
 
 
853
    int w = width() - frameWidth() * 2;
 
854
    int h = height() - frameWidth() * 2;
 
855
    QImage img(w, h, QImage::Format_RGB32);
 
856
    int x, y;
 
857
    uint *pixel = (uint *) img.scanLine(0);
 
858
    for (y = 0; y < h; y++) {
 
859
        const uint *end = pixel + w;
 
860
        x = 0;
 
861
        while (pixel < end) {
 
862
            QPoint p(x, y);
 
863
            QColor c;
 
864
            c.setHsv(huePt(p), satPt(p), 200);
 
865
            *pixel = c.rgb();
 
866
            ++pixel;
 
867
            ++x;
 
868
        }
 
869
    }
 
870
    pix = QPixmap::fromImage(img);
 
871
}
 
872
 
 
873
 
 
874
class QColSpinBox : public QSpinBox
 
875
{
 
876
public:
 
877
    QColSpinBox(QWidget *parent)
 
878
        : QSpinBox(parent) { setRange(0, 255); }
 
879
    void setValue(int i) {
 
880
        bool block = signalsBlocked();
 
881
        blockSignals(true);
 
882
        QSpinBox::setValue(i);
 
883
        blockSignals(block);
 
884
    }
 
885
};
 
886
 
 
887
class QColorShowLabel;
 
888
 
 
889
class QColorShower : public QWidget
 
890
{
 
891
    Q_OBJECT
 
892
public:
 
893
    QColorShower(QColorDialog *parent);
 
894
 
 
895
    //things that don't emit signals
 
896
    void setHsv(int h, int s, int v);
 
897
 
 
898
    int currentAlpha() const
 
899
    { return (colorDialog->options() & QColorDialog::ShowAlphaChannel) ? alphaEd->value() : 255; }
 
900
    void setCurrentAlpha(int a) { alphaEd->setValue(a); rgbEd(); }
 
901
    void showAlpha(bool b);
 
902
    bool isAlphaVisible() const;
 
903
 
 
904
    QRgb currentColor() const { return curCol; }
 
905
    QColor currentQColor() const { return curQColor; }
 
906
    void retranslateStrings();
 
907
    void updateQColor();
 
908
 
 
909
public slots:
 
910
    void setRgb(QRgb rgb);
 
911
 
 
912
signals:
 
913
    void newCol(QRgb rgb);
 
914
    void currentColorChanged(const QColor &color);
 
915
 
 
916
private slots:
 
917
    void rgbEd();
 
918
    void hsvEd();
 
919
private:
 
920
    void showCurrentColor();
 
921
    int hue, sat, val;
 
922
    QRgb curCol;
 
923
    QColor curQColor;
 
924
    QLabel *lblHue;
 
925
    QLabel *lblSat;
 
926
    QLabel *lblVal;
 
927
    QLabel *lblRed;
 
928
    QLabel *lblGreen;
 
929
    QLabel *lblBlue;
 
930
    QColSpinBox *hEd;
 
931
    QColSpinBox *sEd;
 
932
    QColSpinBox *vEd;
 
933
    QColSpinBox *rEd;
 
934
    QColSpinBox *gEd;
 
935
    QColSpinBox *bEd;
 
936
    QColSpinBox *alphaEd;
 
937
    QLabel *alphaLab;
 
938
    QColorShowLabel *lab;
 
939
    bool rgbOriginal;
 
940
    QColorDialog *colorDialog;
 
941
 
 
942
    friend class QColorDialog;
 
943
    friend class QColorDialogPrivate;
 
944
};
 
945
 
 
946
class QColorShowLabel : public QFrame
 
947
{
 
948
    Q_OBJECT
 
949
 
 
950
public:
 
951
    QColorShowLabel(QWidget *parent) : QFrame(parent) {
 
952
        setFrameStyle(QFrame::Panel|QFrame::Sunken);
 
953
        setAcceptDrops(true);
 
954
        mousePressed = false;
 
955
    }
 
956
    void setColor(QColor c) { col = c; }
 
957
 
 
958
signals:
 
959
    void colorDropped(QRgb);
 
960
 
 
961
protected:
 
962
    void paintEvent(QPaintEvent *);
 
963
    void mousePressEvent(QMouseEvent *e);
 
964
    void mouseMoveEvent(QMouseEvent *e);
 
965
    void mouseReleaseEvent(QMouseEvent *e);
 
966
#ifndef QT_NO_DRAGANDDROP
 
967
    void dragEnterEvent(QDragEnterEvent *e);
 
968
    void dragLeaveEvent(QDragLeaveEvent *e);
 
969
    void dropEvent(QDropEvent *e);
 
970
#endif
 
971
 
 
972
private:
 
973
    QColor col;
 
974
    bool mousePressed;
 
975
    QPoint pressPos;
 
976
};
 
977
 
 
978
void QColorShowLabel::paintEvent(QPaintEvent *e)
 
979
{
 
980
    QPainter p(this);
 
981
    drawFrame(&p);
 
982
    p.fillRect(contentsRect()&e->rect(), col);
 
983
}
 
984
 
 
985
void QColorShower::showAlpha(bool b)
 
986
{
 
987
    alphaLab->setVisible(b);
 
988
    alphaEd->setVisible(b);
 
989
}
 
990
 
 
991
inline bool QColorShower::isAlphaVisible() const
 
992
{
 
993
    return alphaLab->isVisible();
 
994
}
 
995
 
 
996
void QColorShowLabel::mousePressEvent(QMouseEvent *e)
 
997
{
 
998
    mousePressed = true;
 
999
    pressPos = e->pos();
 
1000
}
 
1001
 
 
1002
void QColorShowLabel::mouseMoveEvent(QMouseEvent *e)
 
1003
{
 
1004
#ifdef QT_NO_DRAGANDDROP
 
1005
    Q_UNUSED(e);
 
1006
#else
 
1007
    if (!mousePressed)
 
1008
        return;
 
1009
    if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
 
1010
        QMimeData *mime = new QMimeData;
 
1011
        mime->setColorData(col);
 
1012
        QPixmap pix(30, 20);
 
1013
        pix.fill(col);
 
1014
        QPainter p(&pix);
 
1015
        p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
 
1016
        p.end();
 
1017
        QDrag *drg = new QDrag(this);
 
1018
        drg->setMimeData(mime);
 
1019
        drg->setPixmap(pix);
 
1020
        mousePressed = false;
 
1021
        drg->start();
 
1022
    }
 
1023
#endif
 
1024
}
 
1025
 
 
1026
#ifndef QT_NO_DRAGANDDROP
 
1027
void QColorShowLabel::dragEnterEvent(QDragEnterEvent *e)
 
1028
{
 
1029
    if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
 
1030
        e->accept();
 
1031
    else
 
1032
        e->ignore();
 
1033
}
 
1034
 
 
1035
void QColorShowLabel::dragLeaveEvent(QDragLeaveEvent *)
 
1036
{
 
1037
}
 
1038
 
 
1039
void QColorShowLabel::dropEvent(QDropEvent *e)
 
1040
{
 
1041
    QColor color = qvariant_cast<QColor>(e->mimeData()->colorData());
 
1042
    if (color.isValid()) {
 
1043
        col = color;
 
1044
        repaint();
 
1045
        emit colorDropped(col.rgb());
 
1046
        e->accept();
 
1047
    } else {
 
1048
        e->ignore();
 
1049
    }
 
1050
}
 
1051
#endif // QT_NO_DRAGANDDROP
 
1052
 
 
1053
void QColorShowLabel::mouseReleaseEvent(QMouseEvent *)
 
1054
{
 
1055
    if (!mousePressed)
 
1056
        return;
 
1057
    mousePressed = false;
 
1058
}
 
1059
 
 
1060
QColorShower::QColorShower(QColorDialog *parent)
 
1061
    : QWidget(parent)
 
1062
{
 
1063
    colorDialog = parent;
 
1064
 
 
1065
    curCol = qRgb(255, 255, 255);
 
1066
    curQColor = Qt::white;
 
1067
 
 
1068
    QGridLayout *gl = new QGridLayout(this);
 
1069
    gl->setMargin(gl->spacing());
 
1070
    lab = new QColorShowLabel(this);
 
1071
 
 
1072
#ifndef Q_OS_WINCE
 
1073
#ifdef QT_SMALL_COLORDIALOG
 
1074
    lab->setMinimumHeight(60);
 
1075
#endif
 
1076
    lab->setMinimumWidth(60);
 
1077
#else
 
1078
    lab->setMinimumWidth(20);
 
1079
#endif
 
1080
 
 
1081
// For QVGA screens only the comboboxes and color label are visible.
 
1082
// For nHD screens only color and luminence pickers and color label are visible.
 
1083
#if !defined(QT_SMALL_COLORDIALOG)
 
1084
    gl->addWidget(lab, 0, 0, -1, 1);
 
1085
#else
 
1086
    if (nonTouchUI)
 
1087
        gl->addWidget(lab, 0, 0, 1, -1);
 
1088
    else
 
1089
        gl->addWidget(lab, 0, 0, -1, 1);
 
1090
#endif
 
1091
    connect(lab, SIGNAL(colorDropped(QRgb)), this, SIGNAL(newCol(QRgb)));
 
1092
    connect(lab, SIGNAL(colorDropped(QRgb)), this, SLOT(setRgb(QRgb)));
 
1093
 
 
1094
    hEd = new QColSpinBox(this);
 
1095
    hEd->setRange(0, 359);
 
1096
    lblHue = new QLabel(this);
 
1097
#ifndef QT_NO_SHORTCUT
 
1098
    lblHue->setBuddy(hEd);
 
1099
#endif
 
1100
    lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1101
#if !defined(QT_SMALL_COLORDIALOG)
 
1102
    gl->addWidget(lblHue, 0, 1);
 
1103
    gl->addWidget(hEd, 0, 2);
 
1104
#else
 
1105
    if (nonTouchUI) {
 
1106
        gl->addWidget(lblHue, 1, 0);
 
1107
        gl->addWidget(hEd, 2, 0);
 
1108
    } else {
 
1109
        lblHue->hide();
 
1110
        hEd->hide();
 
1111
    }
 
1112
#endif
 
1113
 
 
1114
    sEd = new QColSpinBox(this);
 
1115
    lblSat = new QLabel(this);
 
1116
#ifndef QT_NO_SHORTCUT
 
1117
    lblSat->setBuddy(sEd);
 
1118
#endif
 
1119
    lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1120
#if !defined(QT_SMALL_COLORDIALOG)
 
1121
    gl->addWidget(lblSat, 1, 1);
 
1122
    gl->addWidget(sEd, 1, 2);
 
1123
#else
 
1124
    if (nonTouchUI) {
 
1125
        gl->addWidget(lblSat, 1, 1);
 
1126
        gl->addWidget(sEd, 2, 1);
 
1127
    } else {
 
1128
        lblSat->hide();
 
1129
        sEd->hide();
 
1130
    }
 
1131
#endif
 
1132
 
 
1133
    vEd = new QColSpinBox(this);
 
1134
    lblVal = new QLabel(this);
 
1135
#ifndef QT_NO_SHORTCUT
 
1136
    lblVal->setBuddy(vEd);
 
1137
#endif
 
1138
    lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1139
#if !defined(QT_SMALL_COLORDIALOG)
 
1140
    gl->addWidget(lblVal, 2, 1);
 
1141
    gl->addWidget(vEd, 2, 2);
 
1142
#else
 
1143
    if (nonTouchUI) {
 
1144
        gl->addWidget(lblVal, 1, 2);
 
1145
        gl->addWidget(vEd, 2, 2);
 
1146
    } else {
 
1147
        lblVal->hide();
 
1148
        vEd->hide();
 
1149
    }
 
1150
#endif
 
1151
 
 
1152
    rEd = new QColSpinBox(this);
 
1153
    lblRed = new QLabel(this);
 
1154
#ifndef QT_NO_SHORTCUT
 
1155
    lblRed->setBuddy(rEd);
 
1156
#endif
 
1157
    lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1158
#if !defined(QT_SMALL_COLORDIALOG)
 
1159
    gl->addWidget(lblRed, 0, 3);
 
1160
    gl->addWidget(rEd, 0, 4);
 
1161
#else
 
1162
    if (nonTouchUI) {
 
1163
        gl->addWidget(lblRed, 3, 0);
 
1164
        gl->addWidget(rEd, 4, 0);
 
1165
    } else {
 
1166
        lblRed->hide();
 
1167
        rEd->hide();
 
1168
    }
 
1169
#endif
 
1170
 
 
1171
    gEd = new QColSpinBox(this);
 
1172
    lblGreen = new QLabel(this);
 
1173
#ifndef QT_NO_SHORTCUT
 
1174
    lblGreen->setBuddy(gEd);
 
1175
#endif
 
1176
    lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1177
#if !defined(QT_SMALL_COLORDIALOG)
 
1178
    gl->addWidget(lblGreen, 1, 3);
 
1179
    gl->addWidget(gEd, 1, 4);
 
1180
#else
 
1181
    if (nonTouchUI) {
 
1182
        gl->addWidget(lblGreen, 3, 1);
 
1183
        gl->addWidget(gEd, 4, 1);
 
1184
    } else {
 
1185
        lblGreen->hide();
 
1186
        gEd->hide();
 
1187
    }
 
1188
#endif
 
1189
 
 
1190
    bEd = new QColSpinBox(this);
 
1191
    lblBlue = new QLabel(this);
 
1192
#ifndef QT_NO_SHORTCUT
 
1193
    lblBlue->setBuddy(bEd);
 
1194
#endif
 
1195
    lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1196
#if !defined(QT_SMALL_COLORDIALOG)
 
1197
    gl->addWidget(lblBlue, 2, 3);
 
1198
    gl->addWidget(bEd, 2, 4);
 
1199
#else
 
1200
    if (nonTouchUI) {
 
1201
        gl->addWidget(lblBlue, 3, 2);
 
1202
        gl->addWidget(bEd, 4, 2);
 
1203
    } else {
 
1204
        lblBlue->hide();
 
1205
        bEd->hide();
 
1206
    }
 
1207
#endif
 
1208
 
 
1209
    alphaEd = new QColSpinBox(this);
 
1210
    alphaLab = new QLabel(this);
 
1211
#ifndef QT_NO_SHORTCUT
 
1212
    alphaLab->setBuddy(alphaEd);
 
1213
#endif
 
1214
    alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1215
#if !defined(QT_SMALL_COLORDIALOG)
 
1216
    gl->addWidget(alphaLab, 3, 1, 1, 3);
 
1217
    gl->addWidget(alphaEd, 3, 4);
 
1218
#else
 
1219
    if (nonTouchUI) {
 
1220
        gl->addWidget(alphaLab, 1, 3, 3, 1);
 
1221
        gl->addWidget(alphaEd, 4, 3);
 
1222
    } else {
 
1223
        alphaLab->hide();
 
1224
        alphaEd->hide();
 
1225
    }
 
1226
#endif
 
1227
    alphaEd->hide();
 
1228
    alphaLab->hide();
 
1229
 
 
1230
    connect(hEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
 
1231
    connect(sEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
 
1232
    connect(vEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
 
1233
 
 
1234
    connect(rEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
 
1235
    connect(gEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
 
1236
    connect(bEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
 
1237
    connect(alphaEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
 
1238
 
 
1239
    retranslateStrings();
 
1240
}
 
1241
 
 
1242
inline QRgb QColorDialogPrivate::currentColor() const { return cs->currentColor(); }
 
1243
inline int QColorDialogPrivate::currentAlpha() const { return cs->currentAlpha(); }
 
1244
inline void QColorDialogPrivate::setCurrentAlpha(int a) { cs->setCurrentAlpha(a); }
 
1245
inline void QColorDialogPrivate::showAlpha(bool b) { cs->showAlpha(b); }
 
1246
inline bool QColorDialogPrivate::isAlphaVisible() const { return cs->isAlphaVisible(); }
 
1247
 
 
1248
QColor QColorDialogPrivate::currentQColor() const
 
1249
{
 
1250
    if (!options->testOption(QColorDialogOptions::DontUseNativeDialog) && nativeDialogInUse)
 
1251
        return platformColorDialogHelper()->currentColor();
 
1252
    return cs->currentQColor();
 
1253
}
 
1254
 
 
1255
void QColorShower::showCurrentColor()
 
1256
{
 
1257
    lab->setColor(currentColor());
 
1258
    lab->repaint();
 
1259
}
 
1260
 
 
1261
void QColorShower::rgbEd()
 
1262
{
 
1263
    rgbOriginal = true;
 
1264
    curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
 
1265
 
 
1266
    rgb2hsv(currentColor(), hue, sat, val);
 
1267
 
 
1268
    hEd->setValue(hue);
 
1269
    sEd->setValue(sat);
 
1270
    vEd->setValue(val);
 
1271
 
 
1272
    showCurrentColor();
 
1273
    emit newCol(currentColor());
 
1274
    updateQColor();
 
1275
}
 
1276
 
 
1277
void QColorShower::hsvEd()
 
1278
{
 
1279
    rgbOriginal = false;
 
1280
    hue = hEd->value();
 
1281
    sat = sEd->value();
 
1282
    val = vEd->value();
 
1283
 
 
1284
    QColor c;
 
1285
    c.setHsv(hue, sat, val);
 
1286
    curCol = c.rgb();
 
1287
 
 
1288
    rEd->setValue(qRed(currentColor()));
 
1289
    gEd->setValue(qGreen(currentColor()));
 
1290
    bEd->setValue(qBlue(currentColor()));
 
1291
 
 
1292
    showCurrentColor();
 
1293
    emit newCol(currentColor());
 
1294
    updateQColor();
 
1295
}
 
1296
 
 
1297
void QColorShower::setRgb(QRgb rgb)
 
1298
{
 
1299
    rgbOriginal = true;
 
1300
    curCol = rgb;
 
1301
 
 
1302
    rgb2hsv(currentColor(), hue, sat, val);
 
1303
 
 
1304
    hEd->setValue(hue);
 
1305
    sEd->setValue(sat);
 
1306
    vEd->setValue(val);
 
1307
 
 
1308
    rEd->setValue(qRed(currentColor()));
 
1309
    gEd->setValue(qGreen(currentColor()));
 
1310
    bEd->setValue(qBlue(currentColor()));
 
1311
 
 
1312
    showCurrentColor();
 
1313
    updateQColor();
 
1314
}
 
1315
 
 
1316
void QColorShower::setHsv(int h, int s, int v)
 
1317
{
 
1318
    if (h < -1 || (uint)s > 255 || (uint)v > 255)
 
1319
            return;
 
1320
 
 
1321
    rgbOriginal = false;
 
1322
    hue = h; val = v; sat = s;
 
1323
    QColor c;
 
1324
    c.setHsv(hue, sat, val);
 
1325
    curCol = c.rgb();
 
1326
 
 
1327
    hEd->setValue(hue);
 
1328
    sEd->setValue(sat);
 
1329
    vEd->setValue(val);
 
1330
 
 
1331
    rEd->setValue(qRed(currentColor()));
 
1332
    gEd->setValue(qGreen(currentColor()));
 
1333
    bEd->setValue(qBlue(currentColor()));
 
1334
 
 
1335
    showCurrentColor();
 
1336
    updateQColor();
 
1337
}
 
1338
 
 
1339
void QColorShower::retranslateStrings()
 
1340
{
 
1341
    lblHue->setText(QColorDialog::tr("Hu&e:"));
 
1342
    lblSat->setText(QColorDialog::tr("&Sat:"));
 
1343
    lblVal->setText(QColorDialog::tr("&Val:"));
 
1344
    lblRed->setText(QColorDialog::tr("&Red:"));
 
1345
    lblGreen->setText(QColorDialog::tr("&Green:"));
 
1346
    lblBlue->setText(QColorDialog::tr("Bl&ue:"));
 
1347
    alphaLab->setText(QColorDialog::tr("A&lpha channel:"));
 
1348
}
 
1349
 
 
1350
void QColorShower::updateQColor()
 
1351
{
 
1352
    QColor oldQColor(curQColor);
 
1353
    curQColor.setRgba(qRgba(qRed(curCol), qGreen(curCol), qBlue(curCol), currentAlpha()));
 
1354
    if (curQColor != oldQColor)
 
1355
        emit currentColorChanged(curQColor);
 
1356
}
 
1357
 
 
1358
//sets all widgets to display h,s,v
 
1359
void QColorDialogPrivate::_q_newHsv(int h, int s, int v)
 
1360
{
 
1361
    cs->setHsv(h, s, v);
 
1362
    cp->setCol(h, s);
 
1363
    lp->setCol(h, s, v);
 
1364
}
 
1365
 
 
1366
//sets all widgets to display rgb
 
1367
void QColorDialogPrivate::setCurrentColor(QRgb rgb)
 
1368
{
 
1369
    cs->setRgb(rgb);
 
1370
    _q_newColorTypedIn(rgb);
 
1371
}
 
1372
 
 
1373
// hack; doesn't keep curCol in sync, so use with care
 
1374
void QColorDialogPrivate::setCurrentQColor(const QColor &color)
 
1375
{
 
1376
    Q_Q(QColorDialog);
 
1377
    if (cs->curQColor != color) {
 
1378
        cs->curQColor = color;
 
1379
        emit q->currentColorChanged(color);
 
1380
    }
 
1381
}
 
1382
 
 
1383
bool QColorDialogPrivate::selectColor(const QColor &col)
 
1384
{
 
1385
    QRgb color = col.rgb();
 
1386
    int i = 0, j = 0;
 
1387
    // Check standard colors
 
1388
    if (standard) {
 
1389
        const QRgb *standardColors = QColorDialogOptions::standardColors();
 
1390
        for (i = 0; i < 6; i++) {
 
1391
            for (j = 0; j < 8; j++) {
 
1392
                if (color == standardColors[i + j*6]) {
 
1393
                    _q_newStandard(i, j);
 
1394
                    standard->setCurrent(i, j);
 
1395
                    standard->setSelected(i, j);
 
1396
                    standard->setFocus();
 
1397
                    return true;
 
1398
                }
 
1399
            }
 
1400
        }
 
1401
    }
 
1402
    // Check custom colors
 
1403
    if (custom) {
 
1404
        const QRgb *customColors = QColorDialogOptions::customColors();
 
1405
        for (i = 0; i < 2; i++) {
 
1406
            for (j = 0; j < 8; j++) {
 
1407
                if (color == customColors[i + j*2]) {
 
1408
                    _q_newCustom(i, j);
 
1409
                    custom->setCurrent(i, j);
 
1410
                    custom->setSelected(i, j);
 
1411
                    custom->setFocus();
 
1412
                    return true;
 
1413
                }
 
1414
            }
 
1415
        }
 
1416
    }
 
1417
    return false;
 
1418
}
 
1419
 
 
1420
//sets all widgets except cs to display rgb
 
1421
void QColorDialogPrivate::_q_newColorTypedIn(QRgb rgb)
 
1422
{
 
1423
    int h, s, v;
 
1424
    rgb2hsv(rgb, h, s, v);
 
1425
    cp->setCol(h, s);
 
1426
    lp->setCol(h, s, v);
 
1427
}
 
1428
 
 
1429
void QColorDialogPrivate::_q_newCustom(int r, int c)
 
1430
{
 
1431
    const int i = r + 2 * c;
 
1432
    setCurrentColor(QColorDialogOptions::customColor(i));
 
1433
    nextCust = i;
 
1434
    if (standard)
 
1435
        standard->setSelected(-1,-1);
 
1436
}
 
1437
 
 
1438
void QColorDialogPrivate::_q_newStandard(int r, int c)
 
1439
{
 
1440
    setCurrentColor(QColorDialogOptions::standardColor(r + c * 6));
 
1441
    if (custom)
 
1442
        custom->setSelected(-1,-1);
 
1443
}
 
1444
 
 
1445
void QColorDialogPrivate::init(const QColor &initial)
 
1446
{
 
1447
    Q_Q(QColorDialog);
 
1448
 
 
1449
    q->setSizeGripEnabled(false);
 
1450
    q->setWindowTitle(QColorDialog::tr("Select Color"));
 
1451
 
 
1452
    nativeDialogInUse = (platformColorDialogHelper() != 0);
 
1453
 
 
1454
    nextCust = 0;
 
1455
    QVBoxLayout *mainLay = new QVBoxLayout(q);
 
1456
    // there's nothing in this dialog that benefits from sizing up
 
1457
    mainLay->setSizeConstraint(QLayout::SetFixedSize);
 
1458
 
 
1459
    QHBoxLayout *topLay = new QHBoxLayout();
 
1460
    mainLay->addLayout(topLay);
 
1461
 
 
1462
    leftLay = 0;
 
1463
 
 
1464
#if defined(Q_OS_WINCE) || defined(QT_SMALL_COLORDIALOG)
 
1465
    smallDisplay = true;
 
1466
    const int lumSpace = 20;
 
1467
#else
 
1468
    // small displays (e.g. PDAs) cannot fit the full color dialog,
 
1469
    // so just use the color picker.
 
1470
    smallDisplay = (QApplication::desktop()->width() < 480 || QApplication::desktop()->height() < 350);
 
1471
    const int lumSpace = topLay->spacing() / 2;
 
1472
#endif
 
1473
 
 
1474
    if (!smallDisplay) {
 
1475
        leftLay = new QVBoxLayout;
 
1476
        topLay->addLayout(leftLay);
 
1477
    }
 
1478
 
 
1479
    if (!smallDisplay) {
 
1480
        standard = new QColorWell(q, 6, 8, QColorDialogOptions::standardColors());
 
1481
        lblBasicColors = new QLabel(q);
 
1482
#ifndef QT_NO_SHORTCUT
 
1483
        lblBasicColors->setBuddy(standard);
 
1484
#endif
 
1485
        q->connect(standard, SIGNAL(selected(int,int)), SLOT(_q_newStandard(int,int)));
 
1486
        leftLay->addWidget(lblBasicColors);
 
1487
        leftLay->addWidget(standard);
 
1488
 
 
1489
#if !defined(Q_OS_WINCE)
 
1490
        leftLay->addStretch();
 
1491
#endif
 
1492
 
 
1493
        custom = new QColorWell(q, 2, 8, QColorDialogOptions::customColors());
 
1494
        custom->setAcceptDrops(true);
 
1495
 
 
1496
        q->connect(custom, SIGNAL(selected(int,int)), SLOT(_q_newCustom(int,int)));
 
1497
        lblCustomColors = new QLabel(q);
 
1498
#ifndef QT_NO_SHORTCUT
 
1499
        lblCustomColors->setBuddy(custom);
 
1500
#endif
 
1501
        leftLay->addWidget(lblCustomColors);
 
1502
        leftLay->addWidget(custom);
 
1503
 
 
1504
        addCusBt = new QPushButton(q);
 
1505
        QObject::connect(addCusBt, SIGNAL(clicked()), q, SLOT(_q_addCustom()));
 
1506
        leftLay->addWidget(addCusBt);
 
1507
    } else {
 
1508
        // better color picker size for small displays
 
1509
#if defined(QT_SMALL_COLORDIALOG)
 
1510
        QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()).size();
 
1511
        pWidth = pHeight = qMin(screenSize.width(), screenSize.height());
 
1512
        pHeight -= 20;
 
1513
        if(screenSize.height() > screenSize.width())
 
1514
            pWidth -= 20;
 
1515
#else
 
1516
        pWidth = 150;
 
1517
        pHeight = 100;
 
1518
#endif
 
1519
        custom = 0;
 
1520
        standard = 0;
 
1521
    }
 
1522
 
 
1523
    QVBoxLayout *rightLay = new QVBoxLayout;
 
1524
    topLay->addLayout(rightLay);
 
1525
 
 
1526
    QHBoxLayout *pickLay = new QHBoxLayout;
 
1527
    rightLay->addLayout(pickLay);
 
1528
 
 
1529
    QVBoxLayout *cLay = new QVBoxLayout;
 
1530
    pickLay->addLayout(cLay);
 
1531
    cp = new QColorPicker(q);
 
1532
 
 
1533
    cp->setFrameStyle(QFrame::Panel + QFrame::Sunken);
 
1534
 
 
1535
#if defined(QT_SMALL_COLORDIALOG)
 
1536
    if (!nonTouchUI) {
 
1537
        pickLay->addWidget(cp);
 
1538
        cLay->addSpacing(lumSpace);
 
1539
    } else {
 
1540
        cp->hide();
 
1541
    }
 
1542
#else
 
1543
    cLay->addSpacing(lumSpace);
 
1544
    cLay->addWidget(cp);
 
1545
#endif
 
1546
    cLay->addSpacing(lumSpace);
 
1547
 
 
1548
    lp = new QColorLuminancePicker(q);
 
1549
#if defined(QT_SMALL_COLORDIALOG)
 
1550
    QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()).size();
 
1551
    const int minDimension = qMin(screenSize.height(), screenSize.width());
 
1552
    //set picker to be finger-usable
 
1553
    int pickerWidth = !nonTouchUI ? minDimension/9 : minDimension/12;
 
1554
    lp->setFixedWidth(pickerWidth);
 
1555
    if (!nonTouchUI)
 
1556
        pickLay->addWidget(lp);
 
1557
    else
 
1558
        lp->hide();
 
1559
#else
 
1560
    lp->setFixedWidth(20);
 
1561
    pickLay->addWidget(lp);
 
1562
#endif
 
1563
 
 
1564
    QObject::connect(cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int)));
 
1565
    QObject::connect(lp, SIGNAL(newHsv(int,int,int)), q, SLOT(_q_newHsv(int,int,int)));
 
1566
 
 
1567
    rightLay->addStretch();
 
1568
 
 
1569
    cs = new QColorShower(q);
 
1570
    QObject::connect(cs, SIGNAL(newCol(QRgb)), q, SLOT(_q_newColorTypedIn(QRgb)));
 
1571
    QObject::connect(cs, SIGNAL(currentColorChanged(QColor)),
 
1572
                     q, SIGNAL(currentColorChanged(QColor)));
 
1573
#if defined(QT_SMALL_COLORDIALOG)
 
1574
    if (!nonTouchUI)
 
1575
        pWidth -= cp->size().width();
 
1576
    topLay->addWidget(cs);
 
1577
#else
 
1578
    rightLay->addWidget(cs);
 
1579
#endif
 
1580
 
 
1581
    buttons = new QDialogButtonBox(q);
 
1582
    mainLay->addWidget(buttons);
 
1583
 
 
1584
    ok = buttons->addButton(QDialogButtonBox::Ok);
 
1585
    QObject::connect(ok, SIGNAL(clicked()), q, SLOT(accept()));
 
1586
    ok->setDefault(true);
 
1587
    cancel = buttons->addButton(QDialogButtonBox::Cancel);
 
1588
    QObject::connect(cancel, SIGNAL(clicked()), q, SLOT(reject()));
 
1589
 
 
1590
    retranslateStrings();
 
1591
 
 
1592
#ifdef Q_WS_MAC
 
1593
    delegate = 0;
 
1594
#endif
 
1595
 
 
1596
    q->setCurrentColor(initial);
 
1597
}
 
1598
 
 
1599
void QColorDialogPrivate::initHelper(QPlatformDialogHelper *h)
 
1600
{
 
1601
    QColorDialog *d = q_func();
 
1602
    QObject::connect(h, SIGNAL(currentColorChanged(QColor)), d, SIGNAL(currentColorChanged(QColor)));
 
1603
    QObject::connect(h, SIGNAL(colorSelected(QColor)), d, SIGNAL(colorSelected(QColor)));
 
1604
    static_cast<QPlatformColorDialogHelper *>(h)->setOptions(options);
 
1605
}
 
1606
 
 
1607
void QColorDialogPrivate::helperPrepareShow(QPlatformDialogHelper *)
 
1608
{
 
1609
    options->setWindowTitle(q_func()->windowTitle());
 
1610
}
 
1611
 
 
1612
void QColorDialogPrivate::_q_addCustom()
 
1613
{
 
1614
    QColorDialogOptions::setCustomColor(nextCust, cs->currentColor());
 
1615
    if (custom)
 
1616
        custom->update();
 
1617
    nextCust = (nextCust+1) % 16;
 
1618
}
 
1619
 
 
1620
void QColorDialogPrivate::retranslateStrings()
 
1621
{
 
1622
    if (!smallDisplay) {
 
1623
        lblBasicColors->setText(QColorDialog::tr("&Basic colors"));
 
1624
        lblCustomColors->setText(QColorDialog::tr("&Custom colors"));
 
1625
        addCusBt->setText(QColorDialog::tr("&Add to Custom Colors"));
 
1626
    }
 
1627
 
 
1628
    cs->retranslateStrings();
 
1629
}
 
1630
 
 
1631
static const Qt::WindowFlags DefaultWindowFlags =
 
1632
        Qt::Dialog | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint
 
1633
        | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
 
1634
 
 
1635
/*!
 
1636
    \class QColorDialog
 
1637
    \brief The QColorDialog class provides a dialog widget for specifying colors.
 
1638
 
 
1639
    \ingroup standard-dialogs
 
1640
    \inmodule QtWidgets
 
1641
 
 
1642
    The color dialog's function is to allow users to choose colors.
 
1643
    For example, you might use this in a drawing program to allow the
 
1644
    user to set the brush color.
 
1645
 
 
1646
    The static functions provide modal color dialogs.
 
1647
    \omit
 
1648
    If you require a modeless dialog, use the QColorDialog constructor.
 
1649
    \endomit
 
1650
 
 
1651
    The static getColor() function shows the dialog, and allows the user to
 
1652
    specify a color. This function can also be used to let users choose a
 
1653
    color with a level of transparency: pass the ShowAlphaChannel option as
 
1654
    an additional argument.
 
1655
 
 
1656
    The user can store customCount() different custom colors. The
 
1657
    custom colors are shared by all color dialogs, and remembered
 
1658
    during the execution of the program. Use setCustomColor() to set
 
1659
    the custom colors, and use customColor() to get them.
 
1660
 
 
1661
    The \l{dialogs/standarddialogs}{Standard Dialogs} example shows
 
1662
    how to use QColorDialog as well as other built-in Qt dialogs.
 
1663
 
 
1664
    \image fusion-colordialog.png A color dialog in the Fusion widget style.
 
1665
 
 
1666
    \sa QColor, QFileDialog, QFontDialog, {Standard Dialogs Example}
 
1667
*/
 
1668
 
 
1669
/*!
 
1670
    \since 4.5
 
1671
 
 
1672
    Constructs a color dialog with the given \a parent.
 
1673
*/
 
1674
QColorDialog::QColorDialog(QWidget *parent)
 
1675
    : QDialog(*new QColorDialogPrivate, parent, DefaultWindowFlags)
 
1676
{
 
1677
    Q_D(QColorDialog);
 
1678
    d->init(Qt::white);
 
1679
}
 
1680
 
 
1681
/*!
 
1682
    \since 4.5
 
1683
 
 
1684
    Constructs a color dialog with the given \a parent and specified
 
1685
    \a initial color.
 
1686
*/
 
1687
QColorDialog::QColorDialog(const QColor &initial, QWidget *parent)
 
1688
    : QDialog(*new QColorDialogPrivate, parent, DefaultWindowFlags)
 
1689
{
 
1690
    Q_D(QColorDialog);
 
1691
    d->init(initial);
 
1692
}
 
1693
 
 
1694
/*!
 
1695
    \property QColorDialog::currentColor
 
1696
    \brief the currently selected color in the dialog
 
1697
*/
 
1698
 
 
1699
void QColorDialog::setCurrentColor(const QColor &color)
 
1700
{
 
1701
    Q_D(QColorDialog);
 
1702
    d->setCurrentColor(color.rgb());
 
1703
    d->selectColor(color);
 
1704
    d->setCurrentAlpha(color.alpha());
 
1705
 
 
1706
    if (!testOption(QColorDialog::DontUseNativeDialog) && d->nativeDialogInUse)
 
1707
        d->platformColorDialogHelper()->setCurrentColor(color);
 
1708
}
 
1709
 
 
1710
QColor QColorDialog::currentColor() const
 
1711
{
 
1712
    Q_D(const QColorDialog);
 
1713
    return d->currentQColor();
 
1714
}
 
1715
 
 
1716
/*!
 
1717
    Returns the color that the user selected by clicking the \uicontrol{OK}
 
1718
    or equivalent button.
 
1719
 
 
1720
    \note This color is not always the same as the color held by the
 
1721
    \l currentColor property since the user can choose different colors
 
1722
    before finally selecting the one to use.
 
1723
*/
 
1724
QColor QColorDialog::selectedColor() const
 
1725
{
 
1726
    Q_D(const QColorDialog);
 
1727
    return d->selectedQColor;
 
1728
}
 
1729
 
 
1730
/*!
 
1731
    Sets the given \a option to be enabled if \a on is true;
 
1732
    otherwise, clears the given \a option.
 
1733
 
 
1734
    \sa options, testOption()
 
1735
*/
 
1736
void QColorDialog::setOption(ColorDialogOption option, bool on)
 
1737
{
 
1738
    Q_D(QColorDialog);
 
1739
    d->options->setOption(static_cast<QColorDialogOptions::ColorDialogOption>(option), on);
 
1740
}
 
1741
 
 
1742
/*!
 
1743
    \since 4.5
 
1744
 
 
1745
    Returns true if the given \a option is enabled; otherwise, returns
 
1746
    false.
 
1747
 
 
1748
    \sa options, setOption()
 
1749
*/
 
1750
bool QColorDialog::testOption(ColorDialogOption option) const
 
1751
{
 
1752
    Q_D(const QColorDialog);
 
1753
    return d->options->testOption(static_cast<QColorDialogOptions::ColorDialogOption>(option));
 
1754
}
 
1755
 
 
1756
/*!
 
1757
    \property QColorDialog::options
 
1758
    \brief the various options that affect the look and feel of the dialog
 
1759
 
 
1760
    By default, all options are disabled.
 
1761
 
 
1762
    Options should be set before showing the dialog. Setting them while the
 
1763
    dialog is visible is not guaranteed to have an immediate effect on the
 
1764
    dialog (depending on the option and on the platform).
 
1765
 
 
1766
    \sa setOption(), testOption()
 
1767
*/
 
1768
void QColorDialog::setOptions(ColorDialogOptions options)
 
1769
{
 
1770
    Q_D(QColorDialog);
 
1771
 
 
1772
    if (QColorDialog::options() == options)
 
1773
        return;
 
1774
 
 
1775
    d->options->setOptions(QColorDialogOptions::ColorDialogOptions(int(options)));
 
1776
    d->buttons->setVisible(!(options & NoButtons));
 
1777
    d->showAlpha(options & ShowAlphaChannel);
 
1778
}
 
1779
 
 
1780
QColorDialog::ColorDialogOptions QColorDialog::options() const
 
1781
{
 
1782
    Q_D(const QColorDialog);
 
1783
    return QColorDialog::ColorDialogOptions(int(d->options->options()));
 
1784
}
 
1785
 
 
1786
/*!
 
1787
    \enum QColorDialog::ColorDialogOption
 
1788
 
 
1789
    \since 4.5
 
1790
 
 
1791
    This enum specifies various options that affect the look and feel
 
1792
    of a color dialog.
 
1793
 
 
1794
    \value ShowAlphaChannel Allow the user to select the alpha component of a color.
 
1795
    \value NoButtons Don't display \uicontrol{OK} and \uicontrol{Cancel} buttons. (Useful for "live dialogs".)
 
1796
    \value DontUseNativeDialog Use Qt's standard color dialog on the Mac instead of Apple's
 
1797
                               native color panel.
 
1798
 
 
1799
    \sa options, setOption(), testOption(), windowModality()
 
1800
*/
 
1801
 
 
1802
/*!
 
1803
    \fn void QColorDialog::currentColorChanged(const QColor &color)
 
1804
 
 
1805
    This signal is emitted whenever the current color changes in the dialog.
 
1806
    The current color is specified by \a color.
 
1807
 
 
1808
    \sa color, colorSelected()
 
1809
*/
 
1810
 
 
1811
#ifdef Q_WS_MAC
 
1812
// can only have one Cocoa color panel active
 
1813
bool QColorDialogPrivate::sharedColorPanelAvailable = true;
 
1814
#endif
 
1815
 
 
1816
/*!
 
1817
    \fn void QColorDialog::colorSelected(const QColor &color);
 
1818
 
 
1819
    This signal is emitted just after the user has clicked \uicontrol{OK} to
 
1820
    select a color to use. The chosen color is specified by \a color.
 
1821
 
 
1822
    \sa color, currentColorChanged()
 
1823
*/
 
1824
 
 
1825
/*!
 
1826
    Changes the visibility of the dialog. If \a visible is true, the dialog
 
1827
    is shown; otherwise, it is hidden.
 
1828
*/
 
1829
void QColorDialog::setVisible(bool visible)
 
1830
{
 
1831
    Q_D(QColorDialog);
 
1832
 
 
1833
    if (visible){
 
1834
        if (testAttribute(Qt::WA_WState_ExplicitShowHide) && !testAttribute(Qt::WA_WState_Hidden))
 
1835
            return;
 
1836
    } else if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden))
 
1837
        return;
 
1838
 
 
1839
    if (visible)
 
1840
        d->selectedQColor = QColor();
 
1841
 
 
1842
#if defined(Q_WS_MAC)
 
1843
    if (visible) {
 
1844
        if (d->delegate || (QColorDialogPrivate::sharedColorPanelAvailable &&
 
1845
                !(testAttribute(Qt::WA_DontShowOnScreen) || (d->opts & DontUseNativeDialog)))){
 
1846
            d->openCocoaColorPanel(currentColor(), parentWidget(), windowTitle(), options());
 
1847
            QColorDialogPrivate::sharedColorPanelAvailable = false;
 
1848
            setAttribute(Qt::WA_DontShowOnScreen);
 
1849
        }
 
1850
        setWindowFlags(windowModality() == Qt::WindowModal ? Qt::Sheet : DefaultWindowFlags);
 
1851
    } else {
 
1852
        if (d->delegate) {
 
1853
            d->closeCocoaColorPanel();
 
1854
            setAttribute(Qt::WA_DontShowOnScreen, false);
 
1855
        }
 
1856
    }
 
1857
#else
 
1858
 
 
1859
    if (!(options() & DontUseNativeDialog))
 
1860
        d->setNativeDialogVisible(visible);
 
1861
 
 
1862
    if (d->nativeDialogInUse) {
 
1863
        // Set WA_DontShowOnScreen so that QDialog::setVisible(visible) below
 
1864
        // updates the state correctly, but skips showing the non-native version:
 
1865
        setAttribute(Qt::WA_DontShowOnScreen);
 
1866
    } else {
 
1867
        d->nativeDialogInUse = false;
 
1868
        setAttribute(Qt::WA_DontShowOnScreen, false);
 
1869
    }
 
1870
#endif
 
1871
 
 
1872
    QDialog::setVisible(visible);
 
1873
}
 
1874
 
 
1875
/*!
 
1876
    \overload
 
1877
    \since 4.5
 
1878
 
 
1879
    Opens the dialog and connects its colorSelected() signal to the slot specified
 
1880
    by \a receiver and \a member.
 
1881
 
 
1882
    The signal will be disconnected from the slot when the dialog is closed.
 
1883
*/
 
1884
void QColorDialog::open(QObject *receiver, const char *member)
 
1885
{
 
1886
    Q_D(QColorDialog);
 
1887
    connect(this, SIGNAL(colorSelected(QColor)), receiver, member);
 
1888
    d->receiverToDisconnectOnClose = receiver;
 
1889
    d->memberToDisconnectOnClose = member;
 
1890
    QDialog::open();
 
1891
}
 
1892
 
 
1893
/*!
 
1894
    \fn QColorDialog::open()
 
1895
 
 
1896
    \since 4.5
 
1897
    Shows the dialog as a \l{QDialog#Modal Dialogs}{window modal dialog},
 
1898
    returning immediately.
 
1899
 
 
1900
    \sa QDialog::open()
 
1901
*/
 
1902
 
 
1903
/*!
 
1904
    \since 4.5
 
1905
 
 
1906
    Pops up a modal color dialog with the given window \a title (or "Select Color" if none is
 
1907
    specified), lets the user choose a color, and returns that color. The color is initially set
 
1908
    to \a initial. The dialog is a child of \a parent. It returns an invalid (see
 
1909
    QColor::isValid()) color if the user cancels the dialog.
 
1910
 
 
1911
    The \a options argument allows you to customize the dialog.
 
1912
*/
 
1913
QColor QColorDialog::getColor(const QColor &initial, QWidget *parent, const QString &title,
 
1914
                              ColorDialogOptions options)
 
1915
{
 
1916
    QColorDialog dlg(parent);
 
1917
    if (!title.isEmpty())
 
1918
        dlg.setWindowTitle(title);
 
1919
    dlg.setOptions(options);
 
1920
    dlg.setCurrentColor(initial);
 
1921
    dlg.exec();
 
1922
    return dlg.selectedColor();
 
1923
}
 
1924
 
 
1925
/*!
 
1926
    \obsolete
 
1927
 
 
1928
    Pops up a modal color dialog to allow the user to choose a color
 
1929
    and an alpha channel (transparency) value. The color+alpha is
 
1930
    initially set to \a initial. The dialog is a child of \a parent.
 
1931
 
 
1932
    If \a ok is non-null, \e *\a ok is set to true if the user clicked
 
1933
    \uicontrol{OK}, and to false if the user clicked Cancel.
 
1934
 
 
1935
    If the user clicks Cancel, the \a initial value is returned.
 
1936
 
 
1937
    Use QColorDialog::getColor() instead, passing the
 
1938
    QColorDialog::ShowAlphaChannel option.
 
1939
*/
 
1940
 
 
1941
QRgb QColorDialog::getRgba(QRgb initial, bool *ok, QWidget *parent)
 
1942
{
 
1943
    QColor color(getColor(QColor(initial), parent, QString(), ShowAlphaChannel));
 
1944
    QRgb result = color.isValid() ? color.rgba() : initial;
 
1945
    if (ok)
 
1946
        *ok = color.isValid();
 
1947
    return result;
 
1948
}
 
1949
 
 
1950
/*!
 
1951
    Destroys the color dialog.
 
1952
*/
 
1953
 
 
1954
QColorDialog::~QColorDialog()
 
1955
{
 
1956
#if defined(Q_WS_MAC)
 
1957
    Q_D(QColorDialog);
 
1958
    if (d->delegate) {
 
1959
        d->releaseCocoaColorPanelDelegate();
 
1960
        QColorDialogPrivate::sharedColorPanelAvailable = true;
 
1961
    }
 
1962
#endif
 
1963
}
 
1964
 
 
1965
/*!
 
1966
    \reimp
 
1967
*/
 
1968
void QColorDialog::changeEvent(QEvent *e)
 
1969
{
 
1970
    Q_D(QColorDialog);
 
1971
    if (e->type() == QEvent::LanguageChange)
 
1972
        d->retranslateStrings();
 
1973
    QDialog::changeEvent(e);
 
1974
}
 
1975
 
 
1976
/*!
 
1977
  Closes the dialog and sets its result code to \a result. If this dialog
 
1978
  is shown with exec(), done() causes the local event loop to finish,
 
1979
  and exec() to return \a result.
 
1980
 
 
1981
  \sa QDialog::done()
 
1982
*/
 
1983
void QColorDialog::done(int result)
 
1984
{
 
1985
    Q_D(QColorDialog);
 
1986
    QDialog::done(result);
 
1987
    if (result == Accepted) {
 
1988
        d->selectedQColor = d->currentQColor();
 
1989
        emit colorSelected(d->selectedQColor);
 
1990
    } else {
 
1991
        d->selectedQColor = QColor();
 
1992
    }
 
1993
    if (d->receiverToDisconnectOnClose) {
 
1994
        disconnect(this, SIGNAL(colorSelected(QColor)),
 
1995
                   d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
 
1996
        d->receiverToDisconnectOnClose = 0;
 
1997
    }
 
1998
    d->memberToDisconnectOnClose.clear();
 
1999
}
 
2000
 
 
2001
QT_END_NAMESPACE
 
2002
 
 
2003
#include "qcolordialog.moc"
 
2004
#include "moc_qcolordialog.cpp"
 
2005
 
 
2006
#endif // QT_NO_COLORDIALOG
 
2007