source: vendor/trolltech/current/src/kernel/qpalette.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

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