author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
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 QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qcommandlinkbutton.h" |
|
43 |
#include "qstylepainter.h" |
|
44 |
#include "qstyleoption.h" |
|
45 |
#include "qtextdocument.h" |
|
46 |
#include "qtextlayout.h" |
|
47 |
#include "qcolor.h" |
|
48 |
#include "qfont.h" |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
49 |
#include <qmath.h> |
0 | 50 |
|
51 |
#include "private/qpushbutton_p.h" |
|
52 |
||
53 |
QT_BEGIN_NAMESPACE |
|
54 |
||
55 |
/*! |
|
56 |
\class QCommandLinkButton |
|
57 |
\since 4.4 |
|
58 |
\brief The QCommandLinkButton widget provides a Vista style command link button. |
|
59 |
||
60 |
\ingroup basicwidgets |
|
61 |
||
62 |
||
63 |
The command link is a new control that was introduced by Windows Vista. It's |
|
64 |
intended use is similar to that of a radio button in that it is used to choose |
|
65 |
between a set of mutually exclusive options. Command link buttons should not |
|
66 |
be used by themselves but rather as an alternative to radio buttons in |
|
67 |
Wizards and dialogs and makes pressing the "next" button redundant. |
|
68 |
The appearance is generally similar to that of a flat pushbutton, but |
|
69 |
it allows for a descriptive text in addition to the normal button text. |
|
70 |
By default it will also carry an arrow icon, indicating that pressing the |
|
71 |
control will open another window or page. |
|
72 |
||
73 |
\sa QPushButton QRadioButton |
|
74 |
*/ |
|
75 |
||
76 |
/*! |
|
77 |
\property QCommandLinkButton::description |
|
78 |
\brief A descriptive label to complement the button text |
|
79 |
||
80 |
Setting this property will set a descriptive text on the |
|
81 |
button, complementing the text label. This will usually |
|
82 |
be displayed in a smaller font than the primary text. |
|
83 |
*/ |
|
84 |
||
85 |
/*! |
|
86 |
\property QCommandLinkButton::flat |
|
87 |
\brief This property determines whether the button is displayed as a flat |
|
88 |
panel or with a border. |
|
89 |
||
90 |
By default, this property is set to false. |
|
91 |
||
92 |
\sa QPushButton::flat |
|
93 |
*/ |
|
94 |
||
95 |
class QCommandLinkButtonPrivate : public QPushButtonPrivate |
|
96 |
{ |
|
97 |
Q_DECLARE_PUBLIC(QCommandLinkButton) |
|
98 |
||
99 |
public: |
|
100 |
QCommandLinkButtonPrivate() |
|
101 |
: QPushButtonPrivate(){} |
|
102 |
||
103 |
void init(); |
|
104 |
qreal titleSize() const; |
|
105 |
bool usingVistaStyle() const; |
|
106 |
||
107 |
QFont titleFont() const; |
|
108 |
QFont descriptionFont() const; |
|
109 |
||
110 |
QRect titleRect() const; |
|
111 |
QRect descriptionRect() const; |
|
112 |
||
113 |
int textOffset() const; |
|
114 |
int descriptionOffset() const; |
|
115 |
int descriptionHeight(int width) const; |
|
116 |
QColor mergedColors(const QColor &a, const QColor &b, int value) const; |
|
117 |
||
118 |
int topMargin() const { return 10; } |
|
119 |
int leftMargin() const { return 7; } |
|
120 |
int rightMargin() const { return 4; } |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
121 |
int bottomMargin() const { return 10; } |
0 | 122 |
|
123 |
QString description; |
|
124 |
QColor currentColor; |
|
125 |
}; |
|
126 |
||
127 |
// Mix colors a and b with a ratio in the range [0-255] |
|
128 |
QColor QCommandLinkButtonPrivate::mergedColors(const QColor &a, const QColor &b, int value = 50) const |
|
129 |
{ |
|
130 |
Q_ASSERT(value >= 0); |
|
131 |
Q_ASSERT(value <= 255); |
|
132 |
QColor tmp = a; |
|
133 |
tmp.setRed((tmp.red() * value) / 255 + (b.red() * (255 - value)) / 255); |
|
134 |
tmp.setGreen((tmp.green() * value) / 255 + (b.green() * (255 - value)) / 255); |
|
135 |
tmp.setBlue((tmp.blue() * value) / 255 + (b.blue() * (255 - value)) / 255); |
|
136 |
return tmp; |
|
137 |
} |
|
138 |
||
139 |
QFont QCommandLinkButtonPrivate::titleFont() const |
|
140 |
{ |
|
141 |
Q_Q(const QCommandLinkButton); |
|
142 |
QFont font = q->font(); |
|
143 |
if (usingVistaStyle()) { |
|
144 |
font.setPointSizeF(12.0); |
|
145 |
} else { |
|
146 |
font.setBold(true); |
|
147 |
font.setPointSizeF(9.0); |
|
148 |
} |
|
149 |
||
150 |
// Note the font will be resolved against |
|
151 |
// QPainters font, so we need to restore the mask |
|
152 |
int resolve_mask = font.resolve_mask; |
|
153 |
QFont modifiedFont = q->font().resolve(font); |
|
154 |
modifiedFont.detach(); |
|
155 |
modifiedFont.resolve_mask = resolve_mask; |
|
156 |
return modifiedFont; |
|
157 |
} |
|
158 |
||
159 |
QFont QCommandLinkButtonPrivate::descriptionFont() const |
|
160 |
{ |
|
161 |
Q_Q(const QCommandLinkButton); |
|
162 |
QFont font = q->font(); |
|
163 |
font.setPointSizeF(9.0); |
|
164 |
||
165 |
// Note the font will be resolved against |
|
166 |
// QPainters font, so we need to restore the mask |
|
167 |
int resolve_mask = font.resolve_mask; |
|
168 |
QFont modifiedFont = q->font().resolve(font); |
|
169 |
modifiedFont.detach(); |
|
170 |
modifiedFont.resolve_mask = resolve_mask; |
|
171 |
return modifiedFont; |
|
172 |
} |
|
173 |
||
174 |
QRect QCommandLinkButtonPrivate::titleRect() const |
|
175 |
{ |
|
176 |
Q_Q(const QCommandLinkButton); |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
177 |
QRect r = q->rect().adjusted(textOffset(), topMargin(), -rightMargin(), 0); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
178 |
if (description.isEmpty()) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
179 |
{ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
180 |
QFontMetrics fm(titleFont()); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
181 |
r.setTop(r.top() + qMax(0, (q->icon().actualSize(q->iconSize()).height() |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
182 |
- fm.height()) / 2)); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
183 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
184 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
185 |
return r; |
0 | 186 |
} |
187 |
||
188 |
QRect QCommandLinkButtonPrivate::descriptionRect() const |
|
189 |
{ |
|
190 |
Q_Q(const QCommandLinkButton); |
|
191 |
return q->rect().adjusted(textOffset(), descriptionOffset(), |
|
192 |
-rightMargin(), -bottomMargin()); |
|
193 |
} |
|
194 |
||
195 |
int QCommandLinkButtonPrivate::textOffset() const |
|
196 |
{ |
|
197 |
Q_Q(const QCommandLinkButton); |
|
198 |
return q->icon().actualSize(q->iconSize()).width() + leftMargin() + 6; |
|
199 |
} |
|
200 |
||
201 |
int QCommandLinkButtonPrivate::descriptionOffset() const |
|
202 |
{ |
|
203 |
QFontMetrics fm(titleFont()); |
|
204 |
return topMargin() + fm.height(); |
|
205 |
} |
|
206 |
||
207 |
bool QCommandLinkButtonPrivate::usingVistaStyle() const |
|
208 |
{ |
|
209 |
Q_Q(const QCommandLinkButton); |
|
210 |
//### This is a hack to detect if we are indeed running Vista style themed and not in classic |
|
211 |
// When we add api to query for this, we should change this implementation to use it. |
|
212 |
return q->style()->inherits("QWindowsVistaStyle") |
|
213 |
&& !q->style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal); |
|
214 |
} |
|
215 |
||
216 |
void QCommandLinkButtonPrivate::init() |
|
217 |
{ |
|
218 |
Q_Q(QCommandLinkButton); |
|
219 |
QPushButtonPrivate::init(); |
|
220 |
q->setAttribute(Qt::WA_Hover); |
|
221 |
||
222 |
QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::PushButton); |
|
223 |
policy.setHeightForWidth(true); |
|
224 |
q->setSizePolicy(policy); |
|
225 |
||
226 |
q->setIconSize(QSize(20, 20)); |
|
227 |
QStyleOptionButton opt; |
|
228 |
q->initStyleOption(&opt); |
|
229 |
q->setIcon(q->style()->standardIcon(QStyle::SP_CommandLink, &opt)); |
|
230 |
} |
|
231 |
||
232 |
// Calculates the height of the description text based on widget width |
|
233 |
int QCommandLinkButtonPrivate::descriptionHeight(int widgetWidth) const |
|
234 |
{ |
|
235 |
// Calc width of actual paragraph |
|
236 |
int lineWidth = widgetWidth - textOffset() - rightMargin(); |
|
237 |
||
238 |
qreal descriptionheight = 0; |
|
239 |
if (!description.isEmpty()) { |
|
240 |
QTextLayout layout(description); |
|
241 |
layout.setFont(descriptionFont()); |
|
242 |
layout.beginLayout(); |
|
243 |
while (true) { |
|
244 |
QTextLine line = layout.createLine(); |
|
245 |
if (!line.isValid()) |
|
246 |
break; |
|
247 |
line.setLineWidth(lineWidth); |
|
248 |
line.setPosition(QPointF(0, descriptionheight)); |
|
249 |
descriptionheight += line.height(); |
|
250 |
} |
|
251 |
layout.endLayout(); |
|
252 |
} |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
253 |
return qCeil(descriptionheight); |
0 | 254 |
} |
255 |
||
256 |
/*! |
|
257 |
\reimp |
|
258 |
*/ |
|
259 |
QSize QCommandLinkButton::minimumSizeHint() const |
|
260 |
{ |
|
261 |
Q_D(const QCommandLinkButton); |
|
262 |
QSize size = sizeHint(); |
|
263 |
int minimumHeight = qMax(d->descriptionOffset() + d->bottomMargin(), |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
264 |
icon().actualSize(iconSize()).height() + d->topMargin()); |
0 | 265 |
size.setHeight(minimumHeight); |
266 |
return size; |
|
267 |
} |
|
268 |
||
269 |
/*! |
|
270 |
Constructs a command link with no text and a \a parent. |
|
271 |
*/ |
|
272 |
||
273 |
QCommandLinkButton::QCommandLinkButton(QWidget *parent) |
|
274 |
: QPushButton(*new QCommandLinkButtonPrivate, parent) |
|
275 |
{ |
|
276 |
Q_D(QCommandLinkButton); |
|
277 |
d->init(); |
|
278 |
} |
|
279 |
||
280 |
/*! |
|
281 |
Constructs a command link with the parent \a parent and the text \a |
|
282 |
text. |
|
283 |
*/ |
|
284 |
||
285 |
QCommandLinkButton::QCommandLinkButton(const QString &text, QWidget *parent) |
|
286 |
: QPushButton(*new QCommandLinkButtonPrivate, parent) |
|
287 |
{ |
|
288 |
Q_D(QCommandLinkButton); |
|
289 |
setText(text); |
|
290 |
d->init(); |
|
291 |
} |
|
292 |
||
293 |
/*! |
|
294 |
Constructs a command link with a \a text, a \a description, and a \a parent. |
|
295 |
*/ |
|
296 |
QCommandLinkButton::QCommandLinkButton(const QString &text, const QString &description, QWidget *parent) |
|
297 |
: QPushButton(*new QCommandLinkButtonPrivate, parent) |
|
298 |
{ |
|
299 |
Q_D(QCommandLinkButton); |
|
300 |
setText(text); |
|
301 |
setDescription(description); |
|
302 |
d->init(); |
|
303 |
} |
|
304 |
||
305 |
/*! \reimp */ |
|
306 |
bool QCommandLinkButton::event(QEvent *e) |
|
307 |
{ |
|
308 |
return QPushButton::event(e); |
|
309 |
} |
|
310 |
||
311 |
/*! \reimp */ |
|
312 |
QSize QCommandLinkButton::sizeHint() const |
|
313 |
{ |
|
314 |
// Standard size hints from UI specs |
|
315 |
// Without note: 135, 41 |
|
316 |
// With note: 135, 60 |
|
317 |
Q_D(const QCommandLinkButton); |
|
318 |
||
319 |
QSize size = QPushButton::sizeHint(); |
|
320 |
QFontMetrics fm(d->titleFont()); |
|
321 |
int textWidth = qMax(fm.width(text()), 135); |
|
322 |
int buttonWidth = textWidth + d->textOffset() + d->rightMargin(); |
|
323 |
int heightWithoutDescription = d->descriptionOffset() + d->bottomMargin(); |
|
324 |
||
325 |
size.setWidth(qMax(size.width(), buttonWidth)); |
|
326 |
size.setHeight(qMax(d->description.isEmpty() ? 41 : 60, |
|
327 |
heightWithoutDescription + d->descriptionHeight(buttonWidth))); |
|
328 |
return size; |
|
329 |
} |
|
330 |
||
331 |
/*! \reimp */ |
|
332 |
int QCommandLinkButton::heightForWidth(int width) const |
|
333 |
{ |
|
334 |
Q_D(const QCommandLinkButton); |
|
335 |
int heightWithoutDescription = d->descriptionOffset() + d->bottomMargin(); |
|
336 |
// find the width available for the description area |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
337 |
return qMax(heightWithoutDescription + d->descriptionHeight(width), |
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
338 |
icon().actualSize(iconSize()).height() + d->topMargin() + |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
339 |
d->bottomMargin()); |
0 | 340 |
} |
341 |
||
342 |
/*! \reimp */ |
|
343 |
void QCommandLinkButton::paintEvent(QPaintEvent *) |
|
344 |
{ |
|
345 |
Q_D(QCommandLinkButton); |
|
346 |
QStylePainter p(this); |
|
347 |
p.save(); |
|
348 |
||
349 |
QStyleOptionButton option; |
|
350 |
initStyleOption(&option); |
|
351 |
||
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
352 |
//Enable command link appearance on Vista |
0 | 353 |
option.features |= QStyleOptionButton::CommandLinkButton; |
354 |
option.text = QString(); |
|
355 |
option.icon = QIcon(); //we draw this ourselves |
|
356 |
QSize pixmapSize = icon().actualSize(iconSize()); |
|
357 |
||
358 |
int vOffset = isDown() ? style()->pixelMetric(QStyle::PM_ButtonShiftVertical) : 0; |
|
359 |
int hOffset = isDown() ? style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal) : 0; |
|
360 |
||
361 |
//Draw icon |
|
362 |
p.drawControl(QStyle::CE_PushButton, option); |
|
363 |
if (!icon().isNull()) |
|
364 |
p.drawPixmap(d->leftMargin() + hOffset, d->topMargin() + vOffset, |
|
365 |
icon().pixmap(pixmapSize, isEnabled() ? QIcon::Normal : QIcon::Disabled, |
|
366 |
isChecked() ? QIcon::On : QIcon::Off)); |
|
367 |
||
368 |
//Draw title |
|
369 |
QColor textColor = palette().buttonText().color(); |
|
370 |
if (isEnabled() && d->usingVistaStyle()) { |
|
371 |
textColor = QColor(21, 28, 85); |
|
372 |
if (underMouse() && !isDown()) |
|
373 |
textColor = QColor(7, 64, 229); |
|
374 |
//A simple text color transition |
|
375 |
d->currentColor = d->mergedColors(textColor, d->currentColor, 60); |
|
376 |
option.palette.setColor(QPalette::ButtonText, d->currentColor); |
|
377 |
} |
|
378 |
||
379 |
int textflags = Qt::TextShowMnemonic; |
|
380 |
if (!style()->styleHint(QStyle::SH_UnderlineShortcut, &option, this)) |
|
381 |
textflags |= Qt::TextHideMnemonic; |
|
382 |
||
383 |
p.setFont(d->titleFont()); |
|
384 |
p.drawItemText(d->titleRect().translated(hOffset, vOffset), |
|
385 |
textflags, option.palette, isEnabled(), text(), QPalette::ButtonText); |
|
386 |
||
387 |
//Draw description |
|
388 |
textflags |= Qt::TextWordWrap | Qt::ElideRight; |
|
389 |
p.setFont(d->descriptionFont()); |
|
390 |
p.drawItemText(d->descriptionRect().translated(hOffset, vOffset), textflags, |
|
391 |
option.palette, isEnabled(), description(), QPalette::ButtonText); |
|
392 |
p.restore(); |
|
393 |
} |
|
394 |
||
395 |
void QCommandLinkButton::setDescription(const QString &description) |
|
396 |
{ |
|
397 |
Q_D(QCommandLinkButton); |
|
398 |
d->description = description; |
|
399 |
updateGeometry(); |
|
400 |
update(); |
|
401 |
} |
|
402 |
||
403 |
QString QCommandLinkButton::description() const |
|
404 |
{ |
|
405 |
Q_D(const QCommandLinkButton); |
|
406 |
return d->description; |
|
407 |
} |
|
408 |
||
409 |
QT_END_NAMESPACE |
|
410 |