1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QtGui>
42
43 #include "norwegianwoodstyle.h"
44
45 //! [0]
polish(QPalette & palette)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]
polish(QWidget * widget)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]
unpolish(QWidget * widget)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]
pixelMetric(PixelMetric metric,const QStyleOption * option,const QWidget * widget) const106 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]
styleHint(StyleHint hint,const QStyleOption * option,const QWidget * widget,QStyleHintReturn * returnData) const123 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]
drawPrimitive(PrimitiveElement element,const QStyleOption * option,QPainter * painter,const QWidget * widget) const140 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]
drawControl(ControlElement element,const QStyleOption * option,QPainter * painter,const QWidget * widget) const265 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]
setTexture(QPalette & palette,QPalette::ColorRole role,const QPixmap & pixmap)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]
roundRectPath(const QRect & rect)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]
331