~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/widgets/styles/norwegianwoodstyle.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include "norwegianwoodstyle.h"
 
32
 
 
33
void NorwegianWoodStyle::polish(QPalette &palette)
 
34
{
 
35
    QColor brown(212, 140, 95);
 
36
    QColor beige(236, 182, 120);
 
37
    QColor slightlyOpaqueBlack(0, 0, 0, 63);
 
38
 
 
39
    QPixmap backgroundImage(":/images/woodbackground.png");
 
40
    QPixmap buttonImage(":/images/woodbutton.png");
 
41
    QPixmap midImage = buttonImage;
 
42
 
 
43
    QPainter painter;
 
44
    painter.begin(&midImage);
 
45
    painter.setPen(Qt::NoPen);
 
46
    painter.fillRect(midImage.rect(), slightlyOpaqueBlack);
 
47
    painter.end();
 
48
 
 
49
    palette = QPalette(brown);
 
50
 
 
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);
 
57
 
 
58
    QBrush brush = palette.background();
 
59
    brush.setColor(brush.color().dark());
 
60
 
 
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);
 
67
}
 
68
 
 
69
void NorwegianWoodStyle::polish(QWidget *widget)
 
70
{
 
71
    if (qobject_cast<QPushButton *>(widget)
 
72
            || qobject_cast<QComboBox *>(widget))
 
73
        widget->setAttribute(Qt::WA_Hover, true);
 
74
}
 
75
 
 
76
void NorwegianWoodStyle::unpolish(QWidget *widget)
 
77
{
 
78
    if (qobject_cast<QPushButton *>(widget)
 
79
            || qobject_cast<QComboBox *>(widget))
 
80
        widget->setAttribute(Qt::WA_Hover, false);
 
81
}
 
82
 
 
83
int NorwegianWoodStyle::pixelMetric(PixelMetric metric,
 
84
                                    const QStyleOption *option,
 
85
                                    const QWidget *widget) const
 
86
{
 
87
    switch (metric) {
 
88
    case PM_ComboBoxFrameWidth:
 
89
        return 8;
 
90
    case PM_ScrollBarExtent:
 
91
        return QMotifStyle::pixelMetric(metric, option, widget) + 4;
 
92
    default:
 
93
        return QMotifStyle::pixelMetric(metric, option, widget);
 
94
    }
 
95
}
 
96
 
 
97
int NorwegianWoodStyle::styleHint(StyleHint hint, const QStyleOption *option,
 
98
                                  const QWidget *widget,
 
99
                                  QStyleHintReturn *returnData) const
 
100
{
 
101
    switch (hint) {
 
102
    case SH_DitherDisabledText:
 
103
        return int(false);
 
104
    case SH_EtchDisabledText:
 
105
        return int(true);
 
106
    default:
 
107
        return QMotifStyle::styleHint(hint, option, widget, returnData);
 
108
    }
 
109
}
 
110
 
 
111
void NorwegianWoodStyle::drawPrimitive(PrimitiveElement element,
 
112
                                       const QStyleOption *option,
 
113
                                       QPainter *painter,
 
114
                                       const QWidget *widget) const
 
115
{
 
116
    switch (element) {
 
117
    case PE_PanelButtonCommand:
 
118
        {
 
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);
 
123
 
 
124
            int x, y, width, height;
 
125
            option->rect.getRect(&x, &y, &width, &height);
 
126
 
 
127
            QPainterPath roundRect = roundRectPath(option->rect);
 
128
            int radius = qMin(width, height) / 2;
 
129
 
 
130
            QBrush brush;
 
131
            bool darker;
 
132
 
 
133
            const QStyleOptionButton *buttonOption =
 
134
                    qstyleoption_cast<const QStyleOptionButton *>(option);
 
135
            if (buttonOption
 
136
                    && (buttonOption->features & QStyleOptionButton::Flat)) {
 
137
                brush = option->palette.background();
 
138
                darker = (option->state & (State_Sunken | State_On));
 
139
            } else {
 
140
                if (option->state & (State_Sunken | State_On)) {
 
141
                    brush = option->palette.mid();
 
142
                    darker = !(option->state & State_Sunken);
 
143
                } else {
 
144
                    brush = option->palette.button();
 
145
                    darker = false;
 
146
                }
 
147
            }
 
148
 
 
149
            painter->save();
 
150
            painter->setRenderHint(QPainter::Antialiasing, true);
 
151
            painter->fillPath(roundRect, brush);
 
152
            if (darker)
 
153
                painter->fillPath(roundRect, slightlyOpaqueBlack);
 
154
 
 
155
            int penWidth;
 
156
            if (radius < 10)
 
157
                penWidth = 3;
 
158
            else if (radius < 20)
 
159
                penWidth = 5;
 
160
            else
 
161
                penWidth = 7;
 
162
 
 
163
            QPen topPen(semiTransparentWhite, penWidth);
 
164
            QPen bottomPen(semiTransparentBlack, penWidth);
 
165
 
 
166
            if (option->state & (State_Sunken | State_On))
 
167
                qSwap(topPen, bottomPen);
 
168
 
 
169
            int x1 = x;
 
170
            int x2 = x + radius;
 
171
            int x3 = x + width - radius;
 
172
            int x4 = x + width;
 
173
 
 
174
            if (option->direction == Qt::RightToLeft) {
 
175
                qSwap(x1, x4);
 
176
                qSwap(x2, x3);
 
177
            }
 
178
 
 
179
            QPolygon topHalf;
 
180
            topHalf << QPoint(x1, y)
 
181
                    << QPoint(x4, y)
 
182
                    << QPoint(x3, y + radius)
 
183
                    << QPoint(x2, y + height - radius)
 
184
                    << QPoint(x1, y + height);
 
185
 
 
186
            painter->setClipPath(roundRect);
 
187
            painter->setClipRegion(topHalf, Qt::IntersectClip);
 
188
            painter->setPen(topPen);
 
189
            painter->drawPath(roundRect);
 
190
 
 
191
            QPolygon bottomHalf = topHalf;
 
192
            bottomHalf[0] = QPoint(x4, y + height);
 
193
 
 
194
            painter->setClipPath(roundRect);
 
195
            painter->setClipRegion(bottomHalf, Qt::IntersectClip);
 
196
            painter->setPen(bottomPen);
 
197
            painter->drawPath(roundRect);
 
198
 
 
199
            painter->setPen(option->palette.foreground().color());
 
200
            painter->setClipping(false);
 
201
            painter->drawPath(roundRect);
 
202
 
 
203
            painter->restore();
 
204
        }
 
205
        break;
 
206
    default:
 
207
        QMotifStyle::drawPrimitive(element, option, painter, widget);
 
208
    }
 
209
}
 
210
 
 
211
void NorwegianWoodStyle::drawControl(ControlElement element,
 
212
                                     const QStyleOption *option,
 
213
                                     QPainter *painter,
 
214
                                     const QWidget *widget) const
 
215
{
 
216
    switch (element) {
 
217
    case CE_PushButtonLabel:
 
218
        {
 
219
            QStyleOptionButton myButtonOption;
 
220
            const QStyleOptionButton *buttonOption =
 
221
                    qstyleoption_cast<const QStyleOptionButton *>(option);
 
222
            if (buttonOption) {
 
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());
 
229
                    }
 
230
                }
 
231
            }
 
232
            QMotifStyle::drawControl(element, &myButtonOption, painter, widget);
 
233
        }
 
234
        break;
 
235
    default:
 
236
        QMotifStyle::drawControl(element, option, painter, widget);
 
237
    }
 
238
}
 
239
 
 
240
void NorwegianWoodStyle::setTexture(QPalette &palette, QPalette::ColorRole role,
 
241
                                    const QPixmap &pixmap)
 
242
{
 
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));
 
246
    }
 
247
}
 
248
 
 
249
QPainterPath NorwegianWoodStyle::roundRectPath(const QRect &rect)
 
250
{
 
251
    int radius = qMin(rect.width(), rect.height()) / 2;
 
252
    int diam = 2 * radius;
 
253
 
 
254
    int x1, y1, x2, y2;
 
255
    rect.getCoords(&x1, &y1, &x2, &y2);
 
256
 
 
257
    QPainterPath path;
 
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);
 
266
    path.closeSubpath();
 
267
    return path;
 
268
}