| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \example widgets/styles
|
|---|
| 44 | \title Styles Example
|
|---|
| 45 |
|
|---|
| 46 | The Styles example illustrates how to create custom widget
|
|---|
| 47 | drawing styles using Qt, and demonstrates Qt's predefined styles.
|
|---|
| 48 |
|
|---|
| 49 | \image styles-enabledwood.png Screenshot of the Styles example
|
|---|
| 50 |
|
|---|
| 51 | A style in Qt is a subclass of QStyle or of one of its
|
|---|
| 52 | subclasses. Styles perform drawing on behalf of widgets. Qt
|
|---|
| 53 | provides a whole range of predefined styles, either built into
|
|---|
| 54 | the \l QtGui library or found in plugins. Custom styles are
|
|---|
| 55 | usually created by subclassing one of Qt's existing style and
|
|---|
| 56 | reimplementing a few virtual functions.
|
|---|
| 57 |
|
|---|
| 58 | In this example, the custom style is called \c NorwegianWoodStyle
|
|---|
| 59 | and derives from QMotifStyle. Its main features are the wooden
|
|---|
| 60 | textures used for filling most of the widgets and its round
|
|---|
| 61 | buttons and comboboxes.
|
|---|
| 62 |
|
|---|
| 63 | To implement the style, we use some advanced features provided by
|
|---|
| 64 | QPainter, such as \l{QPainter::Antialiasing}{antialiasing} (to
|
|---|
| 65 | obtain smoother button edges), \l{QColor::alpha()}{alpha blending}
|
|---|
| 66 | (to make the buttons appeared raised or sunken), and
|
|---|
| 67 | \l{QPainterPath}{painter paths} (to fill the buttons and draw the
|
|---|
| 68 | outline). We also use many features of QBrush and QPalette.
|
|---|
| 69 |
|
|---|
| 70 | The example consists of the following classes:
|
|---|
| 71 |
|
|---|
| 72 | \list
|
|---|
| 73 | \o \c NorwegianWoodStyle inherits from QMotifStyle and implements
|
|---|
| 74 | the Norwegian Wood style.
|
|---|
| 75 | \o \c WidgetGallery is a \c QDialog subclass that shows the most
|
|---|
| 76 | common widgets and allows the user to switch style
|
|---|
| 77 | dynamically.
|
|---|
| 78 | \endlist
|
|---|
| 79 |
|
|---|
| 80 | \section1 NorwegianWoodStyle Class Definition
|
|---|
| 81 |
|
|---|
| 82 | Here's the definition of the \c NorwegianWoodStyle class:
|
|---|
| 83 |
|
|---|
| 84 | \snippet examples/widgets/styles/norwegianwoodstyle.h 0
|
|---|
| 85 |
|
|---|
| 86 | The public functions are all declared in QStyle (QMotifStyle's
|
|---|
| 87 | grandparent class) and reimplemented here to override the Motif
|
|---|
| 88 | look and feel. The private functions are helper functions.
|
|---|
| 89 |
|
|---|
| 90 | \section1 NorwegianWoodStyle Class Implementation
|
|---|
| 91 |
|
|---|
| 92 | We will now review the implementation of the \c
|
|---|
| 93 | NorwegianWoodStyle class.
|
|---|
| 94 |
|
|---|
| 95 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 0
|
|---|
| 96 |
|
|---|
| 97 | The \c polish() function is reimplemented from QStyle. It takes a
|
|---|
| 98 | QPalette as a reference and adapts the palette to fit the style.
|
|---|
| 99 | Most styles don't need to reimplement that function. The
|
|---|
| 100 | Norwegian Wood style reimplements it to set a "wooden" palette.
|
|---|
| 101 |
|
|---|
| 102 | We start by defining a few \l{QColor}s that we'll need. Then we
|
|---|
| 103 | load two PNG images. The \c : prefix in the file path indicates
|
|---|
| 104 | that the PNG files are \l{The Qt Resource System}{embedded
|
|---|
| 105 | resources}.
|
|---|
| 106 |
|
|---|
| 107 | \table
|
|---|
| 108 | \row \o \inlineimage widgets/styles/images/woodbackground.png
|
|---|
| 109 |
|
|---|
| 110 | \o \bold{woodbackground.png}
|
|---|
| 111 |
|
|---|
| 112 | This texture is used as the background of most widgets.
|
|---|
| 113 | The wood pattern is horizontal.
|
|---|
| 114 |
|
|---|
| 115 | \row \o \inlineimage widgets/styles/images/woodbutton.png
|
|---|
| 116 |
|
|---|
| 117 | \o \bold{woodbutton.png}
|
|---|
| 118 |
|
|---|
| 119 | This texture is used for filling push buttons and
|
|---|
| 120 | comboboxes. The wood pattern is vertical and more reddish
|
|---|
| 121 | than the texture used for the background.
|
|---|
| 122 | \endtable
|
|---|
| 123 |
|
|---|
| 124 | The \c midImage variable is initialized to be the same as \c
|
|---|
| 125 | buttonImage, but then we use a QPainter and fill it with a 25%
|
|---|
| 126 | opaque black color (a black with an \l{QColor::alpha()}{alpha
|
|---|
| 127 | channel} of 63). The result is a somewhat darker image than \c
|
|---|
| 128 | buttonImage. This image will be used for filling buttons that the
|
|---|
| 129 | user is holding down.
|
|---|
| 130 |
|
|---|
| 131 | \snippet examples/widgets/styles/norwegianwoodstyle.cpp 1
|
|---|
| 132 |
|
|---|
| 133 | We initialize the palette. Palettes have various
|
|---|
| 134 | \l{QPalette::ColorRole}{color roles}, such as QPalette::Base
|
|---|
| 135 | (used for filling text editors, item views, etc.), QPalette::Text
|
|---|
| 136 | (used for foreground text), and QPalette::Background (used for
|
|---|
| 137 | the background of most widgets). Each role has its own QBrush,
|
|---|
| 138 | which usually is a plain color but can also be a brush pattern or
|
|---|
| 139 | even a texture (a QPixmap).
|
|---|
| 140 |
|
|---|
| 141 | In addition to the roles, palettes have several
|
|---|
| 142 | \l{QPalette::ColorGroup}{color groups}: active, disabled, and
|
|---|
| 143 | inactive. The active color group is used for painting widgets in
|
|---|
| 144 | the active window. The disabled group is used for disabled
|
|---|
| 145 | widgets. The inactive group is used for all other widgets. Most
|
|---|
| 146 | palettes have identical active and inactive groups, while the
|
|---|
| 147 | disabled group uses darker shades.
|
|---|
| 148 |
|
|---|
| 149 | We initialize the QPalette object with a brown color. Qt
|
|---|
| 150 | automatically derivates all color roles for all color groups from
|
|---|
| 151 | that single color. We then override some of the default values. For
|
|---|
| 152 | example, we use Qt::darkGreen instead of the default
|
|---|
| 153 | (Qt::darkBlue) for the QPalette::Highlight role. The
|
|---|
| 154 | QPalette::setBrush() overload that we use here sets the same
|
|---|
| 155 | color or brush for all three color groups.
|
|---|
| 156 |
|
|---|
| 157 | The \c setTexture() function is a private function that sets the
|
|---|
| 158 | texture for a certain color role, while preserving the existing
|
|---|
| 159 | color in the QBrush. A QBrush can hold both a solid color and a
|
|---|
| 160 | texture at the same time. The solid color is used for drawing
|
|---|
| 161 | text and other graphical elements where textures don't look good.
|
|---|
| 162 |
|
|---|
|
|---|