156
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
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 qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41 module norwegianwoodstyle;
|
|
42
|
|
43
|
|
44 import qt.gui.QMotifStyle;
|
|
45 import qt.gui.QPalette;
|
|
46 import qt.gui.QPainterPath;
|
|
47 import qt.gui.QPushButton;
|
|
48 import qt.gui.QComboBox;
|
|
49
|
|
50
|
|
51 class NorwegianWoodStyle : public QMotifStyle
|
|
52 {
|
|
53 public:
|
|
54
|
|
55 this() {}
|
|
56
|
|
57 void polish(QPalette palette)
|
|
58 {
|
|
59 auto brown = new QColor(212, 140, 95);
|
|
60 auto beige = new QColor(236, 182, 120);
|
|
61 auto slightlyOpaqueBlack = new QColor(0, 0, 0, 63);
|
|
62
|
|
63 auto backgroundImage = new QPixmap(":/images/woodbackground.png");
|
|
64 auto buttonImage = new QPixmap(":/images/woodbutton.png");
|
|
65 QPixmap midImage = buttonImage;
|
|
66
|
|
67 auto painter = new QPainter;
|
|
68 painter.begin(midImage);
|
|
69 painter.setPen(Qt.NoPen);
|
|
70 painter.fillRect(midImage.rect(), slightlyOpaqueBlack);
|
|
71 painter.end();
|
|
72
|
|
73 palette = new QPalette(brown);
|
|
74
|
|
75 palette.setBrush(QPalette.BrightText, new QBrush(QColor.White));
|
|
76 palette.setBrush(QPalette.Base, new QBrush(beige));
|
|
77 palette.setBrush(QPalette.Highlight, new QBrush(QColor.DarkGreen));
|
|
78 setTexture(palette, QPalette.Button, buttonImage);
|
|
79 setTexture(palette, QPalette.Mid, midImage);
|
|
80 setTexture(palette, QPalette.Window, backgroundImage);
|
|
81
|
|
82 QBrush brush = palette.brush(QPalette.Window); //.background();
|
|
83 brush.setColor(brush.color().darker());
|
|
84
|
|
85 palette.setBrush(QPalette.Disabled, QPalette.WindowText, brush);
|
|
86 palette.setBrush(QPalette.Disabled, QPalette.Text, brush);
|
|
87 palette.setBrush(QPalette.Disabled, QPalette.ButtonText, brush);
|
|
88 palette.setBrush(QPalette.Disabled, QPalette.Base, brush);
|
|
89 palette.setBrush(QPalette.Disabled, QPalette.Button, brush);
|
|
90 palette.setBrush(QPalette.Disabled, QPalette.Mid, brush);
|
|
91 }
|
|
92
|
|
93 void polish(QWidget widget)
|
|
94 {
|
|
95 if (cast(QPushButton) widget || cast(QComboBox) widget)
|
|
96 widget.setAttribute(Qt.WA_Hover, true);
|
|
97 }
|
|
98
|
|
99 void unpolish(QWidget widget)
|
|
100 {
|
|
101 if (cast(QPushButton) widget || cast(QComboBox) widget)
|
|
102 widget.setAttribute(Qt.WA_Hover, false);
|
|
103 }
|
|
104
|
|
105 int pixelMetric(PixelMetric metric, QStyleOption option, QWidget widget)
|
|
106 {
|
|
107 switch (metric) {
|
|
108 case PM_ComboBoxFrameWidth:
|
|
109 return 8;
|
|
110 case PM_ScrollBarExtent:
|
|
111 return QMotifStyle.pixelMetric(metric, option, widget) + 4;
|
|
112 default:
|
|
113 return QMotifStyle.pixelMetric(metric, option, widget);
|
|
114 }
|
|
115 }
|
|
116
|
|
117 int styleHint(StyleHint hint, QStyleOption option, QWidget widget, QStyleHintReturn returnData)
|
|
118 {
|
|
119 switch (hint) {
|
|
120 case SH_DitherDisabledText:
|
|
121 return cast(int) false;
|
|
122 case SH_EtchDisabledText:
|
|
123 return cast(int) true;
|
|
124 default:
|
|
125 return QMotifStyle.styleHint(hint, option, widget, returnData);
|
|
126 }
|
|
127 }
|
|
128
|
|
129 void drawPrimitive(PrimitiveElement element, QStyleOption option, QPainter painter, QWidget widget)
|
|
130 {
|
|
131 switch (element) {
|
|
132 case PE_PanelButtonCommand:
|
|
133 {
|
|
134 int delta = (option.state & State_MouseOver) ? 64 : 0;
|
|
135 auto slightlyOpaqueBlack = new QColor(0, 0, 0, 63);
|
|
136 auto semiTransparentWhite = new QColor(255, 255, 255, 127 + delta);
|
|
137 auto semiTransparentBlack = new QColor(0, 0, 0, 127 - delta);
|
|
138
|
|
139 int x, y, width, height;
|
|
140 option.rect.getRect(&x, &y, &width, &height);
|
|
141
|
|
142 QPainterPath roundRect = roundRectPath(option.rect);
|
|
143 int radius = qMin(width, height) / 2;
|
|
144
|
|
145 QBrush brush;
|
|
146 bool darker;
|
|
147
|
|
148 QStyleOptionButton buttonOption = cast(QStyleOptionButton) option;
|
|
149 if (buttonOption && (buttonOption.features & QStyleOptionButton.Flat)) {
|
|
150 brush = option.palette.brush(QPalette.Window); //background();
|
|
151 darker = cast(bool) (option.state & (State_Sunken | State_On));
|
|
152 } else {
|
|
153 if (option.state & (State_Sunken | State_On)) {
|
|
154 brush = option.palette.mid();
|
|
155 darker = !(option.state & State_Sunken);
|
|
156 } else {
|
|
157 brush = option.palette.button();
|
|
158 darker = false;
|
|
159 }
|
|
160 }
|
|
161
|
|
162 painter.save();
|
|
163 painter.setRenderHint(QPainter.Antialiasing, true);
|
|
164 painter.fillPath(roundRect, brush);
|
|
165 if (darker)
|
|
166 painter.fillPath(roundRect, new QBrush(slightlyOpaqueBlack));
|
|
167
|
|
168 int penWidth;
|
|
169
|
|
170 if (radius < 10)
|
|
171 penWidth = 3;
|
|
172 else if (radius < 20)
|
|
173 penWidth = 5;
|
|
174 else
|
|
175 penWidth = 7;
|
|
176
|
|
177 auto topPen = new QPen(semiTransparentWhite);
|
|
178 auto bottomPen = new QPen(semiTransparentBlack);
|
|
179
|
|
180 topPen.setWidth(penWidth);
|
|
181 bottomPen.setWidth(penWidth);
|
|
182
|
|
183 if (option.state & (State_Sunken | State_On))
|
|
184 qSwap(topPen, bottomPen);
|
|
185
|
|
186 int x1 = x;
|
|
187 int x2 = x + radius;
|
|
188 int x3 = x + width - radius;
|
|
189 int x4 = x + width;
|
|
190
|
|
191 if (option.direction == Qt.RightToLeft) {
|
|
192 qSwap(x1, x4);
|
|
193 qSwap(x2, x3);
|
|
194 }
|
|
195
|
|
196 auto topHalf = new QPolygon();
|
|
197 topHalf.append(QPoint(x1, y));
|
|
198 topHalf.append(QPoint(x4, y));
|
|
199 topHalf.append(QPoint(x3, y + radius));
|
|
200 topHalf.append(QPoint(x2, y + height - radius));
|
|
201 topHalf.append(QPoint(x1, y + height));
|
|
202
|
|
203 painter.setClipPath(roundRect);
|
|
204 painter.setClipRegion(new QRegion(topHalf), Qt.IntersectClip);
|
|
205 painter.setPen(topPen);
|
|
206 painter.drawPath(roundRect);
|
|
207
|
|
208 QPolygon bottomHalf = topHalf;
|
|
209 bottomHalf.replace(0, QPoint(x4, y + height));
|
|
210
|
|
211 painter.setClipPath(roundRect);
|
|
212 painter.setClipRegion(new QRegion(bottomHalf), Qt.IntersectClip);
|
|
213 painter.setPen(bottomPen);
|
|
214 painter.drawPath(roundRect);
|
|
215
|
|
216 painter.setPen(option.palette.brush(QPalette.WindowText).color());
|
|
217 painter.setClipping(false);
|
|
218 painter.drawPath(roundRect);
|
|
219
|
|
220 painter.restore();
|
|
221 }
|
|
222 break;
|
|
223 default:
|
|
224 QMotifStyle.drawPrimitive(element, option, painter, widget);
|
|
225 }
|
|
226 }
|
|
227
|
|
228 void drawControl(ControlElement element, QStyleOption option, QPainter painter, QWidget widget)
|
|
229 {
|
|
230 switch (element) {
|
|
231 case CE_PushButtonLabel:
|
|
232 {
|
|
233 QStyleOptionButton myButtonOption;
|
|
234 QStyleOptionButton buttonOption = cast(QStyleOptionButton) option;
|
|
235 if (buttonOption) {
|
|
236 myButtonOption = buttonOption;
|
|
237 if (myButtonOption.palette.currentColorGroup() != QPalette.Disabled) {
|
|
238 if (myButtonOption.state & (State_Sunken | State_On)) {
|
|
239 myButtonOption.palette.setBrush(QPalette.ButtonText,
|
|
240 myButtonOption.palette.brightText());
|
|
241 }
|
|
242 }
|
|
243 }
|
|
244 QMotifStyle.drawControl(element, myButtonOption, painter, widget);
|
|
245 }
|
|
246 break;
|
|
247 default:
|
|
248 QMotifStyle.drawControl(element, option, painter, widget);
|
|
249 }
|
|
250 }
|
|
251
|
|
252 private:
|
|
253
|
|
254 static void setTexture(QPalette palette, QPalette.ColorRole role, QPixmap pixmap)
|
|
255 {
|
|
256 for (int i = 0; i < QPalette.NColorGroups; ++i) {
|
|
257 QColor color = palette.brush(cast(QPalette.ColorGroup) i, role).color();
|
|
258 palette.setBrush(cast(QPalette.ColorGroup) i, role, new QBrush(color, pixmap));
|
|
259 }
|
|
260 }
|
|
261
|
|
262 static QPainterPath roundRectPath(QRect rect)
|
|
263 {
|
|
264 int radius = qMin(rect.width(), rect.height()) / 2;
|
|
265 int diam = 2 * radius;
|
|
266
|
|
267 int x1, y1, x2, y2;
|
|
268 rect.getCoords(&x1, &y1, &x2, &y2);
|
|
269
|
|
270 auto path = new QPainterPath;
|
|
271 path.moveTo(x2, y1 + radius);
|
|
272 path.arcTo(QRectF(x2 - diam, y1, diam, diam), 0.0, +90.0);
|
|
273 path.lineTo(x1 + radius, y1);
|
|
274 path.arcTo(QRectF(x1, y1, diam, diam), 90.0, +90.0);
|
|
275 path.lineTo(x1, y2 - radius);
|
|
276 path.arcTo(QRectF(x1, y2 - diam, diam, diam), 180.0, +90.0);
|
|
277 path.lineTo(x1 + radius, y2);
|
|
278 path.arcTo(QRectF(x2 - diam, y2 - diam, diam, diam), 270.0, +90.0);
|
|
279 path.closeSubpath();
|
|
280 return path;
|
|
281 }
|
|
282 }
|