~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the dialog module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qcolordialog.h"
 
30
 
 
31
#ifndef QT_NO_COLORDIALOG
 
32
 
 
33
#include "qdialog_p.h"
 
34
#include "qapplication.h"
 
35
#include "qdesktopwidget.h"
 
36
#include "qdrawutil.h"
 
37
#include "qevent.h"
 
38
#include "qimage.h"
 
39
#include "qlabel.h"
 
40
#include "qlayout.h"
 
41
#include "qlineedit.h"
 
42
#include "qmenu.h"
 
43
#include "qpainter.h"
 
44
#include "qpixmap.h"
 
45
#include "qpushbutton.h"
 
46
#include "qsettings.h"
 
47
#include "qstyle.h"
 
48
#include "qstyleoption.h"
 
49
#include "qvalidator.h"
 
50
#include "qmime.h"
 
51
#include "qspinbox.h"
 
52
 
 
53
#ifdef Q_WS_MAC
 
54
QRgb macGetRgba(QRgb initial, bool *ok, QWidget *parent);
 
55
QColor macGetColor(const QColor& initial, QWidget *parent);
 
56
#endif
 
57
 
 
58
//////////// QWellArray BEGIN
 
59
 
 
60
struct QWellArrayData;
 
61
 
 
62
class QWellArray : public QWidget
 
63
{
 
64
    Q_OBJECT
 
65
    Q_PROPERTY(int selectedColumn READ selectedColumn)
 
66
    Q_PROPERTY(int selectedRow READ selectedRow)
 
67
 
 
68
public:
 
69
    QWellArray(int rows, int cols, QWidget* parent=0);
 
70
    ~QWellArray() {}
 
71
    QString cellContent(int row, int col) const;
 
72
 
 
73
    int selectedColumn() const { return selCol; }
 
74
    int selectedRow() const { return selRow; }
 
75
 
 
76
    virtual void setCurrent(int row, int col);
 
77
    virtual void setSelected(int row, int col);
 
78
 
 
79
    QSize sizeHint() const;
 
80
 
 
81
    virtual void setCellBrush(int row, int col, const QBrush &);
 
82
    QBrush cellBrush(int row, int col);
 
83
 
 
84
    inline int cellWidth() const
 
85
        { return cellw; }
 
86
 
 
87
    inline int cellHeight() const
 
88
        { return cellh; }
 
89
 
 
90
    inline int rowAt(int y) const
 
91
        { return y / cellh; }
 
92
 
 
93
    inline int columnAt(int x) const
 
94
        { if (isRightToLeft()) return ncols - (x / cellw) - 1; return x / cellw; }
 
95
 
 
96
    inline int rowY(int row) const
 
97
        { return cellh * row; }
 
98
 
 
99
    inline int columnX(int column) const
 
100
        { if (isRightToLeft()) return cellw * (ncols - column - 1); return cellw * column; }
 
101
 
 
102
    inline int numRows() const
 
103
        { return nrows; }
 
104
 
 
105
    inline int numCols() const
 
106
        {return ncols; }
 
107
 
 
108
    inline QRect cellRect() const
 
109
        { return QRect(0, 0, cellw, cellh); }
 
110
 
 
111
    inline QSize gridSize() const
 
112
        { return QSize(ncols * cellw, nrows * cellh); }
 
113
 
 
114
    QRect cellGeometry(int row, int column)
 
115
        {
 
116
            QRect r;
 
117
            if (row >= 0 && row < nrows && column >= 0 && column < ncols)
 
118
                r.setRect(columnX(column), rowY(row), cellw, cellh);
 
119
            return r;
 
120
        }
 
121
 
 
122
 
 
123
    inline void updateCell(int row, int column) { update(cellGeometry(row, column)); }
 
124
 
 
125
 
 
126
signals:
 
127
    void selected(int row, int col);
 
128
 
 
129
protected:
 
130
    virtual void paintCell(QPainter *, int row, int col);
 
131
    virtual void paintCellContents(QPainter *, int row, int col, const QRect&);
 
132
 
 
133
    void mousePressEvent(QMouseEvent*);
 
134
    void mouseReleaseEvent(QMouseEvent*);
 
135
    void keyPressEvent(QKeyEvent*);
 
136
    void focusInEvent(QFocusEvent*);
 
137
    void focusOutEvent(QFocusEvent*);
 
138
    void paintEvent(QPaintEvent *);
 
139
 
 
140
private:
 
141
    Q_DISABLE_COPY(QWellArray)
 
142
 
 
143
    int nrows;
 
144
    int ncols;
 
145
    int cellw;
 
146
    int cellh;
 
147
    int curRow;
 
148
    int curCol;
 
149
    int selRow;
 
150
    int selCol;
 
151
    QWellArrayData *d;
 
152
};
 
153
 
 
154
void QWellArray::paintEvent(QPaintEvent *e)
 
155
{
 
156
    QRect r = e->rect();
 
157
    int cx = r.x();
 
158
    int cy = r.y();
 
159
    int ch = r.height();
 
160
    int cw = r.width();
 
161
    int colfirst = columnAt(cx);
 
162
    int collast = columnAt(cx + cw);
 
163
    int rowfirst = rowAt(cy);
 
164
    int rowlast = rowAt(cy + ch);
 
165
 
 
166
    if (isRightToLeft()) {
 
167
        int t = colfirst;
 
168
        colfirst = collast;
 
169
        collast = t;
 
170
    }
 
171
 
 
172
    QPainter painter(this);
 
173
    QPainter *p = &painter;
 
174
 
 
175
 
 
176
    if (collast < 0 || collast >= ncols)
 
177
        collast = ncols-1;
 
178
    if (rowlast < 0 || rowlast >= nrows)
 
179
        rowlast = nrows-1;
 
180
 
 
181
    // Go through the rows
 
182
    for (int r = rowfirst; r <= rowlast; ++r) {
 
183
        // get row position and height
 
184
        int rowp = rowY(r);
 
185
 
 
186
        // Go through the columns in the row r
 
187
        // if we know from where to where, go through [colfirst, collast],
 
188
        // else go through all of them
 
189
        for (int c = colfirst; c <= collast; ++c) {
 
190
            // get position and width of column c
 
191
            int colp = columnX(c);
 
192
            // Translate painter and draw the cell
 
193
            p->translate(colp, rowp);
 
194
            paintCell(p, r, c);
 
195
            p->translate(-colp, -rowp);
 
196
        }
 
197
    }
 
198
 
 
199
}
 
200
 
 
201
struct QWellArrayData {
 
202
    QBrush *brush;
 
203
};
 
204
 
 
205
QWellArray::QWellArray(int rows, int cols, QWidget *parent)
 
206
    : QWidget(parent)
 
207
        ,nrows(rows), ncols(cols)
 
208
{
 
209
    d = 0;
 
210
    setFocusPolicy(Qt::StrongFocus);
 
211
    cellw = 28;
 
212
    cellh = 24;
 
213
    curCol = 0;
 
214
    curRow = 0;
 
215
    selCol = -1;
 
216
    selRow = -1;
 
217
 
 
218
}
 
219
 
 
220
 
 
221
QSize QWellArray::sizeHint() const
 
222
{
 
223
    ensurePolished();
 
224
    return gridSize().boundedTo(QSize(640, 480));
 
225
}
 
226
 
 
227
 
 
228
void QWellArray::paintCell(QPainter* p, int row, int col)
 
229
{
 
230
    int w = cellWidth();                        // width of cell in pixels
 
231
    int h = cellHeight();                        // height of cell in pixels
 
232
    int b = 3;
 
233
 
 
234
    const QPalette & g = palette();
 
235
    QStyleOptionFrame opt;
 
236
    int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
 
237
    opt.lineWidth = dfw;
 
238
    opt.midLineWidth = 1;
 
239
    opt.rect.setRect(b, b, w - 2 * b, h - 2 * b);
 
240
    opt.palette = g;
 
241
    opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
 
242
    style()->drawPrimitive(QStyle::PE_Frame, &opt, p, this);
 
243
    b += dfw;
 
244
 
 
245
    if ((row == curRow) && (col == curCol)) {
 
246
        if (hasFocus()) {
 
247
            QStyleOptionFocusRect opt;
 
248
            opt.palette = g;
 
249
            opt.rect.setRect(0, 0, w, h);
 
250
            opt.state = QStyle::State_None;
 
251
            style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p, this);
 
252
        }
 
253
    }
 
254
    QRect cr(b, b, w - 2*b, h - 2*b);
 
255
    paintCellContents(p, row, col, cr);
 
256
}
 
257
 
 
258
/*!
 
259
  Reimplement this function to change the contents of the well array.
 
260
 */
 
261
void QWellArray::paintCellContents(QPainter *p, int row, int col, const QRect &r)
 
262
{
 
263
 
 
264
    if (d) {
 
265
        p->fillRect(r, d->brush[row*numCols()+col]);
 
266
    } else {
 
267
        p->fillRect(r, Qt::white);
 
268
        p->setPen(Qt::black);
 
269
        p->drawLine(r.topLeft(), r.bottomRight());
 
270
        p->drawLine(r.topRight(), r.bottomLeft());
 
271
    }
 
272
}
 
273
 
 
274
 
 
275
/*\reimp
 
276
*/
 
277
void QWellArray::mousePressEvent(QMouseEvent* e)
 
278
{
 
279
    // The current cell marker is set to the cell the mouse is pressed
 
280
    // in.
 
281
    QPoint pos = e->pos();
 
282
    setCurrent(rowAt(pos.y()), columnAt(pos.x()));
 
283
}
 
284
 
 
285
/*\reimp
 
286
*/
 
287
void QWellArray::mouseReleaseEvent(QMouseEvent*)
 
288
{
 
289
    // The current cell marker is set to the cell the mouse is clicked
 
290
    // in.
 
291
    setSelected(curRow, curCol);
 
292
}
 
293
 
 
294
 
 
295
/*
 
296
  Sets the cell currently having the focus. This is not necessarily
 
297
  the same as the currently selected cell.
 
298
*/
 
299
 
 
300
void QWellArray::setCurrent(int row, int col)
 
301
{
 
302
 
 
303
    if ((curRow == row) && (curCol == col))
 
304
        return;
 
305
 
 
306
    if (row < 0 || col < 0)
 
307
        row = col = -1;
 
308
 
 
309
    int oldRow = curRow;
 
310
    int oldCol = curCol;
 
311
 
 
312
    curRow = row;
 
313
    curCol = col;
 
314
 
 
315
    updateCell(oldRow, oldCol);
 
316
    updateCell(curRow, curCol);
 
317
}
 
318
 
 
319
 
 
320
/*
 
321
  Sets the currently selected cell to \a row, \a column. If \a row or
 
322
  \a column are less than zero, the current cell is unselected.
 
323
 
 
324
  Does not set the position of the focus indicator.
 
325
*/
 
326
 
 
327
void QWellArray::setSelected(int row, int col)
 
328
{
 
329
    if ((selRow == row) && (selCol == col))
 
330
        return;
 
331
 
 
332
    int oldRow = selRow;
 
333
    int oldCol = selCol;
 
334
 
 
335
    if (row < 0 || col < 0)
 
336
        row = col = -1;
 
337
 
 
338
    selCol = col;
 
339
    selRow = row;
 
340
 
 
341
    updateCell(oldRow, oldCol);
 
342
    updateCell(selRow, selCol);
 
343
    if (row >= 0)
 
344
        emit selected(row, col);
 
345
 
 
346
    if (isVisible() && qobject_cast<QMenu*>(parentWidget()))
 
347
        parentWidget()->close();
 
348
}
 
349
 
 
350
 
 
351
 
 
352
/*!\reimp
 
353
*/
 
354
void QWellArray::focusInEvent(QFocusEvent*)
 
355
{
 
356
    updateCell(curRow, curCol);
 
357
}
 
358
 
 
359
 
 
360
void QWellArray::setCellBrush(int row, int col, const QBrush &b)
 
361
{
 
362
    if (!d) {
 
363
        d = new QWellArrayData;
 
364
        int i = numRows()*numCols();
 
365
        d->brush = new QBrush[i];
 
366
    }
 
367
    if (row >= 0 && row < numRows() && col >= 0 && col < numCols())
 
368
        d->brush[row*numCols()+col] = b;
 
369
}
 
370
 
 
371
 
 
372
 
 
373
/*
 
374
  Returns the brush set for the cell at \a row, \a column. If no brush is
 
375
  set, \c Qt::NoBrush is returned.
 
376
*/
 
377
 
 
378
QBrush QWellArray::cellBrush(int row, int col)
 
379
{
 
380
    if (d && row >= 0 && row < numRows() && col >= 0 && col < numCols())
 
381
        return d->brush[row*numCols()+col];
 
382
    return Qt::NoBrush;
 
383
}
 
384
 
 
385
 
 
386
 
 
387
/*!\reimp
 
388
*/
 
389
 
 
390
void QWellArray::focusOutEvent(QFocusEvent*)
 
391
{
 
392
    updateCell(curRow, curCol);
 
393
}
 
394
 
 
395
/*\reimp
 
396
*/
 
397
void QWellArray::keyPressEvent(QKeyEvent* e)
 
398
{
 
399
    switch(e->key()) {                        // Look at the key code
 
400
    case Qt::Key_Left:                                // If 'left arrow'-key,
 
401
        if(curCol > 0)                        // and cr't not in leftmost col
 
402
            setCurrent(curRow, curCol - 1);        // set cr't to next left column
 
403
        break;
 
404
    case Qt::Key_Right:                                // Correspondingly...
 
405
        if(curCol < numCols()-1)
 
406
            setCurrent(curRow, curCol + 1);
 
407
        break;
 
408
    case Qt::Key_Up:
 
409
        if(curRow > 0)
 
410
            setCurrent(curRow - 1, curCol);
 
411
        break;
 
412
    case Qt::Key_Down:
 
413
        if(curRow < numRows()-1)
 
414
            setCurrent(curRow + 1, curCol);
 
415
        break;
 
416
    case Qt::Key_Space:
 
417
    case Qt::Key_Return:
 
418
    case Qt::Key_Enter:
 
419
        setSelected(curRow, curCol);
 
420
        break;
 
421
    default:                                // If not an interesting key,
 
422
        e->ignore();                        // we don't accept the event
 
423
        return;
 
424
    }
 
425
 
 
426
}
 
427
 
 
428
//////////// QWellArray END
 
429
 
 
430
static bool initrgb = false;
 
431
static QRgb stdrgb[6*8];
 
432
static QRgb cusrgb[2*8];
 
433
static bool customSet = false;
 
434
 
 
435
 
 
436
static void initRGB()
 
437
{
 
438
    if (initrgb)
 
439
        return;
 
440
    initrgb = true;
 
441
    int i = 0;
 
442
    for (int g = 0; g < 4; g++)
 
443
        for (int r = 0;  r < 4; r++)
 
444
            for (int b = 0; b < 3; b++)
 
445
                stdrgb[i++] = qRgb(r*255/3, g*255/3, b*255/2);
 
446
 
 
447
    for (i = 0; i < 2*8; i++)
 
448
        cusrgb[i] = 0xffffffff;
 
449
}
 
450
 
 
451
/*!
 
452
    Returns the number of custom colors supported by QColorDialog. All
 
453
    color dialogs share the same custom colors.
 
454
*/
 
455
int QColorDialog::customCount()
 
456
{
 
457
    return 2*8;
 
458
}
 
459
 
 
460
/*!
 
461
    Returns custom color number \a i as a QRgb value.
 
462
*/
 
463
QRgb QColorDialog::customColor(int i)
 
464
{
 
465
    initRGB();
 
466
    Q_ASSERT(i >= 0 && i < customCount());
 
467
    return cusrgb[i];
 
468
}
 
469
 
 
470
/*!
 
471
    \fn void QColorDialog::setCustomColor(int number, QRgb color)
 
472
 
 
473
    Sets the custom color \a number to the QRgb \a color value.
 
474
*/
 
475
void QColorDialog::setCustomColor(int i, QRgb c)
 
476
{
 
477
    initRGB();
 
478
    Q_ASSERT(i >= 0 && i < customCount());
 
479
    customSet = true;
 
480
    cusrgb[i] = c;
 
481
}
 
482
 
 
483
/*!
 
484
    \fn void QColorDialog::setStandardColor(int number, QRgb color)
 
485
 
 
486
    Sets the standard color \a number to the QRgb \a color value given.
 
487
*/
 
488
 
 
489
void QColorDialog::setStandardColor(int i, QRgb c)
 
490
{
 
491
    initRGB();
 
492
    Q_ASSERT(i >= 0 && i < 6*8);
 
493
    stdrgb[i] = c;
 
494
}
 
495
 
 
496
static inline void rgb2hsv(QRgb rgb, int&h, int&s, int&v)
 
497
{
 
498
    QColor c;
 
499
    c.setRgb(rgb);
 
500
    c.getHsv(&h, &s, &v);
 
501
}
 
502
 
 
503
class QColorWell : public QWellArray
 
504
{
 
505
public:
 
506
    QColorWell(QWidget *parent, int r, int c, QRgb *vals)
 
507
        :QWellArray(r, c, parent), values(vals), mousePressed(false), oldCurrent(-1, -1)
 
508
    { setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); }
 
509
 
 
510
protected:
 
511
    void paintCellContents(QPainter *, int row, int col, const QRect&);
 
512
    void mousePressEvent(QMouseEvent *e);
 
513
    void mouseMoveEvent(QMouseEvent *e);
 
514
    void mouseReleaseEvent(QMouseEvent *e);
 
515
#ifndef QT_NO_DRAGANDDROP
 
516
    void dragEnterEvent(QDragEnterEvent *e);
 
517
    void dragLeaveEvent(QDragLeaveEvent *e);
 
518
    void dragMoveEvent(QDragMoveEvent *e);
 
519
    void dropEvent(QDropEvent *e);
 
520
#endif
 
521
 
 
522
private:
 
523
    QRgb *values;
 
524
    bool mousePressed;
 
525
    QPoint pressPos;
 
526
    QPoint oldCurrent;
 
527
 
 
528
};
 
529
 
 
530
void QColorWell::paintCellContents(QPainter *p, int row, int col, const QRect &r)
 
531
{
 
532
    int i = row + col*numRows();
 
533
    p->fillRect(r, QColor(values[i]));
 
534
}
 
535
 
 
536
void QColorWell::mousePressEvent(QMouseEvent *e)
 
537
{
 
538
    oldCurrent = QPoint(selectedRow(), selectedColumn());
 
539
    QWellArray::mousePressEvent(e);
 
540
    mousePressed = true;
 
541
    pressPos = e->pos();
 
542
}
 
543
 
 
544
void QColorWell::mouseMoveEvent(QMouseEvent *e)
 
545
{
 
546
    QWellArray::mouseMoveEvent(e);
 
547
#ifndef QT_NO_DRAGANDDROP
 
548
    if (!mousePressed)
 
549
        return;
 
550
    if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
 
551
        setCurrent(oldCurrent.x(), oldCurrent.y());
 
552
        int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
 
553
        QColor col(values[i]);
 
554
        QMimeData *mime = new QMimeData;
 
555
        mime->setColorData(col);
 
556
        QPixmap pix(cellWidth(), cellHeight());
 
557
        pix.fill(col);
 
558
        QPainter p(&pix);
 
559
        p.drawRect(0, 0, pix.width(), pix.height());
 
560
        p.end();
 
561
        QDrag *drg = new QDrag(this);
 
562
        drg->setMimeData(mime);
 
563
        drg->setPixmap(pix);
 
564
        mousePressed = false;
 
565
        drg->start();
 
566
    }
 
567
#endif
 
568
}
 
569
 
 
570
#ifndef QT_NO_DRAGANDDROP
 
571
void QColorWell::dragEnterEvent(QDragEnterEvent *e)
 
572
{
 
573
    if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
 
574
        e->accept();
 
575
    else
 
576
        e->ignore();
 
577
}
 
578
 
 
579
void QColorWell::dragLeaveEvent(QDragLeaveEvent *)
 
580
{
 
581
    if (hasFocus())
 
582
        parentWidget()->setFocus();
 
583
}
 
584
 
 
585
void QColorWell::dragMoveEvent(QDragMoveEvent *e)
 
586
{
 
587
    if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) {
 
588
        setCurrent(rowAt(e->pos().y()), columnAt(e->pos().x()));
 
589
        e->accept();
 
590
    } else {
 
591
        e->ignore();
 
592
    }
 
593
}
 
594
 
 
595
void QColorWell::dropEvent(QDropEvent *e)
 
596
{
 
597
    QColor col = qvariant_cast<QColor>(e->mimeData()->colorData());
 
598
    if (col.isValid()) {
 
599
        int i = rowAt(e->pos().y()) + columnAt(e->pos().x()) * numRows();
 
600
        values[i] = col.rgb();
 
601
        update();
 
602
        e->accept();
 
603
    } else {
 
604
        e->ignore();
 
605
    }
 
606
}
 
607
 
 
608
#endif // QT_NO_DRAGANDDROP
 
609
 
 
610
void QColorWell::mouseReleaseEvent(QMouseEvent *e)
 
611
{
 
612
    if (!mousePressed)
 
613
        return;
 
614
    QWellArray::mouseReleaseEvent(e);
 
615
    mousePressed = false;
 
616
}
 
617
 
 
618
class QColorPicker : public QFrame
 
619
{
 
620
    Q_OBJECT
 
621
public:
 
622
    QColorPicker(QWidget* parent);
 
623
    ~QColorPicker();
 
624
 
 
625
public slots:
 
626
    void setCol(int h, int s);
 
627
 
 
628
signals:
 
629
    void newCol(int h, int s);
 
630
 
 
631
protected:
 
632
    QSize sizeHint() const;
 
633
    void paintEvent(QPaintEvent*);
 
634
    void mouseMoveEvent(QMouseEvent *);
 
635
    void mousePressEvent(QMouseEvent *);
 
636
 
 
637
private:
 
638
    int hue;
 
639
    int sat;
 
640
 
 
641
    QPoint colPt();
 
642
    int huePt(const QPoint &pt);
 
643
    int satPt(const QPoint &pt);
 
644
    void setCol(const QPoint &pt);
 
645
 
 
646
    QPixmap *pix;
 
647
};
 
648
 
 
649
static int pWidth = 220;
 
650
static int pHeight = 200;
 
651
 
 
652
class QColorLuminancePicker : public QWidget
 
653
{
 
654
    Q_OBJECT
 
655
public:
 
656
    QColorLuminancePicker(QWidget* parent=0);
 
657
    ~QColorLuminancePicker();
 
658
 
 
659
public slots:
 
660
    void setCol(int h, int s, int v);
 
661
    void setCol(int h, int s);
 
662
 
 
663
signals:
 
664
    void newHsv(int h, int s, int v);
 
665
 
 
666
protected:
 
667
    void paintEvent(QPaintEvent*);
 
668
    void mouseMoveEvent(QMouseEvent *);
 
669
    void mousePressEvent(QMouseEvent *);
 
670
 
 
671
private:
 
672
    enum { foff = 3, coff = 4 }; //frame and contents offset
 
673
    int val;
 
674
    int hue;
 
675
    int sat;
 
676
 
 
677
    int y2val(int y);
 
678
    int val2y(int val);
 
679
    void setVal(int v);
 
680
 
 
681
    QPixmap *pix;
 
682
};
 
683
 
 
684
 
 
685
int QColorLuminancePicker::y2val(int y)
 
686
{
 
687
    int d = height() - 2*coff - 1;
 
688
    return 255 - (y - coff)*255/d;
 
689
}
 
690
 
 
691
int QColorLuminancePicker::val2y(int v)
 
692
{
 
693
    int d = height() - 2*coff - 1;
 
694
    return coff + (255-v)*d/255;
 
695
}
 
696
 
 
697
QColorLuminancePicker::QColorLuminancePicker(QWidget* parent)
 
698
    :QWidget(parent)
 
699
{
 
700
    hue = 100; val = 100; sat = 100;
 
701
    pix = 0;
 
702
    //    setAttribute(WA_NoErase, true);
 
703
}
 
704
 
 
705
QColorLuminancePicker::~QColorLuminancePicker()
 
706
{
 
707
    delete pix;
 
708
}
 
709
 
 
710
void QColorLuminancePicker::mouseMoveEvent(QMouseEvent *m)
 
711
{
 
712
    setVal(y2val(m->y()));
 
713
}
 
714
void QColorLuminancePicker::mousePressEvent(QMouseEvent *m)
 
715
{
 
716
    setVal(y2val(m->y()));
 
717
}
 
718
 
 
719
void QColorLuminancePicker::setVal(int v)
 
720
{
 
721
    if (val == v)
 
722
        return;
 
723
    val = qMax(0, qMin(v,255));
 
724
    delete pix; pix=0;
 
725
    repaint();
 
726
    emit newHsv(hue, sat, val);
 
727
}
 
728
 
 
729
//receives from a hue,sat chooser and relays.
 
730
void QColorLuminancePicker::setCol(int h, int s)
 
731
{
 
732
    setCol(h, s, val);
 
733
    emit newHsv(h, s, val);
 
734
}
 
735
 
 
736
void QColorLuminancePicker::paintEvent(QPaintEvent *)
 
737
{
 
738
    int w = width() - 5;
 
739
 
 
740
    QRect r(0, foff, w, height() - 2*foff);
 
741
    int wi = r.width() - 2;
 
742
    int hi = r.height() - 2;
 
743
    if (!pix || pix->height() != hi || pix->width() != wi) {
 
744
        delete pix;
 
745
        QImage img(wi, hi, QImage::Format_RGB32);
 
746
        int y;
 
747
        for (y = 0; y < hi; y++) {
 
748
            QColor c;
 
749
            c.setHsv(hue, sat, y2val(y+coff));
 
750
            QRgb r = c.rgb();
 
751
            int x;
 
752
            for (x = 0; x < wi; x++)
 
753
                img.setPixel(x, y, r);
 
754
        }
 
755
        pix = new QPixmap(QPixmap::fromImage(img));
 
756
    }
 
757
    QPainter p(this);
 
758
    p.drawPixmap(1, coff, *pix);
 
759
    const QPalette &g = palette();
 
760
    qDrawShadePanel(&p, r, g, true);
 
761
    p.setPen(g.foreground().color());
 
762
    p.setBrush(g.foreground());
 
763
    QPolygon a;
 
764
    int y = val2y(val);
 
765
    a.setPoints(3, w, y, w+5, y+5, w+5, y-5);
 
766
    p.eraseRect(w, 0, 5, height());
 
767
    p.drawPolygon(a);
 
768
}
 
769
 
 
770
void QColorLuminancePicker::setCol(int h, int s , int v)
 
771
{
 
772
    val = v;
 
773
    hue = h;
 
774
    sat = s;
 
775
    delete pix; pix=0;
 
776
    repaint();
 
777
}
 
778
 
 
779
QPoint QColorPicker::colPt()
 
780
{ return QPoint((360-hue)*(pWidth-1)/360, (255-sat)*(pHeight-1)/255); }
 
781
int QColorPicker::huePt(const QPoint &pt)
 
782
{ return 360 - pt.x()*360/(pWidth-1); }
 
783
int QColorPicker::satPt(const QPoint &pt)
 
784
{ return 255 - pt.y()*255/(pHeight-1) ; }
 
785
void QColorPicker::setCol(const QPoint &pt)
 
786
{ setCol(huePt(pt), satPt(pt)); }
 
787
 
 
788
QColorPicker::QColorPicker(QWidget* parent)
 
789
    : QFrame(parent)
 
790
{
 
791
    hue = 0; sat = 0;
 
792
    setCol(150, 255);
 
793
 
 
794
    QImage img(pWidth, pHeight, QImage::Format_RGB32);
 
795
    int x,y;
 
796
    for (y = 0; y < pHeight; y++)
 
797
        for (x = 0; x < pWidth; x++) {
 
798
            QPoint p(x, y);
 
799
            QColor c;
 
800
            c.setHsv(huePt(p), satPt(p), 200);
 
801
            img.setPixel(x, y, c.rgb());
 
802
        }
 
803
    pix = new QPixmap(QPixmap::fromImage(img));
 
804
    setAttribute(Qt::WA_NoSystemBackground);
 
805
    setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
 
806
}
 
807
 
 
808
QColorPicker::~QColorPicker()
 
809
{
 
810
    delete pix;
 
811
}
 
812
 
 
813
QSize QColorPicker::sizeHint() const
 
814
{
 
815
    return QSize(pWidth + 2*frameWidth(), pHeight + 2*frameWidth());
 
816
}
 
817
 
 
818
void QColorPicker::setCol(int h, int s)
 
819
{
 
820
    int nhue = qMin(qMax(0,h), 359);
 
821
    int nsat = qMin(qMax(0,s), 255);
 
822
    if (nhue == hue && nsat == sat)
 
823
        return;
 
824
    QRect r(colPt(), QSize(20,20));
 
825
    hue = nhue; sat = nsat;
 
826
    r = r.unite(QRect(colPt(), QSize(20,20)));
 
827
    r.translate(contentsRect().x()-9, contentsRect().y()-9);
 
828
    //    update(r);
 
829
    repaint(r);
 
830
}
 
831
 
 
832
void QColorPicker::mouseMoveEvent(QMouseEvent *m)
 
833
{
 
834
    QPoint p = m->pos() - contentsRect().topLeft();
 
835
    setCol(p);
 
836
    emit newCol(hue, sat);
 
837
}
 
838
 
 
839
void QColorPicker::mousePressEvent(QMouseEvent *m)
 
840
{
 
841
    QPoint p = m->pos() - contentsRect().topLeft();
 
842
    setCol(p);
 
843
    emit newCol(hue, sat);
 
844
}
 
845
 
 
846
void QColorPicker::paintEvent(QPaintEvent* )
 
847
{
 
848
    QPainter p(this);
 
849
    drawFrame(&p);
 
850
    QRect r = contentsRect();
 
851
 
 
852
    p.drawPixmap(r.topLeft(), *pix);
 
853
    QPoint pt = colPt() + r.topLeft();
 
854
    p.setPen(Qt::black);
 
855
 
 
856
    p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
 
857
    p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
 
858
 
 
859
}
 
860
 
 
861
class QColSpinBox : public QSpinBox
 
862
{
 
863
public:
 
864
    QColSpinBox(QWidget *parent)
 
865
        : QSpinBox(parent) { setRange(0, 255); }
 
866
    void setValue(int i) {
 
867
        bool block = signalsBlocked();
 
868
        blockSignals(true);
 
869
        QSpinBox::setValue(i);
 
870
        blockSignals(block);
 
871
    }
 
872
};
 
873
 
 
874
class QColorShowLabel;
 
875
 
 
876
class QColorShower : public QWidget
 
877
{
 
878
    Q_OBJECT
 
879
public:
 
880
    QColorShower(QWidget *parent);
 
881
 
 
882
    //things that don't emit signals
 
883
    void setHsv(int h, int s, int v);
 
884
 
 
885
    int currentAlpha() const { return alphaEd->value(); }
 
886
    void setCurrentAlpha(int a) { alphaEd->setValue(a); }
 
887
    void showAlpha(bool b);
 
888
 
 
889
 
 
890
    QRgb currentColor() const { return curCol; }
 
891
 
 
892
public slots:
 
893
    void setRgb(QRgb rgb);
 
894
 
 
895
signals:
 
896
    void newCol(QRgb rgb);
 
897
private slots:
 
898
    void rgbEd();
 
899
    void hsvEd();
 
900
private:
 
901
    void showCurrentColor();
 
902
    int hue, sat, val;
 
903
    QRgb curCol;
 
904
    QColSpinBox *hEd;
 
905
    QColSpinBox *sEd;
 
906
    QColSpinBox *vEd;
 
907
    QColSpinBox *rEd;
 
908
    QColSpinBox *gEd;
 
909
    QColSpinBox *bEd;
 
910
    QColSpinBox *alphaEd;
 
911
    QLabel *alphaLab;
 
912
    QColorShowLabel *lab;
 
913
    bool rgbOriginal;
 
914
};
 
915
 
 
916
class QColorShowLabel : public QFrame
 
917
{
 
918
    Q_OBJECT
 
919
 
 
920
public:
 
921
    QColorShowLabel(QWidget *parent) : QFrame(parent) {
 
922
        setFrameStyle(QFrame::Panel|QFrame::Sunken);
 
923
        setAcceptDrops(true);
 
924
        mousePressed = false;
 
925
    }
 
926
    void setColor(QColor c) { col = c; }
 
927
 
 
928
signals:
 
929
    void colorDropped(QRgb);
 
930
 
 
931
protected:
 
932
    void paintEvent(QPaintEvent *);
 
933
    void mousePressEvent(QMouseEvent *e);
 
934
    void mouseMoveEvent(QMouseEvent *e);
 
935
    void mouseReleaseEvent(QMouseEvent *e);
 
936
#ifndef QT_NO_DRAGANDDROP
 
937
    void dragEnterEvent(QDragEnterEvent *e);
 
938
    void dragLeaveEvent(QDragLeaveEvent *e);
 
939
    void dropEvent(QDropEvent *e);
 
940
#endif
 
941
 
 
942
private:
 
943
    QColor col;
 
944
    bool mousePressed;
 
945
    QPoint pressPos;
 
946
 
 
947
};
 
948
 
 
949
void QColorShowLabel::paintEvent(QPaintEvent *e)
 
950
{
 
951
    QPainter p(this);
 
952
    drawFrame(&p);
 
953
    p.fillRect(contentsRect()&e->rect(), col);
 
954
}
 
955
 
 
956
void QColorShower::showAlpha(bool b)
 
957
{
 
958
    if (b) {
 
959
        alphaLab->show();
 
960
        alphaEd->show();
 
961
    } else {
 
962
        alphaLab->hide();
 
963
        alphaEd->hide();
 
964
    }
 
965
}
 
966
 
 
967
void QColorShowLabel::mousePressEvent(QMouseEvent *e)
 
968
{
 
969
    mousePressed = true;
 
970
    pressPos = e->pos();
 
971
}
 
972
 
 
973
void QColorShowLabel::mouseMoveEvent(QMouseEvent *e)
 
974
{
 
975
#ifndef QT_NO_DRAGANDDROP
 
976
    if (!mousePressed)
 
977
        return;
 
978
    if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
 
979
        QMimeData *mime = new QMimeData;
 
980
        mime->setColorData(col);
 
981
        QPixmap pix(30, 20);
 
982
        pix.fill(col);
 
983
        QPainter p(&pix);
 
984
        p.drawRect(0, 0, pix.width(), pix.height());
 
985
        p.end();
 
986
        QDrag *drg = new QDrag(this);
 
987
        drg->setMimeData(mime);
 
988
        drg->setPixmap(pix);
 
989
        mousePressed = false;
 
990
        drg->start();
 
991
    }
 
992
#endif
 
993
}
 
994
 
 
995
#ifndef QT_NO_DRAGANDDROP
 
996
void QColorShowLabel::dragEnterEvent(QDragEnterEvent *e)
 
997
{
 
998
    if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
 
999
        e->accept();
 
1000
    else
 
1001
        e->ignore();
 
1002
}
 
1003
 
 
1004
void QColorShowLabel::dragLeaveEvent(QDragLeaveEvent *)
 
1005
{
 
1006
}
 
1007
 
 
1008
void QColorShowLabel::dropEvent(QDropEvent *e)
 
1009
{
 
1010
    QColor color = qvariant_cast<QColor>(e->mimeData()->colorData());
 
1011
    if (color.isValid()) {
 
1012
        col = color;
 
1013
        repaint();
 
1014
        emit colorDropped(col.rgb());
 
1015
        e->accept();
 
1016
    } else {
 
1017
        e->ignore();
 
1018
    }
 
1019
}
 
1020
#endif // QT_NO_DRAGANDDROP
 
1021
 
 
1022
void QColorShowLabel::mouseReleaseEvent(QMouseEvent *)
 
1023
{
 
1024
    if (!mousePressed)
 
1025
        return;
 
1026
    mousePressed = false;
 
1027
}
 
1028
 
 
1029
QColorShower::QColorShower(QWidget *parent)
 
1030
    :QWidget(parent)
 
1031
{
 
1032
    curCol = qRgb(-1, -1, -1);
 
1033
 
 
1034
    QGridLayout *gl = new QGridLayout(this);
 
1035
    gl->setMargin(6);
 
1036
    lab = new QColorShowLabel(this);
 
1037
    lab->setMinimumWidth(60);
 
1038
    gl->addWidget(lab, 0, 0, -1, 1);
 
1039
    connect(lab, SIGNAL(colorDropped(QRgb)),
 
1040
             this, SIGNAL(newCol(QRgb)));
 
1041
    connect(lab, SIGNAL(colorDropped(QRgb)),
 
1042
             this, SLOT(setRgb(QRgb)));
 
1043
 
 
1044
    hEd = new QColSpinBox(this);
 
1045
    hEd->setRange(0, 359);
 
1046
    QLabel *l = new QLabel(QColorDialog::tr("Hu&e:"), this);
 
1047
    l->setBuddy(hEd);
 
1048
    l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1049
    gl->addWidget(l, 0, 1);
 
1050
    gl->addWidget(hEd, 0, 2);
 
1051
 
 
1052
    sEd = new QColSpinBox(this);
 
1053
    l = new QLabel(QColorDialog::tr("&Sat:"), this);
 
1054
    l->setBuddy(sEd);
 
1055
    l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1056
    gl->addWidget(l, 1, 1);
 
1057
    gl->addWidget(sEd, 1, 2);
 
1058
 
 
1059
    vEd = new QColSpinBox(this);
 
1060
    l = new QLabel(QColorDialog::tr("&Val:"), this);
 
1061
    l->setBuddy(vEd);
 
1062
    l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1063
    gl->addWidget(l, 2, 1);
 
1064
    gl->addWidget(vEd, 2, 2);
 
1065
 
 
1066
    rEd = new QColSpinBox(this);
 
1067
    l = new QLabel(QColorDialog::tr("&Red:"), this);
 
1068
    l->setBuddy(rEd);
 
1069
    l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1070
    gl->addWidget(l, 0, 3);
 
1071
    gl->addWidget(rEd, 0, 4);
 
1072
 
 
1073
    gEd = new QColSpinBox(this);
 
1074
    l = new QLabel(QColorDialog::tr("&Green:"), this);
 
1075
    l->setBuddy(gEd);
 
1076
    l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1077
    gl->addWidget(l, 1, 3);
 
1078
    gl->addWidget(gEd, 1, 4);
 
1079
 
 
1080
    bEd = new QColSpinBox(this);
 
1081
    l = new QLabel(QColorDialog::tr("Bl&ue:"), this);
 
1082
    l->setBuddy(bEd);
 
1083
    l->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1084
    gl->addWidget(l, 2, 3);
 
1085
    gl->addWidget(bEd, 2, 4);
 
1086
 
 
1087
    alphaEd = new QColSpinBox(this);
 
1088
    alphaLab = new QLabel(QColorDialog::tr("A&lpha channel:"), this);
 
1089
    l->setBuddy(alphaEd);
 
1090
    alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 
1091
    gl->addWidget(alphaLab, 3, 1, 1, 3);
 
1092
    gl->addWidget(alphaEd, 3, 4);
 
1093
    alphaEd->hide();
 
1094
    alphaLab->hide();
 
1095
 
 
1096
    connect(hEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
 
1097
    connect(sEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
 
1098
    connect(vEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd()));
 
1099
 
 
1100
    connect(rEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
 
1101
    connect(gEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
 
1102
    connect(bEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
 
1103
    connect(alphaEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd()));
 
1104
}
 
1105
 
 
1106
void QColorShower::showCurrentColor()
 
1107
{
 
1108
    lab->setColor(currentColor());
 
1109
    lab->repaint();
 
1110
}
 
1111
 
 
1112
void QColorShower::rgbEd()
 
1113
{
 
1114
    rgbOriginal = true;
 
1115
    if (alphaEd->isVisible())
 
1116
        curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
 
1117
    else
 
1118
        curCol = qRgb(rEd->value(), gEd->value(), bEd->value());
 
1119
 
 
1120
    rgb2hsv(currentColor(), hue, sat, val);
 
1121
 
 
1122
    hEd->setValue(hue);
 
1123
    sEd->setValue(sat);
 
1124
    vEd->setValue(val);
 
1125
 
 
1126
    showCurrentColor();
 
1127
    emit newCol(currentColor());
 
1128
}
 
1129
 
 
1130
void QColorShower::hsvEd()
 
1131
{
 
1132
    rgbOriginal = false;
 
1133
    hue = hEd->value();
 
1134
    sat = sEd->value();
 
1135
    val = vEd->value();
 
1136
 
 
1137
    QColor c;
 
1138
    c.setHsv(hue, sat, val);
 
1139
    curCol = c.rgb();
 
1140
 
 
1141
    rEd->setValue(qRed(currentColor()));
 
1142
    gEd->setValue(qGreen(currentColor()));
 
1143
    bEd->setValue(qBlue(currentColor()));
 
1144
 
 
1145
    showCurrentColor();
 
1146
    emit newCol(currentColor());
 
1147
}
 
1148
 
 
1149
void QColorShower::setRgb(QRgb rgb)
 
1150
{
 
1151
    rgbOriginal = true;
 
1152
    curCol = rgb;
 
1153
 
 
1154
    rgb2hsv(currentColor(), hue, sat, val);
 
1155
 
 
1156
    hEd->setValue(hue);
 
1157
    sEd->setValue(sat);
 
1158
    vEd->setValue(val);
 
1159
 
 
1160
    rEd->setValue(qRed(currentColor()));
 
1161
    gEd->setValue(qGreen(currentColor()));
 
1162
    bEd->setValue(qBlue(currentColor()));
 
1163
 
 
1164
    showCurrentColor();
 
1165
}
 
1166
 
 
1167
void QColorShower::setHsv(int h, int s, int v)
 
1168
{
 
1169
    if (h < -1 || (uint)s > 255 || (uint)v > 255)
 
1170
        return;
 
1171
 
 
1172
    rgbOriginal = false;
 
1173
    hue = h; val = v; sat = s;
 
1174
    QColor c;
 
1175
    c.setHsv(hue, sat, val);
 
1176
    curCol = c.rgb();
 
1177
 
 
1178
    hEd->setValue(hue);
 
1179
    sEd->setValue(sat);
 
1180
    vEd->setValue(val);
 
1181
 
 
1182
    rEd->setValue(qRed(currentColor()));
 
1183
    gEd->setValue(qGreen(currentColor()));
 
1184
    bEd->setValue(qBlue(currentColor()));
 
1185
 
 
1186
    showCurrentColor();
 
1187
}
 
1188
 
 
1189
class QColorDialogPrivate : public QDialogPrivate
 
1190
{
 
1191
    Q_DECLARE_PUBLIC(QColorDialog)
 
1192
public:
 
1193
    void init();
 
1194
    QRgb currentColor() const { return cs->currentColor(); }
 
1195
    void setCurrentColor(QRgb rgb);
 
1196
 
 
1197
    int currentAlpha() const { return cs->currentAlpha(); }
 
1198
    void setCurrentAlpha(int a) { cs->setCurrentAlpha(a); }
 
1199
    void showAlpha(bool b) { cs->showAlpha(b); }
 
1200
 
 
1201
    void addCustom();
 
1202
 
 
1203
    void newHsv(int h, int s, int v);
 
1204
    void newColorTypedIn(QRgb rgb);
 
1205
    void newCustom(int, int);
 
1206
    void newStandard(int, int);
 
1207
 
 
1208
    QWellArray *custom;
 
1209
    QWellArray *standard;
 
1210
 
 
1211
    QColorPicker *cp;
 
1212
    QColorLuminancePicker *lp;
 
1213
    QColorShower *cs;
 
1214
    int nextCust;
 
1215
    bool compact;
 
1216
};
 
1217
 
 
1218
//sets all widgets to display h,s,v
 
1219
void QColorDialogPrivate::newHsv(int h, int s, int v)
 
1220
{
 
1221
    cs->setHsv(h, s, v);
 
1222
    cp->setCol(h, s);
 
1223
    lp->setCol(h, s, v);
 
1224
}
 
1225
 
 
1226
//sets all widgets to display rgb
 
1227
void QColorDialogPrivate::setCurrentColor(QRgb rgb)
 
1228
{
 
1229
    cs->setRgb(rgb);
 
1230
    newColorTypedIn(rgb);
 
1231
}
 
1232
 
 
1233
//sets all widgets exept cs to display rgb
 
1234
void QColorDialogPrivate::newColorTypedIn(QRgb rgb)
 
1235
{
 
1236
    int h, s, v;
 
1237
    rgb2hsv(rgb, h, s, v);
 
1238
    cp->setCol(h, s);
 
1239
    lp->setCol(h, s, v);
 
1240
}
 
1241
 
 
1242
void QColorDialogPrivate::newCustom(int r, int c)
 
1243
{
 
1244
    int i = r+2*c;
 
1245
    setCurrentColor(cusrgb[i]);
 
1246
    nextCust = i;
 
1247
    if (standard)
 
1248
        standard->setSelected(-1,-1);
 
1249
}
 
1250
 
 
1251
void QColorDialogPrivate::newStandard(int r, int c)
 
1252
{
 
1253
    setCurrentColor(stdrgb[r+c*6]);
 
1254
    if (custom)
 
1255
        custom->setSelected(-1,-1);
 
1256
}
 
1257
 
 
1258
void QColorDialogPrivate::init()
 
1259
{
 
1260
    Q_Q(QColorDialog);
 
1261
    compact = false;
 
1262
    // small displays (e.g. PDAs cannot fit the full color dialog,
 
1263
    // so just use the color picker.
 
1264
    if (qApp->desktop()->width() < 480 || qApp->desktop()->height() < 350)
 
1265
        compact = true;
 
1266
 
 
1267
    nextCust = 0;
 
1268
    const int lumSpace = 3;
 
1269
    int border = 12;
 
1270
    if (compact)
 
1271
        border = 6;
 
1272
    QHBoxLayout *topLay = new QHBoxLayout(q);
 
1273
    topLay->setMargin(border);
 
1274
    topLay->setSpacing(6);
 
1275
    QVBoxLayout *leftLay = 0;
 
1276
 
 
1277
    if (!compact) {
 
1278
        leftLay = new QVBoxLayout;
 
1279
        topLay->addLayout(leftLay);
 
1280
    }
 
1281
 
 
1282
    initRGB();
 
1283
 
 
1284
    if (!compact) {
 
1285
        standard = new QColorWell(q, 6, 8, stdrgb);
 
1286
        QLabel *lab = new QLabel(QColorDialog::tr("&Basic colors"), q);
 
1287
        lab->setBuddy(standard);
 
1288
        q->connect(standard, SIGNAL(selected(int,int)), SLOT(newStandard(int,int)));
 
1289
        leftLay->addWidget(lab);
 
1290
        leftLay->addWidget(standard);
 
1291
 
 
1292
 
 
1293
        leftLay->addStretch();
 
1294
 
 
1295
        custom = new QColorWell(q, 2, 8, cusrgb);
 
1296
        custom->setAcceptDrops(true);
 
1297
 
 
1298
        q->connect(custom, SIGNAL(selected(int,int)), SLOT(newCustom(int,int)));
 
1299
        lab = new QLabel(QColorDialog::tr("&Custom colors") , q);
 
1300
        lab->setBuddy(custom);
 
1301
        leftLay->addWidget(lab);
 
1302
        leftLay->addWidget(custom);
 
1303
 
 
1304
        QPushButton *custbut = new QPushButton(QColorDialog::tr("&Define Custom Colors >>"), q);
 
1305
        custbut->setEnabled(false);
 
1306
        leftLay->addWidget(custbut);
 
1307
    } else {
 
1308
        // better color picker size for small displays
 
1309
        pWidth = 150;
 
1310
        pHeight = 100;
 
1311
        custom = 0;
 
1312
        standard = 0;
 
1313
    }
 
1314
 
 
1315
    QVBoxLayout *rightLay = new QVBoxLayout;
 
1316
    topLay->addLayout(rightLay);
 
1317
 
 
1318
    QHBoxLayout *pickLay = new QHBoxLayout;
 
1319
    rightLay->addLayout(pickLay);
 
1320
 
 
1321
 
 
1322
    QVBoxLayout *cLay = new QVBoxLayout;
 
1323
    pickLay->addLayout(cLay);
 
1324
    cp = new QColorPicker(q);
 
1325
    cp->setFrameStyle(QFrame::Panel + QFrame::Sunken);
 
1326
    cLay->addSpacing(lumSpace);
 
1327
    cLay->addWidget(cp);
 
1328
    cLay->addSpacing(lumSpace);
 
1329
 
 
1330
    lp = new QColorLuminancePicker(q);
 
1331
    lp->setFixedWidth(20);
 
1332
    pickLay->addWidget(lp);
 
1333
 
 
1334
    QObject::connect(cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int)));
 
1335
    QObject::connect(lp, SIGNAL(newHsv(int,int,int)), q, SLOT(newHsv(int,int,int)));
 
1336
 
 
1337
    rightLay->addStretch();
 
1338
 
 
1339
    cs = new QColorShower(q);
 
1340
    QObject::connect(cs, SIGNAL(newCol(QRgb)), q, SLOT(newColorTypedIn(QRgb)));
 
1341
    rightLay->addWidget(cs);
 
1342
 
 
1343
    QHBoxLayout *buttons;
 
1344
    if (compact) {
 
1345
        buttons = new QHBoxLayout;
 
1346
        rightLay->addLayout(buttons);
 
1347
    } else {
 
1348
        buttons = new QHBoxLayout;
 
1349
        leftLay->addLayout(buttons);
 
1350
    }
 
1351
 
 
1352
    QPushButton *ok, *cancel;
 
1353
    ok = new QPushButton(QColorDialog::tr("OK"), q);
 
1354
    QObject::connect(ok, SIGNAL(clicked()), q, SLOT(accept()));
 
1355
    ok->setDefault(true);
 
1356
    cancel = new QPushButton(QColorDialog::tr("Cancel"), q);
 
1357
    QObject::connect(cancel, SIGNAL(clicked()), q, SLOT(reject()));
 
1358
    buttons->addWidget(ok);
 
1359
    buttons->addWidget(cancel);
 
1360
    buttons->addStretch();
 
1361
 
 
1362
    if (!compact) {
 
1363
        QPushButton *addCusBt = new QPushButton(QColorDialog::tr("&Add to Custom Colors"), q);
 
1364
        rightLay->addWidget(addCusBt);
 
1365
        QObject::connect(addCusBt, SIGNAL(clicked()), q, SLOT(addCustom()));
 
1366
    }
 
1367
}
 
1368
 
 
1369
void QColorDialogPrivate::addCustom()
 
1370
{
 
1371
    cusrgb[nextCust] = cs->currentColor();
 
1372
    if (custom)
 
1373
        custom->update();
 
1374
    nextCust = (nextCust+1) % 16;
 
1375
}
 
1376
 
 
1377
 
 
1378
/*!
 
1379
    \class QColorDialog qcolordialog.h
 
1380
    \brief The QColorDialog class provides a dialog widget for specifying colors.
 
1381
 
 
1382
    \mainclass
 
1383
    \ingroup dialogs
 
1384
    \ingroup multimedia
 
1385
 
 
1386
    The color dialog's function is to allow users to choose colors.
 
1387
    For example, you might use this in a drawing program to allow the
 
1388
    user to set the brush color.
 
1389
 
 
1390
    The static functions provide modal color dialogs.
 
1391
    \omit
 
1392
    If you require a modeless dialog, use the QColorDialog constructor.
 
1393
    \endomit
 
1394
 
 
1395
    The static getColor() function shows the dialog, and allows the
 
1396
    user to specify a color. The getRgba() function does the same, but
 
1397
    also allows the user to specify a color with an alpha channel
 
1398
    (transparency) value.
 
1399
 
 
1400
    The user can store customCount() different custom colors. The
 
1401
    custom colors are shared by all color dialogs, and remembered
 
1402
    during the execution of the program. Use setCustomColor() to set
 
1403
    the custom colors, and use customColor() to get them.
 
1404
 
 
1405
    Additional widgets that allow users to pick colors are available
 
1406
    as \link
 
1407
    http://www.trolltech.com/products/solutions/index.html Qt
 
1408
    Solutions\endlink.
 
1409
 
 
1410
    The \l{dialogs/standarddialogs}{Standard Dialogs} example shows
 
1411
    how to use QColorDialog as well as other built-in Qt dialogs.
 
1412
 
 
1413
    \img qcolordlg-w.png
 
1414
*/
 
1415
 
 
1416
/*!
 
1417
    Constructs a default color dialog called \a name with the given \a parent.
 
1418
    If \a modal is true the dialog will be modal. Use setColor() to set an
 
1419
    initial value.
 
1420
 
 
1421
    \sa getColor()
 
1422
*/
 
1423
 
 
1424
QColorDialog::QColorDialog(QWidget* parent, bool modal) :
 
1425
    QDialog(*new QColorDialogPrivate, parent, (Qt::Dialog | Qt::WindowTitleHint |
 
1426
                     Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint))
 
1427
{
 
1428
    Q_D(QColorDialog);
 
1429
    setModal(modal);
 
1430
    setSizeGripEnabled(false);
 
1431
    d->init();
 
1432
 
 
1433
#ifndef QT_NO_SETTINGS
 
1434
    if (!customSet) {
 
1435
        QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
 
1436
        settings.beginGroup(QLatin1String("Qt"));
 
1437
        for (int i = 0; i < 2*8; ++i) {
 
1438
            QVariant v = settings.value(QLatin1String("customColors/") + QString::number(i));
 
1439
            if (v.isValid()) {
 
1440
                QRgb rgb = v.toUInt();
 
1441
                cusrgb[i] = rgb;
 
1442
            }
 
1443
        }
 
1444
        settings.endGroup(); // Qt
 
1445
    }
 
1446
#endif
 
1447
}
 
1448
 
 
1449
/*!
 
1450
    Pops up a modal color dialog, lets the user choose a color, and
 
1451
    returns that color. The color is initially set to \a initial. The
 
1452
    dialog is a child of \a parent. It returns an invalid (see
 
1453
    QColor::isValid()) color if the user cancels the dialog. All
 
1454
    colors allocated by the dialog will be deallocated before this
 
1455
    function returns.
 
1456
*/
 
1457
 
 
1458
QColor QColorDialog::getColor(const QColor& initial, QWidget *parent)
 
1459
{
 
1460
#if defined(Q_WS_MAC)
 
1461
    return macGetColor(initial, parent);
 
1462
#endif
 
1463
 
 
1464
    QColorDialog *dlg = new QColorDialog(parent, true);  //modal
 
1465
    dlg->setWindowTitle(QColorDialog::tr("Select color"));
 
1466
    dlg->setColor(initial);
 
1467
    dlg->selectColor(initial);
 
1468
    int resultCode = dlg->exec();
 
1469
    QColor result;
 
1470
    if (resultCode == QDialog::Accepted)
 
1471
        result = dlg->color();
 
1472
    delete dlg;
 
1473
    return result;
 
1474
}
 
1475
 
 
1476
 
 
1477
/*!
 
1478
    Pops up a modal color dialog to allow the user to choose a color
 
1479
    and an alpha channel (transparency) value. The color+alpha is
 
1480
    initially set to \a initial. The dialog is a child of \a parent.
 
1481
 
 
1482
    If \a ok is non-null, \e *\a ok is set to true if the user clicked
 
1483
    OK, and to false if the user clicked Cancel.
 
1484
 
 
1485
    If the user clicks Cancel, the \a initial value is returned.
 
1486
*/
 
1487
 
 
1488
QRgb QColorDialog::getRgba(QRgb initial, bool *ok, QWidget *parent)
 
1489
{
 
1490
#if defined(Q_WS_MAC)
 
1491
    return macGetRgba(initial, ok, parent);
 
1492
#endif
 
1493
 
 
1494
    QColorDialog *dlg = new QColorDialog(parent, true);  //modal
 
1495
 
 
1496
    dlg->setWindowTitle(QColorDialog::tr("Select color"));
 
1497
    dlg->setColor(initial);
 
1498
    dlg->selectColor(initial);
 
1499
    dlg->setSelectedAlpha(qAlpha(initial));
 
1500
    int resultCode = dlg->exec();
 
1501
    QRgb result = initial;
 
1502
    if (resultCode == QDialog::Accepted) {
 
1503
        QRgb c = dlg->color().rgb();
 
1504
        int alpha = dlg->selectedAlpha();
 
1505
        result = qRgba(qRed(c), qGreen(c), qBlue(c), alpha);
 
1506
    }
 
1507
    if (ok)
 
1508
        *ok = resultCode == QDialog::Accepted;
 
1509
 
 
1510
    delete dlg;
 
1511
    return result;
 
1512
}
 
1513
 
 
1514
 
 
1515
 
 
1516
 
 
1517
 
 
1518
/*!
 
1519
    Returns the currently selected color in the dialog.
 
1520
 
 
1521
    \sa setColor()
 
1522
*/
 
1523
 
 
1524
QColor QColorDialog::color() const
 
1525
{
 
1526
    Q_D(const QColorDialog);
 
1527
    return QColor(d->currentColor());
 
1528
}
 
1529
 
 
1530
 
 
1531
/*!
 
1532
    Destroys the color dialog.
 
1533
*/
 
1534
 
 
1535
QColorDialog::~QColorDialog()
 
1536
{
 
1537
#ifndef QT_NO_SETTINGS
 
1538
    if (!customSet) {
 
1539
        QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
 
1540
        settings.beginGroup(QLatin1String("Qt"));
 
1541
        for (int i = 0; i < 2*8; ++i)
 
1542
            settings.setValue(QLatin1String("customColors/") + QString::number(i), cusrgb[i]);
 
1543
        settings.endGroup();
 
1544
    }
 
1545
#endif
 
1546
}
 
1547
 
 
1548
 
 
1549
/*!
 
1550
    \fn void QColorDialog::setColor(const QColor &color)
 
1551
 
 
1552
    Sets the color shown in the dialog to the \a color given.
 
1553
 
 
1554
    \sa color()
 
1555
*/
 
1556
 
 
1557
void QColorDialog::setColor(const QColor& c)
 
1558
{
 
1559
    Q_D(QColorDialog);
 
1560
    d->setCurrentColor(c.rgb());
 
1561
}
 
1562
 
 
1563
 
 
1564
 
 
1565
 
 
1566
/*!
 
1567
    \fn void QColorDialog::setSelectedAlpha(int alpha)
 
1568
 
 
1569
    Sets the initial alpha channel value to the \a alpha value given, and
 
1570
    shows the alpha channel entry box.
 
1571
*/
 
1572
 
 
1573
void QColorDialog::setSelectedAlpha(int a)
 
1574
{
 
1575
    Q_D(QColorDialog);
 
1576
    d->showAlpha(true);
 
1577
    d->setCurrentAlpha(a);
 
1578
}
 
1579
 
 
1580
 
 
1581
/*!
 
1582
    Returns the value selected for the alpha channel.
 
1583
*/
 
1584
 
 
1585
int QColorDialog::selectedAlpha() const
 
1586
{
 
1587
    Q_D(const QColorDialog);
 
1588
    return d->currentAlpha();
 
1589
}
 
1590
 
 
1591
/*!
 
1592
    Sets focus to the corresponding button, if any.
 
1593
*/
 
1594
bool QColorDialog::selectColor(const QColor& col)
 
1595
{
 
1596
    Q_D(QColorDialog);
 
1597
    QRgb color = col.rgb();
 
1598
    int i = 0, j = 0;
 
1599
    // Check standard colors
 
1600
    if (d->standard) {
 
1601
        for (i = 0; i < 6; i++) {
 
1602
            for (j = 0; j < 8; j++) {
 
1603
                if (color == stdrgb[i + j*6]) {
 
1604
                    d->newStandard(i, j);
 
1605
                    d->standard->setCurrent(i, j);
 
1606
                    d->standard->setSelected(i, j);
 
1607
                    d->standard->setFocus();
 
1608
                    return true;
 
1609
                }
 
1610
            }
 
1611
        }
 
1612
    }
 
1613
    // Check custom colors
 
1614
    if (d->custom) {
 
1615
        for (i = 0; i < 2; i++) {
 
1616
            for (j = 0; j < 8; j++) {
 
1617
                if (color == cusrgb[i + j*2]) {
 
1618
                    d->newCustom(i, j);
 
1619
                    d->custom->setCurrent(i, j);
 
1620
                    d->custom->setSelected(i, j);
 
1621
                    d->custom->setFocus();
 
1622
                    return true;
 
1623
                }
 
1624
            }
 
1625
        }
 
1626
    }
 
1627
    return false;
 
1628
}
 
1629
 
 
1630
#include "qcolordialog.moc"
 
1631
#include "moc_qcolordialog.cpp"
 
1632
 
 
1633
#endif
 
1634
 
 
1635
/*!
 
1636
    \fn QColor QColorDialog::getColor(const QColor &init, QWidget *parent, const char *name)
 
1637
    \compat
 
1638
*/
 
1639
 
 
1640
/*!
 
1641
    \fn QRgb QColorDialog::getRgba(QRgb rgba, bool *ok, QWidget *parent, const char *name)
 
1642
    \compat
 
1643
*/