source: trunk/src/gui/kernel/qpalette.cpp@ 467

Last change on this file since 467 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 39.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qpalette.h"
43#include "qapplication.h"
44#include "qdatastream.h"
45#include "qvariant.h"
46
47QT_BEGIN_NAMESPACE
48
49static int qt_palette_count = 1;
50
51class QPalettePrivate {
52public:
53 QPalettePrivate() : ref(1), ser_no(qt_palette_count++), detach_no(0) { }
54 QAtomicInt ref;
55 QBrush br[QPalette::NColorGroups][QPalette::NColorRoles];
56 int ser_no;
57 int detach_no;
58};
59
60static QColor qt_mix_colors(QColor a, QColor b)
61{
62 return QColor((a.red() + b.red()) / 2, (a.green() + b.green()) / 2,
63 (a.blue() + b.blue()) / 2, (a.alpha() + b.alpha()) / 2);
64}
65
66#ifdef QT3_SUPPORT
67
68#ifndef QT_NO_DATASTREAM
69QDataStream &qt_stream_out_qcolorgroup(QDataStream &s, const QColorGroup &g)
70{
71 if(s.version() == 1) {
72 // Qt 1.x
73 s << g.color(QPalette::Foreground) << g.color(QPalette::Background)
74 << g.color(QPalette::Light) << g.color(QPalette::Dark)
75 << g.color(QPalette::Mid) << g.color(QPalette::Text) << g.color(QPalette::Base);
76 } else {
77 int max = QPalette::NColorRoles;
78 if (s.version() <= QDataStream::Qt_2_1)
79 max = QPalette::HighlightedText + 1;
80 else if (s.version() <= QDataStream::Qt_4_3)
81 max = QPalette::AlternateBase + 1;
82 for(int r = 0 ; r < max ; r++)
83 s << g.brush((QPalette::ColorRole)r);
84 }
85 return s;
86}
87
88QDataStream &qt_stream_in_qcolorgroup(QDataStream &s, QColorGroup &g)
89{
90 if(s.version() == 1) { // Qt 1.x
91 QColor fg, bg, light, dark, mid, text, base;
92 s >> fg >> bg >> light >> dark >> mid >> text >> base;
93 QPalette p(bg);
94 p.setColor(QPalette::Active, QPalette::Foreground, fg);
95 p.setColor(QPalette::Active, QPalette::Light, light);
96 p.setColor(QPalette::Active, QPalette::Dark, dark);
97 p.setColor(QPalette::Active, QPalette::Mid, mid);
98 p.setColor(QPalette::Active, QPalette::Text, text);
99 p.setColor(QPalette::Active, QPalette::Base, base);
100 g = p;
101 g.setCurrentColorGroup(QPalette::Active);
102 } else {
103 int max = QPalette::NColorRoles;
104 if (s.version() <= QDataStream::Qt_2_1)
105 max = QPalette::HighlightedText + 1;
106 else if (s.version() <= QDataStream::Qt_3_0)
107 max = QPalette::LinkVisited + 1;
108 else if (s.version() <= QDataStream::Qt_4_3)
109 max = QPalette::AlternateBase + 1;
110 QBrush tmp;
111 for(int r = 0 ; r < max; r++) {
112 s >> tmp;
113 g.setBrush((QPalette::ColorRole)r, tmp);
114 }
115 }
116 return s;
117}
118
119QDataStream &operator<<(QDataStream &s, const QColorGroup &g)
120{
121 return qt_stream_out_qcolorgroup(s, g);
122}
123
124QDataStream &operator>>(QDataStream &s, QColorGroup &g)
125{
126 return qt_stream_in_qcolorgroup(s, g);
127}
128#endif
129
130/*!
131 Constructs a palette with the specified \a active, \a disabled and
132 \a inactive color groups.
133*/
134QPalette::QPalette(const QColorGroup &active, const QColorGroup &disabled,
135 const QColorGroup &inactive)
136{
137 Q_ASSERT(QPalette::NColorRoles == QPalette::ToolTipText + 1);
138 init();
139 setColorGroup(Active, active);
140 setColorGroup(Disabled, disabled);
141 setColorGroup(Inactive, inactive);
142}
143
144QColorGroup QPalette::createColorGroup(ColorGroup cr) const
145{
146 QColorGroup ret(*this);
147 ret.setCurrentColorGroup(cr);
148 return ret;
149}
150
151void QPalette::setColorGroup(ColorGroup cg, const QColorGroup &g)
152{
153 setColorGroup(cg, g.brush(WindowText), g.brush(Button), g.brush(Light),
154 g.brush(Dark), g.brush(Mid), g.brush(Text), g.brush(BrightText),
155 g.brush(Base), g.brush(AlternateBase), g.brush(Window),
156 g.brush(Midlight), g.brush(ButtonText), g.brush(Shadow),
157 g.brush(Highlight), g.brush(HighlightedText), g.brush(Link),
158 g.brush(LinkVisited), g.brush(ToolTipBase), g.brush(ToolTipText));
159}
160
161#endif
162
163/*!
164 \fn const QColor &QPalette::color(ColorRole role) const
165
166 \overload
167
168 Returns the color that has been set for the given color \a role in
169 the current ColorGroup.
170
171 \sa brush() ColorRole
172 */
173
174/*!
175 \fn const QBrush &QPalette::brush(ColorRole role) const
176
177 \overload
178
179 Returns the brush that has been set for the given color \a role in
180 the current ColorGroup.
181
182 \sa color() setBrush() ColorRole
183*/
184
185/*!
186 \fn void QPalette::setColor(ColorRole role, const QColor &color)
187
188 \overload
189
190 Sets the color used for the given color \a role, in all color
191 groups, to the specified solid \a color.
192
193 \sa brush() setColor() ColorRole
194*/
195
196/*!
197 \fn void QPalette::setBrush(ColorRole role, const QBrush &brush)
198
199 Sets the brush for the given color \a role to the specified \a
200 brush for all groups in the palette.
201
202 \sa brush() setColor() ColorRole
203*/
204
205/*!
206 \fn const QBrush & QPalette::foreground() const
207 \obsolete
208
209 Use windowText() instead.
210*/
211
212/*!
213 \fn const QBrush & QPalette::windowText() const
214
215 Returns the window text (general foreground) brush of the
216 current color group.
217
218 \sa ColorRole brush()
219*/
220
221/*!
222 \fn const QBrush & QPalette::button() const
223
224 Returns the button brush of the current color group.
225
226 \sa ColorRole brush()
227*/
228
229/*!
230 \fn const QBrush & QPalette::light() const
231
232 Returns the light brush of the current color group.
233
234 \sa ColorRole brush()
235*/
236
237/*!
238 \fn const QBrush& QPalette::midlight() const
239
240 Returns the midlight brush of the current color group.
241
242 \sa ColorRole brush()
243*/
244
245/*!
246 \fn const QBrush & QPalette::dark() const
247
248 Returns the dark brush of the current color group.
249
250 \sa ColorRole brush()
251*/
252
253/*!
254 \fn const QBrush & QPalette::mid() const
255
256 Returns the mid brush of the current color group.
257
258 \sa ColorRole brush()
259*/
260
261/*!
262 \fn const QBrush & QPalette::text() const
263
264 Returns the text foreground brush of the current color group.
265
266 \sa ColorRole brush()
267*/
268
269/*!
270 \fn const QBrush & QPalette::brightText() const
271
272 Returns the bright text foreground brush of the current color group.
273
274 \sa ColorRole brush()
275*/
276
277/*!
278 \fn const QBrush & QPalette::buttonText() const
279
280 Returns the button text foreground brush of the current color group.
281
282 \sa ColorRole brush()
283*/
284
285/*!
286 \fn const QBrush & QPalette::base() const
287
288 Returns the base brush of the current color group.
289
290 \sa ColorRole brush()
291*/
292
293/*!
294 \fn const QBrush & QPalette::alternateBase() const
295
296 Returns the alternate base brush of the current color group.
297
298 \sa ColorRole brush()
299*/
300
301/*!
302 \fn const QBrush & QPalette::toolTipBase() const
303 \since 4.4
304
305 Returns the tool tip base brush of the current color group. This brush is
306 used by QToolTip and QWhatsThis.
307
308 \note Tool tips use the Inactive color group of QPalette, because tool
309 tips are not active windows.
310
311 \sa ColorRole brush()
312*/
313
314/*!
315 \fn const QBrush & QPalette::toolTipText() const
316 \since 4.4
317
318 Returns the tool tip text brush of the current color group. This brush is
319 used by QToolTip and QWhatsThis.
320
321 \note Tool tips use the Inactive color group of QPalette, because tool
322 tips are not active windows.
323
324 \sa ColorRole brush()
325*/
326
327/*!
328 \fn const QBrush & QPalette::background() const
329 \obsolete
330
331 Use window() instead.
332*/
333
334/*!
335 \fn const QBrush & QPalette::window() const
336
337 Returns the window (general background) brush of the current
338 color group.
339
340 \sa ColorRole brush()
341*/
342
343/*!
344 \fn const QBrush & QPalette::shadow() const
345
346 Returns the shadow brush of the current color group.
347
348 \sa ColorRole brush()
349*/
350
351/*!
352 \fn const QBrush & QPalette::highlight() const
353
354 Returns the highlight brush of the current color group.
355
356 \sa ColorRole brush()
357*/
358
359/*!
360 \fn const QBrush & QPalette::highlightedText() const
361
362 Returns the highlighted text brush of the current color group.
363
364 \sa ColorRole brush()
365*/
366
367/*!
368 \fn const QBrush & QPalette::link() const
369
370 Returns the unvisited link text brush of the current color group.
371
372 \sa ColorRole brush()
373*/
374
375/*!
376 \fn const QBrush & QPalette::linkVisited() const
377
378 Returns the visited link text brush of the current color group.
379
380 \sa ColorRole brush()
381*/
382
383/*!
384 \fn ColorGroup QPalette::currentColorGroup() const
385
386 Returns the palette's current color group.
387*/
388
389/*!
390 \fn void QPalette::setCurrentColorGroup(ColorGroup cg)
391
392 Set the palette's current color group to \a cg.
393*/
394
395/*!
396 \class QPalette
397
398 \brief The QPalette class contains color groups for each widget state.
399
400 \ingroup appearance
401 \ingroup shared
402 \ingroup multimedia
403 \mainclass
404
405 A palette consists of three color groups: \e Active, \e Disabled,
406 and \e Inactive. All widgets in Qt contain a palette and
407 use their palette to draw themselves. This makes the user
408 interface easily configurable and easier to keep consistent.
409
410
411 If you create a new widget we strongly recommend that you use the
412 colors in the palette rather than hard-coding specific colors.
413
414 The color groups:
415 \list
416 \i The Active group is used for the window that has keyboard focus.
417 \i The Inactive group is used for other windows.
418 \i The Disabled group is used for widgets (not windows) that are
419 disabled for some reason.
420 \endlist
421
422 Both active and inactive windows can contain disabled widgets.
423 (Disabled widgets are often called \e inaccessible or \e{grayed
424 out}.)
425
426 In most styles, Active and Inactive look the same.
427
428 Colors and brushes can be set for particular roles in any of a palette's
429 color groups with setColor() and setBrush(). A color group contains a
430 group of colors used by widgets for drawing themselves. We recommend that
431 widgets use color group roles from the palette such as "foreground" and
432 "base" rather than literal colors like "red" or "turquoise". The color
433 roles are enumerated and defined in the \l ColorRole documentation.
434
435 We strongly recommend that you use the default palette of the
436 current style (returned by QApplication::palette()) and
437 modify that as necessary. This is done by Qt's widgets when they
438 are drawn.
439
440 To modify a color group you call the functions
441 setColor() and setBrush(), depending on whether you want a pure
442 color or a pixmap pattern.
443
444 There are also corresponding color() and brush() getters, and a
445 commonly used convenience function to get the ColorRole for the current ColorGroup:
446 window(), windowText(), base(), etc.
447
448
449 You can copy a palette using the copy constructor and test to see
450 if two palettes are \e identical using isCopyOf().
451
452 QPalette is optimized by the use of \l{implicit sharing},
453 so it is very efficient to pass QPalette objects as arguments.
454
455 \warning Some styles do not use the palette for all drawing, for
456 instance, if they make use of native theme engines. This is the
457 case for both the Windows XP, Windows Vista, and the Mac OS X
458 styles.
459
460 \sa QApplication::setPalette(), QWidget::setPalette(), QColor
461*/
462
463/*!
464 \enum QPalette::ColorGroup
465
466 \value Disabled
467 \value Active
468 \value Inactive
469 \value Normal synonym for Active
470
471 \omitvalue All
472 \omitvalue NColorGroups
473 \omitvalue Current
474*/
475
476/*!
477 \enum QPalette::ColorRole
478
479 \img palette.png Color Roles
480
481 The ColorRole enum defines the different symbolic color roles used
482 in current GUIs.
483
484 The central roles are:
485
486 \value Window A general background color.
487
488 \value Background This value is obsolete. Use Window instead.
489
490 \value WindowText A general foreground color.
491
492 \value Foreground This value is obsolete. Use WindowText instead.
493
494 \value Base Used mostly as the background color for text entry widgets,
495 but can also be used for other painting - such as the
496 background of combobox drop down lists and toolbar handles.
497 It is usually white or another light color.
498
499 \value AlternateBase Used as the alternate background color in views with
500 alternating row colors (see
501 QAbstractItemView::setAlternatingRowColors()).
502
503 \value ToolTipBase Used as the background color for QToolTip and
504 QWhatsThis. Tool tips use the Inactive color group
505 of QPalette, because tool tips are not active
506 windows.
507
508 \value ToolTipText Used as the foreground color for QToolTip and
509 QWhatsThis. Tool tips use the Inactive color group
510 of QPalette, because tool tips are not active
511 windows.
512
513 \value Text The foreground color used with \c Base. This is usually
514 the same as the \c WindowText, in which case it must provide
515 good contrast with \c Window and \c Base.
516
517 \value Button The general button background color. This background can be different from
518 \c Window as some styles require a different background color for buttons.
519
520 \value ButtonText A foreground color used with the \c Button color.
521
522 \value BrightText A text color that is very different from
523 \c WindowText, and contrasts well with e.g. \c
524 Dark. Typically used for text that needs to be
525 drawn where \c Text or \c WindowText would give
526 poor contrast, such as on pressed push buttons.
527 Note that text colors can be used for things
528 other than just words; text colors are \e
529 usually used for text, but it's quite common to
530 use the text color roles for lines, icons, etc.
531
532
533 There are some color roles used mostly for 3D bevel and shadow effects.
534 All of these are normally derived from \c Window, and used in ways that
535 depend on that relationship. For example, buttons depend on it to make the
536 bevels look attractive, and Motif scroll bars depend on \c Mid to be
537 slightly different from \c Window.
538
539 \value Light Lighter than \c Button color.
540
541 \value Midlight Between \c Button and \c Light.
542
543 \value Dark Darker than \c Button.
544
545 \value Mid Between \c Button and \c Dark.
546
547 \value Shadow A very dark color. By default, the shadow color is
548 Qt::black.
549
550
551 Selected (marked) items have two roles:
552
553 \value Highlight A color to indicate a selected item or the current
554 item. By default, the highlight color is
555 Qt::darkBlue.
556
557 \value HighlightedText A text color that contrasts with \c Highlight.
558 By default, the highlighted text color is Qt::white.
559
560 There are two color roles related to hyperlinks:
561
562 \value Link A text color used for unvisited hyperlinks.
563 By default, the link color is Qt::blue.
564
565 \value LinkVisited A text color used for already visited hyperlinks.
566 By default, the linkvisited color is Qt::magenta.
567
568 Note that we do not use the \c Link and \c LinkVisited roles when
569 rendering rich text in Qt, and that we recommend that you use CSS
570 and the QTextDocument::setDefaultStyleSheet() function to alter
571 the appearance of links. For example:
572
573 \snippet doc/src/snippets/textdocument-css/main.cpp 0
574
575 \value NoRole No role; this special role is often used to indicate that a
576 role has not been assigned.
577
578 \omitvalue NColorRoles
579*/
580
581/*!
582 Constructs a palette object that uses the application's default palette.
583
584 \sa QApplication::setPalette(), QApplication::palette()
585*/
586QPalette::QPalette()
587 : d(QApplication::palette().d),
588 current_group(Active),
589 resolve_mask(0)
590{
591 d->ref.ref();
592}
593
594static void qt_palette_from_color(QPalette &pal, const QColor & button)
595{
596 QColor bg = button,
597 btn = button,
598 fg, base;
599 int h, s, v;
600 bg.getHsv(&h, &s, &v);
601 if(v > 128) {
602 fg = Qt::black;
603 base = Qt::white;
604 } else {
605 fg = Qt::white;
606 base = Qt::black;
607 }
608 //inactive and active are the same..
609 pal.setColorGroup(QPalette::Active, QBrush(fg), QBrush(btn), QBrush(btn.lighter(150)),
610 QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(fg), QBrush(Qt::white),
611 QBrush(base), QBrush(bg));
612 pal.setColorGroup(QPalette::Inactive, QBrush(fg), QBrush(btn), QBrush(btn.lighter(150)),
613 QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(fg), QBrush(Qt::white),
614 QBrush(base), QBrush(bg));
615 pal.setColorGroup(QPalette::Disabled, QBrush(btn.darker()), QBrush(btn), QBrush(btn.lighter(150)),
616 QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(btn.darker()),
617 QBrush(Qt::white), QBrush(bg), QBrush(bg));
618}
619
620
621/*!
622 Constructs a palette from the \a button color. The other colors are
623 automatically calculated, based on this color. \c Window will be
624 the button color as well.
625*/
626QPalette::QPalette(const QColor &button)
627{
628 init();
629 qt_palette_from_color(*this, button);
630}
631
632/*!
633 Constructs a palette from the \a button color. The other colors are
634 automatically calculated, based on this color. \c Window will be
635 the button color as well.
636*/
637QPalette::QPalette(Qt::GlobalColor button)
638{
639 init();
640 qt_palette_from_color(*this, button);
641}
642
643/*!
644 Constructs a palette. You can pass either brushes, pixmaps or
645 plain colors for \a windowText, \a button, \a light, \a dark, \a
646 mid, \a text, \a bright_text, \a base and \a window.
647
648 \sa QBrush
649*/
650QPalette::QPalette(const QBrush &windowText, const QBrush &button,
651 const QBrush &light, const QBrush &dark,
652 const QBrush &mid, const QBrush &text,
653 const QBrush &bright_text, const QBrush &base,
654 const QBrush &window)
655{
656 init();
657 setColorGroup(All, windowText, button, light, dark, mid, text, bright_text,
658 base, window);
659}
660
661
662/*!\obsolete
663
664 Constructs a palette with the specified \a windowText, \a
665 window, \a light, \a dark, \a mid, \a text, and \a base colors.
666 The button color will be set to the window color.
667*/
668QPalette::QPalette(const QColor &windowText, const QColor &window,
669 const QColor &light, const QColor &dark, const QColor &mid,
670 const QColor &text, const QColor &base)
671{
672 init();
673 setColorGroup(All, QBrush(windowText), QBrush(window), QBrush(light),
674 QBrush(dark), QBrush(mid), QBrush(text), QBrush(light),
675 QBrush(base), QBrush(window));
676}
677
678/*!
679 Constructs a palette from a \a button color and a \a window.
680 The other colors are automatically calculated, based on these
681 colors.
682*/
683QPalette::QPalette(const QColor &button, const QColor &window)
684{
685 init();
686 QColor bg = window, btn = button, fg, base, disfg;
687 int h, s, v;
688 bg.getHsv(&h, &s, &v);
689 if(v > 128) {
690 fg = Qt::black;
691 base = Qt::white;
692 disfg = Qt::darkGray;
693 } else {
694 fg = Qt::white;
695 base = Qt::black;
696 disfg = Qt::darkGray;
697 }
698 //inactive and active are identical
699 setColorGroup(Inactive, QBrush(fg), QBrush(btn), QBrush(btn.lighter(150)), QBrush(btn.darker()),
700 QBrush(btn.darker(150)), QBrush(fg), QBrush(Qt::white), QBrush(base),
701 QBrush(bg));
702 setColorGroup(Active, QBrush(fg), QBrush(btn), QBrush(btn.lighter(150)), QBrush(btn.darker()),
703 QBrush(btn.darker(150)), QBrush(fg), QBrush(Qt::white), QBrush(base),
704 QBrush(bg));
705 setColorGroup(Disabled, QBrush(disfg), QBrush(btn), QBrush(btn.lighter(150)),
706 QBrush(btn.darker()), QBrush(btn.darker(150)), QBrush(disfg),
707 QBrush(Qt::white), QBrush(base), QBrush(bg));
708}
709
710/*!
711 Constructs a copy of \a p.
712
713 This constructor is fast thanks to \l{implicit sharing}.
714*/
715QPalette::QPalette(const QPalette &p)
716{
717 d = p.d;
718 d->ref.ref();
719 resolve_mask = p.resolve_mask;
720 current_group = p.current_group;
721}
722
723/*!
724 Destroys the palette.
725*/
726QPalette::~QPalette()
727{
728 if(!d->ref.deref())
729 delete d;
730}
731
732/*!\internal*/
733void QPalette::init() {
734 d = new QPalettePrivate;
735 resolve_mask = 0;
736 current_group = Active; //as a default..
737}
738
739/*!
740 Assigns \a p to this palette and returns a reference to this
741 palette.
742
743 This operation is fast thanks to \l{implicit sharing}.
744*/
745QPalette &QPalette::operator=(const QPalette &p)
746{
747 p.d->ref.ref();
748 resolve_mask = p.resolve_mask;
749 current_group = p.current_group;
750 if(!d->ref.deref())
751 delete d;
752 d = p.d;
753 return *this;
754}
755
756/*!
757 Returns the palette as a QVariant
758*/
759QPalette::operator QVariant() const
760{
761 return QVariant(QVariant::Palette, this);
762}
763
764/*!
765 \fn const QColor &QPalette::color(ColorGroup group, ColorRole role) const
766
767 Returns the color in the specified color \a group, used for the
768 given color \a role.
769
770 \sa brush() setColor() ColorRole
771*/
772
773/*!
774 \fn const QBrush &QPalette::brush(ColorGroup group, ColorRole role) const
775
776 Returns the brush in the specified color \a group, used for the
777 given color \a role.
778
779 \sa color() setBrush() ColorRole
780*/
781const QBrush &QPalette::brush(ColorGroup gr, ColorRole cr) const
782{
783 Q_ASSERT(cr < NColorRoles);
784 if(gr >= (int)NColorGroups) {
785 if(gr == Current) {
786 gr = (ColorGroup)current_group;
787 } else {
788 qWarning("QPalette::brush: Unknown ColorGroup: %d", (int)gr);
789 gr = Active;
790 }
791 }
792 return d->br[gr][cr];
793}
794
795/*!
796 \fn void QPalette::setColor(ColorGroup group, ColorRole role, const QColor &color)
797
798 Sets the brush in the specified color \a group, used for the given
799 color \a role, to the specified solid \a color.
800
801 \sa setBrush() color() ColorRole
802*/
803
804/*!
805 \fn void QPalette::setBrush(ColorGroup group, ColorRole role, const QBrush &brush)
806 \overload
807
808 Sets the brush in the specified color \a group, used for the given
809 color \a role, to \a brush.
810
811 \sa brush() setColor() ColorRole
812*/
813void QPalette::setBrush(ColorGroup cg, ColorRole cr, const QBrush &b)
814{
815 Q_ASSERT(cr < NColorRoles);
816 detach();
817 if(cg >= (int)NColorGroups) {
818 if(cg == All) {
819 for(int i = 0; i < (int)NColorGroups; i++)
820 d->br[i][cr] = b;
821 resolve_mask |= (1<<cr);
822 return;
823 } else if(cg == Current) {
824 cg = (ColorGroup)current_group;
825 } else {
826 qWarning("QPalette::setBrush: Unknown ColorGroup: %d", (int)cg);
827 cg = Active;
828 }
829 }
830 d->br[cg][cr] = b;
831 resolve_mask |= (1<<cr);
832}
833
834/*!
835 \since 4.2
836
837 Returns true if the ColorGroup \a cg and ColorRole \a cr has been
838 set previously on this palette; otherwise returns false.
839
840 \sa setBrush()
841*/
842bool QPalette::isBrushSet(ColorGroup cg, ColorRole cr) const
843{
844 Q_UNUSED(cg);
845 return (resolve_mask & (1<<cr));
846}
847
848/*!
849 \internal
850*/
851void QPalette::detach()
852{
853 if (d->ref != 1) {
854 QPalettePrivate *x = new QPalettePrivate;
855 for(int grp = 0; grp < (int)NColorGroups; grp++) {
856 for(int role = 0; role < (int)NColorRoles; role++)
857 x->br[grp][role] = d->br[grp][role];
858 }
859 if(!d->ref.deref())
860 delete d;
861 d = x;
862 }
863 ++d->detach_no;
864}
865
866/*!
867 \fn bool QPalette::operator!=(const QPalette &p) const
868
869 Returns true (slowly) if this palette is different from \a p;
870 otherwise returns false (usually quickly).
871*/
872
873/*!
874 Returns true (usually quickly) if this palette is equal to \a p;
875 otherwise returns false (slowly).
876*/
877bool QPalette::operator==(const QPalette &p) const
878{
879 if (isCopyOf(p))
880 return true;
881 for(int grp = 0; grp < (int)NColorGroups; grp++) {
882 for(int role = 0; role < (int)NColorRoles; role++) {
883 if(d->br[grp][role] != p.d->br[grp][role])
884 return false;
885 }
886 }
887 return true;
888}
889
890#ifdef QT3_SUPPORT
891bool QColorGroup::operator==(const QColorGroup &other) const
892{
893 if (isCopyOf(other))
894 return true;
895 for (int role = 0; role < int(NColorRoles); role++) {
896 if(d->br[current_group][role] != other.d->br[other.current_group][role])
897 return false;
898 }
899 return true;
900}
901
902/*!
903 Returns the color group as a QVariant
904*/
905QColorGroup::operator QVariant() const
906{
907 return QVariant(QVariant::ColorGroup, this);
908}
909#endif
910
911/*!
912 \fn bool QPalette::isEqual(ColorGroup cg1, ColorGroup cg2) const
913
914 Returns true (usually quickly) if color group \a cg1 is equal to
915 \a cg2; otherwise returns false.
916*/
917bool QPalette::isEqual(QPalette::ColorGroup group1, QPalette::ColorGroup group2) const
918{
919 if(group1 >= (int)NColorGroups) {
920 if(group1 == Current) {
921 group1 = (ColorGroup)current_group;
922 } else {
923 qWarning("QPalette::brush: Unknown ColorGroup(1): %d", (int)group1);
924 group1 = Active;
925 }
926 }
927 if(group2 >= (int)NColorGroups) {
928 if(group2 == Current) {
929 group2 = (ColorGroup)current_group;
930 } else {
931 qWarning("QPalette::brush: Unknown ColorGroup(2): %d", (int)group2);
932 group2 = Active;
933 }
934 }
935 if(group1 == group2)
936 return true;
937 for(int role = 0; role < (int)NColorRoles; role++) {
938 if(d->br[group1][role] != d->br[group2][role])
939 return false;
940 }
941 return true;
942}
943
944/*! \obsolete
945
946 Returns a number that identifies the contents of this QPalette
947 object. Distinct QPalette objects can only have the same serial
948 number if they refer to the same contents (but they don't have
949 to). Also, the serial number of a QPalette may change during the
950 lifetime of the object.
951
952 Use cacheKey() instead.
953
954 \warning The serial number doesn't necessarily change when the
955 palette is altered. This means that it may be dangerous to use it
956 as a cache key.
957
958 \sa operator==()
959*/
960int QPalette::serialNumber() const
961{
962 return d->ser_no;
963}
964
965/*!
966 Returns a number that identifies the contents of this QPalette
967 object. Distinct QPalette objects can have the same key if
968 they refer to the same contents.
969
970 The cacheKey() will change when the palette is altered.
971*/
972qint64 QPalette::cacheKey() const
973{
974 return (((qint64) d->ser_no) << 32) | ((qint64) (d->detach_no));
975}
976
977/*!
978 Returns a new QPalette that has attributes copied from \a other.
979*/
980QPalette QPalette::resolve(const QPalette &other) const
981{
982 if ((*this == other && resolve_mask == other.resolve_mask)
983 || resolve_mask == 0) {
984 QPalette o = other;
985 o.resolve_mask = resolve_mask;
986 return o;
987 }
988
989 QPalette palette(*this);
990 palette.detach();
991
992 for(int role = 0; role < (int)NColorRoles; role++)
993 if (!(resolve_mask & (1<<role)))
994 for(int grp = 0; grp < (int)NColorGroups; grp++)
995 palette.d->br[grp][role] = other.d->br[grp][role];
996
997 return palette;
998}
999
1000/*!
1001 \fn uint QPalette::resolve() const
1002 \internal
1003*/
1004
1005/*!
1006 \fn void QPalette::resolve(uint mask)
1007 \internal
1008*/
1009
1010
1011/*****************************************************************************
1012 QPalette stream functions
1013 *****************************************************************************/
1014
1015#ifndef QT_NO_DATASTREAM
1016
1017static const int NumOldRoles = 7;
1018static const int oldRoles[7] = { QPalette::Foreground, QPalette::Background, QPalette::Light,
1019 QPalette::Dark, QPalette::Mid, QPalette::Text, QPalette::Base };
1020
1021/*!
1022 \relates QPalette
1023
1024 Writes the palette, \a p to the stream \a s and returns a
1025 reference to the stream.
1026
1027 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1028*/
1029
1030QDataStream &operator<<(QDataStream &s, const QPalette &p)
1031{
1032 for (int grp = 0; grp < (int)QPalette::NColorGroups; grp++) {
1033 if (s.version() == 1) {
1034 // Qt 1.x
1035 for (int i = 0; i < NumOldRoles; ++i)
1036 s << p.d->br[grp][oldRoles[i]].color();
1037 } else {
1038 int max = QPalette::ToolTipText + 1;
1039 if (s.version() <= QDataStream::Qt_2_1)
1040 max = QPalette::HighlightedText + 1;
1041 else if (s.version() <= QDataStream::Qt_4_3)
1042 max = QPalette::AlternateBase + 1;
1043 for (int r = 0; r < max; r++)
1044 s << p.d->br[grp][r];
1045 }
1046 }
1047 return s;
1048}
1049
1050static void readV1ColorGroup(QDataStream &s, QPalette &pal, QPalette::ColorGroup grp)
1051{
1052 for (int i = 0; i < NumOldRoles; ++i) {
1053 QColor col;
1054 s >> col;
1055 pal.setColor(grp, (QPalette::ColorRole)oldRoles[i], col);
1056 }
1057}
1058
1059/*!
1060 \relates QPalette
1061
1062 Reads a palette from the stream, \a s into the palette \a p, and
1063 returns a reference to the stream.
1064
1065 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1066*/
1067
1068QDataStream &operator>>(QDataStream &s, QPalette &p)
1069{
1070 if(s.version() == 1) {
1071 p = QPalette();
1072 readV1ColorGroup(s, p, QPalette::Active);
1073 readV1ColorGroup(s, p, QPalette::Disabled);
1074 readV1ColorGroup(s, p, QPalette::Inactive);
1075 } else {
1076 int max = QPalette::NColorRoles;
1077 if (s.version() <= QDataStream::Qt_2_1) {
1078 p = QPalette();
1079 max = QPalette::HighlightedText + 1;
1080 } else if (s.version() <= QDataStream::Qt_4_3) {
1081 p = QPalette();
1082 max = QPalette::AlternateBase + 1;
1083 }
1084
1085 QBrush tmp;
1086 for(int grp = 0; grp < (int)QPalette::NColorGroups; ++grp) {
1087 for(int role = 0; role < max; ++role) {
1088 s >> tmp;
1089 p.setBrush((QPalette::ColorGroup)grp, (QPalette::ColorRole)role, tmp);
1090 }
1091 }
1092 }
1093 return s;
1094}
1095#endif //QT_NO_DATASTREAM
1096
1097/*!
1098 Returns true if this palette and \a p are copies of each other,
1099 i.e. one of them was created as a copy of the other and neither
1100 was subsequently modified; otherwise returns false. This is much
1101 stricter than equality.
1102
1103 \sa operator=() operator==()
1104*/
1105
1106bool QPalette::isCopyOf(const QPalette &p) const
1107{
1108 return d == p.d;
1109}
1110
1111/*!
1112
1113 Sets a the group at \a cg. You can pass either brushes, pixmaps or
1114 plain colors for \a windowText, \a button, \a light, \a dark, \a
1115 mid, \a text, \a bright_text, \a base and \a window.
1116
1117 \sa QBrush
1118*/
1119void QPalette::setColorGroup(ColorGroup cg, const QBrush &windowText, const QBrush &button,
1120 const QBrush &light, const QBrush &dark, const QBrush &mid,
1121 const QBrush &text, const QBrush &bright_text, const QBrush &base,
1122 const QBrush &window)
1123{
1124 QBrush alt_base = QBrush(qt_mix_colors(base.color(), button.color()));
1125 QBrush mid_light = QBrush(qt_mix_colors(button.color(), light.color()));
1126 QColor toolTipBase(255, 255, 220);
1127 QColor toolTipText(0, 0, 0);
1128
1129 setColorGroup(cg, windowText, button, light, dark, mid, text, bright_text, base,
1130 alt_base, window, mid_light, text,
1131 QBrush(Qt::black), QBrush(Qt::darkBlue), QBrush(Qt::white),
1132 QBrush(Qt::blue), QBrush(Qt::magenta), QBrush(toolTipBase),
1133 QBrush(toolTipText));
1134
1135 resolve_mask &= ~(1 << Highlight);
1136 resolve_mask &= ~(1 << HighlightedText);
1137 resolve_mask &= ~(1 << LinkVisited);
1138 resolve_mask &= ~(1 << Link);
1139}
1140
1141
1142/*!\internal*/
1143void
1144QPalette::setColorGroup(ColorGroup cg, const QBrush &foreground, const QBrush &button,
1145 const QBrush &light, const QBrush &dark, const QBrush &mid,
1146 const QBrush &text, const QBrush &bright_text,
1147 const QBrush &base, const QBrush &alternate_base,
1148 const QBrush &background, const QBrush &midlight,
1149 const QBrush &button_text, const QBrush &shadow,
1150 const QBrush &highlight, const QBrush &highlighted_text,
1151 const QBrush &link, const QBrush &link_visited)
1152{
1153 setColorGroup(cg, foreground, button, light, dark, mid,
1154 text, bright_text, base, alternate_base, background,
1155 midlight, button_text, shadow, highlight, highlighted_text,
1156 link, link_visited, background, foreground);
1157}
1158
1159/*!\internal*/
1160void QPalette::setColorGroup(ColorGroup cg, const QBrush &foreground, const QBrush &button,
1161 const QBrush &light, const QBrush &dark, const QBrush &mid,
1162 const QBrush &text, const QBrush &bright_text,
1163 const QBrush &base, const QBrush &alternate_base,
1164 const QBrush &background, const QBrush &midlight,
1165 const QBrush &button_text, const QBrush &shadow,
1166 const QBrush &highlight, const QBrush &highlighted_text,
1167 const QBrush &link, const QBrush &link_visited,
1168 const QBrush &toolTipBase, const QBrush &toolTipText)
1169{
1170 detach();
1171 setBrush(cg, WindowText, foreground);
1172 setBrush(cg, Button, button);
1173 setBrush(cg, Light, light);
1174 setBrush(cg, Dark, dark);
1175 setBrush(cg, Mid, mid);
1176 setBrush(cg, Text, text);
1177 setBrush(cg, BrightText, bright_text);
1178 setBrush(cg, Base, base);
1179 setBrush(cg, AlternateBase, alternate_base);
1180 setBrush(cg, Window, background);
1181 setBrush(cg, Midlight, midlight);
1182 setBrush(cg, ButtonText, button_text);
1183 setBrush(cg, Shadow, shadow);
1184 setBrush(cg, Highlight, highlight);
1185 setBrush(cg, HighlightedText, highlighted_text);
1186 setBrush(cg, Link, link);
1187 setBrush(cg, LinkVisited, link_visited);
1188 setBrush(cg, ToolTipBase, toolTipBase);
1189 setBrush(cg, ToolTipText, toolTipText);
1190}
1191
1192/*!
1193 \fn QPalette QPalette::copy() const
1194
1195 Use simple assignment instead.
1196*/
1197
1198/*!
1199 \fn QColorGroup QPalette::normal() const
1200 \obsolete
1201
1202 Returns the active color group. Use active() instead.
1203
1204 Use createColorGroup(Active) instead.
1205*/
1206
1207/*!
1208 \fn void QPalette::setNormal(const QColorGroup &colorGroup)
1209
1210 Sets the normal color group to \a colorGroup.
1211
1212 \sa QColorGroup
1213*/
1214
1215/*!
1216 \fn QColorGroup QPalette::active() const
1217
1218 Returns the active color group.
1219 \sa QColorGroup
1220*/
1221
1222/*!
1223 \fn QColorGroup QPalette::disabled() const
1224
1225 Returns the disabled color group.
1226 \sa QColorGroup
1227*/
1228
1229/*!
1230 \fn QColorGroup QPalette::inactive() const
1231
1232 Returns the inactive color group.
1233 \sa QColorGroup
1234*/
1235
1236/*!
1237 \fn void QPalette::setActive(const QColorGroup &colorGroup)
1238
1239 Sets the active color group to \a colorGroup.
1240 \sa QColorGroup
1241*/
1242
1243/*!
1244 \fn void QPalette::setDisabled(const QColorGroup &colorGroup)
1245
1246 Sets the disabled color group to \a colorGroup.
1247 \sa QColorGroup
1248*/
1249
1250/*!
1251 \fn void QPalette::setInactive(const QColorGroup &colorGroup)
1252
1253 Sets the inactive color group.
1254 \sa QColorGroup
1255*/
1256
1257/*! \class QColorGroup
1258 \brief The QColorGroup class contains color groups for each widget state.
1259 \compat
1260*/
1261
1262/*! \fn QColorGroup::QColorGroup()
1263
1264 Use QPalette() instead.
1265*/
1266
1267/*! \fn QColorGroup::QColorGroup(const QBrush &foreground, const QBrush &button, \
1268 const QBrush &light, const QBrush &dark, const QBrush &mid, \
1269 const QBrush &text, const QBrush &bright_text,
1270 const QBrush &base, const QBrush &background)
1271
1272 Use QPalette(\a foreground, \a button, \a light, \a dark, \a mid,
1273 \a text, \a bright_text, \a base, \a background) instead.
1274*/
1275
1276/*! \fn QColorGroup::QColorGroup(const QColor &foreground, const QColor &background, \
1277 const QColor &light, const QColor &dark, const QColor &mid, \
1278 const QColor &text, const QColor &base)
1279
1280 Use QColorGroup(\a foreground, \a background, \a light, \a dark,
1281 \a mid, \a text, \a base) instead.
1282*/
1283
1284/*! \fn QColorGroup::QColorGroup(const QColorGroup &other)
1285
1286 Use QPalette(\a other) instead.
1287*/
1288
1289/*! \fn QColorGroup::QColorGroup(const QPalette &pal)
1290
1291 Use QPalette(\a pal) instead.
1292*/
1293
1294/*! \fn const QColor &QColorGroup::foreground() const
1295
1296 Use QPalette::windowText().color() instead.
1297*/
1298
1299/*! \fn const QColor &QColorGroup::button() const
1300
1301 Use QPalette::button().color() instead.
1302*/
1303
1304/*! \fn const QColor &QColorGroup::light() const
1305
1306 Use QPalette::light().color() instead.
1307*/
1308
1309/*! \fn const QColor &QColorGroup::dark() const
1310
1311 Use QPalette::dark().color() instead.
1312*/
1313
1314/*! \fn const QColor &QColorGroup::mid() const
1315
1316 Use QPalette::mid().color() instead.
1317*/
1318
1319/*! \fn const QColor &QColorGroup::text() const
1320
1321 Use QPalette::text().color() instead.
1322*/
1323
1324/*! \fn const QColor &QColorGroup::base() const
1325
1326 Use QPalette::base().color() instead.
1327*/
1328
1329/*! \fn const QColor &QColorGroup::background() const
1330
1331 Use QPalette::window().color() instead.
1332*/
1333
1334/*! \fn const QColor &QColorGroup::midlight() const
1335
1336 Use QPalette::midlight().color() instead.
1337*/
1338
1339/*! \fn const QColor &QColorGroup::brightText() const
1340
1341 Use QPalette::brightText().color() instead.
1342*/
1343
1344/*! \fn const QColor &QColorGroup::buttonText() const
1345
1346 Use QPalette::buttonText().color() instead.
1347*/
1348
1349/*! \fn const QColor &QColorGroup::shadow() const
1350
1351 Use QPalette::shadow().color() instead.
1352*/
1353
1354/*! \fn const QColor &QColorGroup::highlight() const
1355
1356 Use QPalette::highlight().color() instead.
1357*/
1358
1359/*! \fn const QColor &QColorGroup::highlightedText() const
1360
1361 Use QPalette::highlightedText().color() instead.
1362*/
1363
1364/*! \fn const QColor &QColorGroup::link() const
1365
1366 Use QPalette::link().color() instead.
1367*/
1368
1369/*! \fn const QColor &QColorGroup::linkVisited() const
1370
1371 Use QPalette::linkVisited().color() instead.
1372*/
1373
1374/*! \fn QDataStream &operator<<(QDataStream &ds, const QColorGroup &colorGroup)
1375 \relates QColorGroup
1376 \compat
1377*/
1378
1379/*! \fn QDataStream &operator>>(QDataStream &ds, QColorGroup &colorGroup)
1380 \relates QColorGroup
1381 \compat
1382*/
1383
1384/*! \fn bool QColorGroup::operator==(const QColorGroup &other) const
1385
1386 Returns true if this color group is equal to \a other; otherwise
1387 returns false.
1388*/
1389
1390/*! \fn bool QColorGroup::operator!=(const QColorGroup &other) const
1391
1392 Returns true if this color group is not equal to \a other;
1393 otherwise returns false.
1394*/
1395
1396QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.