📄 qpalette.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qpalette.h"#include "qapplication.h"#include "qdatastream.h"#include "qvariant.h"static int qt_palette_count = 1;class QPalettePrivate {public: QPalettePrivate() : ref(1), ser_no(qt_palette_count++) { } QAtomic ref; QBrush br[QPalette::NColorGroups][QPalette::NColorRoles]; int ser_no;};static QColor qt_mix_colors(QColor a, QColor b){ return QColor((a.red() + b.red()) / 2, (a.green() + b.green()) / 2, (a.blue() + b.blue()) / 2, (a.alpha() + b.alpha()) / 2);}#ifdef QT3_SUPPORT#ifndef QT_NO_DATASTREAMQDataStream &qt_stream_out_qcolorgroup(QDataStream &s, const QColorGroup &g){ if(s.version() == 1) { // Qt 1.x s << g.color(QPalette::Foreground) << g.color(QPalette::Background) << g.color(QPalette::Light) << g.color(QPalette::Dark) << g.color(QPalette::Mid) << g.color(QPalette::Text) << g.color(QPalette::Base); } else { int max = QPalette::NColorRoles; if(s.version() <= 3) // Qt 2.x max = 14; for(int r = 0 ; r < max ; r++) s << g.brush((QPalette::ColorRole)r); } return s;}QDataStream &qt_stream_in_qcolorgroup(QDataStream &s, QColorGroup &g){ if(s.version() == 1) { // Qt 1.x QColor fg, bg, light, dark, mid, text, base; s >> fg >> bg >> light >> dark >> mid >> text >> base; QPalette p(bg); p.setColor(QPalette::Active, QPalette::Foreground, fg); p.setColor(QPalette::Active, QPalette::Light, light); p.setColor(QPalette::Active, QPalette::Dark, dark); p.setColor(QPalette::Active, QPalette::Mid, mid); p.setColor(QPalette::Active, QPalette::Text, text); p.setColor(QPalette::Active, QPalette::Base, base); g = p; g.setCurrentColorGroup(QPalette::Active); } else { int max = QPalette::NColorRoles; if (s.version() <= 3) // Qt 2.x max = 14; else if (s.version() <= 4) // Qt 3.x max = 16; QBrush tmp; for(int r = 0 ; r < max; r++) { s >> tmp; g.setBrush((QPalette::ColorRole)r, tmp); } } return s;}QDataStream &operator<<(QDataStream &s, const QColorGroup &g){ return qt_stream_out_qcolorgroup(s, g);}QDataStream &operator>>(QDataStream &s, QColorGroup &g){ return qt_stream_in_qcolorgroup(s, g);}#endif/*! Constructs a palette with the specified \a active, \a disabled and \a inactive color groups.*/QPalette::QPalette(const QColorGroup &active, const QColorGroup &disabled, const QColorGroup &inactive){ init(); setColorGroup(Active, active); setColorGroup(Disabled, disabled); setColorGroup(Inactive, inactive);}QColorGroup QPalette::createColorGroup(ColorGroup cr) const{ QColorGroup ret(*this); ret.setCurrentColorGroup(cr); return ret;}void QPalette::setColorGroup(ColorGroup cg, const QColorGroup &g){ setColorGroup(cg, g.brush(Foreground), g.brush(Button), g.brush(Light), g.brush(Dark), g.brush(Mid), g.brush(Text), g.brush(BrightText), g.brush(Base), g.brush(AlternateBase), g.brush(Background), g.brush(Midlight), g.brush(ButtonText), g.brush(Shadow), g.brush(Highlight), g.brush(HighlightedText), g.brush(Link), g.brush(LinkVisited));}#endif/*! \fn const QColor &QPalette::color(ColorRole r) const \overload Returns the color that has been set for color role \a r in the current ColorGroup. \sa brush() ColorRole *//*! \fn const QBrush &QPalette::brush(ColorRole r) const \overload Returns the brush that has been set for color role \a r in the current ColorGroup. \sa color() setBrush() ColorRole*//*! \fn void QPalette::setColor(ColorRole r, const QColor &c) \overload Sets the color used for color role \a r in all color groups to a solid color \a c. \sa brush() setColor() ColorRole*//*! \fn void QPalette::setBrush(ColorRole cr, const QBrush &brush) Sets the brush for color role \a cr to brush \a brush for all groups in the palette. \sa brush() setColor() ColorRole*//*! \fn const QBrush & QPalette::foreground() const \obsolete Use windowText() instead.*//*! \fn const QBrush & QPalette::windowText() const Returns the window text (general foreground) brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::button() const Returns the button brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::light() const Returns the light brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush& QPalette::midlight() const Returns the midlight brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::dark() const Returns the dark brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::mid() const Returns the mid brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::text() const Returns the text foreground brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::brightText() const Returns the bright text foreground brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::buttonText() const Returns the button text foreground brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::base() const Returns the base brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::alternateBase() const Returns the alternate base brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::background() const \obsolete Use window() instead.*//*! \fn const QBrush & QPalette::window() const Returns the window (general background) brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::shadow() const Returns the shadow brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::highlight() const Returns the highlight brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::highlightedText() const Returns the highlighted text brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::link() const Returns the unvisited link text brush of the current color group. \sa ColorRole brush()*//*! \fn const QBrush & QPalette::linkVisited() const Returns the visited link text brush of the current color group. \sa ColorRole brush()*//*! \fn ColorGroup QPalette::currentColorGroup() const Returns the palette's current color group.*//*! \fn void QPalette::setCurrentColorGroup(ColorGroup cg) Set the palette's current color group to \a cg.*//*! \class QPalette qpalette.h \brief The QPalette class contains color groups for each widget state. \ingroup appearance \ingroup shared \ingroup multimedia \mainclass A palette consists of three color groups: \e Active, \e Disabled, and \e Inactive. All widgets in Qt contain a palette and use their palette to draw themselves. This makes the user interface easily configurable and easier to keep consistent. If you create a new widget we strongly recommend that you use the colors in the palette rather than hard-coding specific colors. The color groups: \list \i The Active group is used for the window that has keyboard focus. \i The Inactive group is used for other windows. \i The Disabled group is used for widgets (not windows) that are disabled for some reason. \endlist Both active and inactive windows can contain disabled widgets. (Disabled widgets are often called \e inaccessible or \e{grayed out}.) In most styles, Active and Inactive look the same. Colors and brushes can be set for particular roles in any of a palette's color groups with setColor() and setBrush(). A color group contains a group of colors used by widgets for drawing themselves. We recommend that widgets use color group roles from the palette such as "foreground" and "base" rather than literal colors like "red" or "turquoise". The color roles are enumerated and defined in the \l ColorRole documentation. We strongly recommend that you use a system-supplied color group and modify that as necessary. To modify a color group you call the functions setColor() and setBrush(), depending on whether you want a pure color or a pixmap pattern. There are also corresponding color() and brush() getters, and a commonly used convenience function to get the ColorRole for the current ColorGroup: background(), foreground(), base(), etc. You can copy a palette using the copy constructor and test to see if two palettes are \e identical using isCopyOf(). QPalette is optimized by the use of \link shclass.html implicit sharing\endlink, so it is very efficient to pass QPalette objects as arguments. \sa QApplication::setPalette(), QWidget::setPalette(), QColor*//*! \enum QPalette::ColorGroup \value Disabled \value Active \value Inactive \value Normal synonym for Active \omitvalue All \omitvalue NColorGroups \omitvalue Current*//*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -