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 examples 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 <QtGui>
|
---|
43 |
|
---|
44 | #include "norwegianwoodstyle.h"
|
---|
45 |
|
---|
46 | //! [0]
|
---|
47 | void NorwegianWoodStyle::polish(QPalette &palette)
|
---|
48 | {
|
---|
49 | QColor brown(212, 140, 95);
|
---|
50 | QColor beige(236, 182, 120);
|
---|
51 | QColor slightlyOpaqueBlack(0, 0, 0, 63);
|
---|
52 |
|
---|
53 | QPixmap backgroundImage(":/images/woodbackground.png");
|
---|
54 | QPixmap buttonImage(":/images/woodbutton.png");
|
---|
55 | QPixmap midImage = buttonImage;
|
---|
56 |
|
---|
57 | QPainter painter;
|
---|
58 | painter.begin(&midImage);
|
---|
59 | painter.setPen(Qt::NoPen);
|
---|
60 | painter.fillRect(midImage.rect(), slightlyOpaqueBlack);
|
---|
61 | painter.end();
|
---|
62 | //! [0]
|
---|
63 |
|
---|
64 | //! [1]
|
---|
65 | palette = QPalette(brown);
|
---|
66 |
|
---|
67 | palette.setBrush(QPalette::BrightText, Qt::white);
|
---|
68 | palette.setBrush(QPalette::Base, beige);
|
---|
69 | palette.setBrush(QPalette::Highlight, Qt::darkGreen);
|
---|
70 | setTexture(palette, QPalette::Button, buttonImage);
|
---|
71 | setTexture(palette, QPalette::Mid, midImage);
|
---|
72 | setTexture(palette, QPalette::Window, backgroundImage);
|
---|
73 |
|
---|
74 | QBrush brush = palette.background();
|
---|
75 | brush.setColor(brush.color().dark());
|
---|
76 |
|
---|
77 | palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush);
|
---|
78 | palette.setBrush(QPalette::Disabled, QPalette::Text, brush);
|
---|
79 | palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush);
|
---|
80 | palette.setBrush(QPalette::Disabled, QPalette::Base, brush);
|
---|
81 | palette.setBrush(QPalette::Disabled, QPalette::Button, brush);
|
---|
82 | palette.setBrush(QPalette::Disabled, QPalette::Mid, brush);
|
---|
83 | }
|
---|
84 | //! [1]
|
---|
85 |
|
---|
86 | //! [3]
|
---|
87 | void NorwegianWoodStyle::polish(QWidget *widget)
|
---|
88 | //! [3] //! [4]
|
---|
89 | {
|
---|
90 | if (qobject_cast<QPushButton *>(widget)
|
---|
91 | || qobject_cast<QComboBox *>(widget))
|
---|
92 | widget->setAttribute(Qt::WA_Hover, true);
|
---|
93 | }
|
---|
94 | //! [4]
|
---|
95 |
|
---|
96 | //! [5]
|
---|
97 | void NorwegianWoodStyle::unpolish(QWidget *widget)
|
---|
98 | //! [5] //! [6]
|
---|
99 | {
|
---|
100 | if (qobject_cast<QPushButton *>(widget)
|
---|
101 | || qobject_cast<QComboBox *>(widget))
|
---|
102 | widget->setAttribute(Qt::WA_Hover, false);
|
---|
103 | }
|
---|
104 | //! [6]
|
---|
105 |
|
---|
106 | //! [7]
|
---|
107 | int NorwegianWoodStyle::pixelMetric(PixelMetric metric,
|
---|
108 | //! [7] //! [8]
|
---|
109 | const QStyleOption *option,
|
---|
110 | const QWidget *widget) const
|
---|
111 | {
|
---|
112 | switch (metric) {
|
---|
113 | case PM_ComboBoxFrameWidth:
|
---|
114 | return 8;
|
---|
115 | case PM_ScrollBarExtent:
|
---|
116 | return QMotifStyle::pixelMetric(metric, option, widget) + 4;
|
---|
117 | default:
|
---|
118 | return QMotifStyle::pixelMetric(metric, option, widget);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | //! [8]
|
---|
122 |
|
---|
123 | //! [9]
|
---|
124 | int NorwegianWoodStyle::styleHint(StyleHint hint, const QStyleOption *option,
|
---|
125 | //! [9] //! [10]
|
---|
126 | const QWidget *widget,
|
---|
127 | QStyleHintReturn *returnData) const
|
---|
128 | {
|
---|
129 | switch (hint) {
|
---|
130 | case SH_DitherDisabledText:
|
---|
131 | return int(false);
|
---|
132 | case SH_EtchDisabledText:
|
---|
133 | return int(true);
|
---|
134 | default:
|
---|
135 | return QMotifStyle::styleHint(hint, option, widget, returnData);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | //! [10]
|
---|
139 |
|
---|
140 | //! [11]
|
---|
141 | void NorwegianWoodStyle::drawPrimitive(PrimitiveElement element,
|
---|
142 | //! [11] //! [12]
|
---|
143 | const QStyleOption *option,
|
---|
144 | QPainter *painter,
|
---|
145 | const QWidget *widget) const
|
---|
146 | {
|
---|
147 | switch (element) {
|
---|
148 | case PE_PanelButtonCommand:
|
---|
149 | {
|
---|
150 | int delta = (option->state & State_MouseOver) ? 64 : 0;
|
---|
151 | QColor slightlyOpaqueBlack(0, 0, 0, 63);
|
---|
152 | QColor semiTransparentWhite(255, 255, 255, 127 + delta);
|
---|
153 | QColor semiTransparentBlack(0, 0, 0, 127 - delta);
|
---|
154 |
|
---|
155 | int x, y, width, height;
|
---|
156 | option->rect.getRect(&x, &y, &width, &height);
|
---|
157 | //! [12]
|
---|
158 |
|
---|
159 | //! [13]
|
---|
160 | QPainterPath roundRect = roundRectPath(option->rect);
|
---|
161 | //! [13] //! [14]
|
---|
162 | int radius = qMin(width, height) / 2;
|
---|
163 | //! [14]
|
---|
164 |
|
---|
165 | //! [15]
|
---|
166 | QBrush brush;
|
---|
167 | //! [15] //! [16]
|
---|
168 | bool darker;
|
---|
169 |
|
---|
170 | const QStyleOptionButton *buttonOption =
|
---|
171 | qstyleoption_cast<const QStyleOptionButton *>(option);
|
---|
172 | if (buttonOption
|
---|
173 | && (buttonOption->features & QStyleOptionButton::Flat)) {
|
---|
174 | brush = option->palette.background();
|
---|
175 | darker = (option->state & (State_Sunken | State_On));
|
---|
176 | } else {
|
---|
177 | if (option->state & (State_Sunken | State_On)) {
|
---|
178 | brush = option->palette.mid();
|
---|
179 | darker = !(option->state & State_Sunken);
|
---|
180 | } else {
|
---|
181 | brush = option->palette.button();
|
---|
182 | darker = false;
|
---|
183 | //! [16] //! [17]
|
---|
184 | }
|
---|
185 | //! [17] //! [18]
|
---|
186 | }
|
---|
187 | //! [18]
|
---|
188 |
|
---|
189 | //! [19]
|
---|
190 | painter->save();
|
---|
191 | //! [19] //! [20]
|
---|
192 | painter->setRenderHint(QPainter::Antialiasing, true);
|
---|
193 | //! [20] //! [21]
|
---|
194 | painter->fillPath(roundRect, brush);
|
---|
195 | //! [21] //! [22]
|
---|
196 | if (darker)
|
---|
197 | //! [22] //! [23]
|
---|
198 | painter->fillPath(roundRect, slightlyOpaqueBlack);
|
---|
199 | //! [23]
|
---|
200 |
|
---|
201 | //! [24]
|
---|
202 | int penWidth;
|
---|
203 | //! [24] //! [25]
|
---|
204 | if (radius < 10)
|
---|
205 | penWidth = 3;
|
---|
206 | else if (radius < 20)
|
---|
207 | penWidth = 5;
|
---|
208 | else
|
---|
209 | penWidth = 7;
|
---|
210 |
|
---|
211 | QPen topPen(semiTransparentWhite, penWidth);
|
---|
212 | QPen bottomPen(semiTransparentBlack, penWidth);
|
---|
213 |
|
---|
214 | if (option->state & (State_Sunken | State_On))
|
---|
215 | qSwap(topPen, bottomPen);
|
---|
216 | //! [25]
|
---|
217 |
|
---|
218 | //! [26]
|
---|
219 | int x1 = x;
|
---|
220 | int x2 = x + radius;
|
---|
221 | int x3 = x + width - radius;
|
---|
222 | int x4 = x + width;
|
---|
223 |
|
---|
224 | if (option->direction == Qt::RightToLeft) {
|
---|
225 | qSwap(x1, x4);
|
---|
226 | qSwap(x2, x3);
|
---|
227 | }
|
---|
228 |
|
---|
229 | QPolygon topHalf;
|
---|
230 | topHalf << QPoint(x1, y)
|
---|
231 | << QPoint(x4, y)
|
---|
232 | << QPoint(x3, y + radius)
|
---|
233 | << QPoint(x2, y + height - radius)
|
---|
234 | << QPoint(x1, y + height);
|
---|
235 |
|
---|
236 | painter->setClipPath(roundRect);
|
---|
237 | painter->setClipRegion(topHalf, Qt::IntersectClip);
|
---|
238 | painter->setPen(topPen);
|
---|
239 | painter->drawPath(roundRect);
|
---|
240 | //! [26] //! [32]
|
---|
241 |
|
---|
242 | QPolygon bottomHalf = topHalf;
|
---|
243 | bottomHalf[0] = QPoint(x4, y + height);
|
---|
244 |
|
---|
245 | painter->setClipPath(roundRect);
|
---|
246 | painter->setClipRegion(bottomHalf, Qt::IntersectClip);
|
---|
247 | painter->setPen(bottomPen);
|
---|
248 | painter->drawPath(roundRect);
|
---|
249 |
|
---|
250 | painter->setPen(option->palette.foreground().color());
|
---|
251 | painter->setClipping(false);
|
---|
252 | painter->drawPath(roundRect);
|
---|
253 |
|
---|
254 | painter->restore();
|
---|
255 | }
|
---|
256 | break;
|
---|
257 | //! [32] //! [33]
|
---|
258 | default:
|
---|
259 | //! [33] //! [34]
|
---|
260 | QMotifStyle::drawPrimitive(element, option, painter, widget);
|
---|
261 | }
|
---|
262 | }
|
---|
263 | //! [34]
|
---|
264 |
|
---|
265 | //! [35]
|
---|
266 | void NorwegianWoodStyle::drawControl(ControlElement element,
|
---|
267 | //! [35] //! [36]
|
---|
268 | const QStyleOption *option,
|
---|
269 | QPainter *painter,
|
---|
270 | const QWidget *widget) const
|
---|
271 | {
|
---|
272 | switch (element) {
|
---|
273 | case CE_PushButtonLabel:
|
---|
274 | {
|
---|
275 | QStyleOptionButton myButtonOption;
|
---|
276 | const QStyleOptionButton *buttonOption =
|
---|
277 | qstyleoption_cast<const QStyleOptionButton *>(option);
|
---|
278 | if (buttonOption) {
|
---|
279 | myButtonOption = *buttonOption;
|
---|
280 | if (myButtonOption.palette.currentColorGroup()
|
---|
281 | != QPalette::Disabled) {
|
---|
282 | if (myButtonOption.state & (State_Sunken | State_On)) {
|
---|
283 | myButtonOption.palette.setBrush(QPalette::ButtonText,
|
---|
284 | myButtonOption.palette.brightText());
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 | QMotifStyle::drawControl(element, &myButtonOption, painter, widget);
|
---|
289 | }
|
---|
290 | break;
|
---|
291 | default:
|
---|
292 | QMotifStyle::drawControl(element, option, painter, widget);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | //! [36]
|
---|
296 |
|
---|
297 | //! [37]
|
---|
298 | void NorwegianWoodStyle::setTexture(QPalette &palette, QPalette::ColorRole role,
|
---|
299 | //! [37] //! [38]
|
---|
300 | const QPixmap &pixmap)
|
---|
301 | {
|
---|
302 | for (int i = 0; i < QPalette::NColorGroups; ++i) {
|
---|
303 | QColor color = palette.brush(QPalette::ColorGroup(i), role).color();
|
---|
304 | palette.setBrush(QPalette::ColorGroup(i), role, QBrush(color, pixmap));
|
---|
305 | }
|
---|
306 | }
|
---|
307 | //! [38]
|
---|
308 |
|
---|
309 | //! [39]
|
---|
310 | QPainterPath NorwegianWoodStyle::roundRectPath(const QRect &rect)
|
---|
311 | //! [39] //! [40]
|
---|
312 | {
|
---|
313 | int radius = qMin(rect.width(), rect.height()) / 2;
|
---|
314 | int diam = 2 * radius;
|
---|
315 |
|
---|
316 | int x1, y1, x2, y2;
|
---|
317 | rect.getCoords(&x1, &y1, &x2, &y2);
|
---|
318 |
|
---|
319 | QPainterPath path;
|
---|
320 | path.moveTo(x2, y1 + radius);
|
---|
321 | path.arcTo(QRect(x2 - diam, y1, diam, diam), 0.0, +90.0);
|
---|
322 | path.lineTo(x1 + radius, y1);
|
---|
323 | path.arcTo(QRect(x1, y1, diam, diam), 90.0, +90.0);
|
---|
324 | path.lineTo(x1, y2 - radius);
|
---|
325 | path.arcTo(QRect(x1, y2 - diam, diam, diam), 180.0, +90.0);
|
---|
326 | path.lineTo(x1 + radius, y2);
|
---|
327 | path.arcTo(QRect(x2 - diam, y2 - diam, diam, diam), 270.0, +90.0);
|
---|
328 | path.closeSubpath();
|
---|
329 | return path;
|
---|
330 | }
|
---|
331 | //! [40]
|
---|