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

« back to all changes in this revision

Viewing changes to src/gui/kernel/qpalette.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 gui 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 "qpalette.h"
 
30
#include "qapplication.h"
 
31
#ifndef QT_NO_PALETTE
 
32
#include "qdatastream.h"
 
33
#include "qvariant.h"
 
34
 
 
35
static int qt_palette_count = 1;
 
36
class QPalettePrivate {
 
37
public:
 
38
    QPalettePrivate() : ref(1), ser_no(qt_palette_count++) { }
 
39
    QAtomic ref;
 
40
    QBrush br[QPalette::NColorGroups][QPalette::NColorRoles];
 
41
    int ser_no;
 
42
};
 
43
 
 
44
static QColor qt_mix_colors(QColor a, QColor b)
 
45
{
 
46
    return QColor((a.red() + b.red()) / 2, (a.green() + b.green()) / 2,
 
47
                  (a.blue() + b.blue()) / 2, (a.alpha() + b.alpha()) / 2);
 
48
}
 
49
 
 
50
#ifdef QT3_SUPPORT
 
51
 
 
52
#ifndef QT_NO_DATASTREAM
 
53
QDataStream &qt_stream_out_qcolorgroup(QDataStream &s, const QColorGroup &g)
 
54
{
 
55
    if(s.version() == 1) {
 
56
        // Qt 1.x
 
57
        s << g.color(QPalette::Foreground) << g.color(QPalette::Background)
 
58
          << g.color(QPalette::Light) << g.color(QPalette::Dark)
 
59
          << g.color(QPalette::Mid) << g.color(QPalette::Text) << g.color(QPalette::Base);
 
60
    } else {
 
61
        int max = QPalette::NColorRoles;
 
62
        if(s.version() <= 3) // Qt 2.x
 
63
            max = 14;
 
64
        for(int r = 0 ; r < max ; r++)
 
65
            s << g.brush((QPalette::ColorRole)r);
 
66
    }
 
67
    return s;
 
68
}
 
69
 
 
70
QDataStream &qt_stream_in_qcolorgroup(QDataStream &s, QColorGroup &g)
 
71
{
 
72
    if(s.version() == 1) {         // Qt 1.x
 
73
        QColor fg, bg, light, dark, mid, text, base;
 
74
        s >> fg >> bg >> light >> dark >> mid >> text >> base;
 
75
        QPalette p(bg);
 
76
        p.setColor(QPalette::Active, QPalette::Foreground, fg);
 
77
        p.setColor(QPalette::Active, QPalette::Light, light);
 
78
        p.setColor(QPalette::Active, QPalette::Dark, dark);
 
79
        p.setColor(QPalette::Active, QPalette::Mid, mid);
 
80
        p.setColor(QPalette::Active, QPalette::Text, text);
 
81
        p.setColor(QPalette::Active, QPalette::Base, base);
 
82
        g = p;
 
83
        g.setCurrentColorGroup(QPalette::Active);
 
84
    } else {
 
85
        int max = QPalette::NColorRoles;
 
86
        if (s.version() <= 3) // Qt 2.x
 
87
            max = 14;
 
88
        else if (s.version() <= 4) // Qt 3.x
 
89
            max = 16;
 
90
        QBrush tmp;
 
91
        for(int r = 0 ; r < max; r++) {
 
92
            s >> tmp;
 
93
            g.setBrush((QPalette::ColorRole)r, tmp);
 
94
        }
 
95
    }
 
96
    return s;
 
97
}
 
98
 
 
99
QDataStream &operator<<(QDataStream &s, const QColorGroup &g)
 
100
{
 
101
    return qt_stream_out_qcolorgroup(s, g);
 
102
}
 
103
 
 
104
QDataStream &operator>>(QDataStream &s, QColorGroup &g)
 
105
{
 
106
    return qt_stream_in_qcolorgroup(s, g);
 
107
}
 
108
#endif
 
109
 
 
110
/*!
 
111
    Constructs a palette with the specified \a active, \a disabled and
 
112
    \a inactive color groups.
 
113
*/
 
114
QPalette::QPalette(const QColorGroup &active, const QColorGroup &disabled,
 
115
                   const QColorGroup &inactive)
 
116
{
 
117
    init();
 
118
    setColorGroup(Active, active);
 
119
    setColorGroup(Disabled, disabled);
 
120
    setColorGroup(Inactive, inactive);
 
121
}
 
122
 
 
123
QColorGroup QPalette::createColorGroup(ColorGroup cr) const
 
124
{
 
125
    QColorGroup ret(*this);
 
126
    ret.setCurrentColorGroup(cr);
 
127
    return ret;
 
128
}
 
129
 
 
130
void QPalette::setColorGroup(ColorGroup cg, const QColorGroup &g)
 
131
{
 
132
    setColorGroup(cg, g.brush(Foreground), g.brush(Button), g.brush(Light),
 
133
                  g.brush(Dark), g.brush(Mid), g.brush(Text), g.brush(BrightText),
 
134
                  g.brush(Base), g.brush(AlternateBase), g.brush(Background),
 
135
                  g.brush(Midlight), g.brush(ButtonText), g.brush(Shadow),
 
136
                  g.brush(Highlight), g.brush(HighlightedText), g.brush(Link),
 
137
                  g.brush(LinkVisited));
 
138
}
 
139
 
 
140
#endif
 
141
 
 
142
/*!
 
143
   \fn const QColor &QPalette::color(ColorRole r) const
 
144
 
 
145
   \overload
 
146
 
 
147
    Returns the color that has been set for color role \a r in the current ColorGroup.
 
148
 
 
149
    \sa brush() ColorRole
 
150
 */
 
151
 
 
152
/*!
 
153
    \fn const QBrush &QPalette::brush(ColorRole r) const
 
154
 
 
155
    \overload
 
156
 
 
157
    Returns the brush that has been set for color role \a r in the current ColorGroup.
 
158
 
 
159
    \sa color() setBrush() ColorRole
 
160
*/
 
161
 
 
162
/*!
 
163
    \fn void QPalette::setColor(ColorRole r, const QColor &c)
 
164
 
 
165
    \overload
 
166
 
 
167
    Sets the brush used for color role \a r in the current ColorGroup to a solid color \a c.
 
168
 
 
169
    \sa brush() setColor() ColorRole
 
170
*/
 
171
 
 
172
/*!
 
173
    \fn void QPalette::setBrush(ColorRole cr, const QBrush &brush)
 
174
 
 
175
    Sets the brush used to color role \a cr and brush \a brush.
 
176
 
 
177
    \sa brush() setColor() ColorRole
 
178
*/
 
179
 
 
180
/*!
 
181
    \fn const QBrush & QPalette::foreground() const
 
182
 
 
183
    Returns the foreground brush of the current color group.
 
184
 
 
185
    \sa ColorRole brush()
 
186
*/
 
187
 
 
188
/*!
 
189
    \fn const QBrush & QPalette::button() const
 
190
 
 
191
    Returns the button brush of the current color group.
 
192
 
 
193
    \sa ColorRole brush()
 
194
*/
 
195
 
 
196
/*!
 
197
    \fn const QBrush & QPalette::light() const
 
198
 
 
199
    Returns the light brush of the current color group.
 
200
 
 
201
    \sa ColorRole brush()
 
202
*/
 
203
 
 
204
/*!
 
205
    \fn const QBrush& QPalette::midlight() const
 
206
 
 
207
    Returns the midlight brush of the current color group.
 
208
 
 
209
    \sa ColorRole brush()
 
210
*/
 
211
 
 
212
/*!
 
213
    \fn const QBrush & QPalette::dark() const
 
214
 
 
215
    Returns the dark brush of the current color group.
 
216
 
 
217
    \sa ColorRole brush()
 
218
*/
 
219
 
 
220
/*!
 
221
    \fn const QBrush & QPalette::mid() const
 
222
 
 
223
    Returns the mid brush of the current color group.
 
224
 
 
225
    \sa ColorRole brush()
 
226
*/
 
227
 
 
228
/*!
 
229
    \fn const QBrush & QPalette::text() const
 
230
 
 
231
    Returns the text foreground brush of the current color group.
 
232
 
 
233
    \sa ColorRole brush()
 
234
*/
 
235
 
 
236
/*!
 
237
    \fn const QBrush & QPalette::brightText() const
 
238
 
 
239
    Returns the bright text foreground brush of the current color group.
 
240
 
 
241
    \sa ColorRole brush()
 
242
*/
 
243
 
 
244
/*!
 
245
    \fn const QBrush & QPalette::buttonText() const
 
246
 
 
247
    Returns the button text foreground brush of the current color group.
 
248
 
 
249
    \sa ColorRole brush()
 
250
*/
 
251
 
 
252
/*!
 
253
    \fn const QBrush & QPalette::base() const
 
254
 
 
255
    Returns the base brush of the current color group.
 
256
 
 
257
    \sa ColorRole brush()
 
258
*/
 
259
 
 
260
/*!
 
261
    \fn const QBrush & QPalette::alternateBase() const
 
262
 
 
263
    Returns the alternate base brush of the current color group.
 
264
 
 
265
    \sa ColorRole brush()
 
266
*/
 
267
 
 
268
/*!
 
269
    \fn const QBrush & QPalette::background() const
 
270
 
 
271
    Returns the background brush of the current color group.
 
272
 
 
273
    \sa ColorRole brush()
 
274
*/
 
275
 
 
276
/*!
 
277
    \fn const QBrush & QPalette::shadow() const
 
278
 
 
279
    Returns the shadow brush of the current color group.
 
280
 
 
281
    \sa ColorRole brush()
 
282
*/
 
283
 
 
284
/*!
 
285
    \fn const QBrush & QPalette::highlight() const
 
286
 
 
287
    Returns the highlight brush of the current color group.
 
288
 
 
289
    \sa ColorRole brush()
 
290
*/
 
291
 
 
292
/*!
 
293
    \fn const QBrush & QPalette::highlightedText() const
 
294
 
 
295
    Returns the highlighted text brush of the current color group.
 
296
 
 
297
    \sa ColorRole brush()
 
298
*/
 
299
 
 
300
/*!
 
301
    \fn const QBrush & QPalette::link() const
 
302
 
 
303
    Returns the unvisited link text brush of the current color group.
 
304
 
 
305
    \sa ColorRole brush()
 
306
*/
 
307
 
 
308
/*!
 
309
    \fn const QBrush & QPalette::linkVisited() const
 
310
 
 
311
    Returns the visited link text brush of the current color group.
 
312
 
 
313
    \sa ColorRole brush()
 
314
*/
 
315
 
 
316
/*!
 
317
    \fn ColorGroup QPalette::currentColorGroup() const
 
318
 
 
319
    Returns the palette's current color group.
 
320
*/
 
321
 
 
322
/*!
 
323
    \fn void QPalette::setCurrentColorGroup(ColorGroup cg)
 
324
 
 
325
    Set the palette's current color group to \a cg.
 
326
*/
 
327
 
 
328
/*!
 
329
    \class QPalette qpalette.h
 
330
 
 
331
    \brief The QPalette class contains color groups for each widget state.
 
332
 
 
333
    \ingroup appearance
 
334
    \ingroup shared
 
335
    \ingroup multimedia
 
336
    \mainclass
 
337
 
 
338
    A palette consists of three color groups: \e Active, \e Disabled,
 
339
    and \e Inactive. All widgets in Qt contain a palette and
 
340
    use their palette to draw themselves. This makes the user
 
341
    interface easily configurable and easier to keep consistent.
 
342
 
 
343
    If you create a new widget we strongly recommend that you use the
 
344
    colors in the palette rather than hard-coding specific colors.
 
345
 
 
346
    The color groups:
 
347
    \list
 
348
    \i The Active group is used for the window that has keyboard focus.
 
349
    \i The Inactive group is used for other windows.
 
350
    \i The Disabled group is used for widgets (not windows) that are
 
351
    disabled for some reason.
 
352
    \endlist
 
353
 
 
354
    Both active and inactive windows can contain disabled widgets.
 
355
    (Disabled widgets are often called \e inaccessible or \e{grayed
 
356
    out}.)
 
357
 
 
358
    In most styles, Active and Inactive look the same.
 
359
 
 
360
    Colors and brushes can be set for particular roles in any of a palette's
 
361
    color groups with setColor() and setBrush().  A color group contains a
 
362
    group of colors used by widgets for drawing themselves. We recommend that
 
363
    widgets use color group roles from the palette such as "foreground" and
 
364
    "base" rather than literal colors like "red" or "turquoise". The color
 
365
    roles are enumerated and defined in the \l ColorRole documentation.
 
366
 
 
367
    We strongly recommend that you use a system-supplied color group
 
368
    and modify that as necessary.
 
369
 
 
370
    To modify a color group you call the functions
 
371
    setColor() and setBrush(), depending on whether you want a pure
 
372
    color or a pixmap pattern.
 
373
 
 
374
    There are also corresponding color() and brush() getters, and a
 
375
    commonly used convenience function to get the ColorRole for the current ColorGroup:
 
376
    background(), foreground(), base(), etc.
 
377
 
 
378
    You can copy a palette using the copy constructor and test to see
 
379
    if two palettes are \e identical using isCopyOf().
 
380
 
 
381
    QPalette is optimized by the use of \link shclass.html implicit
 
382
    sharing\endlink, so it is very efficient to pass QPalette objects as
 
383
    arguments.
 
384
 
 
385
    \sa QApplication::setPalette(), QWidget::setPalette(), QColor
 
386
*/
 
387
 
 
388
/*!
 
389
    \enum QPalette::ColorGroup
 
390
 
 
391
    \value Disabled
 
392
    \value Active
 
393
    \value Inactive
 
394
    \value Normal synonym for Active
 
395
 
 
396
    \omitvalue All
 
397
    \omitvalue NColorGroups
 
398
    \omitvalue Current
 
399
*/
 
400
 
 
401
/*!
 
402
    \enum QPalette::ColorRole
 
403
 
 
404
    The ColorRole enum defines the different symbolic color roles used
 
405
    in current GUIs.
 
406
 
 
407
    The central roles are:
 
408
 
 
409
    \value Background  A general background color.
 
410
 
 
411
    \value Foreground  A general foreground color.
 
412
 
 
413
    \value Base  Used as the background color for text entry widgets;
 
414
                 usually white or another light color.
 
415
 
 
416
    \value AlternateBase  Used as the alternate background color in views with
 
417
                          alternating row colors (see
 
418
                          QAbstractItemView::setAlternatingRowColors()).
 
419
 
 
420
    \value Text  The foreground color used with \c Base. This is usually
 
421
                 the same as the \c Foreground, in which case it must provide
 
422
                 good contrast with \c Background and \c Base.
 
423
 
 
424
    \value Button The general button background color. This background can be different from
 
425
                  \c Background as some styles require a different background color for buttons.
 
426
 
 
427
    \value ButtonText  A foreground color used with the \c Button color.
 
428
 
 
429
    There are some color roles used mostly for 3D bevel and shadow effects.
 
430
    All of these are normally derived from \c Background, and used in ways that
 
431
    depend on that relationship. For example, buttons depend on it to make the
 
432
    bevels look attractive, and Motif scroll bars depend on \c Mid to be
 
433
    slightly different from \c Background.
 
434
 
 
435
    \value Light  Lighter than \c Button color.
 
436
 
 
437
    \value Midlight  Between \c Button and \c Light.
 
438
 
 
439
    \value Dark  Darker than \c Button.
 
440
 
 
441
    \value Mid  Between \c Button and \c Dark.
 
442
 
 
443
    \value Shadow  A very dark color. By default, the shadow color is
 
444
                   \c Qt::black.
 
445
 
 
446
 
 
447
    Selected (marked) items have two roles:
 
448
 
 
449
    \value Highlight   A color to indicate a selected item or the current
 
450
                       item. By default, the highlight color is
 
451
                       \c Qt::darkBlue.
 
452
 
 
453
    \value HighlightedText  A text color that contrasts with \c Highlight.
 
454
    By default, the highlighted text color is \c Qt::white.
 
455
 
 
456
    Finally, there is a special role for text that needs to be drawn where \c
 
457
    Text or \c Foreground would give poor contrast, such as on pressed push
 
458
    buttons.  Note that text colors can be used for things other than just
 
459
    words; text colors are \e usually used for text, but it's quite common to
 
460
    use the text color roles for lines, icons, etc.
 
461
 
 
462
    \value BrightText  A text color that is very different from
 
463
                       \c Foreground, and contrasts well with e.g. \c Dark.
 
464
 
 
465
    \value Link  A text color used for unvisited hyperlinks.
 
466
                 By default, the link color is \c Qt::blue.
 
467
 
 
468
    \value LinkVisited  A text color used for already visited hyperlinks.
 
469
                        By default, the linkvisited color is \c Qt::magenta.
 
470
 
 
471
    \omitvalue NColorRoles
 
472
    \omitvalue NoRole
 
473
 
 
474
 
 
475
    This image shows most of the color roles in use:
 
476
    \img palette.png Color Roles
 
477
*/
 
478
 
 
479
/*!
 
480
    Constructs a palette object that uses the application's default palette.
 
481
 
 
482
    \sa QApplication::setPalette(), QApplication::palette()
 
483
*/
 
484
QPalette::QPalette()
 
485
    : d(QApplication::palette().d),
 
486
      current_group(Active),
 
487
      resolve_mask(0)
 
488
{
 
489
    d->ref.ref();
 
490
}
 
491
 
 
492
static void qt_palette_from_color(QPalette &pal, const QColor & button)
 
493
{
 
494
    QColor bg = button,
 
495
           btn = button,
 
496
           fg, base;
 
497
    int h, s, v;
 
498
    bg.getHsv(&h, &s, &v);
 
499
    if(v > 128) {
 
500
        fg   = Qt::black;
 
501
        base = Qt::white;
 
502
    } else {
 
503
        fg   = Qt::white;
 
504
        base = Qt::black;
 
505
    }
 
506
    //inactive and active are the same..
 
507
    pal.setColorGroup(QPalette::Active, QBrush(fg), QBrush(btn), QBrush(btn.light(150)),
 
508
                      QBrush(btn.dark()), QBrush(btn.dark(150)), QBrush(fg), QBrush(Qt::white),
 
509
                      QBrush(base), QBrush(bg));
 
510
    pal.setColorGroup(QPalette::Inactive, QBrush(fg), QBrush(btn), QBrush(btn.light(150)),
 
511
                      QBrush(btn.dark()), QBrush(btn.dark(150)), QBrush(fg), QBrush(Qt::white),
 
512
                      QBrush(base), QBrush(bg));
 
513
    pal.setColorGroup(QPalette::Disabled, QBrush(btn.dark()), QBrush(btn), QBrush(btn.light(150)),
 
514
                      QBrush(btn.dark()), QBrush(btn.dark(150)), QBrush(btn.dark()),
 
515
                      QBrush(Qt::white), QBrush(bg), QBrush(bg));
 
516
}
 
517
 
 
518
 
 
519
/*!
 
520
  Constructs a palette from the \a button color. The other colors are
 
521
  automatically calculated, based on this color. Background will be
 
522
  the button color as well.
 
523
*/
 
524
QPalette::QPalette(const QColor &button)
 
525
{
 
526
    init();
 
527
    qt_palette_from_color(*this, button);
 
528
}
 
529
 
 
530
/*!
 
531
  Constructs a palette from the \a button color. The other colors are
 
532
  automatically calculated, based on this color. Background will be
 
533
  the button color as well.
 
534
*/
 
535
QPalette::QPalette(Qt::GlobalColor button)
 
536
{
 
537
    init();
 
538
    qt_palette_from_color(*this, button);
 
539
}
 
540
 
 
541
/*!
 
542
    Constructs a palette. You can pass either brushes, pixmaps or
 
543
    plain colors for \a foreground, \a button, \a light, \a dark, \a
 
544
    mid, \a text, \a bright_text, \a base and \a background.
 
545
 
 
546
    \sa QBrush
 
547
*/
 
548
QPalette::QPalette(const QBrush &foreground, const QBrush &button,
 
549
                   const QBrush &light, const QBrush &dark,
 
550
                   const QBrush &mid, const QBrush &text,
 
551
                   const QBrush &bright_text, const QBrush &base,
 
552
                   const QBrush &background)
 
553
{
 
554
    init();
 
555
    setColorGroup(All, foreground, button, light, dark, mid, text, bright_text,
 
556
                  base, background);
 
557
}
 
558
 
 
559
 
 
560
/*!\obsolete
 
561
 
 
562
  Constructs a palette with the specified \a foreground, \a
 
563
  background, \a light, \a dark, \a mid, \a text, and \a base colors.
 
564
  The button color will be set to the background color.
 
565
*/
 
566
QPalette::QPalette(const QColor &foreground, const QColor &background,
 
567
                   const QColor &light, const QColor &dark, const QColor &mid,
 
568
                   const QColor &text, const QColor &base)
 
569
{
 
570
    init();
 
571
    setColorGroup(All, QBrush(foreground), QBrush(background), QBrush(light),
 
572
                  QBrush(dark), QBrush(mid), QBrush(text), QBrush(light),
 
573
                  QBrush(base), QBrush(background));
 
574
}
 
575
 
 
576
/*!
 
577
    Constructs a palette from a \a button color and a \a background.
 
578
    The other colors are automatically calculated, based on these
 
579
    colors.
 
580
*/
 
581
QPalette::QPalette(const QColor &button, const QColor &background)
 
582
{
 
583
    init();
 
584
    QColor bg = background, btn = button, fg, base, disfg;
 
585
    int h, s, v;
 
586
    bg.getHsv(&h, &s, &v);
 
587
    if(v > 128) {
 
588
        fg   = Qt::black;
 
589
        base = Qt::white;
 
590
        disfg = Qt::darkGray;
 
591
    } else {
 
592
        fg   = Qt::white;
 
593
        base = Qt::black;
 
594
        disfg = Qt::darkGray;
 
595
    }
 
596
    //inactive and active are identical
 
597
    setColorGroup(Inactive, QBrush(fg), QBrush(btn), QBrush(btn.light(150)), QBrush(btn.dark()),
 
598
                  QBrush(btn.dark(150)), QBrush(fg), QBrush(Qt::white), QBrush(base),
 
599
                  QBrush(bg));
 
600
    setColorGroup(Active, QBrush(fg), QBrush(btn), QBrush(btn.light(150)), QBrush(btn.dark()),
 
601
                  QBrush(btn.dark(150)), QBrush(fg), QBrush(Qt::white), QBrush(base),
 
602
                  QBrush(bg));
 
603
    setColorGroup(Disabled, QBrush(disfg), QBrush(btn), QBrush(btn.light(150)),
 
604
                  QBrush(btn.dark()), QBrush(btn.dark(150)), QBrush(disfg),
 
605
                  QBrush(Qt::white), QBrush(base), QBrush(bg));
 
606
}
 
607
 
 
608
/*!
 
609
    Constructs a copy of \a p.
 
610
 
 
611
    This constructor is fast because of \link shclass.html implicit sharing\endlink.
 
612
*/
 
613
QPalette::QPalette(const QPalette &p)
 
614
{
 
615
    d = p.d;
 
616
    d->ref.ref();
 
617
    resolve_mask = p.resolve_mask;
 
618
    current_group = p.current_group;
 
619
}
 
620
 
 
621
/*!
 
622
    Destroys the palette.
 
623
*/
 
624
QPalette::~QPalette()
 
625
{
 
626
    if(!d->ref.deref())
 
627
        delete d;
 
628
}
 
629
 
 
630
/*!\internal*/
 
631
void QPalette::init() {
 
632
    d = new QPalettePrivate;
 
633
    resolve_mask = 0;
 
634
    current_group = Active; //as a default..
 
635
}
 
636
 
 
637
/*!
 
638
    Assigns \a p to this palette and returns a reference to this
 
639
    palette.
 
640
 
 
641
    This operation is fast because of \link shclass.html implicit sharing\endlink.
 
642
*/
 
643
QPalette &QPalette::operator=(const QPalette &p)
 
644
{
 
645
    QPalettePrivate *x = p.d;
 
646
    x->ref.ref();
 
647
    resolve_mask = p.resolve_mask;
 
648
    current_group = p.current_group;
 
649
    x = qAtomicSetPtr(&d, x);
 
650
    if(!x->ref.deref())
 
651
        delete x;
 
652
    return *this;
 
653
}
 
654
 
 
655
/*!
 
656
   Returns the palette as a QVariant
 
657
*/
 
658
QPalette::operator QVariant() const
 
659
{
 
660
    return QVariant(QVariant::Palette, this);
 
661
}
 
662
 
 
663
/*!
 
664
    \fn const QColor &QPalette::color(ColorGroup gr, ColorRole r) const
 
665
    Returns the color in color group \a gr, used for color role \a r.
 
666
 
 
667
    \sa brush() setColor() ColorRole
 
668
*/
 
669
 
 
670
/*!
 
671
    Returns the brush in color group \a gr, used for color role \a cr.
 
672
 
 
673
    \sa color() setBrush() ColorRole
 
674
*/
 
675
const QBrush &QPalette::brush(ColorGroup gr, ColorRole cr) const
 
676
{
 
677
    Q_ASSERT(cr < NColorRoles);
 
678
    if(gr >= (int)NColorGroups) {
 
679
        if(gr == Current) {
 
680
            gr = (ColorGroup)current_group;
 
681
        } else {
 
682
            qWarning("QPalette::brush: Unknown ColorGroup: %d", (int)gr);
 
683
            gr = Active;
 
684
        }
 
685
    }
 
686
    return d->br[gr][cr];
 
687
}
 
688
 
 
689
/*!
 
690
    \fn void QPalette::setColor(ColorGroup gr, ColorRole r, const QColor &c)
 
691
    Sets the brush in color group \a gr, used for color role \a r, to
 
692
    the solid color \a c.
 
693
 
 
694
    \sa setBrush() color() ColorRole
 
695
*/
 
696
 
 
697
/*!
 
698
    \overload
 
699
 
 
700
    Sets the brush in color group \a cg, used for color role \a cr, to
 
701
    \a b.
 
702
 
 
703
    \sa brush() setColor() ColorRole
 
704
*/
 
705
void QPalette::setBrush(ColorGroup cg, ColorRole cr, const QBrush &b)
 
706
{
 
707
    Q_ASSERT(cr < NColorRoles);
 
708
    detach();
 
709
    if(cg >= (int)NColorGroups) {
 
710
        if(cg == All) {
 
711
            for(int i = 0; i < (int)NColorGroups; i++)
 
712
                d->br[i][cr] = b;
 
713
            resolve_mask |= (1<<cr);
 
714
            return;
 
715
        } else if(cg == Current) {
 
716
            cg = (ColorGroup)current_group;
 
717
        } else {
 
718
            qWarning("QPalette::setBrush: Unknown ColorGroup: %d", (int)cg);
 
719
            cg = Active;
 
720
        }
 
721
    }
 
722
    d->br[cg][cr] = b;
 
723
    resolve_mask |= (1<<cr);
 
724
}
 
725
 
 
726
 
 
727
/*!
 
728
    \internal
 
729
    \fn void QPalette::detach()
 
730
    Detaches this palette from any other QPalette objects with which
 
731
    it might implicitly share QColorGroup objects. In essence, does
 
732
    the copying part of copy-on-write.
 
733
 
 
734
    Calling this should generally not be necessary; QPalette calls it
 
735
    itself when necessary.
 
736
*/
 
737
void QPalette::detach()
 
738
{
 
739
    if (d->ref != 1) {
 
740
        QPalettePrivate *x = new QPalettePrivate;
 
741
        for(int grp = 0; grp < (int)NColorGroups; grp++) {
 
742
            for(int role = 0; role < (int)NColorRoles; role++)
 
743
                x->br[grp][role] = d->br[grp][role];
 
744
        }
 
745
        x = qAtomicSetPtr(&d, x);
 
746
        if(!x->ref.deref())
 
747
            delete x;
 
748
    }
 
749
}
 
750
 
 
751
/*!
 
752
    \fn bool QPalette::operator!=(const QPalette &p) const
 
753
 
 
754
    Returns true (slowly) if this palette is different from \a p;
 
755
    otherwise returns false (usually quickly).
 
756
*/
 
757
 
 
758
/*!
 
759
    Returns true (usually quickly) if this palette is equal to \a p;
 
760
    otherwise returns false (slowly).
 
761
*/
 
762
bool QPalette::operator==(const QPalette &p) const
 
763
{
 
764
    if (isCopyOf(p))
 
765
        return true;
 
766
    for(int grp = 0; grp < (int)NColorGroups; grp++) {
 
767
        for(int role = 0; role < (int)NColorRoles; role++) {
 
768
            if(d->br[grp][role] != p.d->br[grp][role])
 
769
                return false;
 
770
        }
 
771
    }
 
772
    return true;
 
773
}
 
774
 
 
775
#ifdef QT3_SUPPORT
 
776
bool QColorGroup::operator==(const QColorGroup &other) const
 
777
{
 
778
    if (isCopyOf(other))
 
779
        return true;
 
780
    for (int role = 0; role < int(NColorRoles); role++) {
 
781
        if(d->br[current_group][role] != other.d->br[other.current_group][role])
 
782
            return false;
 
783
    }
 
784
    return true;
 
785
}
 
786
 
 
787
/*!
 
788
   Returns the color group as a QVariant
 
789
*/
 
790
QColorGroup::operator QVariant() const
 
791
{
 
792
    return QVariant(QVariant::ColorGroup, this);
 
793
}
 
794
#endif
 
795
 
 
796
/*!
 
797
    \fn bool QPalette::isEqual(ColorGroup cg1, ColorGroup cg2) const
 
798
 
 
799
    Returns true (usually quickly) if color group \a cg1 is equal to
 
800
    \a cg2; otherwise returns false.
 
801
*/
 
802
bool QPalette::isEqual(QPalette::ColorGroup group1, QPalette::ColorGroup group2) const
 
803
{
 
804
    if(group1 >= (int)NColorGroups) {
 
805
        if(group1 == Current) {
 
806
            group1 = (ColorGroup)current_group;
 
807
        } else {
 
808
            qWarning("QPalette::brush: Unknown ColorGroup(1): %d", (int)group1);
 
809
            group1 = Active;
 
810
        }
 
811
    }
 
812
    if(group2 >= (int)NColorGroups) {
 
813
        if(group2 == Current) {
 
814
            group2 = (ColorGroup)current_group;
 
815
        } else {
 
816
            qWarning("QPalette::brush: Unknown ColorGroup(2): %d", (int)group2);
 
817
            group2 = Active;
 
818
        }
 
819
    }
 
820
    if(group1 == group2)
 
821
        return true;
 
822
    for(int role = 0; role < (int)NColorRoles; role++) {
 
823
        if(d->br[group1][role] != d->br[group2][role])
 
824
                return false;
 
825
    }
 
826
    return true;
 
827
}
 
828
 
 
829
/*!
 
830
    \fn int QPalette::serialNumber() const
 
831
 
 
832
    Returns a number that uniquely identifies this QPalette object.
 
833
    The serial number is intended for caching. Its value may not be
 
834
    used for anything other than equality testing.
 
835
 
 
836
    Note that QPalette uses implicit sharing, and the serial number changes
 
837
    during the lazy copy operation (when the palette is actually modified), not
 
838
    during a shallow copy (copy constructor or assignment).
 
839
 
 
840
    \sa QPixmap QPixmapCache QCache
 
841
*/
 
842
int QPalette::serialNumber() const
 
843
{
 
844
    return d->ser_no;
 
845
}
 
846
 
 
847
/*!
 
848
    Returns a new QPalette that has attributes copied from \a other.
 
849
*/
 
850
QPalette QPalette::resolve(const QPalette &other) const
 
851
{
 
852
    if (*this == other && resolve_mask == other.resolve_mask
 
853
        || resolve_mask == 0) {
 
854
        QPalette o = other;
 
855
        o.resolve_mask = resolve_mask;
 
856
        return o;
 
857
    }
 
858
 
 
859
    QPalette palette(*this);
 
860
    palette.detach();
 
861
 
 
862
    for(int role = 0; role < (int)NColorRoles; role++)
 
863
        if (!(resolve_mask & (1<<role)))
 
864
            for(int grp = 0; grp < (int)NColorGroups; grp++)
 
865
                palette.d->br[grp][role] = other.d->br[grp][role];
 
866
 
 
867
    return palette;
 
868
}
 
869
 
 
870
/*!
 
871
    \fn uint QPalette::resolve() const
 
872
    \internal
 
873
*/
 
874
 
 
875
/*!
 
876
    \fn void QPalette::resolve(uint mask)
 
877
    \internal
 
878
*/
 
879
 
 
880
 
 
881
/*****************************************************************************
 
882
  QPalette stream functions
 
883
 *****************************************************************************/
 
884
 
 
885
#ifndef QT_NO_DATASTREAM
 
886
 
 
887
static const int NumOldRoles = 7;
 
888
static const int oldRoles[7] = { QPalette::Foreground, QPalette::Background, QPalette::Light,
 
889
                                 QPalette::Dark, QPalette::Mid, QPalette::Text, QPalette::Base };
 
890
 
 
891
/*!
 
892
    \relates QPalette
 
893
 
 
894
    Writes the palette, \a p to the stream \a s and returns a
 
895
    reference to the stream.
 
896
 
 
897
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
898
*/
 
899
 
 
900
QDataStream &operator<<(QDataStream &s, const QPalette &p)
 
901
{
 
902
    for (int grp = 0; grp < (int)QPalette::NColorGroups; grp++) {
 
903
        if (s.version() == 1) {
 
904
            // Qt 1.x
 
905
            for (int i = 0; i < NumOldRoles; ++i)
 
906
                s << p.d->br[grp][oldRoles[i]].color();
 
907
        } else {
 
908
            int max = QPalette::NColorRoles;
 
909
            if (s.version() <= 3) // Qt 2.x
 
910
                max = 14;
 
911
            for (int r = 0; r < max; r++)
 
912
                s << p.d->br[grp][r];
 
913
        }
 
914
    }
 
915
    return s;
 
916
}
 
917
 
 
918
static void readV1ColorGroup(QDataStream &s, QPalette &pal, QPalette::ColorGroup grp)
 
919
{
 
920
    for (int i = 0; i < NumOldRoles; ++i) {
 
921
        QColor col;
 
922
        s >> col;
 
923
        pal.setColor(grp, (QPalette::ColorRole)oldRoles[i], col);
 
924
    }
 
925
}
 
926
 
 
927
/*!
 
928
    \relates QPalette
 
929
 
 
930
    Reads a palette from the stream, \a s into the palette \a p, and
 
931
    returns a reference to the stream.
 
932
 
 
933
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
934
*/
 
935
 
 
936
QDataStream &operator>>(QDataStream &s, QPalette &p)
 
937
{
 
938
    if(s.version() == 1) {
 
939
        p = QPalette();
 
940
        readV1ColorGroup(s, p, QPalette::Active);
 
941
        readV1ColorGroup(s, p, QPalette::Disabled);
 
942
        readV1ColorGroup(s, p, QPalette::Inactive);
 
943
    } else {
 
944
        int max = QPalette::NColorRoles;
 
945
        if (s.version() <= 3) { // Qt 2.x
 
946
            p = QPalette();
 
947
            max = 14;
 
948
        }
 
949
 
 
950
        QBrush tmp;
 
951
        for(int grp = 0; grp < (int)QPalette::NColorGroups; ++grp) {
 
952
            for(int role = 0; role < max; ++role) {
 
953
                s >> tmp;
 
954
                p.setBrush((QPalette::ColorGroup)grp, (QPalette::ColorRole)role, tmp);
 
955
            }
 
956
        }
 
957
    }
 
958
    return s;
 
959
}
 
960
#endif //QT_NO_DATASTREAM
 
961
 
 
962
/*!
 
963
    Returns true if this palette and \a p are copies of each other,
 
964
    i.e. one of them was created as a copy of the other and neither
 
965
    was subsequently modified; otherwise returns false. This is much
 
966
    stricter than equality.
 
967
 
 
968
    \sa operator=() operator==()
 
969
*/
 
970
 
 
971
bool QPalette::isCopyOf(const QPalette &p) const
 
972
{
 
973
    return d == p.d;
 
974
}
 
975
 
 
976
/*!
 
977
 
 
978
    Sets a the group at \a cg. You can pass either brushes, pixmaps or
 
979
    plain colors for \a foreground, \a button, \a light, \a dark, \a
 
980
    mid, \a text, \a bright_text, \a base and \a background.
 
981
 
 
982
    \sa QBrush
 
983
*/
 
984
void QPalette::setColorGroup(ColorGroup cg, const QBrush &foreground, const QBrush &button,
 
985
                             const QBrush &light, const QBrush &dark, const QBrush &mid,
 
986
                             const QBrush &text, const QBrush &bright_text, const QBrush &base,
 
987
                             const QBrush &background)
 
988
{
 
989
    QBrush mid_light = QBrush(qt_mix_colors(button.color(), light.color()));
 
990
    setColorGroup(cg, foreground, button, light, dark, mid, text, bright_text, base,
 
991
                  mid_light, background, mid_light, text,
 
992
                  QBrush(Qt::black), QBrush(Qt::darkBlue), QBrush(Qt::white),
 
993
                  QBrush(Qt::blue), QBrush(Qt::magenta));
 
994
 
 
995
    resolve_mask &= ~(1 << Highlight);
 
996
    resolve_mask &= ~(1 << HighlightedText);
 
997
    resolve_mask &= ~(1 << LinkVisited);
 
998
    resolve_mask &= ~(1 << Link);
 
999
}
 
1000
 
 
1001
 
 
1002
/*!\internal*/
 
1003
void
 
1004
QPalette::setColorGroup(ColorGroup cg, const QBrush &foreground, const QBrush &button,
 
1005
                        const QBrush &light, const QBrush &dark, const QBrush &mid,
 
1006
                        const QBrush &text, const QBrush &bright_text,
 
1007
                        const QBrush &base, const QBrush &alternate_base,
 
1008
                        const QBrush &background, const QBrush &midlight,
 
1009
                        const QBrush &button_text, const QBrush &shadow,
 
1010
                        const QBrush &highlight, const QBrush &highlighted_text,
 
1011
                        const QBrush &link, const QBrush &link_visited)
 
1012
{
 
1013
    detach();
 
1014
    setBrush(cg, Foreground, foreground);
 
1015
    setBrush(cg, Button, button);
 
1016
    setBrush(cg, Light, light);
 
1017
    setBrush(cg, Dark, dark);
 
1018
    setBrush(cg, Mid, mid);
 
1019
    setBrush(cg, Text, text);
 
1020
    setBrush(cg, BrightText, bright_text);
 
1021
    setBrush(cg, Base, base);
 
1022
    setBrush(cg, AlternateBase, alternate_base);
 
1023
    setBrush(cg, Background, background);
 
1024
    setBrush(cg, Midlight, midlight);
 
1025
    setBrush(cg, ButtonText, button_text);
 
1026
    setBrush(cg, Shadow, shadow);
 
1027
    setBrush(cg, Highlight, highlight);
 
1028
    setBrush(cg, HighlightedText, highlighted_text);
 
1029
    setBrush(cg, Link, link);
 
1030
    setBrush(cg, LinkVisited, link_visited);
 
1031
}
 
1032
 
 
1033
/*!
 
1034
    \fn QPalette QPalette::copy() const
 
1035
 
 
1036
    Use simple assignment instead.
 
1037
*/
 
1038
 
 
1039
/*!
 
1040
    \fn QColorGroup QPalette::normal() const
 
1041
 
 
1042
    Use createColorGroup(Active) instead.
 
1043
*/
 
1044
 
 
1045
/*!
 
1046
    \fn void QPalette::setNormal(const QColorGroup &colorGroup)
 
1047
 
 
1048
    Use setColorGroup(Active, colorGroup) instead.
 
1049
*/
 
1050
 
 
1051
/*!
 
1052
    \fn QColorGroup QPalette::active() const
 
1053
*/
 
1054
 
 
1055
/*!
 
1056
    \fn QColorGroup QPalette::disabled() const
 
1057
*/
 
1058
 
 
1059
/*!
 
1060
    \fn QColorGroup QPalette::inactive() const
 
1061
*/
 
1062
 
 
1063
/*!
 
1064
    \fn void QPalette::setActive(const QColorGroup &colorGroup)
 
1065
 
 
1066
    Use setColorGroup(Active, colorGroup) instead.
 
1067
*/
 
1068
 
 
1069
/*!
 
1070
    \fn void QPalette::setDisabled(const QColorGroup &colorGroup)
 
1071
 
 
1072
    Use setColorGroup(Disabled, colorGroup) instead.
 
1073
*/
 
1074
 
 
1075
/*!
 
1076
    \fn void QPalette::setInactive(const QColorGroup &colorGroup)
 
1077
 
 
1078
    Use setColorGroup(Inactive, colorGroup) instead.
 
1079
*/
 
1080
 
 
1081
/*! \class QColorGroup
 
1082
    \compat
 
1083
*/
 
1084
 
 
1085
/*! \fn QColorGroup::QColorGroup()
 
1086
 
 
1087
    Use QPalette() instead.
 
1088
*/
 
1089
 
 
1090
/*! \fn QColorGroup::QColorGroup(const QBrush &foreground, const QBrush &button, \
 
1091
                                 const QBrush &light, const QBrush &dark, const QBrush &mid, \
 
1092
                                 const QBrush &text, const QBrush &bright_text,
 
1093
                                 const QBrush &base, const QBrush &background)
 
1094
 
 
1095
    Use QPalette(\a foreground, \a button, \a light, \a dark, \a mid,
 
1096
    \a text, \a bright_text, \a base, \a background) instead.
 
1097
*/
 
1098
 
 
1099
/*! \fn QColorGroup::QColorGroup(const QColor &foreground, const QColor &background, \
 
1100
                                 const QColor &light, const QColor &dark, const QColor &mid, \
 
1101
                                 const QColor &text, const QColor &base)
 
1102
 
 
1103
    Use QColorGroup(\a foreground, \a background, \a light, \a dark,
 
1104
    \a mid, \a text, \a base) instead.
 
1105
*/
 
1106
 
 
1107
/*! \fn QColorGroup::QColorGroup(const QColorGroup &other)
 
1108
 
 
1109
    Use QPalette(\a other) instead.
 
1110
*/
 
1111
 
 
1112
/*! \fn QColorGroup::QColorGroup(const QPalette &pal)
 
1113
 
 
1114
    Use QPalette(\a pal) instead.
 
1115
*/
 
1116
 
 
1117
/*! \fn const QColor &QColorGroup::foreground() const
 
1118
 
 
1119
    Use QPalette::foreground().color() instead.
 
1120
*/
 
1121
 
 
1122
/*! \fn const QColor &QColorGroup::button() const
 
1123
 
 
1124
    Use QPalette::button().color() instead.
 
1125
*/
 
1126
 
 
1127
/*! \fn const QColor &QColorGroup::light() const
 
1128
 
 
1129
    Use QPalette::light().color() instead.
 
1130
*/
 
1131
 
 
1132
/*! \fn const QColor &QColorGroup::dark() const
 
1133
 
 
1134
    Use QPalette::dark().color() instead.
 
1135
*/
 
1136
 
 
1137
/*! \fn const QColor &QColorGroup::mid() const
 
1138
 
 
1139
    Use QPalette::mid().color() instead.
 
1140
*/
 
1141
 
 
1142
/*! \fn const QColor &QColorGroup::text() const
 
1143
 
 
1144
    Use QPalette::text().color() instead.
 
1145
*/
 
1146
 
 
1147
/*! \fn const QColor &QColorGroup::base() const
 
1148
 
 
1149
    Use QPalette::base().color() instead.
 
1150
*/
 
1151
 
 
1152
/*! \fn const QColor &QColorGroup::background() const
 
1153
 
 
1154
    Use QPalette::background().color() instead.
 
1155
*/
 
1156
 
 
1157
/*! \fn const QColor &QColorGroup::midlight() const
 
1158
 
 
1159
    Use QPalette::midlight().color() instead.
 
1160
*/
 
1161
 
 
1162
/*! \fn const QColor &QColorGroup::brightText() const
 
1163
 
 
1164
    Use QPalette::brightText().color() instead.
 
1165
*/
 
1166
 
 
1167
/*! \fn const QColor &QColorGroup::buttonText() const
 
1168
 
 
1169
    Use QPalette::buttonText().color() instead.
 
1170
*/
 
1171
 
 
1172
/*! \fn const QColor &QColorGroup::shadow() const
 
1173
 
 
1174
    Use QPalette::shadow().color() instead.
 
1175
*/
 
1176
 
 
1177
/*! \fn const QColor &QColorGroup::highlight() const
 
1178
 
 
1179
    Use QPalette::highlight().color() instead.
 
1180
*/
 
1181
 
 
1182
/*! \fn const QColor &QColorGroup::highlightedText() const
 
1183
 
 
1184
    Use QPalette::highlightedText().color() instead.
 
1185
*/
 
1186
 
 
1187
/*! \fn const QColor &QColorGroup::link() const
 
1188
 
 
1189
    Use QPalette::link().color() instead.
 
1190
*/
 
1191
 
 
1192
/*! \fn const QColor &QColorGroup::linkVisited() const
 
1193
 
 
1194
    Use QPalette::linkVisited().color() instead.
 
1195
*/
 
1196
 
 
1197
/*! \fn QDataStream &operator<<(QDataStream &ds, const QColorGroup &colorGroup)
 
1198
    \relates QColorGroup
 
1199
    \compat
 
1200
*/
 
1201
 
 
1202
/*! \fn QDataStream &operator>>(QDataStream &ds, QColorGroup &colorGroup)
 
1203
    \relates QColorGroup
 
1204
    \compat
 
1205
*/
 
1206
 
 
1207
/*! \fn bool QColorGroup::operator==(const QColorGroup &other) const
 
1208
 
 
1209
    Returns true if this color group is equal to \a other; otherwise
 
1210
    returns false.
 
1211
*/
 
1212
 
 
1213
/*! \fn bool QColorGroup::operator!=(const QColorGroup &other) const
 
1214
 
 
1215
    Returns true if this color group is not equal to \a other;
 
1216
    otherwise returns false.
 
1217
*/
 
1218
 
 
1219
#endif // QT_NO_PALETTE