1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "norwegianwoodstyle.h"
33
void NorwegianWoodStyle::polish(QPalette &palette)
35
QColor brown(212, 140, 95);
36
QColor beige(236, 182, 120);
37
QColor slightlyOpaqueBlack(0, 0, 0, 63);
39
QPixmap backgroundImage(":/images/woodbackground.png");
40
QPixmap buttonImage(":/images/woodbutton.png");
41
QPixmap midImage = buttonImage;
44
painter.begin(&midImage);
45
painter.setPen(Qt::NoPen);
46
painter.fillRect(midImage.rect(), slightlyOpaqueBlack);
49
palette = QPalette(brown);
51
palette.setBrush(QPalette::BrightText, Qt::white);
52
palette.setBrush(QPalette::Base, beige);
53
palette.setBrush(QPalette::Highlight, Qt::darkGreen);
54
setTexture(palette, QPalette::Button, buttonImage);
55
setTexture(palette, QPalette::Mid, midImage);
56
setTexture(palette, QPalette::Background, backgroundImage);
58
QBrush brush = palette.background();
59
brush.setColor(brush.color().dark());
61
palette.setBrush(QPalette::Disabled, QPalette::Foreground, brush);
62
palette.setBrush(QPalette::Disabled, QPalette::Text, brush);
63
palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush);
64
palette.setBrush(QPalette::Disabled, QPalette::Base, brush);
65
palette.setBrush(QPalette::Disabled, QPalette::Button, brush);
66
palette.setBrush(QPalette::Disabled, QPalette::Mid, brush);
69
void NorwegianWoodStyle::polish(QWidget *widget)
71
if (qobject_cast<QPushButton *>(widget)
72
|| qobject_cast<QComboBox *>(widget))
73
widget->setAttribute(Qt::WA_Hover, true);
76
void NorwegianWoodStyle::unpolish(QWidget *widget)
78
if (qobject_cast<QPushButton *>(widget)
79
|| qobject_cast<QComboBox *>(widget))
80
widget->setAttribute(Qt::WA_Hover, false);
83
int NorwegianWoodStyle::pixelMetric(PixelMetric metric,
84
const QStyleOption *option,
85
const QWidget *widget) const
88
case PM_ComboBoxFrameWidth:
90
case PM_ScrollBarExtent:
91
return QMotifStyle::pixelMetric(metric, option, widget) + 4;
93
return QMotifStyle::pixelMetric(metric, option, widget);
97
int NorwegianWoodStyle::styleHint(StyleHint hint, const QStyleOption *option,
98
const QWidget *widget,
99
QStyleHintReturn *returnData) const
102
case SH_DitherDisabledText:
104
case SH_EtchDisabledText:
107
return QMotifStyle::styleHint(hint, option, widget, returnData);
111
void NorwegianWoodStyle::drawPrimitive(PrimitiveElement element,
112
const QStyleOption *option,
114
const QWidget *widget) const
117
case PE_PanelButtonCommand:
119
int delta = (option->state & State_MouseOver) ? 64 : 0;
120
QColor slightlyOpaqueBlack(0, 0, 0, 63);
121
QColor semiTransparentWhite(255, 255, 255, 127 + delta);
122
QColor semiTransparentBlack(0, 0, 0, 127 - delta);
124
int x, y, width, height;
125
option->rect.getRect(&x, &y, &width, &height);
127
QPainterPath roundRect = roundRectPath(option->rect);
128
int radius = qMin(width, height) / 2;
133
const QStyleOptionButton *buttonOption =
134
qstyleoption_cast<const QStyleOptionButton *>(option);
136
&& (buttonOption->features & QStyleOptionButton::Flat)) {
137
brush = option->palette.background();
138
darker = (option->state & (State_Sunken | State_On));
140
if (option->state & (State_Sunken | State_On)) {
141
brush = option->palette.mid();
142
darker = !(option->state & State_Sunken);
144
brush = option->palette.button();
150
painter->setRenderHint(QPainter::Antialiasing, true);
151
painter->fillPath(roundRect, brush);
153
painter->fillPath(roundRect, slightlyOpaqueBlack);
158
else if (radius < 20)
163
QPen topPen(semiTransparentWhite, penWidth);
164
QPen bottomPen(semiTransparentBlack, penWidth);
166
if (option->state & (State_Sunken | State_On))
167
qSwap(topPen, bottomPen);
171
int x3 = x + width - radius;
174
if (option->direction == Qt::RightToLeft) {
180
topHalf << QPoint(x1, y)
182
<< QPoint(x3, y + radius)
183
<< QPoint(x2, y + height - radius)
184
<< QPoint(x1, y + height);
186
painter->setClipPath(roundRect);
187
painter->setClipRegion(topHalf, Qt::IntersectClip);
188
painter->setPen(topPen);
189
painter->drawPath(roundRect);
191
QPolygon bottomHalf = topHalf;
192
bottomHalf[0] = QPoint(x4, y + height);
194
painter->setClipPath(roundRect);
195
painter->setClipRegion(bottomHalf, Qt::IntersectClip);
196
painter->setPen(bottomPen);
197
painter->drawPath(roundRect);
199
painter->setPen(option->palette.foreground().color());
200
painter->setClipping(false);
201
painter->drawPath(roundRect);
207
QMotifStyle::drawPrimitive(element, option, painter, widget);
211
void NorwegianWoodStyle::drawControl(ControlElement element,
212
const QStyleOption *option,
214
const QWidget *widget) const
217
case CE_PushButtonLabel:
219
QStyleOptionButton myButtonOption;
220
const QStyleOptionButton *buttonOption =
221
qstyleoption_cast<const QStyleOptionButton *>(option);
223
myButtonOption = *buttonOption;
224
if (myButtonOption.palette.currentColorGroup()
225
!= QPalette::Disabled) {
226
if (myButtonOption.state & (State_Sunken | State_On)) {
227
myButtonOption.palette.setBrush(QPalette::ButtonText,
228
myButtonOption.palette.brightText());
232
QMotifStyle::drawControl(element, &myButtonOption, painter, widget);
236
QMotifStyle::drawControl(element, option, painter, widget);
240
void NorwegianWoodStyle::setTexture(QPalette &palette, QPalette::ColorRole role,
241
const QPixmap &pixmap)
243
for (int i = 0; i < QPalette::NColorGroups; ++i) {
244
QColor color = palette.brush(QPalette::ColorGroup(i), role).color();
245
palette.setBrush(QPalette::ColorGroup(i), role, QBrush(color, pixmap));
249
QPainterPath NorwegianWoodStyle::roundRectPath(const QRect &rect)
251
int radius = qMin(rect.width(), rect.height()) / 2;
252
int diam = 2 * radius;
255
rect.getCoords(&x1, &y1, &x2, &y2);
258
path.moveTo(x2, y1 + radius);
259
path.arcTo(QRect(x2 - diam, y1, diam, diam), 0.0, +90.0);
260
path.lineTo(x1 + radius, y1);
261
path.arcTo(QRect(x1, y1, diam, diam), 90.0, +90.0);
262
path.lineTo(x1, y2 - radius);
263
path.arcTo(QRect(x1, y2 - diam, diam, diam), 180.0, +90.0);
264
path.lineTo(x1 + radius, y2);
265
path.arcTo(QRect(x2 - diam, y2 - diam, diam, diam), 270.0, +90.0);