author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
||
43 |
#include "norwegianwoodstyle.h" |
|
44 |
||
45 |
//! [0] |
|
46 |
void NorwegianWoodStyle::polish(QPalette &palette) |
|
47 |
{ |
|
48 |
QColor brown(212, 140, 95); |
|
49 |
QColor beige(236, 182, 120); |
|
50 |
QColor slightlyOpaqueBlack(0, 0, 0, 63); |
|
51 |
||
52 |
QPixmap backgroundImage(":/images/woodbackground.png"); |
|
53 |
QPixmap buttonImage(":/images/woodbutton.png"); |
|
54 |
QPixmap midImage = buttonImage; |
|
55 |
||
56 |
QPainter painter; |
|
57 |
painter.begin(&midImage); |
|
58 |
painter.setPen(Qt::NoPen); |
|
59 |
painter.fillRect(midImage.rect(), slightlyOpaqueBlack); |
|
60 |
painter.end(); |
|
61 |
//! [0] |
|
62 |
||
63 |
//! [1] |
|
64 |
palette = QPalette(brown); |
|
65 |
||
66 |
palette.setBrush(QPalette::BrightText, Qt::white); |
|
67 |
palette.setBrush(QPalette::Base, beige); |
|
68 |
palette.setBrush(QPalette::Highlight, Qt::darkGreen); |
|
69 |
setTexture(palette, QPalette::Button, buttonImage); |
|
70 |
setTexture(palette, QPalette::Mid, midImage); |
|
71 |
setTexture(palette, QPalette::Window, backgroundImage); |
|
72 |
||
73 |
QBrush brush = palette.background(); |
|
74 |
brush.setColor(brush.color().dark()); |
|
75 |
||
76 |
palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush); |
|
77 |
palette.setBrush(QPalette::Disabled, QPalette::Text, brush); |
|
78 |
palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush); |
|
79 |
palette.setBrush(QPalette::Disabled, QPalette::Base, brush); |
|
80 |
palette.setBrush(QPalette::Disabled, QPalette::Button, brush); |
|
81 |
palette.setBrush(QPalette::Disabled, QPalette::Mid, brush); |
|
82 |
} |
|
83 |
//! [1] |
|
84 |
||
85 |
//! [3] |
|
86 |
void NorwegianWoodStyle::polish(QWidget *widget) |
|
87 |
//! [3] //! [4] |
|
88 |
{ |
|
89 |
if (qobject_cast<QPushButton *>(widget) |
|
90 |
|| qobject_cast<QComboBox *>(widget)) |
|
91 |
widget->setAttribute(Qt::WA_Hover, true); |
|
92 |
} |
|
93 |
//! [4] |
|
94 |
||
95 |
//! [5] |
|
96 |
void NorwegianWoodStyle::unpolish(QWidget *widget) |
|
97 |
//! [5] //! [6] |
|
98 |
{ |
|
99 |
if (qobject_cast<QPushButton *>(widget) |
|
100 |
|| qobject_cast<QComboBox *>(widget)) |
|
101 |
widget->setAttribute(Qt::WA_Hover, false); |
|
102 |
} |
|
103 |
//! [6] |
|
104 |
||
105 |
//! [7] |
|
106 |
int NorwegianWoodStyle::pixelMetric(PixelMetric metric, |
|
107 |
//! [7] //! [8] |
|
108 |
const QStyleOption *option, |
|
109 |
const QWidget *widget) const |
|
110 |
{ |
|
111 |
switch (metric) { |
|
112 |
case PM_ComboBoxFrameWidth: |
|
113 |
return 8; |
|
114 |
case PM_ScrollBarExtent: |
|
115 |
return QMotifStyle::pixelMetric(metric, option, widget) + 4; |
|
116 |
default: |
|
117 |
return QMotifStyle::pixelMetric(metric, option, widget); |
|
118 |
} |
|
119 |
} |
|
120 |
//! [8] |
|
121 |
||
122 |
//! [9] |
|
123 |
int NorwegianWoodStyle::styleHint(StyleHint hint, const QStyleOption *option, |
|
124 |
//! [9] //! [10] |
|
125 |
const QWidget *widget, |
|
126 |
QStyleHintReturn *returnData) const |
|
127 |
{ |
|
128 |
switch (hint) { |
|
129 |
case SH_DitherDisabledText: |
|
130 |
return int(false); |
|
131 |
case SH_EtchDisabledText: |
|
132 |
return int(true); |
|
133 |
default: |
|
134 |
return QMotifStyle::styleHint(hint, option, widget, returnData); |
|
135 |
} |
|
136 |
} |
|
137 |
//! [10] |
|
138 |
||
139 |
//! [11] |
|
140 |
void NorwegianWoodStyle::drawPrimitive(PrimitiveElement element, |
|
141 |
//! [11] //! [12] |
|
142 |
const QStyleOption *option, |
|
143 |
QPainter *painter, |
|
144 |
const QWidget *widget) const |
|
145 |
{ |
|
146 |
switch (element) { |
|
147 |
case PE_PanelButtonCommand: |
|
148 |
{ |
|
149 |
int delta = (option->state & State_MouseOver) ? 64 : 0; |
|
150 |
QColor slightlyOpaqueBlack(0, 0, 0, 63); |
|
151 |
QColor semiTransparentWhite(255, 255, 255, 127 + delta); |
|
152 |
QColor semiTransparentBlack(0, 0, 0, 127 - delta); |
|
153 |
||
154 |
int x, y, width, height; |
|
155 |
option->rect.getRect(&x, &y, &width, &height); |
|
156 |
//! [12] |
|
157 |
||
158 |
//! [13] |
|
159 |
QPainterPath roundRect = roundRectPath(option->rect); |
|
160 |
//! [13] //! [14] |
|
161 |
int radius = qMin(width, height) / 2; |
|
162 |
//! [14] |
|
163 |
||
164 |
//! [15] |
|
165 |
QBrush brush; |
|
166 |
//! [15] //! [16] |
|
167 |
bool darker; |
|
168 |
||
169 |
const QStyleOptionButton *buttonOption = |
|
170 |
qstyleoption_cast<const QStyleOptionButton *>(option); |
|
171 |
if (buttonOption |
|
172 |
&& (buttonOption->features & QStyleOptionButton::Flat)) { |
|
173 |
brush = option->palette.background(); |
|
174 |
darker = (option->state & (State_Sunken | State_On)); |
|
175 |
} else { |
|
176 |
if (option->state & (State_Sunken | State_On)) { |
|
177 |
brush = option->palette.mid(); |
|
178 |
darker = !(option->state & State_Sunken); |
|
179 |
} else { |
|
180 |
brush = option->palette.button(); |
|
181 |
darker = false; |
|
182 |
//! [16] //! [17] |
|
183 |
} |
|
184 |
//! [17] //! [18] |
|
185 |
} |
|
186 |
//! [18] |
|
187 |
||
188 |
//! [19] |
|
189 |
painter->save(); |
|
190 |
//! [19] //! [20] |
|
191 |
painter->setRenderHint(QPainter::Antialiasing, true); |
|
192 |
//! [20] //! [21] |
|
193 |
painter->fillPath(roundRect, brush); |
|
194 |
//! [21] //! [22] |
|
195 |
if (darker) |
|
196 |
//! [22] //! [23] |
|
197 |
painter->fillPath(roundRect, slightlyOpaqueBlack); |
|
198 |
//! [23] |
|
199 |
||
200 |
//! [24] |
|
201 |
int penWidth; |
|
202 |
//! [24] //! [25] |
|
203 |
if (radius < 10) |
|
204 |
penWidth = 3; |
|
205 |
else if (radius < 20) |
|
206 |
penWidth = 5; |
|
207 |
else |
|
208 |
penWidth = 7; |
|
209 |
||
210 |
QPen topPen(semiTransparentWhite, penWidth); |
|
211 |
QPen bottomPen(semiTransparentBlack, penWidth); |
|
212 |
||
213 |
if (option->state & (State_Sunken | State_On)) |
|
214 |
qSwap(topPen, bottomPen); |
|
215 |
//! [25] |
|
216 |
||
217 |
//! [26] |
|
218 |
int x1 = x; |
|
219 |
int x2 = x + radius; |
|
220 |
int x3 = x + width - radius; |
|
221 |
int x4 = x + width; |
|
222 |
||
223 |
if (option->direction == Qt::RightToLeft) { |
|
224 |
qSwap(x1, x4); |
|
225 |
qSwap(x2, x3); |
|
226 |
} |
|
227 |
||
228 |
QPolygon topHalf; |
|
229 |
topHalf << QPoint(x1, y) |
|
230 |
<< QPoint(x4, y) |
|
231 |
<< QPoint(x3, y + radius) |
|
232 |
<< QPoint(x2, y + height - radius) |
|
233 |
<< QPoint(x1, y + height); |
|
234 |
||
235 |
painter->setClipPath(roundRect); |
|
236 |
painter->setClipRegion(topHalf, Qt::IntersectClip); |
|
237 |
painter->setPen(topPen); |
|
238 |
painter->drawPath(roundRect); |
|
239 |
//! [26] //! [32] |
|
240 |
||
241 |
QPolygon bottomHalf = topHalf; |
|
242 |
bottomHalf[0] = QPoint(x4, y + height); |
|
243 |
||
244 |
painter->setClipPath(roundRect); |
|
245 |
painter->setClipRegion(bottomHalf, Qt::IntersectClip); |
|
246 |
painter->setPen(bottomPen); |
|
247 |
painter->drawPath(roundRect); |
|
248 |
||
249 |
painter->setPen(option->palette.foreground().color()); |
|
250 |
painter->setClipping(false); |
|
251 |
painter->drawPath(roundRect); |
|
252 |
||
253 |
painter->restore(); |
|
254 |
} |
|
255 |
break; |
|
256 |
//! [32] //! [33] |
|
257 |
default: |
|
258 |
//! [33] //! [34] |
|
259 |
QMotifStyle::drawPrimitive(element, option, painter, widget); |
|
260 |
} |
|
261 |
} |
|
262 |
//! [34] |
|
263 |
||
264 |
//! [35] |
|
265 |
void NorwegianWoodStyle::drawControl(ControlElement element, |
|
266 |
//! [35] //! [36] |
|
267 |
const QStyleOption *option, |
|
268 |
QPainter *painter, |
|
269 |
const QWidget *widget) const |
|
270 |
{ |
|
271 |
switch (element) { |
|
272 |
case CE_PushButtonLabel: |
|
273 |
{ |
|
274 |
QStyleOptionButton myButtonOption; |
|
275 |
const QStyleOptionButton *buttonOption = |
|
276 |
qstyleoption_cast<const QStyleOptionButton *>(option); |
|
277 |
if (buttonOption) { |
|
278 |
myButtonOption = *buttonOption; |
|
279 |
if (myButtonOption.palette.currentColorGroup() |
|
280 |
!= QPalette::Disabled) { |
|
281 |
if (myButtonOption.state & (State_Sunken | State_On)) { |
|
282 |
myButtonOption.palette.setBrush(QPalette::ButtonText, |
|
283 |
myButtonOption.palette.brightText()); |
|
284 |
} |
|
285 |
} |
|
286 |
} |
|
287 |
QMotifStyle::drawControl(element, &myButtonOption, painter, widget); |
|
288 |
} |
|
289 |
break; |
|
290 |
default: |
|
291 |
QMotifStyle::drawControl(element, option, painter, widget); |
|
292 |
} |
|
293 |
} |
|
294 |
//! [36] |
|
295 |
||
296 |
//! [37] |
|
297 |
void NorwegianWoodStyle::setTexture(QPalette &palette, QPalette::ColorRole role, |
|
298 |
//! [37] //! [38] |
|
299 |
const QPixmap &pixmap) |
|
300 |
{ |
|
301 |
for (int i = 0; i < QPalette::NColorGroups; ++i) { |
|
302 |
QColor color = palette.brush(QPalette::ColorGroup(i), role).color(); |
|
303 |
palette.setBrush(QPalette::ColorGroup(i), role, QBrush(color, pixmap)); |
|
304 |
} |
|
305 |
} |
|
306 |
//! [38] |
|
307 |
||
308 |
//! [39] |
|
309 |
QPainterPath NorwegianWoodStyle::roundRectPath(const QRect &rect) |
|
310 |
//! [39] //! [40] |
|
311 |
{ |
|
312 |
int radius = qMin(rect.width(), rect.height()) / 2; |
|
313 |
int diam = 2 * radius; |
|
314 |
||
315 |
int x1, y1, x2, y2; |
|
316 |
rect.getCoords(&x1, &y1, &x2, &y2); |
|
317 |
||
318 |
QPainterPath path; |
|
319 |
path.moveTo(x2, y1 + radius); |
|
320 |
path.arcTo(QRect(x2 - diam, y1, diam, diam), 0.0, +90.0); |
|
321 |
path.lineTo(x1 + radius, y1); |
|
322 |
path.arcTo(QRect(x1, y1, diam, diam), 90.0, +90.0); |
|
323 |
path.lineTo(x1, y2 - radius); |
|
324 |
path.arcTo(QRect(x1, y2 - diam, diam, diam), 180.0, +90.0); |
|
325 |
path.lineTo(x1 + radius, y2); |
|
326 |
path.arcTo(QRect(x2 - diam, y2 - diam, diam, diam), 270.0, +90.0); |
|
327 |
path.closeSubpath(); |
|
328 |
return path; |
|
329 |
} |
|
330 |
//! [40] |