source: trunk/src/gui/styles/qstylesheetstyle.cpp@ 628

Last change on this file since 628 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 232.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
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** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <qglobal.h>
43
44#ifndef QT_NO_STYLE_STYLESHEET
45
46#include "qstylesheetstyle_p.h"
47#include "private/qcssutil_p.h"
48#include <qdebug.h>
49#include <qapplication.h>
50#include <qmenu.h>
51#include <qmenubar.h>
52#include <qpainter.h>
53#include <qstyleoption.h>
54#include <qlineedit.h>
55#include <qwindowsstyle.h>
56#include <qcombobox.h>
57#include <qwindowsstyle.h>
58#include <qplastiquestyle.h>
59#include "private/qcssparser_p.h"
60#include "private/qmath_p.h"
61#include <qabstractscrollarea.h>
62#include "private/qabstractscrollarea_p.h"
63#include <qtooltip.h>
64#include <qshareddata.h>
65#include <qradiobutton.h>
66#include <qtoolbutton.h>
67#include <qscrollbar.h>
68#include <qstring.h>
69#include <qfile.h>
70#include <qcheckbox.h>
71#include <qstatusbar.h>
72#include <qheaderview.h>
73#include <qprogressbar.h>
74#include <private/qwindowsstyle_p.h>
75#include <qtabbar.h>
76#include <QMetaProperty>
77#include <qmainwindow.h>
78#include <qdockwidget.h>
79#include <qmdisubwindow.h>
80#include <qdialog.h>
81#include <private/qwidget_p.h>
82#include <QAbstractSpinBox>
83#include <QLabel>
84#include "qdrawutil.h"
85
86#include <limits.h>
87#include <QtGui/qtoolbar.h>
88
89QT_BEGIN_NAMESPACE
90
91using namespace QCss;
92
93
94class QStyleSheetStylePrivate : public QWindowsStylePrivate
95{
96 Q_DECLARE_PUBLIC(QStyleSheetStyle)
97public:
98 QStyleSheetStylePrivate() { }
99};
100
101
102static QHash<const QWidget *, QVector<StyleRule> > *styleRulesCache = 0;
103static QHash<const QWidget *, QHash<int, bool> > *hasStyleRuleCache = 0;
104typedef QHash<int, QHash<quint64, QRenderRule> > QRenderRules;
105static QHash<const QWidget *, QRenderRules> *renderRulesCache = 0;
106static QHash<const QWidget *, QPalette> *customPaletteWidgets = 0; // widgets whose palette we tampered
107static QHash<const void *, StyleSheet> *styleSheetCache = 0; // parsed style sheets
108static QSet<const QWidget *> *autoFillDisabledWidgets = 0;
109
110
111/* RECURSION_GUARD:
112 * the QStyleSheetStyle is a proxy. If used with others proxy style, we may end up with something like:
113 * QStyleSheetStyle -> ProxyStyle -> QStyleSheetStyle -> OriginalStyle
114 * Recursion may happen if the style call the widget()->style() again.
115 * Not to mention the performence penalty of having two lookup of rules.
116 *
117 * The first instance of QStyleSheetStyle will set globalStyleSheetStyle to itself. The second one
118 * will notice the globalStyleSheetStyle is not istelf and call its base style directly.
119 */
120static const QStyleSheetStyle *globalStyleSheetStyle = 0;
121class QStyleSheetStyleRecursionGuard
122{
123 public:
124 QStyleSheetStyleRecursionGuard(const QStyleSheetStyle *that)
125 : guarded(globalStyleSheetStyle == 0)
126 {
127 if (guarded) globalStyleSheetStyle = that;
128 }
129 ~QStyleSheetStyleRecursionGuard() { if (guarded) globalStyleSheetStyle = 0; }
130 bool guarded;
131};
132#define RECURSION_GUARD(RETURN) \
133 if (globalStyleSheetStyle != 0 && globalStyleSheetStyle != this) { RETURN; } \
134 QStyleSheetStyleRecursionGuard recursion_guard(this);
135
136#define ceil(x) ((int)(x) + ((x) > 0 && (x) != (int)(x)))
137
138enum PseudoElement {
139 PseudoElement_None,
140 PseudoElement_DownArrow,
141 PseudoElement_UpArrow,
142 PseudoElement_LeftArrow,
143 PseudoElement_RightArrow,
144 PseudoElement_Indicator,
145 PseudoElement_ExclusiveIndicator,
146 PseudoElement_PushButtonMenuIndicator,
147 PseudoElement_ComboBoxDropDown,
148 PseudoElement_ComboBoxArrow,
149 PseudoElement_Item,
150 PseudoElement_SpinBoxUpButton,
151 PseudoElement_SpinBoxUpArrow,
152 PseudoElement_SpinBoxDownButton,
153 PseudoElement_SpinBoxDownArrow,
154 PseudoElement_GroupBoxTitle,
155 PseudoElement_GroupBoxIndicator,
156 PseudoElement_ToolButtonMenu,
157 PseudoElement_ToolButtonMenuArrow,
158 PseudoElement_ToolButtonDownArrow,
159 PseudoElement_ToolBoxTab,
160 PseudoElement_ScrollBarSlider,
161 PseudoElement_ScrollBarAddPage,
162 PseudoElement_ScrollBarSubPage,
163 PseudoElement_ScrollBarAddLine,
164 PseudoElement_ScrollBarSubLine,
165 PseudoElement_ScrollBarFirst,
166 PseudoElement_ScrollBarLast,
167 PseudoElement_ScrollBarUpArrow,
168 PseudoElement_ScrollBarDownArrow,
169 PseudoElement_ScrollBarLeftArrow,
170 PseudoElement_ScrollBarRightArrow,
171 PseudoElement_SplitterHandle,
172 PseudoElement_ToolBarHandle,
173 PseudoElement_ToolBarSeparator,
174 PseudoElement_MenuScroller,
175 PseudoElement_MenuTearoff,
176 PseudoElement_MenuCheckMark,
177 PseudoElement_MenuSeparator,
178 PseudoElement_MenuIcon,
179 PseudoElement_MenuRightArrow,
180 PseudoElement_TreeViewBranch,
181 PseudoElement_HeaderViewSection,
182 PseudoElement_HeaderViewUpArrow,
183 PseudoElement_HeaderViewDownArrow,
184 PseudoElement_ProgressBarChunk,
185 PseudoElement_TabBarTab,
186 PseudoElement_TabBarScroller,
187 PseudoElement_TabBarTear,
188 PseudoElement_SliderGroove,
189 PseudoElement_SliderHandle,
190 PseudoElement_SliderAddPage,
191 PseudoElement_SliderSubPage,
192 PseudoElement_SliderTickmark,
193 PseudoElement_TabWidgetPane,
194 PseudoElement_TabWidgetTabBar,
195 PseudoElement_TabWidgetLeftCorner,
196 PseudoElement_TabWidgetRightCorner,
197 PseudoElement_DockWidgetTitle,
198 PseudoElement_DockWidgetCloseButton,
199 PseudoElement_DockWidgetFloatButton,
200 PseudoElement_DockWidgetSeparator,
201 PseudoElement_MdiCloseButton,
202 PseudoElement_MdiMinButton,
203 PseudoElement_MdiNormalButton,
204 PseudoElement_TitleBar,
205 PseudoElement_TitleBarCloseButton,
206 PseudoElement_TitleBarMinButton,
207 PseudoElement_TitleBarMaxButton,
208 PseudoElement_TitleBarShadeButton,
209 PseudoElement_TitleBarUnshadeButton,
210 PseudoElement_TitleBarNormalButton,
211 PseudoElement_TitleBarContextHelpButton,
212 PseudoElement_TitleBarSysMenu,
213 PseudoElement_ViewItem,
214 PseudoElement_ViewItemIcon,
215 PseudoElement_ViewItemText,
216 PseudoElement_ViewItemIndicator,
217 PseudoElement_ScrollAreaCorner,
218 PseudoElement_TabBarTabCloseButton,
219 NumPseudoElements
220};
221
222struct PseudoElementInfo {
223 QStyle::SubControl subControl;
224 const char *name;
225};
226
227static const PseudoElementInfo knownPseudoElements[NumPseudoElements] = {
228 { QStyle::SC_None, "" },
229 { QStyle::SC_None, "down-arrow" },
230 { QStyle::SC_None, "up-arrow" },
231 { QStyle::SC_None, "left-arrow" },
232 { QStyle::SC_None, "right-arrow" },
233 { QStyle::SC_None, "indicator" },
234 { QStyle::SC_None, "indicator" },
235 { QStyle::SC_None, "menu-indicator" },
236 { QStyle::SC_ComboBoxArrow, "drop-down" },
237 { QStyle::SC_ComboBoxArrow, "down-arrow" },
238 { QStyle::SC_None, "item" },
239 { QStyle::SC_SpinBoxUp, "up-button" },
240 { QStyle::SC_SpinBoxUp, "up-arrow" },
241 { QStyle::SC_SpinBoxDown, "down-button" },
242 { QStyle::SC_SpinBoxDown, "down-arrow" },
243 { QStyle::SC_GroupBoxLabel, "title" },
244 { QStyle::SC_GroupBoxCheckBox, "indicator" },
245 { QStyle::SC_ToolButtonMenu, "menu-button" },
246 { QStyle::SC_ToolButtonMenu, "menu-arrow" },
247 { QStyle::SC_None, "menu-indicator" },
248 { QStyle::SC_None, "tab" },
249 { QStyle::SC_ScrollBarSlider, "handle" },
250 { QStyle::SC_ScrollBarAddPage, "add-page" },
251 { QStyle::SC_ScrollBarSubPage, "sub-page" },
252 { QStyle::SC_ScrollBarAddLine, "add-line" },
253 { QStyle::SC_ScrollBarSubLine, "sub-line" },
254 { QStyle::SC_ScrollBarFirst, "first" },
255 { QStyle::SC_ScrollBarLast, "last" },
256 { QStyle::SC_ScrollBarSubLine, "up-arrow" },
257 { QStyle::SC_ScrollBarAddLine, "down-arrow" },
258 { QStyle::SC_ScrollBarSubLine, "left-arrow" },
259 { QStyle::SC_ScrollBarAddLine, "right-arrow" },
260 { QStyle::SC_None, "handle" },
261 { QStyle::SC_None, "handle" },
262 { QStyle::SC_None, "separator" },
263 { QStyle::SC_None, "scroller" },
264 { QStyle::SC_None, "tearoff" },
265 { QStyle::SC_None, "indicator" },
266 { QStyle::SC_None, "separator" },
267 { QStyle::SC_None, "icon" },
268 { QStyle::SC_None, "right-arrow" },
269 { QStyle::SC_None, "branch" },
270 { QStyle::SC_None, "section" },
271 { QStyle::SC_None, "down-arrow" },
272 { QStyle::SC_None, "up-arrow" },
273 { QStyle::SC_None, "chunk" },
274 { QStyle::SC_None, "tab" },
275 { QStyle::SC_None, "scroller" },
276 { QStyle::SC_None, "tear" },
277 { QStyle::SC_SliderGroove, "groove" },
278 { QStyle::SC_SliderHandle, "handle" },
279 { QStyle::SC_None, "add-page" },
280 { QStyle::SC_None, "sub-page"},
281 { QStyle::SC_SliderTickmarks, "tick-mark" },
282 { QStyle::SC_None, "pane" },
283 { QStyle::SC_None, "tab-bar" },
284 { QStyle::SC_None, "left-corner" },
285 { QStyle::SC_None, "right-corner" },
286 { QStyle::SC_None, "title" },
287 { QStyle::SC_None, "close-button" },
288 { QStyle::SC_None, "float-button" },
289 { QStyle::SC_None, "separator" },
290 { QStyle::SC_MdiCloseButton, "close-button" },
291 { QStyle::SC_MdiMinButton, "minimize-button" },
292 { QStyle::SC_MdiNormalButton, "normal-button" },
293 { QStyle::SC_TitleBarLabel, "title" },
294 { QStyle::SC_TitleBarCloseButton, "close-button" },
295 { QStyle::SC_TitleBarMinButton, "minimize-button" },
296 { QStyle::SC_TitleBarMaxButton, "maximize-button" },
297 { QStyle::SC_TitleBarShadeButton, "shade-button" },
298 { QStyle::SC_TitleBarUnshadeButton, "unshade-button" },
299 { QStyle::SC_TitleBarNormalButton, "normal-button" },
300 { QStyle::SC_TitleBarContextHelpButton, "contexthelp-button" },
301 { QStyle::SC_TitleBarSysMenu, "sys-menu" },
302 { QStyle::SC_None, "item" },
303 { QStyle::SC_None, "icon" },
304 { QStyle::SC_None, "text" },
305 { QStyle::SC_None, "indicator" },
306 { QStyle::SC_None, "corner" },
307 { QStyle::SC_None, "close-button" },
308};
309
310
311struct QStyleSheetBorderImageData : public QSharedData
312{
313 QStyleSheetBorderImageData()
314 : horizStretch(QCss::TileMode_Unknown), vertStretch(QCss::TileMode_Unknown)
315 {
316 for (int i = 0; i < 4; i++)
317 cuts[i] = -1;
318 }
319 int cuts[4];
320 QPixmap pixmap;
321 QImage image;
322 QCss::TileMode horizStretch, vertStretch;
323};
324
325struct QStyleSheetBackgroundData : public QSharedData
326{
327 QStyleSheetBackgroundData(const QBrush& b, const QPixmap& p, QCss::Repeat r,
328 Qt::Alignment a, QCss::Origin o, Attachment t, QCss::Origin c)
329 : brush(b), pixmap(p), repeat(r), position(a), origin(o), attachment(t), clip(c) { }
330
331 bool isTransparent() const {
332 if (brush.style() != Qt::NoBrush)
333 return !brush.isOpaque();
334 return pixmap.isNull() ? false : pixmap.hasAlpha();
335 }
336 QBrush brush;
337 QPixmap pixmap;
338 QCss::Repeat repeat;
339 Qt::Alignment position;
340 QCss::Origin origin;
341 QCss::Attachment attachment;
342 QCss::Origin clip;
343};
344
345struct QStyleSheetBorderData : public QSharedData
346{
347 QStyleSheetBorderData() : bi(0)
348 {
349 for (int i = 0; i < 4; i++) {
350 borders[i] = 0;
351 styles[i] = QCss::BorderStyle_None;
352 }
353 }
354
355 QStyleSheetBorderData(int *b, QBrush *c, QCss::BorderStyle *s, QSize *r) : bi(0)
356 {
357 for (int i = 0; i < 4; i++) {
358 borders[i] = b[i];
359 styles[i] = s[i];
360 colors[i] = c[i];
361 radii[i] = r[i];
362 }
363 }
364
365 int borders[4];
366 QBrush colors[4];
367 QCss::BorderStyle styles[4];
368 QSize radii[4]; // topleft, topright, bottomleft, bottomright
369
370 const QStyleSheetBorderImageData *borderImage() const
371 { return bi; }
372 bool hasBorderImage() const { return bi!=0; }
373
374 QSharedDataPointer<QStyleSheetBorderImageData> bi;
375
376 bool isOpaque() const
377 {
378 for (int i = 0; i < 4; i++) {
379 if (styles[i] == QCss::BorderStyle_Native || styles[i] == QCss::BorderStyle_None)
380 continue;
381 if (styles[i] >= QCss::BorderStyle_Dotted && styles[i] <= QCss::BorderStyle_DotDotDash
382 && styles[i] != BorderStyle_Solid)
383 return false;
384 if (!colors[i].isOpaque())
385 return false;
386 if (!radii[i].isEmpty())
387 return false;
388 }
389 if (bi != 0 && bi->pixmap.hasAlpha())
390 return false;
391 return true;
392 }
393};
394
395
396struct QStyleSheetOutlineData : public QStyleSheetBorderData
397{
398 QStyleSheetOutlineData()
399 {
400 for (int i = 0; i < 4; i++) {
401 offsets[i] = 0;
402 }
403 }
404
405 QStyleSheetOutlineData(int *b, QBrush *c, QCss::BorderStyle *s, QSize *r, int *o)
406 : QStyleSheetBorderData(b, c, s, r)
407 {
408 for (int i = 0; i < 4; i++) {
409 offsets[i] = o[i];
410 }
411 }
412
413 int offsets[4];
414};
415
416struct QStyleSheetBoxData : public QSharedData
417{
418 QStyleSheetBoxData(int *m, int *p, int s) : spacing(s)
419 {
420 for (int i = 0; i < 4; i++) {
421 margins[i] = m[i];
422 paddings[i] = p[i];
423 }
424 }
425
426 int margins[4];
427 int paddings[4];
428
429 int spacing;
430};
431
432struct QStyleSheetPaletteData : public QSharedData
433{
434 QStyleSheetPaletteData(const QBrush &fg, const QBrush &sfg, const QBrush &sbg,
435 const QBrush &abg)
436 : foreground(fg), selectionForeground(sfg), selectionBackground(sbg),
437 alternateBackground(abg) { }
438
439 QBrush foreground;
440 QBrush selectionForeground;
441 QBrush selectionBackground;
442 QBrush alternateBackground;
443};
444
445struct QStyleSheetGeometryData : public QSharedData
446{
447 QStyleSheetGeometryData(int w, int h, int minw, int minh, int maxw, int maxh)
448 : minWidth(minw), minHeight(minh), width(w), height(h), maxWidth(maxw), maxHeight(maxh) { }
449
450 int minWidth, minHeight, width, height, maxWidth, maxHeight;
451};
452
453struct QStyleSheetPositionData : public QSharedData
454{
455 QStyleSheetPositionData(int l, int t, int r, int b, Origin o, Qt::Alignment p, QCss::PositionMode m, Qt::Alignment a = 0)
456 : left(l), top(t), bottom(b), right(r), origin(o), position(p), mode(m), textAlignment(a) { }
457
458 int left, top, bottom, right;
459 Origin origin;
460 Qt::Alignment position;
461 QCss::PositionMode mode;
462 Qt::Alignment textAlignment;
463};
464
465struct QStyleSheetImageData : public QSharedData
466{
467 QStyleSheetImageData(const QIcon &i, Qt::Alignment a, const QSize &sz)
468 : icon(i), alignment(a), size(sz) { }
469
470 QIcon icon;
471 Qt::Alignment alignment;
472 QSize size;
473};
474
475class QRenderRule
476{
477public:
478 QRenderRule() : features(0), hasFont(false), pal(0), b(0), bg(0), bd(0), ou(0), geo(0), p(0), img(0), clipset(0) { }
479 QRenderRule(const QVector<QCss::Declaration> &, const QWidget *);
480 ~QRenderRule() { }
481
482 QRect borderRect(const QRect &r) const;
483 QRect outlineRect(const QRect &r) const;
484 QRect paddingRect(const QRect &r) const;
485 QRect contentsRect(const QRect &r) const;
486
487 enum { Margin = 1, Border = 2, Padding = 4, All=Margin|Border|Padding };
488 QRect boxRect(const QRect &r, int flags = All) const;
489 QSize boxSize(const QSize &s, int flags = All) const;
490 QRect originRect(const QRect &rect, Origin origin) const;
491
492 QPainterPath borderClip(QRect rect);
493 void drawBorder(QPainter *, const QRect&);
494 void drawOutline(QPainter *, const QRect&);
495 void drawBorderImage(QPainter *, const QRect&);
496 void drawBackground(QPainter *, const QRect&, const QPoint& = QPoint(0, 0));
497 void drawBackgroundImage(QPainter *, const QRect&, QPoint = QPoint(0, 0));
498 void drawFrame(QPainter *, const QRect&);
499 void drawImage(QPainter *p, const QRect &rect);
500 void drawRule(QPainter *, const QRect&);
501 void configurePalette(QPalette *, QPalette::ColorGroup, const QWidget *, bool);
502 void configurePalette(QPalette *p, QPalette::ColorRole fr, QPalette::ColorRole br);
503
504 const QStyleSheetPaletteData *palette() const { return pal; }
505 const QStyleSheetBoxData *box() const { return b; }
506 const QStyleSheetBackgroundData *background() const { return bg; }
507 const QStyleSheetBorderData *border() const { return bd; }
508 const QStyleSheetOutlineData *outline() const { return ou; }
509 const QStyleSheetGeometryData *geometry() const { return geo; }
510 const QStyleSheetPositionData *position() const { return p; }
511
512 bool hasPalette() const { return pal != 0; }
513 bool hasBackground() const { return bg != 0 && (!bg->pixmap.isNull() || bg->brush.style() != Qt::NoBrush); }
514 bool hasGradientBackground() const { return bg && bg->brush.style() >= Qt::LinearGradientPattern
515 && bg->brush.style() <= Qt::ConicalGradientPattern; }
516
517 bool hasNativeBorder() const {
518 return bd == 0
519 || (!bd->hasBorderImage() && bd->styles[0] == BorderStyle_Native);
520 }
521
522 bool hasNativeOutline() const {
523 return (ou == 0
524 || (!ou->hasBorderImage() && ou->styles[0] == BorderStyle_Native));
525 }
526
527 bool baseStyleCanDraw() const {
528 if (!hasBackground() || (background()->brush.style() == Qt::NoBrush && bg->pixmap.isNull()))
529 return true;
530 if (bg && !bg->pixmap.isNull())
531 return false;
532 if (hasGradientBackground())
533 return features & StyleFeature_BackgroundGradient;
534 return features & StyleFeature_BackgroundColor;
535 }
536
537 bool hasBox() const { return b != 0; }
538 bool hasBorder() const { return bd != 0; }
539 bool hasOutline() const { return ou != 0; }
540 bool hasPosition() const { return p != 0; }
541 bool hasGeometry() const { return geo != 0; }
542 bool hasDrawable() const { return !hasNativeBorder() || hasBackground() || hasImage(); }
543 bool hasImage() const { return img != 0; }
544
545 QSize minimumContentsSize() const
546 { return geo ? QSize(geo->minWidth, geo->minHeight) : QSize(0, 0); }
547 QSize minimumSize() const
548 { return boxSize(minimumContentsSize()); }
549
550 QSize contentsSize() const
551 { return geo ? QSize(geo->width, geo->height)
552 : ((img && img->size.isValid()) ? img->size : QSize()); }
553 QSize contentsSize(const QSize &sz) const
554 {
555 QSize csz = contentsSize();
556 if (csz.width() == -1) csz.setWidth(sz.width());
557 if (csz.height() == -1) csz.setHeight(sz.height());
558 return csz;
559 }
560 bool hasContentsSize() const
561 { return (geo && (geo->width != -1 || geo->height != -1)) || (img && img->size.isValid()); }
562
563 QSize size() const { return boxSize(contentsSize()); }
564 QSize size(const QSize &sz) const { return boxSize(contentsSize(sz)); }
565 QSize adjustSize(const QSize &sz)
566 {
567 if (!geo)
568 return sz;
569 QSize csz = contentsSize();
570 if (csz.width() == -1) csz.setWidth(sz.width());
571 if (csz.height() == -1) csz.setHeight(sz.height());
572 if (geo->maxWidth != -1 && csz.width() > geo->maxWidth) csz.setWidth(geo->maxWidth);
573 if (geo->maxHeight != -1 && csz.height() > geo->maxHeight) csz.setHeight(geo->maxHeight);
574 csz=csz.expandedTo(QSize(geo->minWidth, geo->minHeight));
575 return csz;
576 }
577
578 int features;
579 QBrush defaultBackground;
580 QFont font;
581 bool hasFont;
582
583 QHash<QString, QVariant> styleHints;
584 bool hasStyleHint(const QString& sh) const { return styleHints.contains(sh); }
585 QVariant styleHint(const QString& sh) const { return styleHints.value(sh); }
586
587 void fixupBorder(int);
588
589 QSharedDataPointer<QStyleSheetPaletteData> pal;
590 QSharedDataPointer<QStyleSheetBoxData> b;
591 QSharedDataPointer<QStyleSheetBackgroundData> bg;
592 QSharedDataPointer<QStyleSheetBorderData> bd;
593 QSharedDataPointer<QStyleSheetOutlineData> ou;
594 QSharedDataPointer<QStyleSheetGeometryData> geo;
595 QSharedDataPointer<QStyleSheetPositionData> p;
596 QSharedDataPointer<QStyleSheetImageData> img;
597
598 // Shouldn't be here
599 void setClip(QPainter *p, const QRect &rect);
600 void unsetClip(QPainter *);
601 int clipset;
602 QPainterPath clipPath;
603};
604
605///////////////////////////////////////////////////////////////////////////////////////////
606static const char *knownStyleHints[] = {
607 "activate-on-singleclick",
608 "alignment",
609 "arrow-keys-navigate-into-children",
610 "backward-icon",
611 "button-layout",
612 "cd-icon",
613 "combobox-list-mousetracking",
614 "combobox-popup",
615 "computer-icon",
616 "desktop-icon",
617 "dialog-apply-icon",
618 "dialog-cancel-icon",
619 "dialog-close-icon",
620 "dialog-discard-icon",
621 "dialog-help-icon",
622 "dialog-no-icon",
623 "dialog-ok-icon",
624 "dialog-open-icon",
625 "dialog-reset-icon",
626 "dialog-save-icon",
627 "dialog-yes-icon",
628 "dialogbuttonbox-buttons-have-icons",
629 "directory-closed-icon",
630 "directory-icon",
631 "directory-link-icon",
632 "directory-open-icon",
633 "dither-disable-text",
634 "dockwidget-close-icon",
635 "downarrow-icon",
636 "dvd-icon",
637 "etch-disabled-text",
638 "file-icon",
639 "file-link-icon",
640 "filedialog-backward-icon", // unused
641 "filedialog-contentsview-icon",
642 "filedialog-detailedview-icon",
643 "filedialog-end-icon",
644 "filedialog-infoview-icon",
645 "filedialog-listview-icon",
646 "filedialog-new-directory-icon",
647 "filedialog-parent-directory-icon",
648 "filedialog-start-icon",
649 "floppy-icon",
650 "forward-icon",
651 "gridline-color",
652 "harddisk-icon",
653 "home-icon",
654 "icon-size",
655 "leftarrow-icon",
656 "lineedit-password-character",
657 "mdi-fill-space-on-maximize",
658 "menu-scrollable",
659 "menubar-altkey-navigation",
660 "menubar-separator",
661 "messagebox-critical-icon",
662 "messagebox-information-icon",
663 "messagebox-question-icon",
664 "messagebox-text-interaction-flags",
665 "messagebox-warning-icon",
666 "mouse-tracking",
667 "network-icon",
668 "opacity",
669 "paint-alternating-row-colors-for-empty-area",
670 "rightarrow-icon",
671 "scrollbar-contextmenu",
672 "scrollbar-leftclick-absolute-position",
673 "scrollbar-middleclick-absolute-position",
674 "scrollbar-roll-between-buttons",
675 "scrollbar-scroll-when-pointer-leaves-control",
676 "scrollview-frame-around-contents",
677 "show-decoration-selected",
678 "spinbox-click-autorepeat-rate",
679 "spincontrol-disable-on-bounds",
680 "tabbar-elide-mode",
681 "tabbar-prefer-no-arrows",
682 "titlebar-close-icon",
683 "titlebar-contexthelp-icon",
684 "titlebar-maximize-icon",
685 "titlebar-menu-icon",
686 "titlebar-minimize-icon",
687 "titlebar-normal-icon",
688 "titlebar-shade-icon",
689 "titlebar-unshade-icon",
690 "toolbutton-popup-delay",
691 "trash-icon",
692 "uparrow-icon"
693};
694
695static const int numKnownStyleHints = sizeof(knownStyleHints)/sizeof(knownStyleHints[0]);
696
697static QList<QVariant> subControlLayout(const QString& layout)
698{
699 QList<QVariant> buttons;
700 for (int i = 0; i < layout.count(); i++) {
701 int button = layout[i].toAscii();
702 switch (button) {
703 case 'm':
704 buttons.append(PseudoElement_MdiMinButton);
705 buttons.append(PseudoElement_TitleBarMinButton);
706 break;
707 case 'M':
708 buttons.append(PseudoElement_TitleBarMaxButton);
709 break;
710 case 'X':
711 buttons.append(PseudoElement_MdiCloseButton);
712 buttons.append(PseudoElement_TitleBarCloseButton);
713 break;
714 case 'N':
715 buttons.append(PseudoElement_MdiNormalButton);
716 buttons.append(PseudoElement_TitleBarNormalButton);
717 break;
718 case 'I':
719 buttons.append(PseudoElement_TitleBarSysMenu);
720 break;
721 case 'T':
722 buttons.append(PseudoElement_TitleBar);
723 break;
724 case 'H':
725 buttons.append(PseudoElement_TitleBarContextHelpButton);
726 break;
727 case 'S':
728 buttons.append(PseudoElement_TitleBarShadeButton);
729 break;
730 default:
731 buttons.append(button);
732 break;
733 }
734 }
735 return buttons;
736}
737
738namespace {
739 struct ButtonInfo {
740 QRenderRule rule;
741 int element;
742 int offset;
743 int where;
744 int width;
745 };
746}
747
748QHash<QStyle::SubControl, QRect> QStyleSheetStyle::titleBarLayout(const QWidget *w, const QStyleOptionTitleBar *tb) const
749{
750 QHash<QStyle::SubControl, QRect> layoutRects;
751 const bool isMinimized = tb->titleBarState & Qt::WindowMinimized;
752 const bool isMaximized = tb->titleBarState & Qt::WindowMaximized;
753 QRenderRule subRule = renderRule(w, tb);
754 QRect cr = subRule.contentsRect(tb->rect);
755 QList<QVariant> layout = subRule.styleHint(QLatin1String("button-layout")).toList();
756 if (layout.isEmpty())
757 layout = subControlLayout(QLatin1String("I(T)HSmMX"));
758
759 int offsets[3] = { 0, 0, 0 };
760 enum Where { Left, Right, Center, NoWhere } where = Left;
761 QList<ButtonInfo> infos;
762 for (int i = 0; i < layout.count(); i++) {
763 ButtonInfo info;
764 info.element = layout[i].toInt();
765 if (info.element == '(') {
766 where = Center;
767 } else if (info.element == ')') {
768 where = Right;
769 } else {
770 switch (info.element) {
771 case PseudoElement_TitleBar:
772 if (!(tb->titleBarFlags & (Qt::WindowTitleHint | Qt::WindowSystemMenuHint)))
773 continue;
774 break;
775 case PseudoElement_TitleBarContextHelpButton:
776 if (!(tb->titleBarFlags & Qt::WindowContextHelpButtonHint))
777 continue;
778 break;
779 case PseudoElement_TitleBarMinButton:
780 if (!(tb->titleBarFlags & Qt::WindowMinimizeButtonHint))
781 continue;
782 if (isMinimized)
783 info.element = PseudoElement_TitleBarNormalButton;
784 break;
785 case PseudoElement_TitleBarMaxButton:
786 if (!(tb->titleBarFlags & Qt::WindowMaximizeButtonHint))
787 continue;
788 if (isMaximized)
789 info.element = PseudoElement_TitleBarNormalButton;
790 break;
791 case PseudoElement_TitleBarShadeButton:
792 if (!(tb->titleBarFlags & Qt::WindowShadeButtonHint))
793 continue;
794 if (isMinimized)
795 info.element = PseudoElement_TitleBarUnshadeButton;
796 break;
797 case PseudoElement_TitleBarCloseButton:
798 case PseudoElement_TitleBarSysMenu:
799 if (!(tb->titleBarFlags & Qt::WindowSystemMenuHint))
800 continue;
801 break;
802 default:
803 continue;
804 }
805 if (info.element == PseudoElement_TitleBar) {
806 info.width = tb->fontMetrics.width(tb->text) + 6;
807 subRule.geo = new QStyleSheetGeometryData(info.width, tb->fontMetrics.height(), -1, -1, -1, -1);
808 } else {
809 subRule = renderRule(w, tb, info.element);
810 info.width = subRule.size().width();
811 }
812 info.rule = subRule;
813 info.offset = offsets[where];
814 info.where = where;
815 infos.append(info);
816
817 offsets[where] += info.width;
818 }
819 }
820
821 for (int i = 0; i < infos.count(); i++) {
822 ButtonInfo info = infos[i];
823 QRect lr = cr;
824 switch (info.where) {
825 case Center: {
826 lr.setLeft(cr.left() + offsets[Left]);
827 lr.setRight(cr.right() - offsets[Right]);
828 QRect r(0, 0, offsets[Center], lr.height());
829 r.moveCenter(lr.center());
830 r.setLeft(r.left()+info.offset);
831 r.setWidth(info.width);
832 lr = r;
833 break; }
834 case Left:
835 lr.translate(info.offset, 0);
836 lr.setWidth(info.width);
837 break;
838 case Right:
839 lr.moveLeft(cr.right() + 1 - offsets[Right] + info.offset);
840 lr.setWidth(info.width);
841 break;
842 default:
843 break;
844 }
845 QStyle::SubControl control = knownPseudoElements[info.element].subControl;
846 layoutRects[control] = positionRect(w, info.rule, info.element, lr, tb->direction);
847 }
848
849 return layoutRects;
850}
851
852static QStyle::StandardPixmap subControlIcon(int pe)
853{
854 switch (pe) {
855 case PseudoElement_MdiCloseButton: return QStyle::SP_TitleBarCloseButton;
856 case PseudoElement_MdiMinButton: return QStyle::SP_TitleBarMinButton;
857 case PseudoElement_MdiNormalButton: return QStyle::SP_TitleBarNormalButton;
858 case PseudoElement_TitleBarCloseButton: return QStyle::SP_TitleBarCloseButton;
859 case PseudoElement_TitleBarMinButton: return QStyle::SP_TitleBarMinButton;
860 case PseudoElement_TitleBarMaxButton: return QStyle::SP_TitleBarMaxButton;
861 case PseudoElement_TitleBarShadeButton: return QStyle::SP_TitleBarShadeButton;
862 case PseudoElement_TitleBarUnshadeButton: return QStyle::SP_TitleBarUnshadeButton;
863 case PseudoElement_TitleBarNormalButton: return QStyle::SP_TitleBarNormalButton;
864 case PseudoElement_TitleBarContextHelpButton: return QStyle::SP_TitleBarContextHelpButton;
865 default: break;
866 }
867 return QStyle::SP_CustomBase;
868}
869
870QRenderRule::QRenderRule(const QVector<Declaration> &declarations, const QWidget *widget)
871: features(0), hasFont(false), pal(0), b(0), bg(0), bd(0), ou(0), geo(0), p(0), img(0), clipset(0)
872{
873 QPalette palette = QApplication::palette(); // ###: ideally widget's palette
874 ValueExtractor v(declarations, palette);
875 features = v.extractStyleFeatures();
876
877 int w = -1, h = -1, minw = -1, minh = -1, maxw = -1, maxh = -1;
878 if (v.extractGeometry(&w, &h, &minw, &minh, &maxw, &maxh))
879 geo = new QStyleSheetGeometryData(w, h, minw, minh, maxw, maxh);
880
881 int left = 0, top = 0, right = 0, bottom = 0;
882 Origin origin = Origin_Unknown;
883 Qt::Alignment position = 0;
884 QCss::PositionMode mode = PositionMode_Unknown;
885 Qt::Alignment textAlignment = 0;
886 if (v.extractPosition(&left, &top, &right, &bottom, &origin, &position, &mode, &textAlignment))
887 p = new QStyleSheetPositionData(left, top, right, bottom, origin, position, mode, textAlignment);
888
889 int margins[4], paddings[4], spacing = -1;
890 for (int i = 0; i < 4; i++)
891 margins[i] = paddings[i] = 0;
892 if (v.extractBox(margins, paddings, &spacing))
893 b = new QStyleSheetBoxData(margins, paddings, spacing);
894
895 int borders[4];
896 QBrush colors[4];
897 QCss::BorderStyle styles[4];
898 QSize radii[4];
899 for (int i = 0; i < 4; i++) {
900 borders[i] = 0;
901 styles[i] = BorderStyle_None;
902 }
903 if (v.extractBorder(borders, colors, styles, radii))
904 bd = new QStyleSheetBorderData(borders, colors, styles, radii);
905
906 int offsets[4];
907 for (int i = 0; i < 4; i++) {
908 borders[i] = offsets[i] = 0;
909 styles[i] = BorderStyle_None;
910 }
911 if (v.extractOutline(borders, colors, styles, radii, offsets))
912 ou = new QStyleSheetOutlineData(borders, colors, styles, radii, offsets);
913
914 QBrush brush;
915 QString uri;
916 Repeat repeat = Repeat_XY;
917 Qt::Alignment alignment = Qt::AlignTop | Qt::AlignLeft;
918 Attachment attachment = Attachment_Scroll;
919 origin = Origin_Padding;
920 Origin clip = Origin_Border;
921 if (v.extractBackground(&brush, &uri, &repeat, &alignment, &origin, &attachment, &clip))
922 bg = new QStyleSheetBackgroundData(brush, QPixmap(uri), repeat, alignment, origin, attachment, clip);
923
924 QBrush sfg, fg;
925 QBrush sbg, abg;
926 if (v.extractPalette(&fg, &sfg, &sbg, &abg))
927 pal = new QStyleSheetPaletteData(fg, sfg, sbg, abg);
928
929 QIcon icon;
930 alignment = Qt::AlignCenter;
931 QSize size;
932 if (v.extractImage(&icon, &alignment, &size))
933 img = new QStyleSheetImageData(icon, alignment, size);
934
935 int adj = -255;
936 hasFont = v.extractFont(&font, &adj);
937
938#ifndef QT_NO_TOOLTIP
939 if (widget && qstrcmp(widget->metaObject()->className(), "QTipLabel") == 0)
940 palette = QToolTip::palette();
941#endif
942
943 for (int i = 0; i < declarations.count(); i++) {
944 const Declaration& decl = declarations.at(i);
945 if (decl.d->propertyId == BorderImage) {
946 QString uri;
947 QCss::TileMode horizStretch, vertStretch;
948 int cuts[4];
949
950 decl.borderImageValue(&uri, cuts, &horizStretch, &vertStretch);
951 if (uri.isEmpty() || uri == QLatin1String("none")) {
952 if (bd && bd->bi)
953 bd->bi->pixmap = QPixmap();
954 } else {
955 if (!bd)
956 bd = new QStyleSheetBorderData;
957 if (!bd->bi)
958 bd->bi = new QStyleSheetBorderImageData;
959
960 QStyleSheetBorderImageData *bi = bd->bi;
961 bi->pixmap = QPixmap(uri);
962 for (int i = 0; i < 4; i++)
963 bi->cuts[i] = cuts[i];
964 bi->horizStretch = horizStretch;
965 bi->vertStretch = vertStretch;
966 }
967 } else if (decl.d->propertyId == QtBackgroundRole) {
968 if (bg && bg->brush.style() != Qt::NoBrush)
969 continue;
970 int role = decl.d->values.at(0).variant.toInt();
971 if (role >= Value_FirstColorRole && role <= Value_LastColorRole)
972 defaultBackground = palette.color((QPalette::ColorRole)(role-Value_FirstColorRole));
973 } else if (decl.d->property.startsWith(QLatin1String("qproperty-"), Qt::CaseInsensitive)) {
974 // intentionally left blank...
975 } else if (decl.d->propertyId == UnknownProperty) {
976 bool knownStyleHint = false;
977 for (int i = 0; i < numKnownStyleHints; i++) {
978 QLatin1String styleHint(knownStyleHints[i]);
979 if (decl.d->property.compare(styleHint) == 0) {
980 QString hintName = QString(styleHint);
981 QVariant hintValue;
982 if (hintName.endsWith(QLatin1String("alignment"))) {
983 hintValue = (int) decl.alignmentValue();
984 } else if (hintName.endsWith(QLatin1String("color"))) {
985 hintValue = (int) decl.colorValue().rgba();
986 } else if (hintName.endsWith(QLatin1String("size"))) {
987 hintValue = decl.sizeValue();
988 } else if (hintName.endsWith(QLatin1String("icon"))) {
989 hintValue = decl.iconValue();
990 } else if (hintName == QLatin1String("button-layout")
991 && decl.d->values.count() != 0 && decl.d->values.at(0).type == Value::String) {
992 hintValue = subControlLayout(decl.d->values.at(0).variant.toString());
993 } else {
994 int integer;
995 decl.intValue(&integer);
996 hintValue = integer;
997 }
998 styleHints[decl.d->property] = hintValue;
999 knownStyleHint = true;
1000 break;
1001 }
1002 }
1003 if (!knownStyleHint)
1004 qDebug("Unknown property %s", qPrintable(decl.d->property));
1005 }
1006 }
1007
1008 if (widget) {
1009 QStyleSheetStyle *style = const_cast<QStyleSheetStyle *>(globalStyleSheetStyle);
1010 if (!style)
1011 style = qobject_cast<QStyleSheetStyle *>(widget->style());
1012 if (style)
1013 fixupBorder(style->nativeFrameWidth(widget));
1014
1015 }
1016 if (hasBorder() && border()->hasBorderImage())
1017 defaultBackground = QBrush();
1018}
1019
1020QRect QRenderRule::borderRect(const QRect& r) const
1021{
1022 if (!hasBox())
1023 return r;
1024 const int* m = box()->margins;
1025 return r.adjusted(m[LeftEdge], m[TopEdge], -m[RightEdge], -m[BottomEdge]);
1026}
1027
1028QRect QRenderRule::outlineRect(const QRect& r) const
1029{
1030 QRect br = borderRect(r);
1031 if (!hasOutline())
1032 return br;
1033 const int *b = outline()->borders;
1034 return r.adjusted(b[LeftEdge], b[TopEdge], -b[RightEdge], -b[BottomEdge]);
1035}
1036
1037QRect QRenderRule::paddingRect(const QRect& r) const
1038{
1039 QRect br = borderRect(r);
1040 if (!hasBorder())
1041 return br;
1042 const int *b = border()->borders;
1043 return br.adjusted(b[LeftEdge], b[TopEdge], -b[RightEdge], -b[BottomEdge]);
1044}
1045
1046QRect QRenderRule::contentsRect(const QRect& r) const
1047{
1048 QRect pr = paddingRect(r);
1049 if (!hasBox())
1050 return pr;
1051 const int *p = box()->paddings;
1052 return pr.adjusted(p[LeftEdge], p[TopEdge], -p[RightEdge], -p[BottomEdge]);
1053}
1054
1055QRect QRenderRule::boxRect(const QRect& cr, int flags) const
1056{
1057 QRect r = cr;
1058 if (hasBox()) {
1059 if (flags & Margin) {
1060 const int *m = box()->margins;
1061 r.adjust(-m[LeftEdge], -m[TopEdge], m[RightEdge], m[BottomEdge]);
1062 }
1063 if (flags & Padding) {
1064 const int *p = box()->paddings;
1065 r.adjust(-p[LeftEdge], -p[TopEdge], p[RightEdge], p[BottomEdge]);
1066 }
1067 }
1068 if (!hasNativeBorder() && (flags & Border)) {
1069 const int *b = border()->borders;
1070 r.adjust(-b[LeftEdge], -b[TopEdge], b[RightEdge], b[BottomEdge]);
1071 }
1072 return r;
1073}
1074
1075QSize QRenderRule::boxSize(const QSize &cs, int flags) const
1076{
1077 QSize bs = boxRect(QRect(QPoint(0, 0), cs), flags).size();
1078 if (cs.width() < 0) bs.setWidth(-1);
1079 if (cs.height() < 0) bs.setHeight(-1);
1080 return bs;
1081}
1082
1083void QRenderRule::fixupBorder(int nativeWidth)
1084{
1085 if (bd == 0)
1086 return;
1087
1088 if (!bd->hasBorderImage() || bd->bi->pixmap.isNull()) {
1089 bd->bi = 0;
1090 // ignore the color, border of edges that have none border-style
1091 QBrush color = pal ? pal->foreground : QBrush();
1092 const bool hasRadius = bd->radii[0].isValid() || bd->radii[1].isValid()
1093 || bd->radii[2].isValid() || bd->radii[3].isValid();
1094 for (int i = 0; i < 4; i++) {
1095 if ((bd->styles[i] == BorderStyle_Native) && hasRadius)
1096 bd->styles[i] = BorderStyle_None;
1097
1098 switch (bd->styles[i]) {
1099 case BorderStyle_None:
1100 // border-style: none forces width to be 0
1101 bd->colors[i] = QBrush();
1102 bd->borders[i] = 0;
1103 break;
1104 case BorderStyle_Native:
1105 if (bd->borders[i] == 0)
1106 bd->borders[i] = nativeWidth;
1107 // intentional fall through
1108 default:
1109 if (!bd->colors[i].style() != Qt::NoBrush) // auto-acquire 'color'
1110 bd->colors[i] = color;
1111 break;
1112 }
1113 }
1114
1115 return;
1116 }
1117
1118 // inspect the border image
1119 QStyleSheetBorderImageData *bi = bd->bi;
1120 if (bi->cuts[0] == -1) {
1121 for (int i = 0; i < 4; i++) // assume, cut = border
1122 bi->cuts[i] = int(border()->borders[i]);
1123 }
1124}
1125
1126void QRenderRule::drawBorderImage(QPainter *p, const QRect& rect)
1127{
1128 static const Qt::TileRule tileMode2TileRule[] = {
1129 Qt::StretchTile, Qt::RoundTile, Qt::StretchTile, Qt::RepeatTile, Qt::StretchTile };
1130
1131 const QStyleSheetBorderImageData *borderImageData = border()->borderImage();
1132 const int *targetBorders = border()->borders;
1133 const int *sourceBorders = borderImageData->cuts;
1134 QMargins sourceMargins(sourceBorders[LeftEdge], sourceBorders[TopEdge],
1135 sourceBorders[RightEdge], sourceBorders[BottomEdge]);
1136 QMargins targetMargins(targetBorders[LeftEdge], targetBorders[TopEdge],
1137 targetBorders[RightEdge], targetBorders[BottomEdge]);
1138
1139 bool wasSmoothPixmapTransform = p->renderHints() & QPainter::SmoothPixmapTransform;
1140 p->setRenderHint(QPainter::SmoothPixmapTransform);
1141 qDrawBorderPixmap(p, rect, targetMargins, borderImageData->pixmap,
1142 QRect(QPoint(), borderImageData->pixmap.size()), sourceMargins,
1143 QTileRules(tileMode2TileRule[borderImageData->horizStretch], tileMode2TileRule[borderImageData->vertStretch]));
1144 p->setRenderHint(QPainter::SmoothPixmapTransform, wasSmoothPixmapTransform);
1145}
1146
1147QRect QRenderRule::originRect(const QRect &rect, Origin origin) const
1148{
1149 switch (origin) {
1150 case Origin_Padding:
1151 return paddingRect(rect);
1152 case Origin_Border:
1153 return borderRect(rect);
1154 case Origin_Content:
1155 return contentsRect(rect);
1156 case Origin_Margin:
1157 default:
1158 return rect;
1159 }
1160}
1161
1162void QRenderRule::drawBackgroundImage(QPainter *p, const QRect &rect, QPoint off)
1163{
1164 if (!hasBackground())
1165 return;
1166
1167 const QPixmap& bgp = background()->pixmap;
1168 if (bgp.isNull())
1169 return;
1170
1171 setClip(p, borderRect(rect));
1172
1173 if (background()->origin != background()->clip) {
1174 p->save();
1175 p->setClipRect(originRect(rect, background()->clip), Qt::IntersectClip);
1176 }
1177
1178 if (background()->attachment == Attachment_Fixed)
1179 off = QPoint(0, 0);
1180
1181 QRect r = originRect(rect, background()->origin);
1182 QRect aligned = QStyle::alignedRect(Qt::LeftToRight, background()->position, bgp.size(), r);
1183 QRect inter = aligned.translated(-off).intersected(r);
1184
1185 switch (background()->repeat) {
1186 case Repeat_Y:
1187 p->drawTiledPixmap(inter.x(), r.y(), inter.width(), r.height(), bgp,
1188 inter.x() - aligned.x() + off.x(),
1189 bgp.height() - int(aligned.y() - r.y()) % bgp.height() + off.y());
1190 break;
1191 case Repeat_X:
1192 p->drawTiledPixmap(r.x(), inter.y(), r.width(), inter.height(), bgp,
1193 bgp.width() - int(aligned.x() - r.x())%bgp.width() + off.x(),
1194 inter.y() - aligned.y() + off.y());
1195 break;
1196 case Repeat_XY:
1197 p->drawTiledPixmap(r, bgp,
1198 QPoint(bgp.width() - int(aligned.x() - r.x())% bgp.width() + off.x(),
1199 bgp.height() - int(aligned.y() - r.y())%bgp.height() + off.y()));
1200 break;
1201 case Repeat_None:
1202 default:
1203 p->drawPixmap(inter.x(), inter.y(), bgp, inter.x() - aligned.x() + off.x(),
1204 inter.y() - aligned.y() + off.y(), inter.width(), inter.height());
1205 break;
1206 }
1207
1208
1209 if (background()->origin != background()->clip)
1210 p->restore();
1211
1212 unsetClip(p);
1213}
1214
1215void QRenderRule::drawOutline(QPainter *p, const QRect &rect)
1216{
1217 if (!hasOutline())
1218 return;
1219
1220 bool wasAntialiased = p->renderHints() & QPainter::Antialiasing;
1221 p->setRenderHint(QPainter::Antialiasing);
1222 qDrawBorder(p, rect, ou->styles, ou->borders, ou->colors, ou->radii);
1223 p->setRenderHint(QPainter::Antialiasing, wasAntialiased);
1224}
1225
1226void QRenderRule::drawBorder(QPainter *p, const QRect& rect)
1227{
1228 if (!hasBorder())
1229 return;
1230
1231 if (border()->hasBorderImage()) {
1232 drawBorderImage(p, rect);
1233 return;
1234 }
1235
1236 bool wasAntialiased = p->renderHints() & QPainter::Antialiasing;
1237 p->setRenderHint(QPainter::Antialiasing);
1238 qDrawBorder(p, rect, bd->styles, bd->borders, bd->colors, bd->radii);
1239 p->setRenderHint(QPainter::Antialiasing, wasAntialiased);
1240}
1241
1242QPainterPath QRenderRule::borderClip(QRect r)
1243{
1244 if (!hasBorder())
1245 return QPainterPath();
1246
1247 QSize tlr, trr, blr, brr;
1248 qNormalizeRadii(r, bd->radii, &tlr, &trr, &blr, &brr);
1249 if (tlr.isNull() && trr.isNull() && blr.isNull() && brr.isNull())
1250 return QPainterPath();
1251
1252 const QRectF rect(r);
1253 const int *borders = border()->borders;
1254 QPainterPath path;
1255 qreal curY = rect.y() + borders[TopEdge]/2.0;
1256 path.moveTo(rect.x() + tlr.width(), curY);
1257 path.lineTo(rect.right() - trr.width(), curY);
1258 qreal curX = rect.right() - borders[RightEdge]/2.0;
1259 path.arcTo(curX - 2*trr.width() + borders[RightEdge], curY,
1260 trr.width()*2 - borders[RightEdge], trr.height()*2 - borders[TopEdge], 90, -90);
1261
1262 path.lineTo(curX, rect.bottom() - brr.height());
1263 curY = rect.bottom() - borders[BottomEdge]/2.0;
1264 path.arcTo(curX - 2*brr.width() + borders[RightEdge], curY - 2*brr.height() + borders[BottomEdge],
1265 brr.width()*2 - borders[RightEdge], brr.height()*2 - borders[BottomEdge], 0, -90);
1266
1267 path.lineTo(rect.x() + blr.width(), curY);
1268 curX = rect.left() + borders[LeftEdge]/2.0;
1269 path.arcTo(curX, rect.bottom() - 2*blr.height() + borders[BottomEdge]/2,
1270 blr.width()*2 - borders[LeftEdge], blr.height()*2 - borders[BottomEdge], 270, -90);
1271
1272 path.lineTo(curX, rect.top() + tlr.height());
1273 path.arcTo(curX, rect.top() + borders[TopEdge]/2,
1274 tlr.width()*2 - borders[LeftEdge], tlr.height()*2 - borders[TopEdge], 180, -90);
1275
1276 path.closeSubpath();
1277 return path;
1278}
1279
1280/*! \internal
1281 Clip the painter to the border (in case we are using radius border)
1282 */
1283void QRenderRule::setClip(QPainter *p, const QRect &rect)
1284{
1285 if (clipset++)
1286 return;
1287 clipPath = borderClip(rect);
1288 if (!clipPath.isEmpty()) {
1289 p->save();
1290 p->setClipPath(clipPath, Qt::IntersectClip);
1291 }
1292}
1293
1294void QRenderRule::unsetClip(QPainter *p)
1295{
1296 if (--clipset)
1297 return;
1298 if (!clipPath.isEmpty())
1299 p->restore();
1300}
1301
1302void QRenderRule::drawBackground(QPainter *p, const QRect& rect, const QPoint& off)
1303{
1304 QBrush brush = hasBackground() ? background()->brush : QBrush();
1305 if (brush.style() == Qt::NoBrush)
1306 brush = defaultBackground;
1307
1308 if (brush.style() != Qt::NoBrush) {
1309 Origin origin = hasBackground() ? background()->clip : Origin_Border;
1310 // ### fix for gradients
1311 const QPainterPath &borderPath = borderClip(originRect(rect, origin));
1312 if (!borderPath.isEmpty()) {
1313 // Drawn intead of being used as clipping path for better visual quality
1314 bool wasAntialiased = p->renderHints() & QPainter::Antialiasing;
1315 p->setRenderHint(QPainter::Antialiasing);
1316 p->fillPath(borderPath, brush);
1317 p->setRenderHint(QPainter::Antialiasing, wasAntialiased);
1318 } else {
1319 p->fillRect(originRect(rect, origin), brush);
1320 }
1321 }
1322
1323 drawBackgroundImage(p, rect, off);
1324}
1325
1326void QRenderRule::drawFrame(QPainter *p, const QRect& rect)
1327{
1328 drawBackground(p, rect);
1329 if (hasBorder())
1330 drawBorder(p, borderRect(rect));
1331}
1332
1333void QRenderRule::drawImage(QPainter *p, const QRect &rect)
1334{
1335 if (!hasImage())
1336 return;
1337 img->icon.paint(p, rect, img->alignment);
1338}
1339
1340void QRenderRule::drawRule(QPainter *p, const QRect& rect)
1341{
1342 drawFrame(p, rect);
1343 drawImage(p, contentsRect(rect));
1344}
1345
1346// *shudder* , *horror*, *whoa* <-- what you might feel when you see the functions below
1347void QRenderRule::configurePalette(QPalette *p, QPalette::ColorRole fr, QPalette::ColorRole br)
1348{
1349 if (bg && bg->brush.style() != Qt::NoBrush) {
1350 if (br != QPalette::NoRole)
1351 p->setBrush(br, bg->brush);
1352 p->setBrush(QPalette::Window, bg->brush);
1353 }
1354
1355 if (!hasPalette())
1356 return;
1357
1358 if (pal->foreground.style() != Qt::NoBrush) {
1359 if (fr != QPalette::NoRole)
1360 p->setBrush(fr, pal->foreground);
1361 p->setBrush(QPalette::WindowText, pal->foreground);
1362 p->setBrush(QPalette::Text, pal->foreground);
1363 }
1364 if (pal->selectionBackground.style() != Qt::NoBrush)
1365 p->setBrush(QPalette::Highlight, pal->selectionBackground);
1366 if (pal->selectionForeground.style() != Qt::NoBrush)
1367 p->setBrush(QPalette::HighlightedText, pal->selectionForeground);
1368 if (pal->alternateBackground.style() != Qt::NoBrush)
1369 p->setBrush(QPalette::AlternateBase, pal->alternateBackground);
1370}
1371
1372void QRenderRule::configurePalette(QPalette *p, QPalette::ColorGroup cg, const QWidget *w, bool embedded)
1373{
1374 if (bg && bg->brush.style() != Qt::NoBrush) {
1375 p->setBrush(cg, QPalette::Base, bg->brush); // for windows, windowxp
1376 p->setBrush(cg, QPalette::Button, bg->brush); // for plastique
1377 p->setBrush(cg, w->backgroundRole(), bg->brush);
1378 p->setBrush(cg, QPalette::Window, bg->brush);
1379 }
1380
1381 if (embedded) {
1382 /* For embedded widgets (ComboBox, SpinBox and ScrollArea) we want the embedded widget
1383 * to be transparent when we have a transparent background or border image */
1384 if ((hasBackground() && background()->isTransparent())
1385 || (hasBorder() && border()->hasBorderImage() && !border()->borderImage()->pixmap.isNull()))
1386 p->setBrush(cg, w->backgroundRole(), Qt::NoBrush);
1387 }
1388
1389 if (!hasPalette())
1390 return;
1391
1392 if (pal->foreground.style() != Qt::NoBrush) {
1393 p->setBrush(cg, QPalette::ButtonText, pal->foreground);
1394 p->setBrush(cg, w->foregroundRole(), pal->foreground);
1395 p->setBrush(cg, QPalette::WindowText, pal->foreground);
1396 p->setBrush(cg, QPalette::Text, pal->foreground);
1397 }
1398 if (pal->selectionBackground.style() != Qt::NoBrush)
1399 p->setBrush(cg, QPalette::Highlight, pal->selectionBackground);
1400 if (pal->selectionForeground.style() != Qt::NoBrush)
1401 p->setBrush(cg, QPalette::HighlightedText, pal->selectionForeground);
1402 if (pal->alternateBackground.style() != Qt::NoBrush)
1403 p->setBrush(cg, QPalette::AlternateBase, pal->alternateBackground);
1404}
1405
1406///////////////////////////////////////////////////////////////////////////////
1407// Style rules
1408#define WIDGET(x) (static_cast<QWidget *>(x.ptr))
1409
1410static inline QWidget *parentWidget(const QWidget *w)
1411{
1412 if(qobject_cast<const QLabel *>(w) && qstrcmp(w->metaObject()->className(), "QTipLabel") == 0) {
1413 QWidget *p = qvariant_cast<QWidget *>(w->property("_q_stylesheet_parent"));
1414 if (p)
1415 return p;
1416 }
1417 return w->parentWidget();
1418}
1419
1420class QStyleSheetStyleSelector : public StyleSelector
1421{
1422public:
1423 QStyleSheetStyleSelector() { }
1424
1425 QStringList nodeNames(NodePtr node) const
1426 {
1427 if (isNullNode(node))
1428 return QStringList();
1429 const QMetaObject *metaObject = WIDGET(node)->metaObject();
1430#ifndef QT_NO_TOOLTIP
1431 if (qstrcmp(metaObject->className(), "QTipLabel") == 0)
1432 return QStringList(QLatin1String("QToolTip"));
1433#endif
1434 QStringList result;
1435 do {
1436 result += QString::fromLatin1(metaObject->className()).replace(QLatin1Char(':'), QLatin1Char('-'));
1437 metaObject = metaObject->superClass();
1438 } while (metaObject != 0);
1439 return result;
1440 }
1441 QString attribute(NodePtr node, const QString& name) const
1442 {
1443 if (isNullNode(node))
1444 return QString();
1445
1446 QHash<QString, QString> &cache = m_attributeCache[WIDGET(node)];
1447 QHash<QString, QString>::const_iterator cacheIt = cache.constFind(name);
1448 if (cacheIt != cache.constEnd())
1449 return cacheIt.value();
1450
1451 QVariant value = WIDGET(node)->property(name.toLatin1());
1452 if (!value.isValid()) {
1453 if (name == QLatin1String("class")) {
1454 QString className = QString::fromLatin1(WIDGET(node)->metaObject()->className());
1455 if (className.contains(QLatin1Char(':')))
1456 className.replace(QLatin1Char(':'), QLatin1Char('-'));
1457 cache[name] = className;
1458 return className;
1459 } else if (name == QLatin1String("style")) {
1460 QStyleSheetStyle *proxy = qobject_cast<QStyleSheetStyle *>(WIDGET(node)->style());
1461 if (proxy) {
1462 QString styleName = QString::fromLatin1(proxy->baseStyle()->metaObject()->className());
1463 cache[name] = styleName;
1464 return styleName;
1465 }
1466 }
1467 }
1468 QString valueStr;
1469 if(value.type() == QVariant::StringList || value.type() == QVariant::List)
1470 valueStr = value.toStringList().join(QLatin1String(" "));
1471 else
1472 valueStr = value.toString();
1473 cache[name] = valueStr;
1474 return valueStr;
1475 }
1476 bool nodeNameEquals(NodePtr node, const QString& nodeName) const
1477 {
1478 if (isNullNode(node))
1479 return false;
1480 const QMetaObject *metaObject = WIDGET(node)->metaObject();
1481#ifndef QT_NO_TOOLTIP
1482 if (qstrcmp(metaObject->className(), "QTipLabel") == 0)
1483 return nodeName == QLatin1String("QToolTip");
1484#endif
1485 do {
1486 const ushort *uc = (const ushort *)nodeName.constData();
1487 const ushort *e = uc + nodeName.length();
1488 const uchar *c = (uchar *)metaObject->className();
1489 while (*c && uc != e && (*uc == *c || (*c == ':' && *uc == '-'))) {
1490 ++uc;
1491 ++c;
1492 }
1493 if (uc == e && !*c)
1494 return true;
1495 metaObject = metaObject->superClass();
1496 } while (metaObject != 0);
1497 return false;
1498 }
1499 bool hasAttributes(NodePtr) const
1500 { return true; }
1501 QStringList nodeIds(NodePtr node) const
1502 { return isNullNode(node) ? QStringList() : QStringList(WIDGET(node)->objectName()); }
1503 bool isNullNode(NodePtr node) const
1504 { return node.ptr == 0; }
1505 NodePtr parentNode(NodePtr node) const
1506 { NodePtr n; n.ptr = isNullNode(node) ? 0 : parentWidget(WIDGET(node)); return n; }
1507 NodePtr previousSiblingNode(NodePtr) const
1508 { NodePtr n; n.ptr = 0; return n; }
1509 NodePtr duplicateNode(NodePtr node) const
1510 { return node; }
1511 void freeNode(NodePtr) const
1512 { }
1513
1514private:
1515 mutable QHash<const QWidget *, QHash<QString, QString> > m_attributeCache;
1516};
1517
1518QVector<QCss::StyleRule> QStyleSheetStyle::styleRules(const QWidget *w) const
1519{
1520 QHash<const QWidget *, QVector<StyleRule> >::const_iterator cacheIt = styleRulesCache->constFind(w);
1521 if (cacheIt != styleRulesCache->constEnd())
1522 return cacheIt.value();
1523
1524 if (!initWidget(w)) {
1525 return QVector<StyleRule>();
1526 }
1527
1528 QStyleSheetStyleSelector styleSelector;
1529
1530 StyleSheet defaultSs;
1531 QHash<const void *, StyleSheet>::const_iterator defaultCacheIt = styleSheetCache->constFind(baseStyle());
1532 if (defaultCacheIt == styleSheetCache->constEnd()) {
1533 defaultSs = getDefaultStyleSheet();
1534 styleSheetCache->insert(baseStyle(), defaultSs);
1535 } else {
1536 defaultSs = defaultCacheIt.value();
1537 }
1538 styleSelector.styleSheets += defaultSs;
1539
1540 if (!qApp->styleSheet().isEmpty()) {
1541 StyleSheet appSs;
1542 QHash<const void *, StyleSheet>::const_iterator appCacheIt = styleSheetCache->constFind(qApp);
1543 if (appCacheIt == styleSheetCache->constEnd()) {
1544 QString ss = qApp->styleSheet();
1545 if (ss.startsWith(QLatin1String("file:///")))
1546 ss.remove(0, 8);
1547 parser.init(ss, qApp->styleSheet() != ss);
1548 if (!parser.parse(&appSs))
1549 qWarning("Could not parse application stylesheet");
1550 appSs.origin = StyleSheetOrigin_Inline;
1551 appSs.depth = 1;
1552 styleSheetCache->insert(qApp, appSs);
1553 } else {
1554 appSs = appCacheIt.value();
1555 }
1556 styleSelector.styleSheets += appSs;
1557 }
1558
1559 QVector<QCss::StyleSheet> widgetSs;
1560 for (const QWidget *wid = w; wid; wid = parentWidget(wid)) {
1561 if (wid->styleSheet().isEmpty())
1562 continue;
1563 StyleSheet ss;
1564 QHash<const void *, StyleSheet>::const_iterator widCacheIt = styleSheetCache->constFind(wid);
1565 if (widCacheIt == styleSheetCache->constEnd()) {
1566 parser.init(wid->styleSheet());
1567 if (!parser.parse(&ss)) {
1568 parser.init(QLatin1String("* {") + wid->styleSheet() + QLatin1Char('}'));
1569 if (!parser.parse(&ss))
1570 qWarning("Could not parse stylesheet of widget %p", wid);
1571 }
1572 ss.origin = StyleSheetOrigin_Inline;
1573 styleSheetCache->insert(wid, ss);
1574 } else {
1575 ss = widCacheIt.value();
1576 }
1577 widgetSs.append(ss);
1578 }
1579
1580 for (int i = 0; i < widgetSs.count(); i++)
1581 widgetSs[i].depth = widgetSs.count() - i + 2;
1582
1583 styleSelector.styleSheets += widgetSs;
1584
1585 StyleSelector::NodePtr n;
1586 n.ptr = (void *)w;
1587 QVector<QCss::StyleRule> rules = styleSelector.styleRulesForNode(n);
1588 styleRulesCache->insert(w, rules);
1589 return rules;
1590}
1591
1592/////////////////////////////////////////////////////////////////////////////////////////
1593// Rendering rules
1594static QVector<Declaration> declarations(const QVector<StyleRule> &styleRules, const QString &part, quint64 pseudoClass = PseudoClass_Unspecified)
1595{
1596 QVector<Declaration> decls;
1597 for (int i = 0; i < styleRules.count(); i++) {
1598 const Selector& selector = styleRules.at(i).selectors.at(0);
1599 // Rules with pseudo elements don't cascade. This is an intentional
1600 // diversion for CSS
1601 if (part.compare(selector.pseudoElement(), Qt::CaseInsensitive) != 0)
1602 continue;
1603 quint64 negated = 0;
1604 quint64 cssClass = selector.pseudoClass(&negated);
1605 if ((pseudoClass == PseudoClass_Any) || (cssClass == PseudoClass_Unspecified)
1606 || ((((cssClass & pseudoClass) == cssClass)) && ((negated & pseudoClass) == 0)))
1607 decls += styleRules.at(i).declarations;
1608 }
1609 return decls;
1610}
1611
1612int QStyleSheetStyle::nativeFrameWidth(const QWidget *w)
1613{
1614 QStyle *base = baseStyle();
1615
1616#ifndef QT_NO_SPINBOX
1617 if (qobject_cast<const QAbstractSpinBox *>(w))
1618 return base->pixelMetric(QStyle::PM_SpinBoxFrameWidth, 0, w);
1619#endif
1620
1621#ifndef QT_NO_COMBOBOX
1622 if (qobject_cast<const QComboBox *>(w))
1623 return base->pixelMetric(QStyle::PM_ComboBoxFrameWidth, 0, w);
1624#endif
1625
1626#ifndef QT_NO_MENU
1627 if (qobject_cast<const QMenu *>(w))
1628 return base->pixelMetric(QStyle::PM_MenuPanelWidth, 0, w);
1629#endif
1630
1631#ifndef QT_NO_MENUBAR
1632 if (qobject_cast<const QMenuBar *>(w))
1633 return base->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, w);
1634#endif
1635#ifndef QT_NO_FRAME
1636 if (const QFrame *frame = qobject_cast<const QFrame *>(w)) {
1637 if (frame->frameShape() == QFrame::NoFrame)
1638 return 0;
1639 }
1640#endif
1641
1642 if (qstrcmp(w->metaObject()->className(), "QTipLabel") == 0)
1643 return base->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, w);
1644
1645 return base->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, w);
1646}
1647
1648static quint64 pseudoClass(QStyle::State state)
1649{
1650 quint64 pc = 0;
1651 if (state & QStyle::State_Enabled) {
1652 pc |= PseudoClass_Enabled;
1653 if (state & QStyle::State_MouseOver)
1654 pc |= PseudoClass_Hover;
1655 } else {
1656 pc |= PseudoClass_Disabled;
1657 }
1658 if (state & QStyle::State_Active)
1659 pc |= PseudoClass_Active;
1660 if (state & QStyle::State_Window)
1661 pc |= PseudoClass_Window;
1662 if (state & QStyle::State_Sunken)
1663 pc |= PseudoClass_Pressed;
1664 if (state & QStyle::State_HasFocus)
1665 pc |= PseudoClass_Focus;
1666 if (state & QStyle::State_On)
1667 pc |= (PseudoClass_On | PseudoClass_Checked);
1668 if (state & QStyle::State_Off)
1669 pc |= (PseudoClass_Off | PseudoClass_Unchecked);
1670 if (state & QStyle::State_NoChange)
1671 pc |= PseudoClass_Indeterminate;
1672 if (state & QStyle::State_Selected)
1673 pc |= PseudoClass_Selected;
1674 if (state & QStyle::State_Horizontal)
1675 pc |= PseudoClass_Horizontal;
1676 else
1677 pc |= PseudoClass_Vertical;
1678 if (state & (QStyle::State_Open | QStyle::State_On | QStyle::State_Sunken))
1679 pc |= PseudoClass_Open;
1680 else
1681 pc |= PseudoClass_Closed;
1682 if (state & QStyle::State_Children)
1683 pc |= PseudoClass_Children;
1684 if (state & QStyle::State_Sibling)
1685 pc |= PseudoClass_Sibling;
1686 if (state & QStyle::State_ReadOnly)
1687 pc |= PseudoClass_ReadOnly;
1688 if (state & QStyle::State_Item)
1689 pc |= PseudoClass_Item;
1690#ifdef QT_KEYPAD_NAVIGATION
1691 if (state & QStyle::State_HasEditFocus)
1692 pc |= PseudoClass_EditFocus;
1693#endif
1694 return pc;
1695}
1696
1697static void qt_check_if_internal_widget(const QWidget **w, int *element)
1698{
1699#ifdef QT_NO_DOCKWIDGET
1700 Q_UNUSED(w);
1701 Q_UNUSED(element);
1702#else
1703 if (*w && qstrcmp((*w)->metaObject()->className(), "QDockWidgetTitleButton") == 0) {
1704 if ((*w)->objectName() == QLatin1String("qt_dockwidget_closebutton")) {
1705 *element = PseudoElement_DockWidgetCloseButton;
1706 } else if ((*w)->objectName() == QLatin1String("qt_dockwidget_floatbutton")) {
1707 *element = PseudoElement_DockWidgetFloatButton;
1708 }
1709 *w = (*w)->parentWidget();
1710 }
1711#endif
1712}
1713
1714QRenderRule QStyleSheetStyle::renderRule(const QWidget *w, int element, quint64 state) const
1715{
1716 qt_check_if_internal_widget(&w, &element);
1717 QHash<quint64, QRenderRule> &cache = (*renderRulesCache)[w][element];
1718 QHash<quint64, QRenderRule>::const_iterator cacheIt = cache.constFind(state);
1719 if (cacheIt != cache.constEnd())
1720 return cacheIt.value();
1721
1722 if (!initWidget(w))
1723 return QRenderRule();
1724
1725 quint64 stateMask = 0;
1726 const QVector<StyleRule> rules = styleRules(w);
1727 for (int i = 0; i < rules.count(); i++) {
1728 const Selector& selector = rules.at(i).selectors.at(0);
1729 quint64 negated = 0;
1730 stateMask |= selector.pseudoClass(&negated);
1731 stateMask |= negated;
1732 }
1733
1734 cacheIt = cache.constFind(state & stateMask);
1735 if (cacheIt != cache.constEnd()) {
1736 const QRenderRule &newRule = cacheIt.value();
1737 cache[state] = newRule;
1738 return newRule;
1739 }
1740
1741
1742 const QString part = QLatin1String(knownPseudoElements[element].name);
1743 QVector<Declaration> decls = declarations(rules, part, state);
1744 QRenderRule newRule(decls, w);
1745 cache[state] = newRule;
1746 if ((state & stateMask) != state)
1747 cache[state&stateMask] = newRule;
1748 return newRule;
1749}
1750
1751QRenderRule QStyleSheetStyle::renderRule(const QWidget *w, const QStyleOption *opt, int pseudoElement) const
1752{
1753 quint64 extraClass = 0;
1754 QStyle::State state = opt ? opt->state : QStyle::State(QStyle::State_None);
1755
1756 if (const QStyleOptionComplex *complex = qstyleoption_cast<const QStyleOptionComplex *>(opt)) {
1757 if (pseudoElement != PseudoElement_None) {
1758 // if not an active subcontrol, just pass enabled/disabled
1759 QStyle::SubControl subControl = knownPseudoElements[pseudoElement].subControl;
1760
1761 if (!(complex->activeSubControls & subControl))
1762 state &= (QStyle::State_Enabled | QStyle::State_Horizontal | QStyle::State_HasFocus);
1763 }
1764
1765 switch (pseudoElement) {
1766 case PseudoElement_ComboBoxDropDown:
1767 case PseudoElement_ComboBoxArrow:
1768 state |= (complex->state & (QStyle::State_On|QStyle::State_ReadOnly));
1769 break;
1770 case PseudoElement_SpinBoxUpButton:
1771 case PseudoElement_SpinBoxDownButton:
1772 case PseudoElement_SpinBoxUpArrow:
1773 case PseudoElement_SpinBoxDownArrow:
1774#ifndef QT_NO_SPINBOX
1775 if (const QStyleOptionSpinBox *sb = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
1776 bool on = false;
1777 bool up = pseudoElement == PseudoElement_SpinBoxUpButton
1778 || pseudoElement == PseudoElement_SpinBoxUpArrow;
1779 if ((sb->stepEnabled & QAbstractSpinBox::StepUpEnabled) && up)
1780 on = true;
1781 else if ((sb->stepEnabled & QAbstractSpinBox::StepDownEnabled) && !up)
1782 on = true;
1783 state |= (on ? QStyle::State_On : QStyle::State_Off);
1784 }
1785#endif // QT_NO_SPINBOX
1786 break;
1787 case PseudoElement_GroupBoxTitle:
1788 state |= (complex->state & (QStyle::State_MouseOver | QStyle::State_Sunken));
1789 break;
1790 case PseudoElement_ToolButtonMenu:
1791 case PseudoElement_ToolButtonMenuArrow:
1792 case PseudoElement_ToolButtonDownArrow:
1793 state |= complex->state & QStyle::State_MouseOver;
1794 if (complex->state & QStyle::State_Sunken ||
1795 complex->activeSubControls & QStyle::SC_ToolButtonMenu)
1796 state |= QStyle::State_Sunken;
1797 break;
1798 case PseudoElement_SliderGroove:
1799 state |= complex->state & QStyle::State_MouseOver;
1800 break;
1801 default:
1802 break;
1803 }
1804
1805 if (const QStyleOptionComboBox *combo = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
1806 // QStyle::State_On is set when the popup is being shown
1807 // Propagate EditField Pressed state
1808 if (pseudoElement == PseudoElement_None
1809 && (complex->activeSubControls & QStyle::SC_ComboBoxEditField)
1810 && (!(state & QStyle::State_MouseOver))) {
1811 state |= QStyle::State_Sunken;
1812 }
1813
1814 if (!combo->frame)
1815 extraClass |= PseudoClass_Frameless;
1816 if (!combo->editable)
1817 extraClass |= PseudoClass_ReadOnly;
1818 else
1819 extraClass |= PseudoClass_Editable;
1820#ifndef QT_NO_SPINBOX
1821 } else if (const QStyleOptionSpinBox *spin = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
1822 if (!spin->frame)
1823 extraClass |= PseudoClass_Frameless;
1824#endif // QT_NO_SPINBOX
1825 } else if (const QStyleOptionGroupBox *gb = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
1826 if (gb->features & QStyleOptionFrameV2::Flat)
1827 extraClass |= PseudoClass_Flat;
1828 if (gb->lineWidth == 0)
1829 extraClass |= PseudoClass_Frameless;
1830 } else if (const QStyleOptionTitleBar *tb = qstyleoption_cast<const QStyleOptionTitleBar *>(opt)) {
1831 if (tb->titleBarState & Qt::WindowMinimized) {
1832 extraClass |= PseudoClass_Minimized;
1833 }
1834 else if (tb->titleBarState & Qt::WindowMaximized)
1835 extraClass |= PseudoClass_Maximized;
1836 }
1837 } else {
1838 // handle simple style options
1839 if (const QStyleOptionMenuItem *mi = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
1840 if (mi->menuItemType == QStyleOptionMenuItem::DefaultItem)
1841 extraClass |= PseudoClass_Default;
1842 if (mi->checkType == QStyleOptionMenuItem::Exclusive)
1843 extraClass |= PseudoClass_Exclusive;
1844 else if (mi->checkType == QStyleOptionMenuItem::NonExclusive)
1845 extraClass |= PseudoClass_NonExclusive;
1846 if (mi->checkType != QStyleOptionMenuItem::NotCheckable)
1847 extraClass |= (mi->checked) ? (PseudoClass_On|PseudoClass_Checked)
1848 : (PseudoClass_Off|PseudoClass_Unchecked);
1849 } else if (const QStyleOptionHeader *hdr = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
1850 if (hdr->position == QStyleOptionHeader::OnlyOneSection)
1851 extraClass |= PseudoClass_OnlyOne;
1852 else if (hdr->position == QStyleOptionHeader::Beginning)
1853 extraClass |= PseudoClass_First;
1854 else if (hdr->position == QStyleOptionHeader::End)
1855 extraClass |= PseudoClass_Last;
1856 else if (hdr->position == QStyleOptionHeader::Middle)
1857 extraClass |= PseudoClass_Middle;
1858
1859 if (hdr->selectedPosition == QStyleOptionHeader::NextAndPreviousAreSelected)
1860 extraClass |= (PseudoClass_NextSelected | PseudoClass_PreviousSelected);
1861 else if (hdr->selectedPosition == QStyleOptionHeader::NextIsSelected)
1862 extraClass |= PseudoClass_NextSelected;
1863 else if (hdr->selectedPosition == QStyleOptionHeader::PreviousIsSelected)
1864 extraClass |= PseudoClass_PreviousSelected;
1865#ifndef QT_NO_TABWIDGET
1866 } else if (const QStyleOptionTabWidgetFrame *tab = qstyleoption_cast<const QStyleOptionTabWidgetFrame *>(opt)) {
1867 switch (tab->shape) {
1868 case QTabBar::RoundedNorth:
1869 case QTabBar::TriangularNorth:
1870 extraClass |= PseudoClass_Top;
1871 break;
1872 case QTabBar::RoundedSouth:
1873 case QTabBar::TriangularSouth:
1874 extraClass |= PseudoClass_Bottom;
1875 break;
1876 case QTabBar::RoundedEast:
1877 case QTabBar::TriangularEast:
1878 extraClass |= PseudoClass_Left;
1879 break;
1880 case QTabBar::RoundedWest:
1881 case QTabBar::TriangularWest:
1882 extraClass |= PseudoClass_Right;
1883 break;
1884 default:
1885 break;
1886 }
1887#endif
1888#ifndef QT_NO_TABBAR
1889 } else if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
1890 if (tab->position == QStyleOptionTab::OnlyOneTab)
1891 extraClass |= PseudoClass_OnlyOne;
1892 else if (tab->position == QStyleOptionTab::Beginning)
1893 extraClass |= PseudoClass_First;
1894 else if (tab->position == QStyleOptionTab::End)
1895 extraClass |= PseudoClass_Last;
1896 else if (tab->position == QStyleOptionTab::Middle)
1897 extraClass |= PseudoClass_Middle;
1898
1899 if (tab->selectedPosition == QStyleOptionTab::NextIsSelected)
1900 extraClass |= PseudoClass_NextSelected;
1901 else if (tab->selectedPosition == QStyleOptionTab::PreviousIsSelected)
1902 extraClass |= PseudoClass_PreviousSelected;
1903
1904 switch (tab->shape) {
1905 case QTabBar::RoundedNorth:
1906 case QTabBar::TriangularNorth:
1907 extraClass |= PseudoClass_Top;
1908 break;
1909 case QTabBar::RoundedSouth:
1910 case QTabBar::TriangularSouth:
1911 extraClass |= PseudoClass_Bottom;
1912 break;
1913 case QTabBar::RoundedEast:
1914 case QTabBar::TriangularEast:
1915 extraClass |= PseudoClass_Left;
1916 break;
1917 case QTabBar::RoundedWest:
1918 case QTabBar::TriangularWest:
1919 extraClass |= PseudoClass_Right;
1920 break;
1921 default:
1922 break;
1923 }
1924#endif // QT_NO_TABBAR
1925 } else if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
1926 if (btn->features & QStyleOptionButton::Flat)
1927 extraClass |= PseudoClass_Flat;
1928 if (btn->features & QStyleOptionButton::DefaultButton)
1929 extraClass |= PseudoClass_Default;
1930 } else if (const QStyleOptionFrame *frm = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
1931 if (frm->lineWidth == 0)
1932 extraClass |= PseudoClass_Frameless;
1933 if (const QStyleOptionFrameV2 *frame2 = qstyleoption_cast<const QStyleOptionFrameV2 *>(opt)) {
1934 if (frame2->features & QStyleOptionFrameV2::Flat)
1935 extraClass |= PseudoClass_Flat;
1936 }
1937 }
1938#ifndef QT_NO_TOOLBAR
1939 else if (const QStyleOptionToolBar *tb = qstyleoption_cast<const QStyleOptionToolBar *>(opt)) {
1940 if (tb->toolBarArea == Qt::LeftToolBarArea)
1941 extraClass |= PseudoClass_Left;
1942 else if (tb->toolBarArea == Qt::RightToolBarArea)
1943 extraClass |= PseudoClass_Right;
1944 else if (tb->toolBarArea == Qt::TopToolBarArea)
1945 extraClass |= PseudoClass_Top;
1946 else if (tb->toolBarArea == Qt::BottomToolBarArea)
1947 extraClass |= PseudoClass_Bottom;
1948
1949 if (tb->positionWithinLine == QStyleOptionToolBar::Beginning)
1950 extraClass |= PseudoClass_First;
1951 else if (tb->positionWithinLine == QStyleOptionToolBar::Middle)
1952 extraClass |= PseudoClass_Middle;
1953 else if (tb->positionWithinLine == QStyleOptionToolBar::End)
1954 extraClass |= PseudoClass_Last;
1955 else if (tb->positionWithinLine == QStyleOptionToolBar::OnlyOne)
1956 extraClass |= PseudoClass_OnlyOne;
1957 }
1958#endif // QT_NO_TOOLBAR
1959#ifndef QT_NO_TOOLBOX
1960 else if (const QStyleOptionToolBoxV2 *tab = qstyleoption_cast<const QStyleOptionToolBoxV2 *>(opt)) {
1961 if (tab->position == QStyleOptionToolBoxV2::OnlyOneTab)
1962 extraClass |= PseudoClass_OnlyOne;
1963 else if (tab->position == QStyleOptionToolBoxV2::Beginning)
1964 extraClass |= PseudoClass_First;
1965 else if (tab->position == QStyleOptionToolBoxV2::End)
1966 extraClass |= PseudoClass_Last;
1967 else if (tab->position == QStyleOptionToolBoxV2::Middle)
1968 extraClass |= PseudoClass_Middle;
1969
1970 if (tab->selectedPosition == QStyleOptionToolBoxV2::NextIsSelected)
1971 extraClass |= PseudoClass_NextSelected;
1972 else if (tab->selectedPosition == QStyleOptionToolBoxV2::PreviousIsSelected)
1973 extraClass |= PseudoClass_PreviousSelected;
1974 }
1975#endif // QT_NO_TOOLBOX
1976#ifndef QT_NO_DOCKWIDGET
1977 else if (const QStyleOptionDockWidgetV2 *dw = qstyleoption_cast<const QStyleOptionDockWidgetV2 *>(opt)) {
1978 if (dw->verticalTitleBar)
1979 extraClass |= PseudoClass_Vertical;
1980 else
1981 extraClass |= PseudoClass_Horizontal;
1982 if (dw->closable)
1983 extraClass |= PseudoClass_Closable;
1984 if (dw->floatable)
1985 extraClass |= PseudoClass_Floatable;
1986 if (dw->movable)
1987 extraClass |= PseudoClass_Movable;
1988 }
1989#endif // QT_NO_DOCKWIDGET
1990#ifndef QT_NO_ITEMVIEWS
1991 else if (const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(opt)) {
1992 if (v2->features & QStyleOptionViewItemV2::Alternate)
1993 extraClass |= PseudoClass_Alternate;
1994 if (const QStyleOptionViewItemV4 *v4 = qstyleoption_cast<const QStyleOptionViewItemV4 *>(opt)) {
1995 if (v4->viewItemPosition == QStyleOptionViewItemV4::OnlyOne)
1996 extraClass |= PseudoClass_OnlyOne;
1997 else if (v4->viewItemPosition == QStyleOptionViewItemV4::Beginning)
1998 extraClass |= PseudoClass_First;
1999 else if (v4->viewItemPosition == QStyleOptionViewItemV4::End)
2000 extraClass |= PseudoClass_Last;
2001 else if (v4->viewItemPosition == QStyleOptionViewItemV4::Middle)
2002 extraClass |= PseudoClass_Middle;
2003 }
2004 }
2005#endif
2006#ifndef QT_NO_LINEEDIT
2007 // LineEdit sets Sunken flag to indicate Sunken frame (argh)
2008 if (const QLineEdit *lineEdit = qobject_cast<const QLineEdit *>(w)) {
2009 state &= ~QStyle::State_Sunken;
2010 if (lineEdit->hasFrame()) {
2011 extraClass &= ~PseudoClass_Frameless;
2012 } else {
2013 extraClass |= PseudoClass_Frameless;
2014 }
2015 } else
2016#endif
2017 if (const QFrame *frm = qobject_cast<const QFrame *>(w)) {
2018 if (frm->lineWidth() == 0)
2019 extraClass |= PseudoClass_Frameless;
2020 }
2021 }
2022
2023 return renderRule(w, pseudoElement, pseudoClass(state) | extraClass);
2024}
2025
2026bool QStyleSheetStyle::hasStyleRule(const QWidget *w, int part) const
2027{
2028 QHash<int, bool> &cache = (*hasStyleRuleCache)[w];
2029 QHash<int, bool>::const_iterator cacheIt = cache.constFind(part);
2030 if (cacheIt != cache.constEnd())
2031 return cacheIt.value();
2032
2033 if (!initWidget(w))
2034 return false;
2035
2036
2037 const QVector<StyleRule> &rules = styleRules(w);
2038 if (part == PseudoElement_None) {
2039 bool result = w && !rules.isEmpty();
2040 cache[part] = result;
2041 return result;
2042 }
2043
2044 QString pseudoElement = QLatin1String(knownPseudoElements[part].name);
2045 QVector<Declaration> declarations;
2046 for (int i = 0; i < rules.count(); i++) {
2047 const Selector& selector = rules.at(i).selectors.at(0);
2048 if (pseudoElement.compare(selector.pseudoElement(), Qt::CaseInsensitive) == 0) {
2049 cache[part] = true;
2050 return true;
2051 }
2052 }
2053
2054 cache[part] = false;
2055 return false;
2056}
2057
2058static Origin defaultOrigin(int pe)
2059{
2060 switch (pe) {
2061 case PseudoElement_ScrollBarAddPage:
2062 case PseudoElement_ScrollBarSubPage:
2063 case PseudoElement_ScrollBarAddLine:
2064 case PseudoElement_ScrollBarSubLine:
2065 case PseudoElement_ScrollBarFirst:
2066 case PseudoElement_ScrollBarLast:
2067 case PseudoElement_GroupBoxTitle:
2068 case PseudoElement_GroupBoxIndicator: // never used
2069 case PseudoElement_ToolButtonMenu:
2070 case PseudoElement_SliderAddPage:
2071 case PseudoElement_SliderSubPage:
2072 return Origin_Border;
2073
2074 case PseudoElement_SpinBoxUpButton:
2075 case PseudoElement_SpinBoxDownButton:
2076 case PseudoElement_PushButtonMenuIndicator:
2077 case PseudoElement_ComboBoxDropDown:
2078 case PseudoElement_ToolButtonDownArrow:
2079 case PseudoElement_MenuCheckMark:
2080 case PseudoElement_MenuIcon:
2081 case PseudoElement_MenuRightArrow:
2082 return Origin_Padding;
2083
2084 case PseudoElement_Indicator:
2085 case PseudoElement_ExclusiveIndicator:
2086 case PseudoElement_ComboBoxArrow:
2087 case PseudoElement_ScrollBarSlider:
2088 case PseudoElement_ScrollBarUpArrow:
2089 case PseudoElement_ScrollBarDownArrow:
2090 case PseudoElement_ScrollBarLeftArrow:
2091 case PseudoElement_ScrollBarRightArrow:
2092 case PseudoElement_SpinBoxUpArrow:
2093 case PseudoElement_SpinBoxDownArrow:
2094 case PseudoElement_ToolButtonMenuArrow:
2095 case PseudoElement_HeaderViewUpArrow:
2096 case PseudoElement_HeaderViewDownArrow:
2097 case PseudoElement_SliderGroove:
2098 case PseudoElement_SliderHandle:
2099 return Origin_Content;
2100
2101 default:
2102 return Origin_Margin;
2103 }
2104}
2105
2106static Qt::Alignment defaultPosition(int pe)
2107{
2108 switch (pe) {
2109 case PseudoElement_Indicator:
2110 case PseudoElement_ExclusiveIndicator:
2111 case PseudoElement_MenuCheckMark:
2112 case PseudoElement_MenuIcon:
2113 return Qt::AlignLeft | Qt::AlignVCenter;
2114
2115 case PseudoElement_ScrollBarAddLine:
2116 case PseudoElement_ScrollBarLast:
2117 case PseudoElement_SpinBoxDownButton:
2118 case PseudoElement_PushButtonMenuIndicator:
2119 case PseudoElement_ToolButtonDownArrow:
2120 return Qt::AlignRight | Qt::AlignBottom;
2121
2122 case PseudoElement_ScrollBarSubLine:
2123 case PseudoElement_ScrollBarFirst:
2124 case PseudoElement_SpinBoxUpButton:
2125 case PseudoElement_ComboBoxDropDown:
2126 case PseudoElement_ToolButtonMenu:
2127 case PseudoElement_DockWidgetCloseButton:
2128 case PseudoElement_DockWidgetFloatButton:
2129 return Qt::AlignRight | Qt::AlignTop;
2130
2131 case PseudoElement_ScrollBarUpArrow:
2132 case PseudoElement_ScrollBarDownArrow:
2133 case PseudoElement_ScrollBarLeftArrow:
2134 case PseudoElement_ScrollBarRightArrow:
2135 case PseudoElement_SpinBoxUpArrow:
2136 case PseudoElement_SpinBoxDownArrow:
2137 case PseudoElement_ComboBoxArrow:
2138 case PseudoElement_DownArrow:
2139 case PseudoElement_ToolButtonMenuArrow:
2140 case PseudoElement_SliderGroove:
2141 return Qt::AlignCenter;
2142
2143 case PseudoElement_GroupBoxTitle:
2144 case PseudoElement_GroupBoxIndicator: // never used
2145 return Qt::AlignLeft | Qt::AlignTop;
2146
2147 case PseudoElement_HeaderViewUpArrow:
2148 case PseudoElement_HeaderViewDownArrow:
2149 case PseudoElement_MenuRightArrow:
2150 return Qt::AlignRight | Qt::AlignVCenter;
2151
2152 default:
2153 return 0;
2154 }
2155}
2156
2157QSize QStyleSheetStyle::defaultSize(const QWidget *w, QSize sz, const QRect& rect, int pe) const
2158{
2159 QStyle *base = baseStyle();
2160
2161 switch (pe) {
2162 case PseudoElement_Indicator:
2163 case PseudoElement_MenuCheckMark:
2164 if (sz.width() == -1)
2165 sz.setWidth(base->pixelMetric(PM_IndicatorWidth, 0, w));
2166 if (sz.height() == -1)
2167 sz.setHeight(base->pixelMetric(PM_IndicatorHeight, 0, w));
2168 break;
2169
2170 case PseudoElement_ExclusiveIndicator:
2171 case PseudoElement_GroupBoxIndicator:
2172 if (sz.width() == -1)
2173 sz.setWidth(base->pixelMetric(PM_ExclusiveIndicatorWidth, 0, w));
2174 if (sz.height() == -1)
2175 sz.setHeight(base->pixelMetric(PM_ExclusiveIndicatorHeight, 0, w));
2176 break;
2177
2178 case PseudoElement_PushButtonMenuIndicator: {
2179 int pm = base->pixelMetric(PM_MenuButtonIndicator, 0, w);
2180 if (sz.width() == -1)
2181 sz.setWidth(pm);
2182 if (sz.height() == -1)
2183 sz.setHeight(pm);
2184 }
2185 break;
2186
2187 case PseudoElement_ComboBoxDropDown:
2188 if (sz.width() == -1)
2189 sz.setWidth(16);
2190 break;
2191
2192 case PseudoElement_ComboBoxArrow:
2193 case PseudoElement_DownArrow:
2194 case PseudoElement_ToolButtonMenuArrow:
2195 case PseudoElement_ToolButtonDownArrow:
2196 case PseudoElement_MenuRightArrow:
2197 if (sz.width() == -1)
2198 sz.setWidth(13);
2199 if (sz.height() == -1)
2200 sz.setHeight(13);
2201 break;
2202
2203 case PseudoElement_SpinBoxUpButton:
2204 case PseudoElement_SpinBoxDownButton:
2205 if (sz.width() == -1)
2206 sz.setWidth(16);
2207 if (sz.height() == -1)
2208 sz.setHeight(rect.height()/2);
2209 break;
2210
2211 case PseudoElement_ToolButtonMenu:
2212 if (sz.width() == -1)
2213 sz.setWidth(base->pixelMetric(PM_MenuButtonIndicator, 0, w));
2214 break;
2215
2216 case PseudoElement_HeaderViewUpArrow:
2217 case PseudoElement_HeaderViewDownArrow: {
2218 int pm = base->pixelMetric(PM_HeaderMargin, 0, w);
2219 if (sz.width() == -1)
2220 sz.setWidth(pm);
2221 if (sz.height() == 1)
2222 sz.setHeight(pm);
2223 break;
2224 }
2225
2226 case PseudoElement_ScrollBarFirst:
2227 case PseudoElement_ScrollBarLast:
2228 case PseudoElement_ScrollBarAddLine:
2229 case PseudoElement_ScrollBarSubLine:
2230 case PseudoElement_ScrollBarSlider: {
2231 int pm = pixelMetric(QStyle::PM_ScrollBarExtent, 0, w);
2232 if (sz.width() == -1)
2233 sz.setWidth(pm);
2234 if (sz.height() == -1)
2235 sz.setHeight(pm);
2236 break;
2237 }
2238
2239 case PseudoElement_DockWidgetCloseButton:
2240 case PseudoElement_DockWidgetFloatButton: {
2241 int iconSize = pixelMetric(PM_SmallIconSize, 0, w);
2242 return QSize(iconSize, iconSize);
2243 }
2244
2245 default:
2246 break;
2247 }
2248
2249 // expand to rectangle
2250 if (sz.height() == -1)
2251 sz.setHeight(rect.height());
2252 if (sz.width() == -1)
2253 sz.setWidth(rect.width());
2254
2255 return sz;
2256}
2257
2258static PositionMode defaultPositionMode(int pe)
2259{
2260 switch (pe) {
2261 case PseudoElement_ScrollBarFirst:
2262 case PseudoElement_ScrollBarLast:
2263 case PseudoElement_ScrollBarAddLine:
2264 case PseudoElement_ScrollBarSubLine:
2265 case PseudoElement_ScrollBarAddPage:
2266 case PseudoElement_ScrollBarSubPage:
2267 case PseudoElement_ScrollBarSlider:
2268 case PseudoElement_SliderGroove:
2269 case PseudoElement_SliderHandle:
2270 case PseudoElement_TabWidgetPane:
2271 return PositionMode_Absolute;
2272 default:
2273 return PositionMode_Static;
2274 }
2275}
2276
2277QRect QStyleSheetStyle::positionRect(const QWidget *w, const QRenderRule &rule2, int pe,
2278 const QRect &originRect, Qt::LayoutDirection dir) const
2279{
2280 const QStyleSheetPositionData *p = rule2.position();
2281 PositionMode mode = (p && p->mode != PositionMode_Unknown) ? p->mode : defaultPositionMode(pe);
2282 Qt::Alignment position = (p && p->position != 0) ? p->position : defaultPosition(pe);
2283 QRect r;
2284
2285 if (mode != PositionMode_Absolute) {
2286 QSize sz = defaultSize(w, rule2.size(), originRect, pe);
2287 sz = sz.expandedTo(rule2.minimumContentsSize());
2288 r = QStyle::alignedRect(dir, position, sz, originRect);
2289 if (p) {
2290 int left = p->left ? p->left : -p->right;
2291 int top = p->top ? p->top : -p->bottom;
2292 r.translate(dir == Qt::LeftToRight ? left : -left, top);
2293 }
2294 } else {
2295 r = p ? originRect.adjusted(dir == Qt::LeftToRight ? p->left : p->right, p->top,
2296 dir == Qt::LeftToRight ? -p->right : -p->left, -p->bottom)
2297 : originRect;
2298 if (rule2.hasContentsSize()) {
2299 QSize sz = rule2.size().expandedTo(rule2.minimumContentsSize());
2300 if (sz.width() == -1) sz.setWidth(r.width());
2301 if (sz.height() == -1) sz.setHeight(r.height());
2302 r = QStyle::alignedRect(dir, position, sz, r);
2303 }
2304 }
2305 return r;
2306}
2307
2308QRect QStyleSheetStyle::positionRect(const QWidget *w, const QRenderRule& rule1, const QRenderRule& rule2, int pe,
2309 const QRect& rect, Qt::LayoutDirection dir) const
2310{
2311 const QStyleSheetPositionData *p = rule2.position();
2312 Origin origin = (p && p->origin != Origin_Unknown) ? p->origin : defaultOrigin(pe);
2313 QRect originRect = rule1.originRect(rect, origin);
2314 return positionRect(w, rule2, pe, originRect, dir);
2315}
2316
2317
2318/** \internal
2319 For widget that have an embedded widget (such as combobox) return that embedded widget.
2320 otherwise return the widget itself
2321 */
2322static QWidget *embeddedWidget(QWidget *w)
2323{
2324#ifndef QT_NO_COMBOBOX
2325 if (QComboBox *cmb = qobject_cast<QComboBox *>(w)) {
2326 if (cmb->isEditable())
2327 return cmb->lineEdit();
2328 else
2329 return cmb;
2330 }
2331#endif
2332
2333#ifndef QT_NO_SPINBOX
2334 if (QAbstractSpinBox *sb = qobject_cast<QAbstractSpinBox *>(w))
2335 return qFindChild<QLineEdit *>(sb);
2336#endif
2337
2338#ifndef QT_NO_SCROLLAREA
2339 if (QAbstractScrollArea *sa = qobject_cast<QAbstractScrollArea *>(w))
2340 return sa->viewport();
2341#endif
2342
2343 return w;
2344}
2345
2346/** \internal
2347 in case w is an embedded widget, return the container widget
2348 (i.e, the widget for which the rules actualy apply)
2349 (exemple, if w is a lineedit embedded in a combobox, return the combobox)
2350
2351 if w is not embedded, return w itself
2352*/
2353static QWidget *containerWidget(const QWidget *w)
2354{
2355#ifndef QT_NO_LINEEDIT
2356 if (qobject_cast<const QLineEdit *>(w)) {
2357 //if the QLineEdit is an embeddedWidget, we need the rule of the real widget
2358#ifndef QT_NO_COMBOBOX
2359 if (qobject_cast<const QComboBox *>(w->parentWidget()))
2360 return w->parentWidget();
2361#endif
2362#ifndef QT_NO_SPINBOX
2363 if (qobject_cast<const QAbstractSpinBox *>(w->parentWidget()))
2364 return w->parentWidget();
2365#endif
2366 }
2367#endif // QT_NO_LINEEDIT
2368
2369#ifndef QT_NO_SCROLLAREA
2370 if (const QAbstractScrollArea *sa = qobject_cast<const QAbstractScrollArea *>(w->parentWidget())) {
2371 if (sa->viewport() == w)
2372 return w->parentWidget();
2373 }
2374#endif
2375
2376 return const_cast<QWidget *>(w);
2377}
2378
2379/** \internal
2380 returns true if the widget can NOT be styled directly
2381 */
2382static bool unstylable(const QWidget *w)
2383{
2384 if (w->windowType() == Qt::Desktop)
2385 return true;
2386
2387 if (!w->styleSheet().isEmpty())
2388 return false;
2389
2390 if (containerWidget(w) != w)
2391 return true;
2392
2393#ifndef QT_NO_FRAME
2394 // detect QComboBoxPrivateContainer
2395 else if (qobject_cast<const QFrame *>(w)) {
2396 if (0
2397#ifndef QT_NO_COMBOBOX
2398 || qobject_cast<const QComboBox *>(w->parentWidget())
2399#endif
2400 )
2401 return true;
2402 }
2403#endif
2404 return false;
2405}
2406
2407static quint64 extendedPseudoClass(const QWidget *w)
2408{
2409 quint64 pc = w->isWindow() ? quint64(PseudoClass_Window) : 0;
2410 if (const QAbstractSlider *slider = qobject_cast<const QAbstractSlider *>(w)) {
2411 pc |= ((slider->orientation() == Qt::Vertical) ? PseudoClass_Vertical : PseudoClass_Horizontal);
2412 } else
2413#ifndef QT_NO_COMBOBOX
2414 if (const QComboBox *combo = qobject_cast<const QComboBox *>(w)) {
2415 if (combo->isEditable())
2416 pc |= (combo->isEditable() ? PseudoClass_Editable : PseudoClass_ReadOnly);
2417 } else
2418#endif
2419#ifndef QT_NO_LINEEDIT
2420 if (const QLineEdit *edit = qobject_cast<const QLineEdit *>(w)) {
2421 pc |= (edit->isReadOnly() ? PseudoClass_ReadOnly : PseudoClass_Editable);
2422 } else
2423#endif
2424 { } // required for the above ifdef'ery to work
2425 return pc;
2426}
2427
2428// sets up the geometry of the widget. We set a dynamic property when
2429// we modify the min/max size of the widget. The min/max size is restored
2430// to their original value when a new stylesheet that does not contain
2431// the CSS properties is set and when the widget has this dynamic property set.
2432// This way we don't trample on users who had setup a min/max size in code and
2433// don't use stylesheets at all.
2434void QStyleSheetStyle::setGeometry(QWidget *w)
2435{
2436 QRenderRule rule = renderRule(w, PseudoElement_None, PseudoClass_Enabled | extendedPseudoClass(w));
2437 const QStyleSheetGeometryData *geo = rule.geometry();
2438 if (w->property("_q_stylesheet_minw").toBool()
2439 && ((!rule.hasGeometry() || geo->minWidth == -1))) {
2440 w->setMinimumWidth(0);
2441 w->setProperty("_q_stylesheet_minw", QVariant());
2442 }
2443 if (w->property("_q_stylesheet_minh").toBool()
2444 && ((!rule.hasGeometry() || geo->minHeight == -1))) {
2445 w->setMinimumHeight(0);
2446 w->setProperty("_q_stylesheet_minh", QVariant());
2447 }
2448 if (w->property("_q_stylesheet_maxw").toBool()
2449 && ((!rule.hasGeometry() || geo->maxWidth == -1))) {
2450 w->setMaximumWidth(QWIDGETSIZE_MAX);
2451 w->setProperty("_q_stylesheet_maxw", QVariant());
2452 }
2453 if (w->property("_q_stylesheet_maxh").toBool()
2454 && ((!rule.hasGeometry() || geo->maxHeight == -1))) {
2455 w->setMaximumHeight(QWIDGETSIZE_MAX);
2456 w->setProperty("_q_stylesheet_maxh", QVariant());
2457 }
2458
2459
2460 if (rule.hasGeometry()) {
2461 if (geo->minWidth != -1) {
2462 w->setProperty("_q_stylesheet_minw", true);
2463 w->setMinimumWidth(rule.boxSize(QSize(qMax(geo->width, geo->minWidth), 0)).width());
2464 }
2465 if (geo->minHeight != -1) {
2466 w->setProperty("_q_stylesheet_minh", true);
2467 w->setMinimumHeight(rule.boxSize(QSize(0, qMax(geo->height, geo->minHeight))).height());
2468 }
2469 if (geo->maxWidth != -1) {
2470 w->setProperty("_q_stylesheet_maxw", true);
2471 w->setMaximumWidth(rule.boxSize(QSize(qMin(geo->width == -1 ? QWIDGETSIZE_MAX : geo->width,
2472 geo->maxWidth == -1 ? QWIDGETSIZE_MAX : geo->maxWidth), 0)).width());
2473 }
2474 if (geo->maxHeight != -1) {
2475 w->setProperty("_q_stylesheet_maxh", true);
2476 w->setMaximumHeight(rule.boxSize(QSize(0, qMin(geo->height == -1 ? QWIDGETSIZE_MAX : geo->height,
2477 geo->maxHeight == -1 ? QWIDGETSIZE_MAX : geo->maxHeight))).height());
2478 }
2479 }
2480}
2481
2482void QStyleSheetStyle::setProperties(QWidget *w)
2483{
2484 QHash<QString, QVariant> propertyHash;
2485 QVector<Declaration> decls = declarations(styleRules(w), QString());
2486
2487 // run through the declarations in order
2488 for (int i = 0; i < decls.count(); i++) {
2489 const Declaration &decl = decls.at(i);
2490 QString property = decl.d->property;
2491 if (!property.startsWith(QLatin1String("qproperty-"), Qt::CaseInsensitive))
2492 continue;
2493 property.remove(0, 10); // strip "qproperty-"
2494 const QVariant value = w->property(property.toLatin1());
2495 const QMetaObject *metaObject = w->metaObject();
2496 int index = metaObject->indexOfProperty(property.toLatin1());
2497 if (index == -1) {
2498 qWarning() << w << " does not have a property named " << property;
2499 continue;
2500 }
2501 QMetaProperty metaProperty = metaObject->property(index);
2502 if (!metaProperty.isWritable() || !metaProperty.isDesignable()) {
2503 qWarning() << w << " cannot design property named " << property;
2504 continue;
2505 }
2506 QVariant v;
2507 switch (value.type()) {
2508 case QVariant::Icon: v = decl.iconValue(); break;
2509 case QVariant::Image: v = QImage(decl.uriValue()); break;
2510 case QVariant::Pixmap: v = QPixmap(decl.uriValue()); break;
2511 case QVariant::Rect: v = decl.rectValue(); break;
2512 case QVariant::Size: v = decl.sizeValue(); break;
2513 case QVariant::Color: v = decl.colorValue(); break;
2514 case QVariant::Brush: v = decl.brushValue(); break;
2515#ifndef QT_NO_SHORTCUT
2516 case QVariant::KeySequence: v = QKeySequence(decl.d->values.at(0).variant.toString()); break;
2517#endif
2518 default: v = decl.d->values.at(0).variant; break;
2519 }
2520 propertyHash[property] = v;
2521 }
2522 // apply the values
2523 const QList<QString> properties = propertyHash.keys();
2524 for (int i = 0; i < properties.count(); i++) {
2525 const QString &property = properties.at(i);
2526 w->setProperty(property.toLatin1(), propertyHash[property]);
2527 }
2528}
2529
2530void QStyleSheetStyle::setPalette(QWidget *w)
2531{
2532 struct RuleRoleMap {
2533 int state;
2534 QPalette::ColorGroup group;
2535 } map[3] = {
2536 { PseudoClass_Active | PseudoClass_Enabled, QPalette::Active },
2537 { PseudoClass_Disabled, QPalette::Disabled },
2538 { PseudoClass_Enabled, QPalette::Inactive }
2539 };
2540
2541 QPalette p = w->palette();
2542 QWidget *ew = embeddedWidget(w);
2543
2544 for (int i = 0; i < 3; i++) {
2545 QRenderRule rule = renderRule(w, PseudoElement_None, map[i].state | extendedPseudoClass(w));
2546 if (i == 0) {
2547 if (!w->property("_q_styleSheetWidgetFont").isValid()) {
2548 saveWidgetFont(w, w->font());
2549 }
2550 updateStyleSheetFont(w);
2551 if (ew != w)
2552 updateStyleSheetFont(ew);
2553 }
2554
2555 rule.configurePalette(&p, map[i].group, ew, ew != w);
2556 }
2557
2558 customPaletteWidgets->insert(w, w->palette());
2559 w->setPalette(p);
2560 if (ew != w)
2561 ew->setPalette(p);
2562}
2563
2564void QStyleSheetStyle::unsetPalette(QWidget *w)
2565{
2566 if (customPaletteWidgets->contains(w)) {
2567 QPalette p = customPaletteWidgets->value(w);
2568 w->setPalette(p);
2569 QWidget *ew = embeddedWidget(w);
2570 if (ew != w)
2571 ew->setPalette(p);
2572 customPaletteWidgets->remove(w);
2573 }
2574 QVariant oldFont = w->property("_q_styleSheetWidgetFont");
2575 if (oldFont.isValid()) {
2576 w->setFont(qVariantValue<QFont>(oldFont));
2577 }
2578 if (autoFillDisabledWidgets->contains(w)) {
2579 embeddedWidget(w)->setAutoFillBackground(true);
2580 autoFillDisabledWidgets->remove(w);
2581 }
2582}
2583
2584static void updateWidgets(const QList<const QWidget *>& widgets)
2585{
2586 if (!styleRulesCache->isEmpty() || !hasStyleRuleCache->isEmpty() || !renderRulesCache->isEmpty()) {
2587 for (int i = 0; i < widgets.size(); ++i) {
2588 const QWidget *widget = widgets.at(i);
2589 styleRulesCache->remove(widget);
2590 hasStyleRuleCache->remove(widget);
2591 renderRulesCache->remove(widget);
2592 }
2593 }
2594 for (int i = 0; i < widgets.size(); ++i) {
2595 QWidget *widget = const_cast<QWidget *>(widgets.at(i));
2596 if (widget == 0)
2597 continue;
2598 widget->style()->polish(widget);
2599 QEvent event(QEvent::StyleChange);
2600 QApplication::sendEvent(widget, &event);
2601 widget->update();
2602 widget->updateGeometry();
2603 }
2604}
2605
2606/////////////////////////////////////////////////////////////////////////////////////////
2607// The stylesheet style
2608int QStyleSheetStyle::numinstances = 0;
2609
2610QStyleSheetStyle::QStyleSheetStyle(QStyle *base)
2611 : QWindowsStyle(*new QStyleSheetStylePrivate), base(base), refcount(1)
2612{
2613 ++numinstances;
2614 if (numinstances == 1) {
2615 styleRulesCache = new QHash<const QWidget *, QVector<StyleRule> >;
2616 hasStyleRuleCache = new QHash<const QWidget *, QHash<int, bool> >;
2617 renderRulesCache = new QHash<const QWidget *, QRenderRules>;
2618 customPaletteWidgets = new QHash<const QWidget *, QPalette>;
2619 styleSheetCache = new QHash<const void *, StyleSheet>;
2620 autoFillDisabledWidgets = new QSet<const QWidget *>;
2621 }
2622}
2623
2624QStyleSheetStyle::~QStyleSheetStyle()
2625{
2626 --numinstances;
2627 if (numinstances == 0) {
2628 delete styleRulesCache;
2629 styleRulesCache = 0;
2630 delete hasStyleRuleCache;
2631 hasStyleRuleCache = 0;
2632 delete renderRulesCache;
2633 renderRulesCache = 0;
2634 delete customPaletteWidgets;
2635 customPaletteWidgets = 0;
2636 delete styleSheetCache;
2637 styleSheetCache = 0;
2638 delete autoFillDisabledWidgets;
2639 autoFillDisabledWidgets = 0;
2640 }
2641}
2642QStyle *QStyleSheetStyle::baseStyle() const
2643{
2644 if (base)
2645 return base;
2646 if (QStyleSheetStyle *me = qobject_cast<QStyleSheetStyle *>(QApplication::style()))
2647 return me->base;
2648 return QApplication::style();
2649}
2650
2651void QStyleSheetStyle::widgetDestroyed(QObject *o)
2652{
2653 styleRulesCache->remove((const QWidget *)o);
2654 hasStyleRuleCache->remove((const QWidget *)o);
2655 renderRulesCache->remove((const QWidget *)o);
2656 customPaletteWidgets->remove((const QWidget *)o);
2657 styleSheetCache->remove((const QWidget *)o);
2658 autoFillDisabledWidgets->remove((const QWidget *)o);
2659}
2660
2661/*!
2662 * Make sure that the cache will be clean by connecting destroyed if needed.
2663 * return false if the widget is not stylable;
2664 */
2665bool QStyleSheetStyle::initWidget(const QWidget *w) const
2666{
2667 if (!w)
2668 return false;
2669 if(w->testAttribute(Qt::WA_StyleSheet))
2670 return true;
2671
2672 if(unstylable(w))
2673 return false;
2674
2675 const_cast<QWidget *>(w)->setAttribute(Qt::WA_StyleSheet, true);
2676 QObject::connect(w, SIGNAL(destroyed(QObject*)), this, SLOT(widgetDestroyed(QObject*)));
2677 return true;
2678}
2679
2680void QStyleSheetStyle::polish(QWidget *w)
2681{
2682 baseStyle()->polish(w);
2683 RECURSION_GUARD(return)
2684
2685 if (!initWidget(w))
2686 return;
2687
2688 if (styleRulesCache->contains(w)) {
2689 // the widget accessed its style pointer before polish (or repolish)
2690 // (exemple: the QAbstractSpinBox constructor ask for the stylehint)
2691 styleRulesCache->remove(w);
2692 hasStyleRuleCache->remove(w);
2693 renderRulesCache->remove(w);
2694 }
2695 setGeometry(w);
2696 setProperties(w);
2697 unsetPalette(w);
2698 setPalette(w);
2699
2700 //set the WA_Hover attribute if one of the selector depends of the hover state
2701 QVector<StyleRule> rules = styleRules(w);
2702 for (int i = 0; i < rules.count(); i++) {
2703 const Selector& selector = rules.at(i).selectors.at(0);
2704 quint64 negated = 0;
2705 quint64 cssClass = selector.pseudoClass(&negated);
2706 if ( cssClass & PseudoClass_Hover || negated & PseudoClass_Hover) {
2707 w->setAttribute(Qt::WA_Hover);
2708 embeddedWidget(w)->setAttribute(Qt::WA_Hover);
2709 }
2710 }
2711
2712
2713#ifndef QT_NO_SCROLLAREA
2714 if (QAbstractScrollArea *sa = qobject_cast<QAbstractScrollArea *>(w)) {
2715 QRenderRule rule = renderRule(sa, PseudoElement_None, PseudoClass_Enabled);
2716 if ((rule.hasBorder() && rule.border()->hasBorderImage())
2717 || (rule.hasBackground() && !rule.background()->pixmap.isNull())) {
2718 QObject::connect(sa->horizontalScrollBar(), SIGNAL(valueChanged(int)),
2719 sa, SLOT(update()), Qt::UniqueConnection);
2720 QObject::connect(sa->verticalScrollBar(), SIGNAL(valueChanged(int)),
2721 sa, SLOT(update()), Qt::UniqueConnection);
2722 }
2723 }
2724#endif
2725
2726#ifndef QT_NO_PROGRESSBAR
2727 if (QProgressBar *pb = qobject_cast<QProgressBar *>(w)) {
2728 QWindowsStyle::polish(pb);
2729 }
2730#endif
2731
2732 QRenderRule rule = renderRule(w, PseudoElement_None, PseudoClass_Any);
2733 if (rule.hasDrawable() || rule.hasBox()) {
2734 if (w->metaObject() == &QWidget::staticMetaObject
2735#ifndef QT_NO_ITEMVIEWS
2736 || qobject_cast<QHeaderView *>(w)
2737#endif
2738#ifndef QT_NO_TABBAR
2739 || qobject_cast<QTabBar *>(w)
2740#endif
2741#ifndef QT_NO_FRAME
2742 || qobject_cast<QFrame *>(w)
2743#endif
2744#ifndef QT_NO_MAINWINDOW
2745 || qobject_cast<QMainWindow *>(w)
2746#endif
2747#ifndef QT_NO_MDIAREA
2748 || qobject_cast<QMdiSubWindow *>(w)
2749#endif
2750#ifndef QT_NO_MENUBAR
2751 || qobject_cast<QMenuBar *>(w)
2752#endif
2753 || qobject_cast<QDialog *>(w)) {
2754 w->setAttribute(Qt::WA_StyledBackground, true);
2755 }
2756 QWidget *ew = embeddedWidget(w);
2757 if (ew->autoFillBackground()) {
2758 ew->setAutoFillBackground(false);
2759 autoFillDisabledWidgets->insert(w);
2760 if (ew != w) { //eg. viewport of a scrollarea
2761 //(in order to draw the background anyway in case we don't.)
2762 ew->setAttribute(Qt::WA_StyledBackground, true);
2763 }
2764 }
2765 if (!rule.hasBackground() || rule.background()->isTransparent() || rule.hasBox()
2766 || (!rule.hasNativeBorder() && !rule.border()->isOpaque()))
2767 w->setAttribute(Qt::WA_OpaquePaintEvent, false);
2768 }
2769}
2770
2771void QStyleSheetStyle::polish(QApplication *app)
2772{
2773 baseStyle()->polish(app);
2774}
2775
2776void QStyleSheetStyle::polish(QPalette &pal)
2777{
2778 baseStyle()->polish(pal);
2779}
2780
2781void QStyleSheetStyle::repolish(QWidget *w)
2782{
2783 QList<const QWidget *> children = qFindChildren<const QWidget *>(w, QString());
2784 children.append(w);
2785 styleSheetCache->remove(w);
2786 updateWidgets(children);
2787}
2788
2789void QStyleSheetStyle::repolish(QApplication *app)
2790{
2791 Q_UNUSED(app);
2792 const QList<const QWidget*> allWidgets = styleRulesCache->keys();
2793 styleSheetCache->remove(qApp);
2794 styleRulesCache->clear();
2795 hasStyleRuleCache->clear();
2796 renderRulesCache->clear();
2797 updateWidgets(allWidgets);
2798}
2799
2800void QStyleSheetStyle::unpolish(QWidget *w)
2801{
2802 if (!w || !w->testAttribute(Qt::WA_StyleSheet)) {
2803 baseStyle()->unpolish(w);
2804 return;
2805 }
2806
2807 styleRulesCache->remove(w);
2808 hasStyleRuleCache->remove(w);
2809 renderRulesCache->remove(w);
2810 styleSheetCache->remove(w);
2811 unsetPalette(w);
2812 w->setProperty("_q_stylesheet_minw", QVariant());
2813 w->setProperty("_q_stylesheet_minh", QVariant());
2814 w->setProperty("_q_stylesheet_maxw", QVariant());
2815 w->setProperty("_q_stylesheet_maxh", QVariant());
2816 w->setAttribute(Qt::WA_StyleSheet, false);
2817 QObject::disconnect(w, 0, this, 0);
2818#ifndef QT_NO_SCROLLAREA
2819 if (QAbstractScrollArea *sa = qobject_cast<QAbstractScrollArea *>(w)) {
2820 QObject::disconnect(sa->horizontalScrollBar(), SIGNAL(valueChanged(int)),
2821 sa, SLOT(update()));
2822 QObject::disconnect(sa->verticalScrollBar(), SIGNAL(valueChanged(int)),
2823 sa, SLOT(update()));
2824 }
2825#endif
2826#ifndef QT_NO_PROGRESSBAR
2827 if (QProgressBar *pb = qobject_cast<QProgressBar *>(w))
2828 QWindowsStyle::unpolish(pb);
2829#endif
2830 baseStyle()->unpolish(w);
2831}
2832
2833void QStyleSheetStyle::unpolish(QApplication *app)
2834{
2835 baseStyle()->unpolish(app);
2836 RECURSION_GUARD(return)
2837 styleRulesCache->clear();
2838 hasStyleRuleCache->clear();
2839 renderRulesCache->clear();
2840 styleSheetCache->remove(qApp);
2841}
2842
2843#ifndef QT_NO_TABBAR
2844inline static bool verticalTabs(QTabBar::Shape shape)
2845{
2846 return shape == QTabBar::RoundedWest
2847 || shape == QTabBar::RoundedEast
2848 || shape == QTabBar::TriangularWest
2849 || shape == QTabBar::TriangularEast;
2850}
2851#endif // QT_NO_TABBAR
2852
2853void QStyleSheetStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p,
2854 const QWidget *w) const
2855{
2856 RECURSION_GUARD(baseStyle()->drawComplexControl(cc, opt, p, w); return)
2857
2858 QRenderRule rule = renderRule(w, opt);
2859
2860 switch (cc) {
2861 case CC_ComboBox:
2862 if (const QStyleOptionComboBox *cmb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
2863 QStyleOptionComboBox cmbOpt(*cmb);
2864 cmbOpt.rect = rule.borderRect(opt->rect);
2865 if (rule.hasNativeBorder()) {
2866 rule.drawBackgroundImage(p, cmbOpt.rect);
2867 rule.configurePalette(&cmbOpt.palette, QPalette::ButtonText, QPalette::Button);
2868 bool customDropDown = (opt->subControls & QStyle::SC_ComboBoxArrow)
2869 && (hasStyleRule(w, PseudoElement_ComboBoxDropDown) || hasStyleRule(w, PseudoElement_ComboBoxArrow));
2870 if (customDropDown)
2871 cmbOpt.subControls &= ~QStyle::SC_ComboBoxArrow;
2872 if (rule.baseStyleCanDraw()) {
2873 baseStyle()->drawComplexControl(cc, &cmbOpt, p, w);
2874 } else {
2875 QWindowsStyle::drawComplexControl(cc, &cmbOpt, p, w);
2876 }
2877 if (!customDropDown)
2878 return;
2879 } else {
2880 rule.drawRule(p, opt->rect);
2881 }
2882
2883 if (opt->subControls & QStyle::SC_ComboBoxArrow) {
2884 QRenderRule subRule = renderRule(w, opt, PseudoElement_ComboBoxDropDown);
2885 if (subRule.hasDrawable()) {
2886 QRect r = subControlRect(CC_ComboBox, opt, SC_ComboBoxArrow, w);
2887 subRule.drawRule(p, r);
2888 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_ComboBoxArrow);
2889 r = positionRect(w, subRule, subRule2, PseudoElement_ComboBoxArrow, r, opt->direction);
2890 subRule2.drawRule(p, r);
2891 } else {
2892 cmbOpt.subControls = QStyle::SC_ComboBoxArrow;
2893 QWindowsStyle::drawComplexControl(cc, &cmbOpt, p, w);
2894 }
2895 }
2896
2897 return;
2898 }
2899 break;
2900
2901#ifndef QT_NO_SPINBOX
2902 case CC_SpinBox:
2903 if (const QStyleOptionSpinBox *spin = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
2904 QStyleOptionSpinBox spinOpt(*spin);
2905 rule.configurePalette(&spinOpt.palette, QPalette::ButtonText, QPalette::Button);
2906 rule.configurePalette(&spinOpt.palette, QPalette::Text, QPalette::Base);
2907 spinOpt.rect = rule.borderRect(opt->rect);
2908 bool customUp = true, customDown = true;
2909 QRenderRule upRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton);
2910 QRenderRule downRule = renderRule(w, opt, PseudoElement_SpinBoxDownButton);
2911 bool upRuleMatch = upRule.hasGeometry() || upRule.hasPosition();
2912 bool downRuleMatch = downRule.hasGeometry() || downRule.hasPosition();
2913 if (rule.hasNativeBorder() && !upRuleMatch && !downRuleMatch) {
2914 rule.drawBackgroundImage(p, spinOpt.rect);
2915 customUp = (opt->subControls & QStyle::SC_SpinBoxUp)
2916 && (hasStyleRule(w, PseudoElement_SpinBoxUpButton) || hasStyleRule(w, PseudoElement_UpArrow));
2917 if (customUp)
2918 spinOpt.subControls &= ~QStyle::SC_SpinBoxUp;
2919 customDown = (opt->subControls & QStyle::SC_SpinBoxDown)
2920 && (hasStyleRule(w, PseudoElement_SpinBoxDownButton) || hasStyleRule(w, PseudoElement_DownArrow));
2921 if (customDown)
2922 spinOpt.subControls &= ~QStyle::SC_SpinBoxDown;
2923 if (rule.baseStyleCanDraw()) {
2924 baseStyle()->drawComplexControl(cc, &spinOpt, p, w);
2925 } else {
2926 QWindowsStyle::drawComplexControl(cc, &spinOpt, p, w);
2927 }
2928 if (!customUp && !customDown)
2929 return;
2930 } else {
2931 rule.drawRule(p, opt->rect);
2932 }
2933
2934 if ((opt->subControls & QStyle::SC_SpinBoxUp) && customUp) {
2935 QRenderRule subRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton);
2936 if (subRule.hasDrawable()) {
2937 QRect r = subControlRect(CC_SpinBox, opt, SC_SpinBoxUp, w);
2938 subRule.drawRule(p, r);
2939 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_SpinBoxUpArrow);
2940 r = positionRect(w, subRule, subRule2, PseudoElement_SpinBoxUpArrow, r, opt->direction);
2941 subRule2.drawRule(p, r);
2942 } else {
2943 spinOpt.subControls = QStyle::SC_SpinBoxUp;
2944 QWindowsStyle::drawComplexControl(cc, &spinOpt, p, w);
2945 }
2946 }
2947
2948 if ((opt->subControls & QStyle::SC_SpinBoxDown) && customDown) {
2949 QRenderRule subRule = renderRule(w, opt, PseudoElement_SpinBoxDownButton);
2950 if (subRule.hasDrawable()) {
2951 QRect r = subControlRect(CC_SpinBox, opt, SC_SpinBoxDown, w);
2952 subRule.drawRule(p, r);
2953 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_SpinBoxDownArrow);
2954 r = positionRect(w, subRule, subRule2, PseudoElement_SpinBoxDownArrow, r, opt->direction);
2955 subRule2.drawRule(p, r);
2956 } else {
2957 spinOpt.subControls = QStyle::SC_SpinBoxDown;
2958 QWindowsStyle::drawComplexControl(cc, &spinOpt, p, w);
2959 }
2960 }
2961 return;
2962 }
2963 break;
2964#endif // QT_NO_SPINBOX
2965
2966 case CC_GroupBox:
2967 if (const QStyleOptionGroupBox *gb = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
2968
2969 QRect labelRect, checkBoxRect, titleRect, frameRect;
2970 bool hasTitle = (gb->subControls & QStyle::SC_GroupBoxCheckBox) || !gb->text.isEmpty();
2971
2972 if (!rule.hasDrawable() && (!hasTitle || !hasStyleRule(w, PseudoElement_GroupBoxTitle))
2973 && !hasStyleRule(w, PseudoElement_Indicator) && !rule.hasBox() && !rule.hasFont && !rule.hasPalette()) {
2974 // let the native style draw the combobox if there is no style for it.
2975 break;
2976 }
2977 rule.drawBackground(p, opt->rect);
2978
2979 QRenderRule titleRule = renderRule(w, opt, PseudoElement_GroupBoxTitle);
2980 bool clipSet = false;
2981
2982 if (hasTitle) {
2983 labelRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, w);
2984 //Some native style (such as mac) may return a too small rectangle (because they use smaller fonts), so we may need to expand it a little bit.
2985 labelRect.setSize(labelRect.size().expandedTo(ParentStyle::subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, w).size()));
2986 if (gb->subControls & QStyle::SC_GroupBoxCheckBox) {
2987 checkBoxRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxCheckBox, w);
2988 titleRect = titleRule.boxRect(checkBoxRect.united(labelRect));
2989 } else {
2990 titleRect = titleRule.boxRect(labelRect);
2991 }
2992 if (!titleRule.hasBackground() || !titleRule.background()->isTransparent()) {
2993 clipSet = true;
2994 p->save();
2995 p->setClipRegion(QRegion(opt->rect) - titleRect);
2996 }
2997 }
2998
2999 frameRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxFrame, w);
3000 QStyleOptionFrameV2 frame;
3001 frame.QStyleOption::operator=(*gb);
3002 frame.features = gb->features;
3003 frame.lineWidth = gb->lineWidth;
3004 frame.midLineWidth = gb->midLineWidth;
3005 frame.rect = frameRect;
3006 drawPrimitive(PE_FrameGroupBox, &frame, p, w);
3007
3008 if (clipSet)
3009 p->restore();
3010
3011 // draw background and frame of the title
3012 if (hasTitle)
3013 titleRule.drawRule(p, titleRect);
3014
3015 // draw the indicator
3016 if (gb->subControls & QStyle::SC_GroupBoxCheckBox) {
3017 QStyleOptionButton box;
3018 box.QStyleOption::operator=(*gb);
3019 box.rect = checkBoxRect;
3020 drawPrimitive(PE_IndicatorCheckBox, &box, p, w);
3021 }
3022
3023 // draw the text
3024 if (!gb->text.isEmpty()) {
3025 int alignment = int(Qt::AlignCenter | Qt::TextShowMnemonic);
3026 if (!styleHint(QStyle::SH_UnderlineShortcut, opt, w)) {
3027 alignment |= Qt::TextHideMnemonic;
3028 }
3029
3030 QPalette pal = gb->palette;
3031 if (gb->textColor.isValid())
3032 pal.setColor(QPalette::WindowText, gb->textColor);
3033 titleRule.configurePalette(&pal, QPalette::WindowText, QPalette::Window);
3034 drawItemText(p, labelRect, alignment, pal, gb->state & State_Enabled,
3035 gb->text, QPalette::WindowText);
3036 }
3037
3038 return;
3039 }
3040 break;
3041
3042 case CC_ToolButton:
3043 if (const QStyleOptionToolButton *tool = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
3044 QStyleOptionToolButton toolOpt(*tool);
3045 rule.configurePalette(&toolOpt.palette, QPalette::ButtonText, QPalette::Button);
3046 toolOpt.font = rule.font.resolve(toolOpt.font);
3047 toolOpt.rect = rule.borderRect(opt->rect);
3048 bool customArrow = (tool->features & (QStyleOptionToolButton::HasMenu | QStyleOptionToolButton::MenuButtonPopup));
3049 bool customDropDown = tool->features & QStyleOptionToolButton::MenuButtonPopup;
3050 if (rule.hasNativeBorder()) {
3051 if (tool->subControls & SC_ToolButton) {
3052 //in some case (eg. the button is "auto raised") the style doesn't draw the background
3053 //so we need to draw the background.
3054 // use the same condition as in QCommonStyle
3055 State bflags = tool->state & ~State_Sunken;
3056 if (bflags & State_AutoRaise && (!(bflags & State_MouseOver) || !(bflags & State_Enabled)))
3057 bflags &= ~State_Raised;
3058 if (tool->state & State_Sunken && tool->activeSubControls & SC_ToolButton)
3059 bflags |= State_Sunken;
3060 if (!(bflags & (State_Sunken | State_On | State_Raised)))
3061 rule.drawBackground(p, toolOpt.rect);
3062 }
3063 customArrow = customArrow && hasStyleRule(w, PseudoElement_ToolButtonDownArrow);
3064 if (customArrow)
3065 toolOpt.features &= ~QStyleOptionToolButton::HasMenu;
3066 customDropDown = customDropDown && hasStyleRule(w, PseudoElement_ToolButtonMenu);
3067 if (customDropDown)
3068 toolOpt.subControls &= ~QStyle::SC_ToolButtonMenu;
3069
3070 if (rule.baseStyleCanDraw() && !(tool->features & QStyleOptionToolButton::Arrow)) {
3071 baseStyle()->drawComplexControl(cc, &toolOpt, p, w);
3072 } else {
3073 QWindowsStyle::drawComplexControl(cc, &toolOpt, p, w);
3074 }
3075
3076 if (!customArrow && !customDropDown)
3077 return;
3078 } else {
3079 rule.drawRule(p, opt->rect);
3080 toolOpt.rect = rule.contentsRect(opt->rect);
3081 if (rule.hasFont)
3082 toolOpt.font = rule.font;
3083 drawControl(CE_ToolButtonLabel, &toolOpt, p, w);
3084 }
3085
3086 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolButtonMenu);
3087 QRect r = subControlRect(CC_ToolButton, opt, QStyle::SC_ToolButtonMenu, w);
3088 if (customDropDown) {
3089 if (opt->subControls & QStyle::SC_ToolButtonMenu) {
3090 if (subRule.hasDrawable()) {
3091 subRule.drawRule(p, r);
3092 } else {
3093 toolOpt.rect = r;
3094 baseStyle()->drawPrimitive(PE_IndicatorButtonDropDown, &toolOpt, p, w);
3095 }
3096 }
3097 }
3098
3099 if (customArrow) {
3100 QRenderRule subRule2 = customDropDown ? renderRule(w, opt, PseudoElement_ToolButtonMenuArrow)
3101 : renderRule(w, opt, PseudoElement_ToolButtonDownArrow);
3102 QRect r2 = customDropDown
3103 ? positionRect(w, subRule, subRule2, PseudoElement_ToolButtonMenuArrow, r, opt->direction)
3104 : positionRect(w, rule, subRule2, PseudoElement_ToolButtonDownArrow, opt->rect, opt->direction);
3105 if (subRule2.hasDrawable()) {
3106 subRule2.drawRule(p, r2);
3107 } else {
3108 toolOpt.rect = r2;
3109 baseStyle()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &toolOpt, p, w);
3110 }
3111 }
3112
3113 return;
3114 }
3115 break;
3116
3117#ifndef QT_NO_SCROLLBAR
3118 case CC_ScrollBar:
3119 if (const QStyleOptionSlider *sb = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
3120 QStyleOptionSlider sbOpt(*sb);
3121 if (!rule.hasDrawable()) {
3122 sbOpt.rect = rule.borderRect(opt->rect);
3123 rule.drawBackgroundImage(p, opt->rect);
3124 baseStyle()->drawComplexControl(cc, &sbOpt, p, w);
3125 } else {
3126 rule.drawRule(p, opt->rect);
3127 QWindowsStyle::drawComplexControl(cc, opt, p, w);
3128 }
3129 return;
3130 }
3131 break;
3132#endif // QT_NO_SCROLLBAR
3133
3134#ifndef QT_NO_SLIDER
3135 case CC_Slider:
3136 if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
3137 rule.drawRule(p, opt->rect);
3138
3139 QRenderRule grooveSubRule = renderRule(w, opt, PseudoElement_SliderGroove);
3140 QRenderRule handleSubRule = renderRule(w, opt, PseudoElement_SliderHandle);
3141 if (!grooveSubRule.hasDrawable()) {
3142 QStyleOptionSlider slOpt(*slider);
3143 bool handleHasRule = handleSubRule.hasDrawable();
3144 // If the style specifies a different handler rule, draw the groove without the handler.
3145 if (handleHasRule)
3146 slOpt.subControls &= ~SC_SliderHandle;
3147 baseStyle()->drawComplexControl(cc, &slOpt, p, w);
3148 if (!handleHasRule)
3149 return;
3150 }
3151
3152 QRect gr = subControlRect(cc, opt, SC_SliderGroove, w);
3153 if (slider->subControls & SC_SliderGroove) {
3154 grooveSubRule.drawRule(p, gr);
3155 }
3156
3157 if (slider->subControls & SC_SliderHandle) {
3158 QRect hr = subControlRect(cc, opt, SC_SliderHandle, w);
3159
3160 QRenderRule subRule1 = renderRule(w, opt, PseudoElement_SliderSubPage);
3161 if (subRule1.hasDrawable()) {
3162 QRect r(gr.topLeft(),
3163 slider->orientation == Qt::Horizontal
3164 ? QPoint(hr.x()+hr.width()/2, gr.y()+gr.height())
3165 : QPoint(gr.x()+gr.width(), hr.y()+hr.height()/2));
3166 subRule1.drawRule(p, r);
3167 }
3168
3169 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_SliderAddPage);
3170 if (subRule2.hasDrawable()) {
3171 QRect r(slider->orientation == Qt::Horizontal
3172 ? QPoint(hr.x()+hr.width()/2+1, gr.y())
3173 : QPoint(gr.x(), hr.y()+hr.height()/2+1),
3174 gr.bottomRight());
3175 subRule2.drawRule(p, r);
3176 }
3177
3178 handleSubRule.drawRule(p, grooveSubRule.boxRect(hr, Margin));
3179 }
3180
3181 if (slider->subControls & SC_SliderTickmarks) {
3182 // TODO...
3183 }
3184
3185 return;
3186 }
3187 break;
3188#endif // QT_NO_SLIDER
3189
3190 case CC_MdiControls:
3191 if (hasStyleRule(w, PseudoElement_MdiCloseButton)
3192 || hasStyleRule(w, PseudoElement_MdiNormalButton)
3193 || hasStyleRule(w, PseudoElement_MdiMinButton)) {
3194 QList<QVariant> layout = rule.styleHint(QLatin1String("button-layout")).toList();
3195 if (layout.isEmpty())
3196 layout = subControlLayout(QLatin1String("mNX"));
3197
3198 QStyleOptionComplex optCopy(*opt);
3199 optCopy.subControls = 0;
3200 for (int i = 0; i < layout.count(); i++) {
3201 int layoutButton = layout[i].toInt();
3202 if (layoutButton < PseudoElement_MdiCloseButton
3203 || layoutButton > PseudoElement_MdiNormalButton)
3204 continue;
3205 QStyle::SubControl control = knownPseudoElements[layoutButton].subControl;
3206 if (!(opt->subControls & control))
3207 continue;
3208 QRenderRule subRule = renderRule(w, opt, layoutButton);
3209 if (subRule.hasDrawable()) {
3210 QRect rect = subRule.boxRect(subControlRect(CC_MdiControls, opt, control, w), Margin);
3211 subRule.drawRule(p, rect);
3212 QIcon icon = standardIcon(subControlIcon(layoutButton), opt);
3213 icon.paint(p, subRule.contentsRect(rect), Qt::AlignCenter);
3214 } else {
3215 optCopy.subControls |= control;
3216 }
3217 }
3218
3219 if (optCopy.subControls)
3220 baseStyle()->drawComplexControl(CC_MdiControls, &optCopy, p, w);
3221 return;
3222 }
3223 break;
3224
3225 case CC_TitleBar:
3226 if (const QStyleOptionTitleBar *tb = qstyleoption_cast<const QStyleOptionTitleBar *>(opt)) {
3227 QRenderRule subRule = renderRule(w, opt, PseudoElement_TitleBar);
3228 if (!subRule.hasDrawable() && !subRule.hasBox() && !subRule.hasBorder())
3229 break;
3230 subRule.drawRule(p, opt->rect);
3231 QHash<QStyle::SubControl, QRect> layout = titleBarLayout(w, tb);
3232
3233 QRect ir;
3234 ir = layout[SC_TitleBarLabel];
3235 if (ir.isValid()) {
3236 if (subRule.hasPalette())
3237 p->setPen(subRule.palette()->foreground.color());
3238 p->fillRect(ir, Qt::white);
3239 p->drawText(ir.x(), ir.y(), ir.width(), ir.height(), Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, tb->text);
3240 }
3241
3242 QPixmap pm;
3243
3244 ir = layout[SC_TitleBarSysMenu];
3245 if (ir.isValid()) {
3246 QRenderRule subSubRule = renderRule(w, opt, PseudoElement_TitleBarSysMenu);
3247 subSubRule.drawRule(p, ir);
3248 ir = subSubRule.contentsRect(ir);
3249 if (!tb->icon.isNull()) {
3250 tb->icon.paint(p, ir);
3251 } else {
3252 int iconSize = pixelMetric(PM_SmallIconSize, tb, w);
3253 pm = standardIcon(SP_TitleBarMenuButton, 0, w).pixmap(iconSize, iconSize);
3254 drawItemPixmap(p, ir, Qt::AlignCenter, pm);
3255 }
3256 }
3257
3258 ir = layout[SC_TitleBarCloseButton];
3259 if (ir.isValid()) {
3260 QRenderRule subSubRule = renderRule(w, opt, PseudoElement_TitleBarCloseButton);
3261 subSubRule.drawRule(p, ir);
3262
3263 QSize sz = subSubRule.contentsRect(ir).size();
3264 if ((tb->titleBarFlags & Qt::WindowType_Mask) == Qt::Tool)
3265 pm = standardIcon(SP_DockWidgetCloseButton, 0, w).pixmap(sz);
3266 else
3267 pm = standardIcon(SP_TitleBarCloseButton, 0, w).pixmap(sz);
3268 drawItemPixmap(p, ir, Qt::AlignCenter, pm);
3269 }
3270
3271 int pes[] = {
3272 PseudoElement_TitleBarMaxButton,
3273 PseudoElement_TitleBarMinButton,
3274 PseudoElement_TitleBarNormalButton,
3275 PseudoElement_TitleBarShadeButton,
3276 PseudoElement_TitleBarUnshadeButton,
3277 PseudoElement_TitleBarContextHelpButton
3278 };
3279
3280 for (unsigned int i = 0; i < sizeof(pes)/sizeof(int); i++) {
3281 int pe = pes[i];
3282 QStyle::SubControl sc = knownPseudoElements[pe].subControl;
3283 ir = layout[sc];
3284 if (!ir.isValid())
3285 continue;
3286 QRenderRule subSubRule = renderRule(w, opt, pe);
3287 subSubRule.drawRule(p, ir);
3288 pm = standardIcon(subControlIcon(pe), 0, w).pixmap(subSubRule.contentsRect(ir).size());
3289 drawItemPixmap(p, ir, Qt::AlignCenter, pm);
3290 }
3291
3292 return;
3293 }
3294 break;
3295
3296
3297 default:
3298 break;
3299 }
3300
3301 baseStyle()->drawComplexControl(cc, opt, p, w);
3302}
3303
3304void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter *p,
3305 const QWidget *w) const
3306{
3307 RECURSION_GUARD(baseStyle()->drawControl(ce, opt, p, w); return)
3308
3309 QRenderRule rule = renderRule(w, opt);
3310 int pe1 = PseudoElement_None, pe2 = PseudoElement_None;
3311 bool fallback = false;
3312
3313 switch (ce) {
3314 case CE_ToolButtonLabel:
3315 if (const QStyleOptionToolButton *btn = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
3316 if (rule.hasBox() || btn->features & QStyleOptionToolButton::Arrow) {
3317 QCommonStyle::drawControl(ce, opt, p, w);
3318 } else {
3319 QStyleOptionToolButton butOpt(*btn);
3320 rule.configurePalette(&butOpt.palette, QPalette::ButtonText, QPalette::Button);
3321 baseStyle()->drawControl(ce, &butOpt, p, w);
3322 }
3323 return;
3324 }
3325 break;
3326
3327 case CE_PushButton:
3328 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
3329 if (rule.hasDrawable() || rule.hasBox() || rule.hasPosition() || rule.hasPalette() ||
3330 ((btn->features & QStyleOptionButton::HasMenu) && hasStyleRule(w, PseudoElement_PushButtonMenuIndicator))) {
3331 ParentStyle::drawControl(ce, opt, p, w);
3332 return;
3333 }
3334 }
3335 break;
3336 case CE_PushButtonBevel:
3337 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
3338 QStyleOptionButton btnOpt(*btn);
3339 btnOpt.rect = rule.borderRect(opt->rect);
3340 if (rule.hasNativeBorder()) {
3341 rule.drawBackgroundImage(p, btnOpt.rect);
3342 rule.configurePalette(&btnOpt.palette, QPalette::ButtonText, QPalette::Button);
3343 bool customMenu = (btn->features & QStyleOptionButton::HasMenu
3344 && hasStyleRule(w, PseudoElement_PushButtonMenuIndicator));
3345 if (customMenu)
3346 btnOpt.features &= ~QStyleOptionButton::HasMenu;
3347 if (rule.baseStyleCanDraw()) {
3348 baseStyle()->drawControl(ce, &btnOpt, p, w);
3349 } else {
3350 QWindowsStyle::drawControl(ce, &btnOpt, p, w);
3351 }
3352 if (!customMenu)
3353 return;
3354 } else {
3355 rule.drawRule(p, opt->rect);
3356 }
3357
3358 if (btn->features & QStyleOptionButton::HasMenu) {
3359 QRenderRule subRule = renderRule(w, opt, PseudoElement_PushButtonMenuIndicator);
3360 QRect ir = positionRect(w, rule, subRule, PseudoElement_PushButtonMenuIndicator, opt->rect, opt->direction);
3361 if (subRule.hasDrawable()) {
3362 subRule.drawRule(p, ir);
3363 } else {
3364 btnOpt.rect = ir;
3365 baseStyle()->drawPrimitive(PE_IndicatorArrowDown, &btnOpt, p, w);
3366 }
3367 }
3368 }
3369 return;
3370
3371 case CE_PushButtonLabel:
3372 if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
3373 QStyleOptionButton butOpt(*button);
3374 rule.configurePalette(&butOpt.palette, QPalette::ButtonText, QPalette::Button);
3375 if (rule.hasPosition() && rule.position()->textAlignment != 0) {
3376 Qt::Alignment textAlignment = rule.position()->textAlignment;
3377 QRect textRect = button->rect;
3378 uint tf = Qt::TextShowMnemonic;
3379 const uint verticalAlignMask = Qt::AlignVCenter | Qt::AlignTop | Qt::AlignLeft;
3380 tf |= (textAlignment & verticalAlignMask) ? (textAlignment & verticalAlignMask) : Qt::AlignVCenter;
3381 if (!styleHint(SH_UnderlineShortcut, button, w))
3382 tf |= Qt::TextHideMnemonic;
3383 if (!button->icon.isNull()) {
3384 //Group both icon and text
3385 QRect iconRect;
3386 QIcon::Mode mode = button->state & State_Enabled ? QIcon::Normal : QIcon::Disabled;
3387 if (mode == QIcon::Normal && button->state & State_HasFocus)
3388 mode = QIcon::Active;
3389 QIcon::State state = QIcon::Off;
3390 if (button->state & State_On)
3391 state = QIcon::On;
3392
3393 QPixmap pixmap = button->icon.pixmap(button->iconSize, mode, state);
3394 int labelWidth = pixmap.width();
3395 int labelHeight = pixmap.height();
3396 int iconSpacing = 4;//### 4 is currently hardcoded in QPushButton::sizeHint()
3397 int textWidth = button->fontMetrics.boundingRect(opt->rect, tf, button->text).width();
3398 if (!button->text.isEmpty())
3399 labelWidth += (textWidth + iconSpacing);
3400
3401 //Determine label alignment:
3402 if (textAlignment & Qt::AlignLeft) { /*left*/
3403 iconRect = QRect(textRect.x(), textRect.y() + (textRect.height() - labelHeight) / 2,
3404 pixmap.width(), pixmap.height());
3405 } else if (textAlignment & Qt::AlignHCenter) { /* center */
3406 iconRect = QRect(textRect.x() + (textRect.width() - labelWidth) / 2,
3407 textRect.y() + (textRect.height() - labelHeight) / 2,
3408 pixmap.width(), pixmap.height());
3409 } else { /*right*/
3410 iconRect = QRect(textRect.x() + textRect.width() - labelWidth,
3411 textRect.y() + (textRect.height() - labelHeight) / 2,
3412 pixmap.width(), pixmap.height());
3413 }
3414
3415 iconRect = visualRect(button->direction, textRect, iconRect);
3416
3417 tf |= Qt::AlignLeft; //left align, we adjust the text-rect instead
3418
3419 if (button->direction == Qt::RightToLeft)
3420 textRect.setRight(iconRect.left() - iconSpacing);
3421 else
3422 textRect.setLeft(iconRect.left() + iconRect.width() + iconSpacing);
3423
3424 if (button->state & (State_On | State_Sunken))
3425 iconRect.translate(pixelMetric(PM_ButtonShiftHorizontal, opt, w),
3426 pixelMetric(PM_ButtonShiftVertical, opt, w));
3427 p->drawPixmap(iconRect, pixmap);
3428 } else {
3429 tf |= textAlignment;
3430 }
3431 if (button->state & (State_On | State_Sunken))
3432 textRect.translate(pixelMetric(PM_ButtonShiftHorizontal, opt, w),
3433 pixelMetric(PM_ButtonShiftVertical, opt, w));
3434
3435 if (button->features & QStyleOptionButton::HasMenu) {
3436 int indicatorSize = pixelMetric(PM_MenuButtonIndicator, button, w);
3437 if (button->direction == Qt::LeftToRight)
3438 textRect = textRect.adjusted(0, 0, -indicatorSize, 0);
3439 else
3440 textRect = textRect.adjusted(indicatorSize, 0, 0, 0);
3441 }
3442 drawItemText(p, textRect, tf, butOpt.palette, (button->state & State_Enabled),
3443 button->text, QPalette::ButtonText);
3444 } else {
3445 ParentStyle::drawControl(ce, &butOpt, p, w);
3446 }
3447 }
3448 return;
3449
3450 case CE_RadioButton:
3451 case CE_CheckBox:
3452 rule.drawRule(p, opt->rect);
3453 ParentStyle::drawControl(ce, opt, p, w);
3454 return;
3455
3456 case CE_RadioButtonLabel:
3457 case CE_CheckBoxLabel:
3458 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
3459 QStyleOptionButton butOpt(*btn);
3460 rule.configurePalette(&butOpt.palette, QPalette::ButtonText, QPalette::Button);
3461 ParentStyle::drawControl(ce, &butOpt, p, w);
3462 }
3463 return;
3464
3465 case CE_Splitter:
3466 pe1 = PseudoElement_SplitterHandle;
3467 break;
3468
3469 case CE_ToolBar:
3470 if (rule.hasBackground()) {
3471 rule.drawBackground(p, opt->rect);
3472 }
3473 if (rule.hasBorder()) {
3474 rule.drawBorder(p, rule.borderRect(opt->rect));
3475 } else {
3476#ifndef QT_NO_TOOLBAR
3477 if (const QStyleOptionToolBar *tb = qstyleoption_cast<const QStyleOptionToolBar *>(opt)) {
3478 QStyleOptionToolBar newTb(*tb);
3479 newTb.rect = rule.borderRect(opt->rect);
3480 baseStyle()->drawControl(ce, &newTb, p, w);
3481 }
3482#endif // QT_NO_TOOLBAR
3483 }
3484 return;
3485
3486 case CE_MenuEmptyArea:
3487 case CE_MenuBarEmptyArea:
3488 if (rule.hasDrawable()) {
3489 // Drawn by PE_Widget
3490 return;
3491 }
3492 break;
3493
3494 case CE_MenuTearoff:
3495 case CE_MenuScroller:
3496 if (const QStyleOptionMenuItem *m = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
3497 QStyleOptionMenuItem mi(*m);
3498 int pe = ce == CE_MenuTearoff ? PseudoElement_MenuTearoff : PseudoElement_MenuScroller;
3499 QRenderRule subRule = renderRule(w, opt, pe);
3500 mi.rect = subRule.contentsRect(opt->rect);
3501 rule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3502 subRule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3503
3504 if (subRule.hasDrawable()) {
3505 subRule.drawRule(p, opt->rect);
3506 } else {
3507 baseStyle()->drawControl(ce, &mi, p, w);
3508 }
3509 }
3510 return;
3511
3512 case CE_MenuItem:
3513 if (const QStyleOptionMenuItem *m = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
3514 QStyleOptionMenuItem mi(*m);
3515
3516 int pseudo = (mi.menuItemType == QStyleOptionMenuItem::Separator) ? PseudoElement_MenuSeparator : PseudoElement_Item;
3517 QRenderRule subRule = renderRule(w, opt, pseudo);
3518 mi.rect = subRule.contentsRect(opt->rect);
3519 rule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3520 rule.configurePalette(&mi.palette, QPalette::HighlightedText, QPalette::Highlight);
3521 subRule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3522 subRule.configurePalette(&mi.palette, QPalette::HighlightedText, QPalette::Highlight);
3523 QFont oldFont = p->font();
3524 if (subRule.hasFont)
3525 p->setFont(subRule.font.resolve(p->font()));
3526
3527 // We fall back to drawing with the style sheet code whenever at least one of the
3528 // items are styled in an incompatible way, such as having a background image.
3529 QRenderRule allRules = renderRule(w, PseudoElement_Item, PseudoClass_Any);
3530
3531 if ((pseudo == PseudoElement_MenuSeparator) && subRule.hasDrawable()) {
3532 subRule.drawRule(p, opt->rect);
3533 } else if ((pseudo == PseudoElement_Item)
3534 && (allRules.hasBox() || allRules.hasBorder()
3535 || (allRules.background() && !allRules.background()->pixmap.isNull()))) {
3536 subRule.drawRule(p, opt->rect);
3537 if (subRule.hasBackground()) {
3538 mi.palette.setBrush(QPalette::Highlight, Qt::NoBrush);
3539 mi.palette.setBrush(QPalette::Button, Qt::NoBrush);
3540 } else {
3541 mi.palette.setBrush(QPalette::Highlight, mi.palette.brush(QPalette::Button));
3542 }
3543 mi.palette.setBrush(QPalette::HighlightedText, mi.palette.brush(QPalette::ButtonText));
3544
3545 bool checkable = mi.checkType != QStyleOptionMenuItem::NotCheckable;
3546 bool checked = checkable ? mi.checked : false;
3547
3548 bool dis = !(opt->state & QStyle::State_Enabled),
3549 act = opt->state & QStyle::State_Selected;
3550
3551 if (!mi.icon.isNull()) {
3552 QIcon::Mode mode = dis ? QIcon::Disabled : QIcon::Normal;
3553 if (act && !dis)
3554 mode = QIcon::Active;
3555 QPixmap pixmap;
3556 if (checked)
3557 pixmap = mi.icon.pixmap(pixelMetric(PM_SmallIconSize), mode, QIcon::On);
3558 else
3559 pixmap = mi.icon.pixmap(pixelMetric(PM_SmallIconSize), mode);
3560 int pixw = pixmap.width();
3561 int pixh = pixmap.height();
3562 QRenderRule iconRule = renderRule(w, opt, PseudoElement_MenuIcon);
3563 if (!iconRule.hasGeometry()) {
3564 iconRule.geo = new QStyleSheetGeometryData(pixw, pixh, pixw, pixh, -1, -1);
3565 } else {
3566 iconRule.geo->width = pixw;
3567 iconRule.geo->height = pixh;
3568 }
3569 QRect iconRect = positionRect(w, subRule, iconRule, PseudoElement_MenuIcon, opt->rect, opt->direction);
3570 iconRule.drawRule(p, iconRect);
3571 QRect pmr(0, 0, pixw, pixh);
3572 pmr.moveCenter(iconRect.center());
3573 p->drawPixmap(pmr.topLeft(), pixmap);
3574 } else if (checkable) {
3575 QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark);
3576 if (subSubRule.hasDrawable() || checked) {
3577 QStyleOptionMenuItem newMi = mi;
3578 newMi.rect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction);
3579 drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, w);
3580 }
3581 }
3582
3583 QRect textRect = subRule.contentsRect(opt->rect);
3584 textRect.setWidth(textRect.width() - mi.tabWidth);
3585 QString s = mi.text;
3586 p->setPen(mi.palette.buttonText().color());
3587 if (!s.isEmpty()) {
3588 int text_flags = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine;
3589 if (!styleHint(SH_UnderlineShortcut, &mi, w))
3590 text_flags |= Qt::TextHideMnemonic;
3591 int t = s.indexOf(QLatin1Char('\t'));
3592 if (t >= 0) {
3593 QRect vShortcutRect = visualRect(opt->direction, mi.rect,
3594 QRect(textRect.topRight(), QPoint(mi.rect.right(), textRect.bottom())));
3595 p->drawText(vShortcutRect, text_flags, s.mid(t + 1));
3596 s = s.left(t);
3597 }
3598 p->drawText(textRect, text_flags, s.left(t));
3599 }
3600
3601 if (mi.menuItemType == QStyleOptionMenuItem::SubMenu) {// draw sub menu arrow
3602 PrimitiveElement arrow = (opt->direction == Qt::RightToLeft) ? PE_IndicatorArrowLeft : PE_IndicatorArrowRight;
3603 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_MenuRightArrow);
3604 mi.rect = positionRect(w, subRule, subRule2, PseudoElement_MenuRightArrow, opt->rect, mi.direction);
3605 drawPrimitive(arrow, &mi, p, w);
3606 }
3607 } else if (hasStyleRule(w, PseudoElement_MenuCheckMark) || hasStyleRule(w, PseudoElement_MenuRightArrow)) {
3608 QWindowsStyle::drawControl(ce, &mi, p, w);
3609 if (mi.checkType != QStyleOptionMenuItem::NotCheckable && !mi.checked) {
3610 // We have a style defined, but QWindowsStyle won't draw anything if not checked.
3611 // So we mimick what QWindowsStyle would do.
3612 int checkcol = qMax<int>(mi.maxIconWidth, QWindowsStylePrivate::windowsCheckMarkWidth);
3613 QRect vCheckRect = visualRect(opt->direction, mi.rect, QRect(mi.rect.x(), mi.rect.y(), checkcol, mi.rect.height()));
3614 if (mi.state.testFlag(State_Enabled) && mi.state.testFlag(State_Selected)) {
3615 qDrawShadePanel(p, vCheckRect, mi.palette, true, 1, &mi.palette.brush(QPalette::Button));
3616 } else {
3617 QBrush fill(mi.palette.light().color(), Qt::Dense4Pattern);
3618 qDrawShadePanel(p, vCheckRect, mi.palette, true, 1, &fill);
3619 }
3620 QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark);
3621 if (subSubRule.hasDrawable()) {
3622 QStyleOptionMenuItem newMi(mi);
3623 newMi.rect = visualRect(opt->direction, mi.rect, QRect(mi.rect.x() + QWindowsStylePrivate::windowsItemFrame,
3624 mi.rect.y() + QWindowsStylePrivate::windowsItemFrame,
3625 checkcol - 2 * QWindowsStylePrivate::windowsItemFrame,
3626 mi.rect.height() - 2 * QWindowsStylePrivate::windowsItemFrame));
3627 drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, w);
3628 }
3629 }
3630 } else {
3631 if (rule.hasDrawable() && !subRule.hasDrawable() && !(opt->state & QStyle::State_Selected)) {
3632 mi.palette.setColor(QPalette::Window, Qt::transparent);
3633 mi.palette.setColor(QPalette::Button, Qt::transparent);
3634 }
3635 if (rule.baseStyleCanDraw() && subRule.baseStyleCanDraw()) {
3636 baseStyle()->drawControl(ce, &mi, p, w);
3637 } else {
3638 ParentStyle::drawControl(ce, &mi, p, w);
3639 }
3640 }
3641
3642 if (subRule.hasFont)
3643 p->setFont(oldFont);
3644
3645 return;
3646 }
3647 return;
3648
3649 case CE_MenuBarItem:
3650 if (const QStyleOptionMenuItem *m = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
3651 QStyleOptionMenuItem mi(*m);
3652 QRenderRule subRule = renderRule(w, opt, PseudoElement_Item);
3653 mi.rect = subRule.contentsRect(opt->rect);
3654 rule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3655 subRule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3656
3657 if (subRule.hasDrawable()) {
3658 subRule.drawRule(p, opt->rect);
3659 QCommonStyle::drawControl(ce, &mi, p, w);
3660 } else {
3661 if (rule.hasDrawable() && !(opt->state & QStyle::State_Selected)) {
3662 // So that the menu bar background is not hidden by the items
3663 mi.palette.setColor(QPalette::Window, Qt::transparent);
3664 mi.palette.setColor(QPalette::Button, Qt::transparent);
3665 }
3666 baseStyle()->drawControl(ce, &mi, p, w);
3667 }
3668 }
3669 return;
3670
3671#ifndef QT_NO_COMBOBOX
3672 case CE_ComboBoxLabel:
3673 if (!rule.hasBox())
3674 break;
3675 if (const QStyleOptionComboBox *cb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
3676 QRect editRect = subControlRect(CC_ComboBox, cb, SC_ComboBoxEditField, w);
3677 p->save();
3678 p->setClipRect(editRect);
3679 if (!cb->currentIcon.isNull()) {
3680 int spacing = rule.hasBox() ? rule.box()->spacing : -1;
3681 if (spacing == -1)
3682 spacing = 6;
3683 QIcon::Mode mode = cb->state & State_Enabled ? QIcon::Normal : QIcon::Disabled;
3684 QPixmap pixmap = cb->currentIcon.pixmap(cb->iconSize, mode);
3685 QRect iconRect(editRect);
3686 iconRect.setWidth(cb->iconSize.width());
3687 iconRect = alignedRect(cb->direction,
3688 Qt::AlignLeft | Qt::AlignVCenter,
3689 iconRect.size(), editRect);
3690 drawItemPixmap(p, iconRect, Qt::AlignCenter, pixmap);
3691
3692 if (cb->direction == Qt::RightToLeft)
3693 editRect.translate(-spacing - cb->iconSize.width(), 0);
3694 else
3695 editRect.translate(cb->iconSize.width() + spacing, 0);
3696 }
3697 if (!cb->currentText.isEmpty() && !cb->editable) {
3698 drawItemText(p, editRect.adjusted(0, 0, 0, 0), Qt::AlignLeft | Qt::AlignVCenter, cb->palette,
3699 cb->state & State_Enabled, cb->currentText, QPalette::Text);
3700 }
3701 p->restore();
3702 return;
3703 }
3704 break;
3705#endif // QT_NO_COMBOBOX
3706
3707 case CE_Header:
3708 if (hasStyleRule(w, PseudoElement_HeaderViewUpArrow)
3709 || hasStyleRule(w, PseudoElement_HeaderViewDownArrow)) {
3710 ParentStyle::drawControl(ce, opt, p, w);
3711 return;
3712 }
3713 if(hasStyleRule(w, PseudoElement_HeaderViewSection)) {
3714 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
3715 if (!subRule.hasNativeBorder() || !subRule.baseStyleCanDraw()
3716 || subRule.hasBackground() || subRule.hasPalette()) {
3717 ParentStyle::drawControl(ce, opt, p, w);
3718 return;
3719 }
3720 }
3721 break;
3722 case CE_HeaderSection:
3723 if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
3724 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
3725 if (subRule.hasNativeBorder()) {
3726 QStyleOptionHeader hdr(*header);
3727 subRule.configurePalette(&hdr.palette, QPalette::ButtonText, QPalette::Button);
3728
3729 if (subRule.baseStyleCanDraw()) {
3730 baseStyle()->drawControl(CE_HeaderSection, &hdr, p, w);
3731 } else {
3732 QWindowsStyle::drawControl(CE_HeaderSection, &hdr, p, w);
3733 }
3734 } else {
3735 subRule.drawRule(p, opt->rect);
3736 }
3737 return;
3738 }
3739 break;
3740
3741 case CE_HeaderLabel:
3742 if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
3743 QStyleOptionHeader hdr(*header);
3744 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
3745 subRule.configurePalette(&hdr.palette, QPalette::ButtonText, QPalette::Button);
3746 QFont oldFont = p->font();
3747 if (subRule.hasFont)
3748 p->setFont(subRule.font.resolve(p->font()));
3749 baseStyle()->drawControl(ce, &hdr, p, w);
3750 if (subRule.hasFont)
3751 p->setFont(oldFont);
3752 return;
3753 }
3754 break;
3755
3756 case CE_HeaderEmptyArea:
3757 if (rule.hasDrawable()) {
3758 return;
3759 }
3760 break;
3761
3762 case CE_ProgressBar:
3763 QWindowsStyle::drawControl(ce, opt, p, w);
3764 return;
3765
3766 case CE_ProgressBarGroove:
3767 if (!rule.hasNativeBorder()) {
3768 rule.drawRule(p, rule.boxRect(opt->rect, Margin));
3769 return;
3770 }
3771 break;
3772
3773 case CE_ProgressBarContents: {
3774 QRenderRule subRule = renderRule(w, opt, PseudoElement_ProgressBarChunk);
3775 if (subRule.hasDrawable()) {
3776 if (const QStyleOptionProgressBarV2 *pb = qstyleoption_cast<const QStyleOptionProgressBarV2 *>(opt)) {
3777 p->save();
3778 p->setClipRect(pb->rect);
3779
3780 qint64 minimum = qint64(pb->minimum);
3781 qint64 maximum = qint64(pb->maximum);
3782 qint64 progress = qint64(pb->progress);
3783 bool vertical = (pb->orientation == Qt::Vertical);
3784 bool inverted = pb->invertedAppearance;
3785
3786 QTransform m;
3787 QRect rect = pb->rect;
3788 if (vertical) {
3789 rect = QRect(rect.y(), rect.x(), rect.height(), rect.width());
3790 m.rotate(90);
3791 m.translate(0, -(rect.height() + rect.y()*2));
3792 }
3793
3794 bool reverse = ((!vertical && (pb->direction == Qt::RightToLeft)) || vertical);
3795 if (inverted)
3796 reverse = !reverse;
3797 const bool indeterminate = pb->minimum == pb->maximum;
3798 qreal fillRatio = indeterminate ? 0.50 : qreal(progress - minimum)/(maximum - minimum);
3799 int fillWidth = int(rect.width() * fillRatio);
3800 int chunkWidth = fillWidth;
3801 if (subRule.hasContentsSize()) {
3802 QSize sz = subRule.size();
3803 chunkWidth = (opt->state & QStyle::State_Horizontal) ? sz.width() : sz.height();
3804 }
3805
3806 QRect r = rect;
3807 if (pb->minimum == 0 && pb->maximum == 0) {
3808 Q_D(const QWindowsStyle);
3809 int chunkCount = fillWidth/chunkWidth;
3810 int offset = (d->animateStep*8%rect.width());
3811 int x = reverse ? r.left() + r.width() - offset - chunkWidth : r.x() + offset;
3812 while (chunkCount > 0) {
3813 r.setRect(x, rect.y(), chunkWidth, rect.height());
3814 r = m.mapRect(QRectF(r)).toRect();
3815 subRule.drawRule(p, r);
3816 x += reverse ? -chunkWidth : chunkWidth;
3817 if (reverse ? x < rect.left() : x > rect.right())
3818 break;
3819 --chunkCount;
3820 }
3821
3822 r = rect;
3823 x = reverse ? r.right() - (r.left() - x - chunkWidth)
3824 : r.left() + (x - r.right() - chunkWidth);
3825 while (chunkCount > 0) {
3826 r.setRect(x, rect.y(), chunkWidth, rect.height());
3827 r = m.mapRect(QRectF(r)).toRect();
3828 subRule.drawRule(p, r);
3829 x += reverse ? -chunkWidth : chunkWidth;
3830 --chunkCount;
3831 };
3832 } else {
3833 int x = reverse ? r.left() + r.width() - chunkWidth : r.x();
3834
3835 for (int i = 0; i < ceil(qreal(fillWidth)/chunkWidth); ++i) {
3836 r.setRect(x, rect.y(), chunkWidth, rect.height());
3837 r = m.mapRect(QRectF(r)).toRect();
3838 subRule.drawRule(p, r);
3839 x += reverse ? -chunkWidth : chunkWidth;
3840 }
3841 }
3842
3843 p->restore();
3844 return;
3845 }
3846 }
3847 }
3848 break;
3849
3850 case CE_ProgressBarLabel:
3851 if (const QStyleOptionProgressBarV2 *pb = qstyleoption_cast<const QStyleOptionProgressBarV2 *>(opt)) {
3852 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_ProgressBarChunk)) {
3853 drawItemText(p, pb->rect, pb->textAlignment | Qt::TextSingleLine, pb->palette,
3854 pb->state & State_Enabled, pb->text, QPalette::Text);
3855 } else {
3856 QStyleOptionProgressBarV2 pbCopy(*pb);
3857 rule.configurePalette(&pbCopy.palette, QPalette::HighlightedText, QPalette::Highlight);
3858 baseStyle()->drawControl(ce, &pbCopy, p, w);
3859 }
3860 return;
3861 }
3862 break;
3863
3864 case CE_SizeGrip:
3865 if (const QStyleOptionSizeGrip *sgOpt = qstyleoption_cast<const QStyleOptionSizeGrip *>(opt)) {
3866 if (rule.hasDrawable()) {
3867 rule.drawFrame(p, opt->rect);
3868 p->save();
3869 switch (sgOpt->corner) {
3870 case Qt::BottomRightCorner: break;
3871 case Qt::BottomLeftCorner: p->rotate(90); break;
3872 case Qt::TopLeftCorner: p->rotate(180); break;
3873 case Qt::TopRightCorner: p->rotate(270); break;
3874 default: break;
3875 }
3876 rule.drawImage(p, opt->rect);
3877 p->restore();
3878 } else {
3879 QStyleOptionSizeGrip sg(*sgOpt);
3880 sg.rect = rule.contentsRect(opt->rect);
3881 baseStyle()->drawControl(CE_SizeGrip, &sg, p, w);
3882 }
3883 return;
3884 }
3885 break;
3886
3887 case CE_ToolBoxTab:
3888 QWindowsStyle::drawControl(ce, opt, p, w);
3889 return;
3890
3891 case CE_ToolBoxTabShape: {
3892 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolBoxTab);
3893 if (subRule.hasDrawable()) {
3894 subRule.drawRule(p, opt->rect);
3895 return;
3896 }
3897 }
3898 break;
3899
3900 case CE_ToolBoxTabLabel:
3901 if (const QStyleOptionToolBox *box = qstyleoption_cast<const QStyleOptionToolBox *>(opt)) {
3902 QStyleOptionToolBox boxCopy(*box);
3903 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolBoxTab);
3904 subRule.configurePalette(&boxCopy.palette, QPalette::ButtonText, QPalette::Button);
3905 QFont oldFont = p->font();
3906 if (subRule.hasFont)
3907 p->setFont(subRule.font);
3908 boxCopy.rect = subRule.contentsRect(opt->rect);
3909 QWindowsStyle::drawControl(ce, &boxCopy, p , w);
3910 if (subRule.hasFont)
3911 p->setFont(oldFont);
3912 return;
3913 }
3914 break;
3915
3916 case CE_ScrollBarAddPage:
3917 pe1 = PseudoElement_ScrollBarAddPage;
3918 break;
3919
3920 case CE_ScrollBarSubPage:
3921 pe1 = PseudoElement_ScrollBarSubPage;
3922 break;
3923
3924 case CE_ScrollBarAddLine:
3925 pe1 = PseudoElement_ScrollBarAddLine;
3926 pe2 = (opt->state & QStyle::State_Horizontal) ? PseudoElement_ScrollBarRightArrow : PseudoElement_ScrollBarDownArrow;
3927 fallback = true;
3928 break;
3929
3930 case CE_ScrollBarSubLine:
3931 pe1 = PseudoElement_ScrollBarSubLine;
3932 pe2 = (opt->state & QStyle::State_Horizontal) ? PseudoElement_ScrollBarLeftArrow : PseudoElement_ScrollBarUpArrow;
3933 fallback = true;
3934 break;
3935
3936 case CE_ScrollBarFirst:
3937 pe1 = PseudoElement_ScrollBarFirst;
3938 break;
3939
3940 case CE_ScrollBarLast:
3941 pe1 = PseudoElement_ScrollBarLast;
3942 break;
3943
3944 case CE_ScrollBarSlider:
3945 pe1 = PseudoElement_ScrollBarSlider;
3946 fallback = true;
3947 break;
3948
3949#ifndef QT_NO_ITEMVIEWS
3950 case CE_ItemViewItem:
3951 if (const QStyleOptionViewItemV4 *vopt = qstyleoption_cast<const QStyleOptionViewItemV4 *>(opt)) {
3952 QRenderRule subRule = renderRule(w, opt, PseudoElement_ViewItem);
3953 if (subRule.hasDrawable() || hasStyleRule(w, PseudoElement_Indicator)) {
3954 QStyleOptionViewItemV4 optCopy(*vopt);
3955 subRule.configurePalette(&optCopy.palette, vopt->state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text,
3956 vopt->state & QStyle::State_Selected ? QPalette::Highlight : QPalette::Base);
3957 QWindowsStyle::drawControl(ce, &optCopy, p, w);
3958 } else {
3959 QStyleOptionViewItemV4 voptCopy(*vopt);
3960 subRule.configurePalette(&voptCopy.palette, QPalette::Text, QPalette::NoRole);
3961 baseStyle()->drawControl(ce, &voptCopy, p, w);
3962 }
3963 return;
3964 }
3965 break;
3966#endif // QT_NO_ITEMVIEWS
3967
3968#ifndef QT_NO_TABBAR
3969 case CE_TabBarTab:
3970 if (hasStyleRule(w, PseudoElement_TabBarTab)) {
3971 QWindowsStyle::drawControl(ce, opt, p, w);
3972 return;
3973 }
3974 break;
3975
3976 case CE_TabBarTabLabel:
3977 case CE_TabBarTabShape:
3978 if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
3979 QStyleOptionTabV3 tabCopy(*tab);
3980 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab);
3981 QRect r = positionRect(w, subRule, PseudoElement_TabBarTab, opt->rect, opt->direction);
3982 if (ce == CE_TabBarTabShape && subRule.hasDrawable()) {
3983 subRule.drawRule(p, r);
3984 return;
3985 }
3986 subRule.configurePalette(&tabCopy.palette, QPalette::WindowText, QPalette::Window);
3987 QFont oldFont = p->font();
3988 if (subRule.hasFont)
3989 p->setFont(subRule.font);
3990 if (subRule.hasBox() || !subRule.hasNativeBorder()) {
3991 tabCopy.rect = ce == CE_TabBarTabShape ? subRule.borderRect(r)
3992 : subRule.contentsRect(r);
3993 QWindowsStyle::drawControl(ce, &tabCopy, p, w);
3994 } else {
3995 baseStyle()->drawControl(ce, &tabCopy, p, w);
3996 }
3997 if (subRule.hasFont)
3998 p->setFont(oldFont);
3999
4000 return;
4001 }
4002 break;
4003#endif // QT_NO_TABBAR
4004
4005 case CE_ColumnViewGrip:
4006 if (rule.hasDrawable()) {
4007 rule.drawRule(p, opt->rect);
4008 return;
4009 }
4010 break;
4011
4012 case CE_DockWidgetTitle:
4013 if (const QStyleOptionDockWidgetV2 *dwOpt = qstyleoption_cast<const QStyleOptionDockWidgetV2 *>(opt)) {
4014 QRenderRule subRule = renderRule(w, opt, PseudoElement_DockWidgetTitle);
4015 if (!subRule.hasDrawable() && !subRule.hasPosition())
4016 break;
4017 if (subRule.hasDrawable()) {
4018 subRule.drawRule(p, opt->rect);
4019 } else {
4020 QStyleOptionDockWidgetV2 dwCopy(*dwOpt);
4021 dwCopy.title = QString();
4022 baseStyle()->drawControl(ce, &dwCopy, p, w);
4023 }
4024
4025 if (!dwOpt->title.isEmpty()) {
4026 QRect r = opt->rect;
4027 if (dwOpt->verticalTitleBar) {
4028 QSize s = r.size();
4029 s.transpose();
4030 r.setSize(s);
4031
4032 p->save();
4033 p->translate(r.left(), r.top() + r.width());
4034 p->rotate(-90);
4035 p->translate(-r.left(), -r.top());
4036 }
4037
4038 Qt::Alignment alignment = 0;
4039 if (subRule.hasPosition())
4040 alignment = subRule.position()->textAlignment;
4041 if (alignment == 0)
4042 alignment = Qt::AlignLeft;
4043 drawItemText(p, subRule.contentsRect(opt->rect),
4044 alignment | Qt::TextShowMnemonic, dwOpt->palette,
4045 dwOpt->state & State_Enabled, dwOpt->title,
4046 QPalette::WindowText);
4047
4048 if (dwOpt->verticalTitleBar)
4049 p->restore();
4050 }
4051
4052 return;
4053 }
4054 break;
4055 case CE_ShapedFrame:
4056 if (const QStyleOptionFrame *frm = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
4057 if (rule.hasNativeBorder()) {
4058 QStyleOptionFrameV3 frmOpt(*frm);
4059 rule.configurePalette(&frmOpt.palette, QPalette::Text, QPalette::Base);
4060 frmOpt.rect = rule.borderRect(frmOpt.rect);
4061 baseStyle()->drawControl(ce, &frmOpt, p, w);
4062 }
4063 // else, borders are already drawn in PE_Widget
4064 }
4065 return;
4066
4067
4068 default:
4069 break;
4070 }
4071
4072 if (pe1 != PseudoElement_None) {
4073 QRenderRule subRule = renderRule(w, opt, pe1);
4074 if (subRule.bg != 0 || subRule.hasDrawable()) {
4075 //We test subRule.bg dirrectly because hasBackground() would return false for background:none.
4076 //But we still don't want the default drawning in that case (example for QScrollBar::add-page) (task 198926)
4077 subRule.drawRule(p, opt->rect);
4078 } else if (fallback) {
4079 QWindowsStyle::drawControl(ce, opt, p, w);
4080 pe2 = PseudoElement_None;
4081 } else {
4082 baseStyle()->drawControl(ce, opt, p, w);
4083 }
4084 if (pe2 != PseudoElement_None) {
4085 QRenderRule subSubRule = renderRule(w, opt, pe2);
4086 QRect r = positionRect(w, subRule, subSubRule, pe2, opt->rect, opt->direction);
4087 subSubRule.drawRule(p, r);
4088 }
4089 return;
4090 }
4091
4092 baseStyle()->drawControl(ce, opt, p, w);
4093}
4094
4095void QStyleSheetStyle::drawItemPixmap(QPainter *p, const QRect &rect, int alignment, const
4096 QPixmap &pixmap) const
4097{
4098 baseStyle()->drawItemPixmap(p, rect, alignment, pixmap);
4099}
4100
4101void QStyleSheetStyle::drawItemText(QPainter *painter, const QRect& rect, int alignment, const QPalette &pal,
4102 bool enabled, const QString& text, QPalette::ColorRole textRole) const
4103{
4104 baseStyle()->drawItemText(painter, rect, alignment, pal, enabled, text, textRole);
4105}
4106
4107void QStyleSheetStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p,
4108 const QWidget *w) const
4109{
4110 RECURSION_GUARD(baseStyle()->drawPrimitive(pe, opt, p, w); return)
4111
4112 int pseudoElement = PseudoElement_None;
4113 QRenderRule rule = renderRule(w, opt);
4114 QRect rect = opt->rect;
4115
4116 switch (pe) {
4117
4118 case PE_FrameStatusBar: {
4119 QRenderRule subRule = renderRule(w->parentWidget(), opt, PseudoElement_Item);
4120 if (subRule.hasDrawable()) {
4121 subRule.drawRule(p, opt->rect);
4122 return;
4123 }
4124 break;
4125 }
4126
4127 case PE_IndicatorArrowDown:
4128 pseudoElement = PseudoElement_DownArrow;
4129 break;
4130
4131 case PE_IndicatorRadioButton:
4132 pseudoElement = PseudoElement_ExclusiveIndicator;
4133 break;
4134
4135 case PE_IndicatorViewItemCheck:
4136 pseudoElement = PseudoElement_ViewItemIndicator;
4137 break;
4138
4139 case PE_IndicatorCheckBox:
4140 pseudoElement = PseudoElement_Indicator;
4141 break;
4142
4143 case PE_IndicatorHeaderArrow:
4144 if (const QStyleOptionHeader *hdr = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
4145 pseudoElement = hdr->sortIndicator == QStyleOptionHeader::SortUp
4146 ? PseudoElement_HeaderViewUpArrow
4147 : PseudoElement_HeaderViewDownArrow;
4148 }
4149 break;
4150
4151 case PE_PanelButtonTool:
4152 case PE_PanelButtonCommand:
4153 if (qobject_cast<const QAbstractButton *>(w) && rule.hasBackground() && rule.hasNativeBorder()) {
4154 //the window style will draw the borders
4155 ParentStyle::drawPrimitive(pe, opt, p, w);
4156 if (!rule.background()->pixmap.isNull() || rule.hasImage()) {
4157 rule.drawRule(p, rule.boxRect(opt->rect, QRenderRule::Margin).adjusted(1,1,-1,-1));
4158 }
4159 return;
4160 }
4161 if (!rule.hasNativeBorder()) {
4162 rule.drawRule(p, rule.boxRect(opt->rect, QRenderRule::Margin));
4163 return;
4164 }
4165 break;
4166
4167 case PE_IndicatorButtonDropDown: {
4168 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolButtonMenu);
4169 if (!subRule.hasNativeBorder()) {
4170 rule.drawBorder(p, opt->rect);
4171 return;
4172 }
4173 break;
4174 }
4175
4176 case PE_FrameDefaultButton:
4177 if (rule.hasNativeBorder()) {
4178 if (rule.baseStyleCanDraw())
4179 break;
4180 QWindowsStyle::drawPrimitive(pe, opt, p, w);
4181 }
4182 return;
4183
4184 case PE_FrameWindow:
4185 case PE_FrameDockWidget:
4186 case PE_Frame:
4187 if (const QStyleOptionFrame *frm = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
4188 if (rule.hasNativeBorder()) {
4189 QStyleOptionFrameV2 frmOpt(*frm);
4190 rule.configurePalette(&frmOpt.palette, QPalette::Text, QPalette::Base);
4191 if (!qstyleoption_cast<const QStyleOptionFrameV3 *>(opt)) //if it comes from CE_ShapedFrame, the margins are already sustracted
4192 frmOpt.rect = rule.borderRect(frmOpt.rect);
4193 baseStyle()->drawPrimitive(pe, &frmOpt, p, w);
4194 } else {
4195 rule.drawBorder(p, rule.borderRect(opt->rect));
4196 }
4197 }
4198 return;
4199
4200 case PE_PanelLineEdit:
4201 if (const QStyleOptionFrame *frm = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
4202#ifndef QT_NO_SPINBOX
4203 if (w && qobject_cast<const QAbstractSpinBox *>(w->parentWidget())) {
4204 QRenderRule spinboxRule = renderRule(w->parentWidget(), opt);
4205 if (!spinboxRule.hasNativeBorder() || !spinboxRule.baseStyleCanDraw())
4206 return;
4207 rule = spinboxRule;
4208 }
4209#endif
4210 if (rule.hasNativeBorder()) {
4211 QStyleOptionFrame frmOpt(*frm);
4212 rule.configurePalette(&frmOpt.palette, QPalette::Text, QPalette::Base);
4213 frmOpt.rect = rule.borderRect(frmOpt.rect);
4214 if (rule.baseStyleCanDraw()) {
4215 rule.drawBackgroundImage(p, opt->rect);
4216 baseStyle()->drawPrimitive(pe, &frmOpt, p, w);
4217 } else {
4218 rule.drawBackground(p, opt->rect);
4219 if (frmOpt.lineWidth > 0)
4220 baseStyle()->drawPrimitive(PE_FrameLineEdit, &frmOpt, p, w);
4221 }
4222 } else {
4223 rule.drawRule(p, opt->rect);
4224 }
4225 }
4226 return;
4227
4228 case PE_Widget:
4229 if (!rule.hasDrawable()) {
4230 QWidget *container = containerWidget(w);
4231 if (autoFillDisabledWidgets->contains(container)
4232 && (container == w || !renderRule(container, opt).hasBackground())) {
4233 //we do not have a background, but we disabled the autofillbackground anyway. so fill the background now.
4234 // (this may happen if we have rules like :focus)
4235 p->fillRect(opt->rect, opt->palette.brush(w->backgroundRole()));
4236 }
4237 break;
4238 }
4239#ifndef QT_NO_SCROLLAREA
4240 if (const QAbstractScrollArea *sa = qobject_cast<const QAbstractScrollArea *>(w)) {
4241 const QAbstractScrollAreaPrivate *sap = sa->d_func();
4242 rule.drawBackground(p, opt->rect, sap->contentsOffset());
4243 if (rule.hasBorder())
4244 rule.drawBorder(p, rule.borderRect(opt->rect));
4245 break;
4246 }
4247#endif
4248 //fall tghought
4249 case PE_PanelMenu:
4250 case PE_PanelStatusBar:
4251 if(rule.hasDrawable()) {
4252 rule.drawRule(p, opt->rect);
4253 return;
4254 }
4255 break;
4256
4257 case PE_PanelMenuBar:
4258 if (rule.hasDrawable()) {
4259 // Drawn by PE_Widget
4260 return;
4261 }
4262 break;
4263
4264 case PE_IndicatorToolBarSeparator:
4265 case PE_IndicatorToolBarHandle: {
4266 PseudoElement ps = pe == PE_IndicatorToolBarHandle ? PseudoElement_ToolBarHandle : PseudoElement_ToolBarSeparator;
4267 QRenderRule subRule = renderRule(w, opt, ps);
4268 if (subRule.hasDrawable()) {
4269 subRule.drawRule(p, opt->rect);
4270 return;
4271 }
4272 }
4273 break;
4274
4275 case PE_IndicatorMenuCheckMark:
4276 pseudoElement = PseudoElement_MenuCheckMark;
4277 break;
4278
4279 case PE_IndicatorArrowLeft:
4280 pseudoElement = PseudoElement_LeftArrow;
4281 break;
4282
4283 case PE_IndicatorArrowRight:
4284 pseudoElement = PseudoElement_RightArrow;
4285 break;
4286
4287 case PE_IndicatorColumnViewArrow:
4288 if (const QStyleOptionViewItem *viewOpt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
4289 bool reverse = (viewOpt->direction == Qt::RightToLeft);
4290 pseudoElement = reverse ? PseudoElement_LeftArrow : PseudoElement_RightArrow;
4291 } else {
4292 pseudoElement = PseudoElement_RightArrow;
4293 }
4294 break;
4295
4296 case PE_IndicatorBranch:
4297 if (const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(opt)) {
4298 QRenderRule subRule = renderRule(w, opt, PseudoElement_TreeViewBranch);
4299 if (subRule.hasDrawable()) {
4300 if ((v2->state & QStyle::State_Selected) && v2->showDecorationSelected)
4301 p->fillRect(v2->rect, v2->palette.highlight());
4302 else if (v2->features & QStyleOptionViewItemV2::Alternate)
4303 p->fillRect(v2->rect, v2->palette.alternateBase());
4304 subRule.drawRule(p, opt->rect);
4305 } else {
4306 baseStyle()->drawPrimitive(pe, v2, p, w);
4307 }
4308 }
4309 return;
4310
4311 case PE_PanelTipLabel:
4312 if (!rule.hasDrawable())
4313 break;
4314
4315 if (const QStyleOptionFrame *frmOpt = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
4316 if (rule.hasNativeBorder()) {
4317 rule.drawBackground(p, opt->rect);
4318 QStyleOptionFrame optCopy(*frmOpt);
4319 optCopy.rect = rule.borderRect(opt->rect);
4320 optCopy.palette.setBrush(QPalette::Window, Qt::NoBrush); // oh dear
4321 baseStyle()->drawPrimitive(pe, &optCopy, p, w);
4322 } else {
4323 rule.drawRule(p, opt->rect);
4324 }
4325 }
4326 return;
4327
4328 case PE_FrameGroupBox:
4329 if (rule.hasNativeBorder())
4330 break;
4331 rule.drawBorder(p, opt->rect);
4332 return;
4333
4334#ifndef QT_NO_TABWIDGET
4335 case PE_FrameTabWidget:
4336 if (const QStyleOptionTabWidgetFrame *frm = qstyleoption_cast<const QStyleOptionTabWidgetFrame *>(opt)) {
4337 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabWidgetPane);
4338 if (subRule.hasNativeBorder()) {
4339 subRule.drawBackground(p, opt->rect);
4340 QStyleOptionTabWidgetFrameV2 frmCopy(*frm);
4341 subRule.configurePalette(&frmCopy.palette, QPalette::WindowText, QPalette::Window);
4342 baseStyle()->drawPrimitive(pe, &frmCopy, p, w);
4343 } else {
4344 subRule.drawRule(p, opt->rect);
4345 }
4346 return;
4347 }
4348 break;
4349#endif // QT_NO_TABWIDGET
4350
4351 case PE_IndicatorProgressChunk:
4352 pseudoElement = PseudoElement_ProgressBarChunk;
4353 break;
4354
4355 case PE_IndicatorTabTear:
4356 pseudoElement = PseudoElement_TabBarTear;
4357 break;
4358
4359 case PE_FrameFocusRect:
4360 if (!rule.hasNativeOutline()) {
4361 rule.drawOutline(p, opt->rect);
4362 return;
4363 }
4364 break;
4365
4366 case PE_IndicatorDockWidgetResizeHandle:
4367 pseudoElement = PseudoElement_DockWidgetSeparator;
4368 break;
4369
4370 case PE_PanelItemViewItem:
4371 pseudoElement = PseudoElement_ViewItem;
4372 break;
4373
4374 case PE_PanelScrollAreaCorner:
4375 pseudoElement = PseudoElement_ScrollAreaCorner;
4376 break;
4377
4378 case PE_IndicatorSpinDown:
4379 case PE_IndicatorSpinMinus:
4380 pseudoElement = PseudoElement_SpinBoxDownArrow;
4381 break;
4382
4383 case PE_IndicatorSpinUp:
4384 case PE_IndicatorSpinPlus:
4385 pseudoElement = PseudoElement_SpinBoxUpArrow;
4386 break;
4387#ifndef QT_NO_TABBAR
4388 case PE_IndicatorTabClose:
4389 if (w)
4390 w = w->parentWidget(); //match on the QTabBar instead of the CloseButton
4391 pseudoElement = PseudoElement_TabBarTabCloseButton;
4392#endif
4393
4394 default:
4395 break;
4396 }
4397
4398 if (pseudoElement != PseudoElement_None) {
4399 QRenderRule subRule = renderRule(w, opt, pseudoElement);
4400 if (subRule.hasDrawable()) {
4401 subRule.drawRule(p, rect);
4402 } else {
4403 baseStyle()->drawPrimitive(pe, opt, p, w);
4404 }
4405 } else {
4406 baseStyle()->drawPrimitive(pe, opt, p, w);
4407 }
4408}
4409
4410QPixmap QStyleSheetStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap& pixmap,
4411 const QStyleOption *option) const
4412{
4413 return baseStyle()->generatedIconPixmap(iconMode, pixmap, option);
4414}
4415
4416QStyle::SubControl QStyleSheetStyle::hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex *opt,
4417 const QPoint &pt, const QWidget *w) const
4418{
4419 RECURSION_GUARD(return baseStyle()->hitTestComplexControl(cc, opt, pt, w))
4420 switch (cc) {
4421 case CC_TitleBar:
4422 if (const QStyleOptionTitleBar *tb = qstyleoption_cast<const QStyleOptionTitleBar *>(opt)) {
4423 QRenderRule rule = renderRule(w, opt, PseudoElement_TitleBar);
4424 if (rule.hasDrawable() || rule.hasBox() || rule.hasBorder()) {
4425 QHash<QStyle::SubControl, QRect> layout = titleBarLayout(w, tb);
4426 QRect r;
4427 QStyle::SubControl sc = QStyle::SC_None;
4428 uint ctrl = SC_TitleBarSysMenu;
4429 while (ctrl <= SC_TitleBarLabel) {
4430 r = layout[QStyle::SubControl(ctrl)];
4431 if (r.isValid() && r.contains(pt)) {
4432 sc = QStyle::SubControl(ctrl);
4433 break;
4434 }
4435 ctrl <<= 1;
4436 }
4437 return sc;
4438 }
4439 }
4440 break;
4441
4442 case CC_MdiControls:
4443 if (hasStyleRule(w, PseudoElement_MdiCloseButton)
4444 || hasStyleRule(w, PseudoElement_MdiNormalButton)
4445 || hasStyleRule(w, PseudoElement_MdiMinButton))
4446 return QWindowsStyle::hitTestComplexControl(cc, opt, pt, w);
4447 break;
4448
4449 case CC_ScrollBar: {
4450 QRenderRule rule = renderRule(w, opt);
4451 if (!rule.hasDrawable() && !rule.hasBox())
4452 break;
4453 }
4454 // intentionally falls through
4455 case CC_SpinBox:
4456 case CC_GroupBox:
4457 case CC_ComboBox:
4458 case CC_Slider:
4459 case CC_ToolButton:
4460 return QWindowsStyle::hitTestComplexControl(cc, opt, pt, w);
4461 default:
4462 break;
4463 }
4464
4465 return baseStyle()->hitTestComplexControl(cc, opt, pt, w);
4466}
4467
4468QRect QStyleSheetStyle::itemPixmapRect(const QRect &rect, int alignment, const QPixmap &pixmap) const
4469{
4470 return baseStyle()->itemPixmapRect(rect, alignment, pixmap);
4471}
4472
4473QRect QStyleSheetStyle::itemTextRect(const QFontMetrics &metrics, const QRect& rect, int alignment,
4474 bool enabled, const QString& text) const
4475{
4476 return baseStyle()->itemTextRect(metrics, rect, alignment, enabled, text);
4477}
4478
4479int QStyleSheetStyle::pixelMetric(PixelMetric m, const QStyleOption *opt, const QWidget *w) const
4480{
4481 RECURSION_GUARD(return baseStyle()->pixelMetric(m, opt, w))
4482
4483 QRenderRule rule = renderRule(w, opt);
4484 QRenderRule subRule;
4485
4486 switch (m) {
4487 case PM_MenuButtonIndicator:
4488#ifndef QT_NO_TOOLBUTTON
4489 // QToolButton adds this directly to the width
4490 if (qobject_cast<const QToolButton *>(w) && (rule.hasBox() || !rule.hasNativeBorder()))
4491 return 0;
4492#endif
4493 subRule = renderRule(w, opt, PseudoElement_PushButtonMenuIndicator);
4494 if (subRule.hasContentsSize())
4495 return subRule.size().width();
4496 break;
4497
4498 case PM_ButtonShiftHorizontal:
4499 case PM_ButtonShiftVertical:
4500 case PM_ButtonMargin:
4501 case PM_ButtonDefaultIndicator:
4502 if (rule.hasBox())
4503 return 0;
4504 break;
4505
4506 case PM_DefaultFrameWidth:
4507 if (!rule.hasNativeBorder())
4508 return rule.border()->borders[LeftEdge];
4509 break;
4510
4511 case PM_ExclusiveIndicatorWidth:
4512 case PM_IndicatorWidth:
4513 case PM_ExclusiveIndicatorHeight:
4514 case PM_IndicatorHeight:
4515 subRule = renderRule(w, opt, PseudoElement_Indicator);
4516 if (subRule.hasContentsSize()) {
4517 return (m == PM_ExclusiveIndicatorWidth) || (m == PM_IndicatorWidth)
4518 ? subRule.size().width() : subRule.size().height();
4519 }
4520 break;
4521
4522 case PM_DockWidgetFrameWidth:
4523 case PM_ToolTipLabelFrameWidth: // border + margin + padding (support only one width)
4524 if (!rule.hasDrawable())
4525 break;
4526
4527 return (rule.border() ? rule.border()->borders[LeftEdge] : 0)
4528 + (rule.hasBox() ? rule.box()->margins[LeftEdge] + rule.box()->paddings[LeftEdge]: 0);
4529
4530 case PM_ToolBarFrameWidth:
4531 if (rule.hasBorder() || rule.hasBox())
4532 return (rule.border() ? rule.border()->borders[LeftEdge] : 0)
4533 + (rule.hasBox() ? rule.box()->paddings[LeftEdge]: 0);
4534 break;
4535
4536 case PM_MenuPanelWidth:
4537 case PM_MenuBarPanelWidth:
4538 if (rule.hasBorder() || rule.hasBox())
4539 return (rule.border() ? rule.border()->borders[LeftEdge] : 0)
4540 + (rule.hasBox() ? rule.box()->margins[LeftEdge]: 0);
4541 break;
4542
4543
4544 case PM_MenuHMargin:
4545 case PM_MenuBarHMargin:
4546 if (rule.hasBox())
4547 return rule.box()->paddings[LeftEdge];
4548 break;
4549
4550 case PM_MenuVMargin:
4551 case PM_MenuBarVMargin:
4552 if (rule.hasBox())
4553 return rule.box()->paddings[TopEdge];
4554 break;
4555
4556 case PM_DockWidgetTitleBarButtonMargin:
4557 case PM_ToolBarItemMargin:
4558 if (rule.hasBox())
4559 return rule.box()->margins[TopEdge];
4560 break;
4561
4562 case PM_ToolBarItemSpacing:
4563 case PM_MenuBarItemSpacing:
4564 if (rule.hasBox() && rule.box()->spacing != -1)
4565 return rule.box()->spacing;
4566 break;
4567
4568 case PM_MenuTearoffHeight:
4569 case PM_MenuScrollerHeight: {
4570 PseudoElement ps = m == PM_MenuTearoffHeight ? PseudoElement_MenuTearoff : PseudoElement_MenuScroller;
4571 subRule = renderRule(w, opt, ps);
4572 if (subRule.hasContentsSize())
4573 return subRule.size().height();
4574 break;
4575 }
4576
4577 case PM_ToolBarExtensionExtent:
4578 break;
4579
4580 case PM_SplitterWidth:
4581 case PM_ToolBarSeparatorExtent:
4582 case PM_ToolBarHandleExtent: {
4583 PseudoElement ps;
4584 if (m == PM_ToolBarHandleExtent) ps = PseudoElement_ToolBarHandle;
4585 else if (m == PM_SplitterWidth) ps = PseudoElement_SplitterHandle;
4586 else ps = PseudoElement_ToolBarSeparator;
4587 subRule = renderRule(w, opt, ps);
4588 if (subRule.hasContentsSize()) {
4589 QSize sz = subRule.size();
4590 return (opt && opt->state & QStyle::State_Horizontal) ? sz.width() : sz.height();
4591 }
4592 break;
4593 }
4594
4595 case PM_RadioButtonLabelSpacing:
4596 if (rule.hasBox() && rule.box()->spacing != -1)
4597 return rule.box()->spacing;
4598 break;
4599 case PM_CheckBoxLabelSpacing:
4600 if (qobject_cast<const QCheckBox *>(w)) {
4601 if (rule.hasBox() && rule.box()->spacing != -1)
4602 return rule.box()->spacing;
4603 }
4604 // assume group box
4605 subRule = renderRule(w, opt, PseudoElement_GroupBoxTitle);
4606 if (subRule.hasBox() && subRule.box()->spacing != -1)
4607 return subRule.box()->spacing;
4608 break;
4609
4610#ifndef QT_NO_SCROLLBAR
4611 case PM_ScrollBarExtent:
4612 if (rule.hasContentsSize()) {
4613 QSize sz = rule.size();
4614 if (const QStyleOptionSlider *sb = qstyleoption_cast<const QStyleOptionSlider *>(opt))
4615 return sb->orientation == Qt::Horizontal ? sz.height() : sz.width();
4616 return sz.width() == -1 ? sz.height() : sz.width();
4617 }
4618 break;
4619
4620 case PM_ScrollBarSliderMin:
4621 if (hasStyleRule(w, PseudoElement_ScrollBarSlider)) {
4622 subRule = renderRule(w, opt, PseudoElement_ScrollBarSlider);
4623 QSize msz = subRule.minimumSize();
4624 if (const QStyleOptionSlider *sb = qstyleoption_cast<const QStyleOptionSlider *>(opt))
4625 return sb->orientation == Qt::Horizontal ? msz.width() : msz.height();
4626 return msz.width() == -1 ? msz.height() : msz.width();
4627 }
4628 break;
4629#endif // QT_NO_SCROLLBAR
4630
4631 case PM_ProgressBarChunkWidth:
4632 subRule = renderRule(w, opt, PseudoElement_ProgressBarChunk);
4633 if (subRule.hasContentsSize()) {
4634 QSize sz = subRule.size();
4635 return (opt->state & QStyle::State_Horizontal)
4636 ? sz.width() : sz.height();
4637 }
4638 break;
4639
4640#ifndef QT_NO_TABWIDGET
4641 case PM_TabBarTabHSpace:
4642 case PM_TabBarTabVSpace:
4643 subRule = renderRule(w, opt, PseudoElement_TabBarTab);
4644 if (subRule.hasBox() || subRule.hasBorder())
4645 return 0;
4646 break;
4647
4648 case PM_TabBarScrollButtonWidth: {
4649 subRule = renderRule(w, opt, PseudoElement_TabBarScroller);
4650 if (subRule.hasContentsSize()) {
4651 QSize sz = subRule.size();
4652 return sz.width() != -1 ? sz.width() : sz.height();
4653 }
4654 }
4655 break;
4656
4657 case PM_TabBarTabShiftHorizontal:
4658 case PM_TabBarTabShiftVertical:
4659 subRule = renderRule(w, opt, PseudoElement_TabBarTab);
4660 if (subRule.hasBox())
4661 return 0;
4662 break;
4663
4664 case PM_TabBarBaseOverlap: {
4665 const QWidget *tabWidget = qobject_cast<const QTabWidget *>(w) ? w : w->parentWidget();
4666 if (hasStyleRule(tabWidget, PseudoElement_TabWidgetPane)) {
4667 return 0;
4668 }
4669 break;
4670 }
4671#endif // QT_NO_TABWIDGET
4672
4673 case PM_SliderThickness: // horizontal slider's height (sizeHint)
4674 case PM_SliderLength: // minimum length of slider
4675 if (rule.hasContentsSize()) {
4676 bool horizontal = opt->state & QStyle::State_Horizontal;
4677 if (m == PM_SliderThickness) {
4678 QSize sz = rule.size();
4679 return horizontal ? sz.height() : sz.width();
4680 } else {
4681 QSize msz = rule.minimumContentsSize();
4682 return horizontal ? msz.width() : msz.height();
4683 }
4684 }
4685 break;
4686
4687 case PM_SliderControlThickness: {
4688 QRenderRule subRule = renderRule(w, opt, PseudoElement_SliderHandle);
4689 if (!subRule.hasContentsSize())
4690 break;
4691 QSize size = subRule.size();
4692 return (opt->state & QStyle::State_Horizontal) ? size.height() : size.width();
4693 }
4694
4695 case PM_ToolBarIconSize:
4696 case PM_ListViewIconSize:
4697 case PM_IconViewIconSize:
4698 case PM_TabBarIconSize:
4699 case PM_MessageBoxIconSize:
4700 case PM_ButtonIconSize:
4701 case PM_SmallIconSize:
4702 if (rule.hasStyleHint(QLatin1String("icon-size"))) {
4703 return rule.styleHint(QLatin1String("icon-size")).toSize().width();
4704 }
4705 break;
4706
4707 case PM_DockWidgetTitleMargin: {
4708 QRenderRule subRule = renderRule(w, opt, PseudoElement_DockWidgetTitle);
4709 if (!subRule.hasBox())
4710 break;
4711 return (subRule.border() ? subRule.border()->borders[TopEdge] : 0)
4712 + (subRule.hasBox() ? subRule.box()->margins[TopEdge] + subRule.box()->paddings[TopEdge]: 0);
4713 }
4714
4715 case PM_DockWidgetSeparatorExtent: {
4716 QRenderRule subRule = renderRule(w, opt, PseudoElement_DockWidgetSeparator);
4717 if (!subRule.hasContentsSize())
4718 break;
4719 QSize sz = subRule.size();
4720 return qMax(sz.width(), sz.height());
4721 }
4722
4723 case PM_TitleBarHeight: {
4724 QRenderRule subRule = renderRule(w, opt, PseudoElement_TitleBar);
4725 if (subRule.hasContentsSize())
4726 return subRule.size().height();
4727 else if (subRule.hasBox() || subRule.hasBorder()) {
4728 QFontMetrics fm = opt ? opt->fontMetrics : w->fontMetrics();
4729 return subRule.size(QSize(0, fm.height())).height();
4730 }
4731 break;
4732 }
4733
4734 case PM_MdiSubWindowFrameWidth:
4735 if (rule.hasBox() || rule.hasBorder()) {
4736 return (rule.border() ? rule.border()->borders[LeftEdge] : 0)
4737 + (rule.hasBox() ? rule.box()->paddings[LeftEdge]+rule.box()->margins[LeftEdge]: 0);
4738 }
4739 break;
4740
4741 case PM_MdiSubWindowMinimizedWidth: {
4742 QRenderRule subRule = renderRule(w, PseudoElement_None, PseudoClass_Minimized);
4743 int width = subRule.size().width();
4744 if (width != -1)
4745 return width;
4746 break;
4747 }
4748 default:
4749 break;
4750 }
4751
4752 return baseStyle()->pixelMetric(m, opt, w);
4753}
4754
4755QSize QStyleSheetStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt,
4756 const QSize &csz, const QWidget *w) const
4757{
4758 RECURSION_GUARD(return baseStyle()->sizeFromContents(ct, opt, csz, w))
4759
4760 QRenderRule rule = renderRule(w, opt);
4761 QSize sz = rule.adjustSize(csz);
4762
4763 switch (ct) {
4764 case CT_SpinBox: // ### hopelessly broken QAbstractSpinBox (part 1)
4765 if (rule.hasBox() || !rule.hasNativeBorder())
4766 return csz;
4767 return rule.baseStyleCanDraw() ? baseStyle()->sizeFromContents(ct, opt, sz, w)
4768 : QWindowsStyle::sizeFromContents(ct, opt, sz, w);
4769 case CT_ToolButton:
4770 if (rule.hasBox() || !rule.hasNativeBorder() || !rule.baseStyleCanDraw())
4771 sz += QSize(3, 3); // ### broken QToolButton
4772 //fall thought
4773 case CT_ComboBox:
4774 case CT_PushButton:
4775 if (rule.hasBox() || !rule.hasNativeBorder()) {
4776 if(ct == CT_ComboBox) {
4777 //add some space for the drop down.
4778 QRenderRule subRule = renderRule(w, opt, PseudoElement_ComboBoxDropDown);
4779 QRect comboRect = positionRect(w, rule, subRule, PseudoElement_ComboBoxDropDown, opt->rect, opt->direction);
4780 //+2 because there is hardcoded margins in QCommonStyle::drawControl(CE_ComboBoxLabel)
4781 sz += QSize(comboRect.width() + 2, 0);
4782 }
4783 return rule.boxSize(sz);
4784 }
4785 sz = rule.baseStyleCanDraw() ? baseStyle()->sizeFromContents(ct, opt, sz, w)
4786 : QWindowsStyle::sizeFromContents(ct, opt, sz, w);
4787 return rule.boxSize(sz, Margin);
4788
4789 case CT_HeaderSection: {
4790 if (const QStyleOptionHeader *hdr = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
4791 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
4792 if (subRule.hasGeometry() || subRule.hasBox() || !subRule.hasNativeBorder()) {
4793 sz = subRule.adjustSize(csz);
4794 if (!subRule.hasGeometry()) {
4795 QSize nativeContentsSize;
4796 bool nullIcon = hdr->icon.isNull();
4797 int iconSize = nullIcon ? 0 : pixelMetric(QStyle::PM_SmallIconSize, hdr, w);
4798 QSize txt = hdr->fontMetrics.size(0, hdr->text);
4799 nativeContentsSize.setHeight(qMax(iconSize, txt.height()));
4800 nativeContentsSize.setWidth(iconSize + txt.width());
4801 sz = sz.expandedTo(nativeContentsSize);
4802 }
4803 return subRule.size(sz);
4804 }
4805 return subRule.baseStyleCanDraw() ? baseStyle()->sizeFromContents(ct, opt, sz, w)
4806 : QWindowsStyle::sizeFromContents(ct, opt, sz, w);
4807 }
4808 }
4809 break;
4810 case CT_GroupBox:
4811 case CT_LineEdit:
4812#ifndef QT_NO_SPINBOX
4813 // ### hopelessly broken QAbstractSpinBox (part 2)
4814 if (QAbstractSpinBox *spinBox = qobject_cast<QAbstractSpinBox *>(w ? w->parentWidget() : 0)) {
4815 QRenderRule rule = renderRule(spinBox, opt);
4816 if (rule.hasBox() || !rule.hasNativeBorder())
4817 return csz;
4818 return rule.baseStyleCanDraw() ? baseStyle()->sizeFromContents(ct, opt, sz, w)
4819 : QWindowsStyle::sizeFromContents(ct, opt, sz, w);
4820 }
4821#endif
4822 if (rule.hasBox() || !rule.hasNativeBorder()) {
4823 return rule.boxSize(sz);
4824 }
4825 break;
4826
4827 case CT_CheckBox:
4828 case CT_RadioButton:
4829 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
4830 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_Indicator)) {
4831 bool isRadio = (ct == CT_RadioButton);
4832 int iw = pixelMetric(isRadio ? PM_ExclusiveIndicatorWidth
4833 : PM_IndicatorWidth, btn, w);
4834 int ih = pixelMetric(isRadio ? PM_ExclusiveIndicatorHeight
4835 : PM_IndicatorHeight, btn, w);
4836
4837 int spacing = pixelMetric(isRadio ? PM_RadioButtonLabelSpacing
4838 : PM_CheckBoxLabelSpacing, btn, w);
4839 sz.setWidth(sz.width() + iw + spacing);
4840 sz.setHeight(qMax(sz.height(), ih));
4841 return rule.boxSize(sz);
4842 }
4843 }
4844 break;
4845
4846 case CT_Menu:
4847 case CT_MenuBar: // already has everything!
4848 case CT_ScrollBar:
4849 if (rule.hasBox() || rule.hasBorder())
4850 return sz;
4851 break;
4852
4853 case CT_MenuItem:
4854 if (const QStyleOptionMenuItem *mi = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
4855 PseudoElement pe = (mi->menuItemType == QStyleOptionMenuItem::Separator)
4856 ? PseudoElement_MenuSeparator : PseudoElement_Item;
4857 QRenderRule subRule = renderRule(w, opt, pe);
4858 if ((pe == PseudoElement_MenuSeparator) && subRule.hasContentsSize()) {
4859 return QSize(sz.width(), subRule.size().height());
4860 } else if ((pe == PseudoElement_Item) && (subRule.hasBox() || subRule.hasBorder())) {
4861 int width = csz.width();
4862 if (mi->text.contains(QLatin1Char('\t')))
4863 width += 12; //as in QCommonStyle
4864 return subRule.boxSize(subRule.adjustSize(QSize(width, csz.height())));
4865 }
4866 }
4867 break;
4868
4869 case CT_Splitter:
4870 case CT_MenuBarItem: {
4871 PseudoElement pe = (ct == CT_Splitter) ? PseudoElement_SplitterHandle : PseudoElement_Item;
4872 QRenderRule subRule = renderRule(w, opt, pe);
4873 if (subRule.hasBox() || subRule.hasBorder())
4874 return subRule.boxSize(sz);
4875 break;
4876 }
4877
4878 case CT_ProgressBar:
4879 case CT_SizeGrip:
4880 return (rule.hasContentsSize())
4881 ? rule.size(sz)
4882 : rule.boxSize(baseStyle()->sizeFromContents(ct, opt, sz, w));
4883 break;
4884
4885 case CT_Slider:
4886 if (rule.hasBorder() || rule.hasBox() || rule.hasGeometry())
4887 return rule.boxSize(sz);
4888 break;
4889
4890#ifndef QT_NO_TABBAR
4891 case CT_TabBarTab: {
4892 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab);
4893 if (subRule.hasBox() || !subRule.hasNativeBorder()) {
4894 int spaceForIcon = 0;
4895 bool vertical = false;
4896 if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
4897 if (!tab->icon.isNull())
4898 spaceForIcon = 6 /* icon offset */ + 4 /* spacing */ + 2 /* magic */; // ###: hardcoded to match with common style
4899 vertical = verticalTabs(tab->shape);
4900 }
4901 sz = csz + QSize(vertical ? 0 : spaceForIcon, vertical ? spaceForIcon : 0);
4902 return subRule.boxSize(subRule.adjustSize(sz));
4903 }
4904#ifdef Q_WS_MAC
4905 if (baseStyle()->inherits("QMacStyle")) {
4906 //adjust the size after the call to the style because the mac style ignore the size arguments anyway.
4907 //this might cause the (max-){width,height} property to include the native style border while they should not.
4908 return subRule.adjustSize(baseStyle()->sizeFromContents(ct, opt, csz, w));
4909 }
4910#endif
4911 sz = subRule.adjustSize(csz);
4912 break;
4913 }
4914#endif // QT_NO_TABBAR
4915
4916 case CT_MdiControls:
4917 if (const QStyleOptionComplex *ccOpt = qstyleoption_cast<const QStyleOptionComplex *>(opt)) {
4918 if (!hasStyleRule(w, PseudoElement_MdiCloseButton)
4919 && !hasStyleRule(w, PseudoElement_MdiNormalButton)
4920 && !hasStyleRule(w, PseudoElement_MdiMinButton))
4921 break;
4922
4923 QList<QVariant> layout = rule.styleHint(QLatin1String("button-layout")).toList();
4924 if (layout.isEmpty())
4925 layout = subControlLayout(QLatin1String("mNX"));
4926
4927 int width = 0, height = 0;
4928 for (int i = 0; i < layout.count(); i++) {
4929 int layoutButton = layout[i].toInt();
4930 if (layoutButton < PseudoElement_MdiCloseButton
4931 || layoutButton > PseudoElement_MdiNormalButton)
4932 continue;
4933 QStyle::SubControl sc = knownPseudoElements[layoutButton].subControl;
4934 if (!(ccOpt->subControls & sc))
4935 continue;
4936 QRenderRule subRule = renderRule(w, opt, layoutButton);
4937 QSize sz = subRule.size();
4938 width += sz.width();
4939 height = qMax(height, sz.height());
4940 }
4941
4942 return QSize(width, height);
4943 }
4944 break;
4945
4946#ifndef QT_NO_ITEMVIEWS
4947 case CT_ItemViewItem: {
4948 QRenderRule subRule = renderRule(w, opt, PseudoElement_ViewItem);
4949 sz = baseStyle()->sizeFromContents(ct, opt, csz, w);
4950 sz = subRule.adjustSize(sz);
4951 if (subRule.hasBox() || subRule.hasBorder())
4952 sz = subRule.boxSize(sz);
4953 return sz;
4954 }
4955#endif // QT_NO_ITEMVIEWS
4956
4957 default:
4958 break;
4959 }
4960
4961 return baseStyle()->sizeFromContents(ct, opt, sz, w);
4962}
4963
4964/*!
4965 \internal
4966*/
4967static QLatin1String propertyNameForStandardPixmap(QStyle::StandardPixmap sp)
4968{
4969 switch (sp) {
4970 case QStyle::SP_TitleBarMenuButton: return QLatin1String("titlebar-menu-icon");
4971 case QStyle::SP_TitleBarMinButton: return QLatin1String("titlebar-minimize-icon");
4972 case QStyle::SP_TitleBarMaxButton: return QLatin1String("titlebar-maximize-icon");
4973 case QStyle::SP_TitleBarCloseButton: return QLatin1String("titlebar-close-icon");
4974 case QStyle::SP_TitleBarNormalButton: return QLatin1String("titlebar-normal-icon");
4975 case QStyle::SP_TitleBarShadeButton: return QLatin1String("titlebar-shade-icon");
4976 case QStyle::SP_TitleBarUnshadeButton: return QLatin1String("titlebar-unshade-icon");
4977 case QStyle::SP_TitleBarContextHelpButton: return QLatin1String("titlebar-contexthelp-icon");
4978 case QStyle::SP_DockWidgetCloseButton: return QLatin1String("dockwidget-close-icon");
4979 case QStyle::SP_MessageBoxInformation: return QLatin1String("messagebox-information-icon");
4980 case QStyle::SP_MessageBoxWarning: return QLatin1String("messagebox-warning-icon");
4981 case QStyle::SP_MessageBoxCritical: return QLatin1String("messagebox-critical-icon");
4982 case QStyle::SP_MessageBoxQuestion: return QLatin1String("messagebox-question-icon");
4983 case QStyle::SP_DesktopIcon: return QLatin1String("desktop-icon");
4984 case QStyle::SP_TrashIcon: return QLatin1String("trash-icon");
4985 case QStyle::SP_ComputerIcon: return QLatin1String("computer-icon");
4986 case QStyle::SP_DriveFDIcon: return QLatin1String("floppy-icon");
4987 case QStyle::SP_DriveHDIcon: return QLatin1String("harddisk-icon");
4988 case QStyle::SP_DriveCDIcon: return QLatin1String("cd-icon");
4989 case QStyle::SP_DriveDVDIcon: return QLatin1String("dvd-icon");
4990 case QStyle::SP_DriveNetIcon: return QLatin1String("network-icon");
4991 case QStyle::SP_DirOpenIcon: return QLatin1String("directory-open-icon");
4992 case QStyle::SP_DirClosedIcon: return QLatin1String("directory-closed-icon");
4993 case QStyle::SP_DirLinkIcon: return QLatin1String("directory-link-icon");
4994 case QStyle::SP_FileIcon: return QLatin1String("file-icon");
4995 case QStyle::SP_FileLinkIcon: return QLatin1String("file-link-icon");
4996 case QStyle::SP_FileDialogStart: return QLatin1String("filedialog-start-icon");
4997 case QStyle::SP_FileDialogEnd: return QLatin1String("filedialog-end-icon");
4998 case QStyle::SP_FileDialogToParent: return QLatin1String("filedialog-parent-directory-icon");
4999 case QStyle::SP_FileDialogNewFolder: return QLatin1String("filedialog-new-directory-icon");
5000 case QStyle::SP_FileDialogDetailedView: return QLatin1String("filedialog-detailedview-icon");
5001 case QStyle::SP_FileDialogInfoView: return QLatin1String("filedialog-infoview-icon");
5002 case QStyle::SP_FileDialogContentsView: return QLatin1String("filedialog-contentsview-icon");
5003 case QStyle::SP_FileDialogListView: return QLatin1String("filedialog-listview-icon");
5004 case QStyle::SP_FileDialogBack: return QLatin1String("filedialog-backward-icon");
5005 case QStyle::SP_DirIcon: return QLatin1String("directory-icon");
5006 case QStyle::SP_DialogOkButton: return QLatin1String("dialog-ok-icon");
5007 case QStyle::SP_DialogCancelButton: return QLatin1String("dialog-cancel-icon");
5008 case QStyle::SP_DialogHelpButton: return QLatin1String("dialog-help-icon");
5009 case QStyle::SP_DialogOpenButton: return QLatin1String("dialog-open-icon");
5010 case QStyle::SP_DialogSaveButton: return QLatin1String("dialog-save-icon");
5011 case QStyle::SP_DialogCloseButton: return QLatin1String("dialog-close-icon");
5012 case QStyle::SP_DialogApplyButton: return QLatin1String("dialog-apply-icon");
5013 case QStyle::SP_DialogResetButton: return QLatin1String("dialog-reset-icon");
5014 case QStyle::SP_DialogDiscardButton: return QLatin1String("discard-icon");
5015 case QStyle::SP_DialogYesButton: return QLatin1String("dialog-yes-icon");
5016 case QStyle::SP_DialogNoButton: return QLatin1String("dialog-no-icon");
5017 case QStyle::SP_ArrowUp: return QLatin1String("uparrow-icon");
5018 case QStyle::SP_ArrowDown: return QLatin1String("downarrow-icon");
5019 case QStyle::SP_ArrowLeft: return QLatin1String("leftarrow-icon");
5020 case QStyle::SP_ArrowRight: return QLatin1String("rightarrow-icon");
5021 case QStyle::SP_ArrowBack: return QLatin1String("backward-icon");
5022 case QStyle::SP_ArrowForward: return QLatin1String("forward-icon");
5023 case QStyle::SP_DirHomeIcon: return QLatin1String("home-icon");
5024 default: return QLatin1String("");
5025 }
5026}
5027
5028QIcon QStyleSheetStyle::standardIconImplementation(StandardPixmap standardIcon, const QStyleOption *opt,
5029 const QWidget *w) const
5030{
5031 RECURSION_GUARD(return baseStyle()->standardIcon(standardIcon, opt, w))
5032 QString s = propertyNameForStandardPixmap(standardIcon);
5033 if (!s.isEmpty()) {
5034 QRenderRule rule = renderRule(w, opt);
5035 if (rule.hasStyleHint(s))
5036 return qVariantValue<QIcon>(rule.styleHint(s));
5037 }
5038 return baseStyle()->standardIcon(standardIcon, opt, w);
5039}
5040
5041QPalette QStyleSheetStyle::standardPalette() const
5042{
5043 return baseStyle()->standardPalette();
5044}
5045
5046QPixmap QStyleSheetStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt,
5047 const QWidget *w) const
5048{
5049 RECURSION_GUARD(return baseStyle()->standardPixmap(standardPixmap, opt, w))
5050 QString s = propertyNameForStandardPixmap(standardPixmap);
5051 if (!s.isEmpty()) {
5052 QRenderRule rule = renderRule(w, opt);
5053 if (rule.hasStyleHint(s)) {
5054 QIcon icon = qVariantValue<QIcon>(rule.styleHint(s));
5055 return icon.pixmap(16, 16); // ###: unhard-code this if someone complains
5056 }
5057 }
5058 return baseStyle()->standardPixmap(standardPixmap, opt, w);
5059}
5060
5061int QStyleSheetStyle::layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2,
5062 Qt::Orientation orientation, const QStyleOption *option,
5063 const QWidget *widget) const
5064{
5065 return baseStyle()->layoutSpacing(control1, control2, orientation, option, widget);
5066}
5067
5068int QStyleSheetStyle::layoutSpacingImplementation(QSizePolicy::ControlType control1 ,
5069 QSizePolicy::ControlType control2,
5070 Qt::Orientation orientation,
5071 const QStyleOption * option ,
5072 const QWidget * widget) const
5073{
5074 return baseStyle()->layoutSpacing(control1, control2, orientation, option, widget);
5075}
5076
5077int QStyleSheetStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w,
5078 QStyleHintReturn *shret) const
5079{
5080 RECURSION_GUARD(return baseStyle()->styleHint(sh, opt, w, shret))
5081 // Prevent endless loop if somebody use isActiveWindow property as selector.
5082 // QWidget::isActiveWindow uses this styleHint to determine if the window is active or not
5083 if (sh == SH_Widget_ShareActivation)
5084 return baseStyle()->styleHint(sh, opt, w, shret);
5085
5086 QRenderRule rule = renderRule(w, opt);
5087 QString s;
5088 switch (sh) {
5089 case SH_LineEdit_PasswordCharacter: s = QLatin1String("lineedit-password-character"); break;
5090 case SH_DitherDisabledText: s = QLatin1String("dither-disabled-text"); break;
5091 case SH_EtchDisabledText: s = QLatin1String("etch-disabled-text"); break;
5092 case SH_ItemView_ActivateItemOnSingleClick: s = QLatin1String("activate-on-singleclick"); break;
5093 case SH_ItemView_ShowDecorationSelected: s = QLatin1String("show-decoration-selected"); break;
5094 case SH_Table_GridLineColor: s = QLatin1String("gridline-color"); break;
5095 case SH_DialogButtonLayout: s = QLatin1String("button-layout"); break;
5096 case SH_ToolTipLabel_Opacity: s = QLatin1String("opacity"); break;
5097 case SH_ComboBox_Popup: s = QLatin1String("combobox-popup"); break;
5098 case SH_ComboBox_ListMouseTracking: s = QLatin1String("combobox-list-mousetracking"); break;
5099 case SH_MenuBar_AltKeyNavigation: s = QLatin1String("menubar-altkey-navigation"); break;
5100 case SH_Menu_Scrollable: s = QLatin1String("menu-scrollable"); break;
5101 case SH_DrawMenuBarSeparator: s = QLatin1String("menubar-separator"); break;
5102 case SH_MenuBar_MouseTracking: s = QLatin1String("mouse-tracking"); break;
5103 case SH_SpinBox_ClickAutoRepeatRate: s = QLatin1String("spinbox-click-autorepeat-rate"); break;
5104 case SH_SpinControls_DisableOnBounds: s = QLatin1String("spincontrol-disable-on-bounds"); break;
5105 case SH_MessageBox_TextInteractionFlags: s = QLatin1String("messagebox-text-interaction-flags"); break;
5106 case SH_ToolButton_PopupDelay: s = QLatin1String("toolbutton-popup-delay"); break;
5107 case SH_ToolBox_SelectedPageTitleBold:
5108 if (renderRule(w, opt, PseudoElement_ToolBoxTab).hasFont)
5109 return 0;
5110 break;
5111 case SH_GroupBox_TextLabelColor:
5112 if (rule.hasPalette() && rule.palette()->foreground.style() != Qt::NoBrush)
5113 return rule.palette()->foreground.color().rgba();
5114 break;
5115 case SH_ScrollView_FrameOnlyAroundContents: s = QLatin1String("scrollview-frame-around-contents"); break;
5116 case SH_ScrollBar_ContextMenu: s = QLatin1String("scrollbar-contextmenu"); break;
5117 case SH_ScrollBar_LeftClickAbsolutePosition: s = QLatin1String("scrollbar-leftclick-absolute-position"); break;
5118 case SH_ScrollBar_MiddleClickAbsolutePosition: s = QLatin1String("scrollbar-middleclick-absolute-position"); break;
5119 case SH_ScrollBar_RollBetweenButtons: s = QLatin1String("scrollbar-roll-between-buttons"); break;
5120 case SH_ScrollBar_ScrollWhenPointerLeavesControl: s = QLatin1String("scrollbar-scroll-when-pointer-leaves-control"); break;
5121 case SH_TabBar_Alignment:
5122#ifndef QT_NO_TABWIDGET
5123 if (qobject_cast<const QTabWidget *>(w)) {
5124 rule = renderRule(w, opt, PseudoElement_TabWidgetTabBar);
5125 if (rule.hasPosition())
5126 return rule.position()->position;
5127 }
5128#endif // QT_NO_TABWIDGET
5129 s = QLatin1String("alignment");
5130 break;
5131#ifndef QT_NO_TABBAR
5132 case SH_TabBar_CloseButtonPosition:
5133 rule = renderRule(w, opt, PseudoElement_TabBarTabCloseButton);
5134 if (rule.hasPosition()) {
5135 Qt::Alignment align = rule.position()->position;
5136 if (align & Qt::AlignLeft || align & Qt::AlignTop)
5137 return QTabBar::LeftSide;
5138 if (align & Qt::AlignRight || align & Qt::AlignBottom)
5139 return QTabBar::RightSide;
5140 }
5141 break;
5142#endif
5143 case SH_TabBar_ElideMode: s = QLatin1String("tabbar-elide-mode"); break;
5144 case SH_TabBar_PreferNoArrows: s = QLatin1String("tabbar-prefer-no-arrows"); break;
5145 case SH_ComboBox_PopupFrameStyle:
5146#ifndef QT_NO_COMBOBOX
5147 if (qobject_cast<const QComboBox *>(w)) {
5148 QAbstractItemView *view = qFindChild<QAbstractItemView *>(w);
5149 if (view) {
5150 view->ensurePolished();
5151 QRenderRule subRule = renderRule(view, PseudoElement_None);
5152 if (subRule.hasBox() || !subRule.hasNativeBorder())
5153 return QFrame::NoFrame;
5154 }
5155 }
5156#endif // QT_NO_COMBOBOX
5157 break;
5158 case SH_DialogButtonBox_ButtonsHaveIcons: s = QLatin1String("dialogbuttonbox-buttons-have-icons"); break;
5159 case SH_Workspace_FillSpaceOnMaximize: s = QLatin1String("mdi-fill-space-on-maximize"); break;
5160 case SH_TitleBar_NoBorder:
5161 if (rule.hasBorder())
5162 return !rule.border()->borders[LeftEdge];
5163 break;
5164 case SH_TitleBar_AutoRaise: { // plain absurd
5165 QRenderRule subRule = renderRule(w, opt, PseudoElement_TitleBar);
5166 if (subRule.hasDrawable())
5167 return 1;
5168 break;
5169 }
5170 case SH_ItemView_ArrowKeysNavigateIntoChildren: s = QLatin1String("arrow-keys-navigate-into-children"); break;
5171 case SH_ItemView_PaintAlternatingRowColorsForEmptyArea: s = QLatin1String("paint-alternating-row-colors-for-empty-area"); break;
5172 default: break;
5173 }
5174 if (!s.isEmpty() && rule.hasStyleHint(s)) {
5175 return rule.styleHint(s).toInt();
5176 }
5177
5178 return baseStyle()->styleHint(sh, opt, w, shret);
5179}
5180
5181QRect QStyleSheetStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc,
5182 const QWidget *w) const
5183{
5184 RECURSION_GUARD(return baseStyle()->subControlRect(cc, opt, sc, w))
5185
5186 QRenderRule rule = renderRule(w, opt);
5187 switch (cc) {
5188 case CC_ComboBox:
5189 if (const QStyleOptionComboBox *cb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
5190 if (rule.hasBox() || !rule.hasNativeBorder()) {
5191 switch (sc) {
5192 case SC_ComboBoxFrame: return rule.borderRect(opt->rect);
5193 case SC_ComboBoxEditField:
5194 {
5195 QRenderRule subRule = renderRule(w, opt, PseudoElement_ComboBoxDropDown);
5196 QRect r = rule.contentsRect(opt->rect);
5197 QRect r2 = positionRect(w, rule, subRule, PseudoElement_ComboBoxDropDown,
5198 opt->rect, opt->direction);
5199 if (subRule.hasPosition() && subRule.position()->position & Qt::AlignLeft) {
5200 return visualRect(opt->direction, r, r.adjusted(r2.width(),0,0,0));
5201 } else {
5202 return visualRect(opt->direction, r, r.adjusted(0,0,-r2.width(),0));
5203 }
5204 }
5205 case SC_ComboBoxArrow: {
5206 QRenderRule subRule = renderRule(w, opt, PseudoElement_ComboBoxDropDown);
5207 return positionRect(w, rule, subRule, PseudoElement_ComboBoxDropDown, opt->rect, opt->direction);
5208 }
5209 case SC_ComboBoxListBoxPopup:
5210 default:
5211 return baseStyle()->subControlRect(cc, opt, sc, w);
5212 }
5213 }
5214
5215 QStyleOptionComboBox comboBox(*cb);
5216 comboBox.rect = rule.borderRect(opt->rect);
5217 return rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, &comboBox, sc, w)
5218 : QWindowsStyle::subControlRect(cc, &comboBox, sc, w);
5219 }
5220 break;
5221
5222#ifndef QT_NO_SPINBOX
5223 case CC_SpinBox:
5224 if (const QStyleOptionSpinBox *spin = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
5225 QRenderRule upRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton);
5226 QRenderRule downRule = renderRule(w, opt, PseudoElement_SpinBoxDownButton);
5227 bool ruleMatch = rule.hasBox() || !rule.hasNativeBorder();
5228 bool upRuleMatch = upRule.hasGeometry() || upRule.hasPosition();
5229 bool downRuleMatch = downRule.hasGeometry() || upRule.hasPosition();
5230 if (ruleMatch || upRuleMatch || downRuleMatch) {
5231 switch (sc) {
5232 case SC_SpinBoxFrame:
5233 return rule.borderRect(opt->rect);
5234 case SC_SpinBoxEditField:
5235 {
5236 QRect r = rule.contentsRect(opt->rect);
5237 // Use the widest button on each side to determine edit field size.
5238 Qt::Alignment upAlign, downAlign;
5239
5240 upAlign = upRule.hasPosition() ? upRule.position()->position
5241 : Qt::Alignment(Qt::AlignRight);
5242 upAlign = resolveAlignment(opt->direction, upAlign);
5243
5244 downAlign = downRule.hasPosition() ? downRule.position()->position
5245 : Qt::Alignment(Qt::AlignRight);
5246 downAlign = resolveAlignment(opt->direction, downAlign);
5247
5248 int upSize = subControlRect(CC_SpinBox, opt, SC_SpinBoxUp, w).width();
5249 int downSize = subControlRect(CC_SpinBox, opt, SC_SpinBoxDown, w).width();
5250 int widestL = qMax((upAlign & Qt::AlignLeft) ? upSize : 0,
5251 (downAlign & Qt::AlignLeft) ? downSize : 0);
5252 int widestR = qMax((upAlign & Qt::AlignRight) ? upSize : 0,
5253 (downAlign & Qt::AlignRight) ? downSize : 0);
5254 r.setRight(r.right() - widestR);
5255 r.setLeft(r.left() + widestL);
5256 return r;
5257 }
5258 case SC_SpinBoxDown:
5259 if (downRuleMatch)
5260 return positionRect(w, rule, downRule, PseudoElement_SpinBoxDownButton,
5261 opt->rect, opt->direction);
5262 break;
5263 case SC_SpinBoxUp:
5264 if (upRuleMatch)
5265 return positionRect(w, rule, upRule, PseudoElement_SpinBoxUpButton,
5266 opt->rect, opt->direction);
5267 break;
5268 default:
5269 break;
5270 }
5271
5272 return baseStyle()->subControlRect(cc, opt, sc, w);
5273 }
5274
5275 QStyleOptionSpinBox spinBox(*spin);
5276 spinBox.rect = rule.borderRect(opt->rect);
5277 return rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, &spinBox, sc, w)
5278 : QWindowsStyle::subControlRect(cc, &spinBox, sc, w);
5279 }
5280 break;
5281#endif // QT_NO_SPINBOX
5282
5283 case CC_GroupBox:
5284 if (const QStyleOptionGroupBox *gb = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
5285 switch (sc) {
5286 case SC_GroupBoxFrame:
5287 case SC_GroupBoxContents: {
5288 if (rule.hasBox() || !rule.hasNativeBorder()) {
5289 return sc == SC_GroupBoxFrame ? rule.borderRect(opt->rect)
5290 : rule.contentsRect(opt->rect);
5291 }
5292 QStyleOptionGroupBox groupBox(*gb);
5293 groupBox.rect = rule.borderRect(opt->rect);
5294 return baseStyle()->subControlRect(cc, &groupBox, sc, w);
5295 }
5296 default:
5297 case SC_GroupBoxLabel:
5298 case SC_GroupBoxCheckBox: {
5299 QRenderRule indRule = renderRule(w, opt, PseudoElement_GroupBoxIndicator);
5300 QRenderRule labelRule = renderRule(w, opt, PseudoElement_GroupBoxTitle);
5301 if (!labelRule.hasPosition() && !labelRule.hasGeometry() && !labelRule.hasBox()
5302 && !labelRule.hasBorder() && !indRule.hasContentsSize()) {
5303 QStyleOptionGroupBox groupBox(*gb);
5304 groupBox.rect = rule.borderRect(opt->rect);
5305 return baseStyle()->subControlRect(cc, &groupBox, sc, w);
5306 }
5307 int tw = opt->fontMetrics.width(gb->text);
5308 int th = opt->fontMetrics.height();
5309 int spacing = pixelMetric(QStyle::PM_CheckBoxLabelSpacing, opt, w);
5310 int iw = pixelMetric(QStyle::PM_IndicatorWidth, opt, w);
5311 int ih = pixelMetric(QStyle::PM_IndicatorHeight, opt, w);
5312
5313 if (gb->subControls & QStyle::SC_GroupBoxCheckBox) {
5314 tw = tw + iw + spacing;
5315 th = qMax(th, ih);
5316 }
5317 if (!labelRule.hasGeometry()) {
5318 labelRule.geo = new QStyleSheetGeometryData(tw, th, tw, th, -1, -1);
5319 } else {
5320 labelRule.geo->width = tw;
5321 labelRule.geo->height = th;
5322 }
5323 if (!labelRule.hasPosition()) {
5324 labelRule.p = new QStyleSheetPositionData(0, 0, 0, 0, defaultOrigin(PseudoElement_GroupBoxTitle),
5325 gb->textAlignment, PositionMode_Static);
5326 }
5327 QRect r = positionRect(w, rule, labelRule, PseudoElement_GroupBoxTitle,
5328 opt->rect, opt->direction);
5329 if (gb->subControls & SC_GroupBoxCheckBox) {
5330 r = labelRule.contentsRect(r);
5331 if (sc == SC_GroupBoxLabel) {
5332 r.setLeft(r.left() + iw + spacing);
5333 r.setTop(r.center().y() - th/2);
5334 } else {
5335 r = QRect(r.left(), r.center().y() - ih/2, iw, ih);
5336 }
5337 return r;
5338 } else {
5339 return labelRule.contentsRect(r);
5340 }
5341 }
5342 } // switch
5343 }
5344 break;
5345
5346 case CC_ToolButton:
5347 if (const QStyleOptionToolButton *tb = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
5348 if (rule.hasBox() || !rule.hasNativeBorder()) {
5349 switch (sc) {
5350 case SC_ToolButton: return rule.borderRect(opt->rect);
5351 case SC_ToolButtonMenu: {
5352 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolButtonMenu);
5353 return positionRect(w, rule, subRule, PseudoElement_ToolButtonMenu, opt->rect, opt->direction);
5354 }
5355 default:
5356 break;
5357 }
5358 }
5359
5360 QStyleOptionToolButton tool(*tb);
5361 tool.rect = rule.borderRect(opt->rect);
5362 return rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, &tool, sc, w)
5363 : QWindowsStyle::subControlRect(cc, &tool, sc, w);
5364 }
5365 break;
5366
5367#ifndef QT_NO_SCROLLBAR
5368 case CC_ScrollBar:
5369 if (const QStyleOptionSlider *sb = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
5370 QStyleOptionSlider styleOptionSlider(*sb);
5371 styleOptionSlider.rect = rule.borderRect(opt->rect);
5372 if (rule.hasDrawable() || rule.hasBox()) {
5373 QRect grooveRect;
5374 if (!rule.hasBox()) {
5375 grooveRect = rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, sb, SC_ScrollBarGroove, w)
5376 : QWindowsStyle::subControlRect(cc, sb, SC_ScrollBarGroove, w);
5377 } else {
5378 grooveRect = rule.contentsRect(opt->rect);
5379 }
5380
5381 PseudoElement pe = PseudoElement_None;
5382
5383 switch (sc) {
5384 case SC_ScrollBarGroove:
5385 return grooveRect;
5386 case SC_ScrollBarAddPage:
5387 case SC_ScrollBarSubPage:
5388 case SC_ScrollBarSlider: {
5389 QRect contentRect = grooveRect;
5390 if (hasStyleRule(w, PseudoElement_ScrollBarSlider)) {
5391 QRenderRule sliderRule = renderRule(w, opt, PseudoElement_ScrollBarSlider);
5392 Origin origin = sliderRule.hasPosition() ? sliderRule.position()->origin : defaultOrigin(PseudoElement_ScrollBarSlider);
5393 contentRect = rule.originRect(opt->rect, origin);
5394 }
5395 int maxlen = (styleOptionSlider.orientation == Qt::Horizontal) ? contentRect.width() : contentRect.height();
5396 int sliderlen;
5397 if (sb->maximum != sb->minimum) {
5398 uint range = sb->maximum - sb->minimum;
5399 sliderlen = (qint64(sb->pageStep) * maxlen) / (range + sb->pageStep);
5400
5401 int slidermin = pixelMetric(PM_ScrollBarSliderMin, sb, w);
5402 if (sliderlen < slidermin || range > INT_MAX / 2)
5403 sliderlen = slidermin;
5404 if (sliderlen > maxlen)
5405 sliderlen = maxlen;
5406 } else {
5407 sliderlen = maxlen;
5408 }
5409
5410 int sliderstart = (styleOptionSlider.orientation == Qt::Horizontal ? contentRect.left() : contentRect.top())
5411 + sliderPositionFromValue(sb->minimum, sb->maximum, sb->sliderPosition,
5412 maxlen - sliderlen, sb->upsideDown);
5413
5414 QRect sr = (sb->orientation == Qt::Horizontal)
5415 ? QRect(sliderstart, contentRect.top(), sliderlen, contentRect.height())
5416 : QRect(contentRect.left(), sliderstart, contentRect.width(), sliderlen);
5417 if (sc == SC_ScrollBarSlider) {
5418 return sr;
5419 } else if (sc == SC_ScrollBarSubPage) {
5420 return QRect(contentRect.topLeft(), sb->orientation == Qt::Horizontal ? sr.bottomLeft() : sr.topRight());
5421 } else { // SC_ScrollBarAddPage
5422 return QRect(sb->orientation == Qt::Horizontal ? sr.topRight() : sr.bottomLeft(), contentRect.bottomRight());
5423 }
5424 break;
5425 }
5426 case SC_ScrollBarAddLine: pe = PseudoElement_ScrollBarAddLine; break;
5427 case SC_ScrollBarSubLine: pe = PseudoElement_ScrollBarSubLine; break;
5428 case SC_ScrollBarFirst: pe = PseudoElement_ScrollBarFirst; break;
5429 case SC_ScrollBarLast: pe = PseudoElement_ScrollBarLast; break;
5430 default: break;
5431 }
5432 if (hasStyleRule(w,pe)) {
5433 QRenderRule subRule = renderRule(w, opt, pe);
5434 if (subRule.hasPosition() || subRule.hasGeometry() || subRule.hasBox()) {
5435 const QStyleSheetPositionData *pos = subRule.position();
5436 QRect originRect = grooveRect;
5437 if (rule.hasBox()) {
5438 Origin origin = (pos && pos->origin != Origin_Unknown) ? pos->origin : defaultOrigin(pe);
5439 originRect = rule.originRect(opt->rect, origin);
5440 }
5441 return positionRect(w, subRule, pe, originRect, styleOptionSlider.direction);
5442 }
5443 }
5444 }
5445 return rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, &styleOptionSlider, sc, w)
5446 : QWindowsStyle::subControlRect(cc, &styleOptionSlider, sc, w);
5447 }
5448 break;
5449#endif // QT_NO_SCROLLBAR
5450
5451#ifndef QT_NO_SLIDER
5452 case CC_Slider:
5453 if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
5454 QRenderRule subRule = renderRule(w, opt, PseudoElement_SliderGroove);
5455 if (!subRule.hasDrawable())
5456 break;
5457 subRule.img = 0;
5458 QRect gr = positionRect(w, rule, subRule, PseudoElement_SliderGroove, opt->rect, opt->direction);
5459 switch (sc) {
5460 case SC_SliderGroove:
5461 return gr;
5462 case SC_SliderHandle: {
5463 bool horizontal = slider->orientation & Qt::Horizontal;
5464 QRect cr = subRule.contentsRect(gr);
5465 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_SliderHandle);
5466 int len = horizontal ? subRule2.size().width() : subRule2.size().height();
5467 subRule2.img = 0;
5468 subRule2.geo = 0;
5469 cr = positionRect(w, subRule2, PseudoElement_SliderHandle, cr, opt->direction);
5470 int thickness = horizontal ? cr.height() : cr.width();
5471 int sliderPos = sliderPositionFromValue(slider->minimum, slider->maximum, slider->sliderPosition,
5472 (horizontal ? cr.width() : cr.height()) - len, slider->upsideDown);
5473 cr = horizontal ? QRect(cr.x() + sliderPos, cr.y(), len, thickness)
5474 : QRect(cr.x(), cr.y() + sliderPos, thickness, len);
5475 return subRule2.borderRect(cr);
5476 break; }
5477 case SC_SliderTickmarks:
5478 // TODO...
5479 default:
5480 break;
5481 }
5482 }
5483 break;
5484#endif // QT_NO_SLIDER
5485
5486 case CC_MdiControls:
5487 if (hasStyleRule(w, PseudoElement_MdiCloseButton)
5488 || hasStyleRule(w, PseudoElement_MdiNormalButton)
5489 || hasStyleRule(w, PseudoElement_MdiMinButton)) {
5490 QList<QVariant> layout = rule.styleHint(QLatin1String("button-layout")).toList();
5491 if (layout.isEmpty())
5492 layout = subControlLayout(QLatin1String("mNX"));
5493
5494 int x = 0, width = 0;
5495 QRenderRule subRule;
5496 for (int i = 0; i < layout.count(); i++) {
5497 int layoutButton = layout[i].toInt();
5498 if (layoutButton < PseudoElement_MdiCloseButton
5499 || layoutButton > PseudoElement_MdiNormalButton)
5500 continue;
5501 QStyle::SubControl control = knownPseudoElements[layoutButton].subControl;
5502 if (!(opt->subControls & control))
5503 continue;
5504 subRule = renderRule(w, opt, layoutButton);
5505 width = subRule.size().width();
5506 if (sc == control)
5507 break;
5508 x += width;
5509 }
5510
5511 return subRule.borderRect(QRect(x, opt->rect.top(), width, opt->rect.height()));
5512 }
5513 break;
5514
5515 case CC_TitleBar:
5516 if (const QStyleOptionTitleBar *tb = qstyleoption_cast<const QStyleOptionTitleBar *>(opt)) {
5517 QRenderRule subRule = renderRule(w, opt, PseudoElement_TitleBar);
5518 if (!subRule.hasDrawable() && !subRule.hasBox() && !subRule.hasBorder())
5519 break;
5520 QHash<QStyle::SubControl, QRect> layoutRects = titleBarLayout(w, tb);
5521 return layoutRects.value(sc);
5522 }
5523 break;
5524
5525 default:
5526 break;
5527 }
5528
5529 return baseStyle()->subControlRect(cc, opt, sc, w);
5530}
5531
5532QRect QStyleSheetStyle::subElementRect(SubElement se, const QStyleOption *opt, const QWidget *w) const
5533{
5534 RECURSION_GUARD(return baseStyle()->subElementRect(se, opt, w))
5535
5536 QRenderRule rule = renderRule(w, opt);
5537#ifndef QT_NO_TABBAR
5538 int pe = PseudoElement_None;
5539#endif
5540
5541 switch (se) {
5542 case SE_PushButtonContents:
5543 case SE_PushButtonFocusRect:
5544 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
5545 QStyleOptionButton btnOpt(*btn);
5546 if (rule.hasBox() || !rule.hasNativeBorder())
5547 return visualRect(opt->direction, opt->rect, rule.contentsRect(opt->rect));
5548 return rule.baseStyleCanDraw() ? baseStyle()->subElementRect(se, &btnOpt, w)
5549 : QWindowsStyle::subElementRect(se, &btnOpt, w);
5550 }
5551 break;
5552
5553 case SE_LineEditContents:
5554 case SE_FrameContents:
5555 case SE_ShapedFrameContents:
5556 if (rule.hasBox() || !rule.hasNativeBorder()) {
5557 return visualRect(opt->direction, opt->rect, rule.contentsRect(opt->rect));
5558 }
5559 break;
5560
5561 case SE_CheckBoxIndicator:
5562 case SE_RadioButtonIndicator:
5563 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_Indicator)) {
5564 PseudoElement pe = se == SE_CheckBoxIndicator ? PseudoElement_Indicator : PseudoElement_ExclusiveIndicator;
5565 QRenderRule subRule = renderRule(w, opt, pe);
5566 return positionRect(w, rule, subRule, pe, opt->rect, opt->direction);
5567 }
5568 break;
5569
5570 case SE_CheckBoxContents:
5571 case SE_RadioButtonContents:
5572 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_Indicator)) {
5573 bool isRadio = se == SE_RadioButtonContents;
5574 QRect ir = subElementRect(isRadio ? SE_RadioButtonIndicator : SE_CheckBoxIndicator,
5575 opt, w);
5576 ir = visualRect(opt->direction, opt->rect, ir);
5577 int spacing = pixelMetric(isRadio ? PM_RadioButtonLabelSpacing : PM_CheckBoxLabelSpacing, 0, w);
5578 QRect cr = rule.contentsRect(opt->rect);
5579 ir.setRect(ir.left() + ir.width() + spacing, cr.y(),
5580 cr.width() - ir.width() - spacing, cr.height());
5581 return visualRect(opt->direction, opt->rect, ir);
5582 }
5583 break;
5584
5585 case SE_ToolBoxTabContents:
5586 if (w && hasStyleRule(w->parentWidget(), PseudoElement_ToolBoxTab)) {
5587 QRenderRule subRule = renderRule(w->parentWidget(), opt, PseudoElement_ToolBoxTab);
5588 return visualRect(opt->direction, opt->rect, subRule.contentsRect(opt->rect));
5589 }
5590 break;
5591
5592 case SE_RadioButtonFocusRect:
5593 case SE_RadioButtonClickRect: // focusrect | indicator
5594 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_Indicator)) {
5595 return opt->rect;
5596 }
5597 break;
5598
5599 case SE_CheckBoxFocusRect:
5600 case SE_CheckBoxClickRect: // relies on indicator and contents
5601 return ParentStyle::subElementRect(se, opt, w);
5602
5603#ifndef QT_NO_ITEMVIEWS
5604 case SE_ViewItemCheckIndicator:
5605 if (!qstyleoption_cast<const QStyleOptionViewItemV4 *>(opt)) {
5606 return subElementRect(SE_CheckBoxIndicator, opt, w);
5607 }
5608 // intentionally falls through
5609 case SE_ItemViewItemText:
5610 case SE_ItemViewItemDecoration:
5611 case SE_ItemViewItemFocusRect:
5612 if (const QStyleOptionViewItemV4 *vopt = qstyleoption_cast<const QStyleOptionViewItemV4 *>(opt)) {
5613 QRenderRule subRule = renderRule(w, opt, PseudoElement_ViewItem);
5614 QStyleOptionViewItemV4 optCopy(*vopt);
5615 optCopy.rect = subRule.contentsRect(vopt->rect);
5616 QRect rect = ParentStyle::subElementRect(se, &optCopy, w);
5617 PseudoElement pe = PseudoElement_None;
5618 if (se == SE_ItemViewItemText || se == SE_ItemViewItemFocusRect)
5619 pe = PseudoElement_ViewItemText;
5620 else if (se == SE_ItemViewItemDecoration && vopt->features & QStyleOptionViewItemV2::HasDecoration)
5621 pe = PseudoElement_ViewItemIcon;
5622 else if (se == SE_ItemViewItemCheckIndicator && vopt->features & QStyleOptionViewItemV2::HasCheckIndicator)
5623 pe = PseudoElement_ViewItemIndicator;
5624 else
5625 break;
5626 QRenderRule subRule2 = renderRule(w, opt, pe);
5627 return positionRect(w, subRule2, pe, rect, opt->direction);
5628 }
5629 break;
5630#endif // QT_NO_ITEMVIEWS
5631
5632 case SE_HeaderArrow: {
5633 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewUpArrow);
5634 if (subRule.hasPosition() || subRule.hasGeometry())
5635 return positionRect(w, rule, subRule, PseudoElement_HeaderViewUpArrow, opt->rect, opt->direction);
5636 }
5637 break;
5638
5639 case SE_HeaderLabel: {
5640 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
5641 if (subRule.hasBox() || !subRule.hasNativeBorder())
5642 return subRule.contentsRect(opt->rect);
5643 }
5644 break;
5645
5646 case SE_ProgressBarGroove:
5647 case SE_ProgressBarContents:
5648 case SE_ProgressBarLabel:
5649 if (const QStyleOptionProgressBarV2 *pb = qstyleoption_cast<const QStyleOptionProgressBarV2 *>(opt)) {
5650 if (rule.hasBox() || !rule.hasNativeBorder() || rule.hasPosition() || hasStyleRule(w, PseudoElement_ProgressBarChunk)) {
5651 if (se == SE_ProgressBarGroove)
5652 return rule.borderRect(pb->rect);
5653 else if (se == SE_ProgressBarContents)
5654 return rule.contentsRect(pb->rect);
5655
5656 QSize sz = pb->fontMetrics.size(0, pb->text);
5657 return QStyle::alignedRect(Qt::LeftToRight, rule.hasPosition() ? rule.position()->textAlignment : pb->textAlignment,
5658 sz, pb->rect);
5659 }
5660 }
5661 break;
5662
5663#ifndef QT_NO_TABBAR
5664 case SE_TabWidgetLeftCorner:
5665 pe = PseudoElement_TabWidgetLeftCorner;
5666 // intentionally falls through
5667 case SE_TabWidgetRightCorner:
5668 if (pe == PseudoElement_None)
5669 pe = PseudoElement_TabWidgetRightCorner;
5670 // intentionally falls through
5671 case SE_TabWidgetTabBar:
5672 if (pe == PseudoElement_None)
5673 pe = PseudoElement_TabWidgetTabBar;
5674 // intentionally falls through
5675 case SE_TabWidgetTabPane:
5676 case SE_TabWidgetTabContents:
5677 if (pe == PseudoElement_None)
5678 pe = PseudoElement_TabWidgetPane;
5679
5680 if (hasStyleRule(w, pe)) {
5681 QRect r = QWindowsStyle::subElementRect(pe == PseudoElement_TabWidgetPane ? SE_TabWidgetTabPane : se, opt, w);
5682 QRenderRule subRule = renderRule(w, opt, pe);
5683 r = positionRect(w, subRule, pe, r, opt->direction);
5684 if (pe == PseudoElement_TabWidgetTabBar) {
5685 Q_ASSERT(opt);
5686 r = opt->rect.intersected(r);
5687 }
5688 if (se == SE_TabWidgetTabContents)
5689 r = subRule.contentsRect(r);
5690 return r;
5691 }
5692 break;
5693
5694 case SE_TabBarTearIndicator: {
5695 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTear);
5696 if (subRule.hasContentsSize()) {
5697 QRect r;
5698 if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
5699 switch (tab->shape) {
5700 case QTabBar::RoundedNorth:
5701 case QTabBar::TriangularNorth:
5702 case QTabBar::RoundedSouth:
5703 case QTabBar::TriangularSouth:
5704 r.setRect(tab->rect.left(), tab->rect.top(), subRule.size().width(), opt->rect.height());
5705 break;
5706 case QTabBar::RoundedWest:
5707 case QTabBar::TriangularWest:
5708 case QTabBar::RoundedEast:
5709 case QTabBar::TriangularEast:
5710 r.setRect(tab->rect.left(), tab->rect.top(), opt->rect.width(), subRule.size().height());
5711 break;
5712 default:
5713 break;
5714 }
5715 r = visualRect(opt->direction, opt->rect, r);
5716 }
5717 return r;
5718 }
5719 break;
5720 }
5721 case SE_TabBarTabText:
5722 case SE_TabBarTabLeftButton:
5723 case SE_TabBarTabRightButton: {
5724 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab);
5725 if (subRule.hasBox() || !subRule.hasNativeBorder()) {
5726 return ParentStyle::subElementRect(se, opt, w);
5727 }
5728 break;
5729 }
5730#endif // QT_NO_TABBAR
5731
5732 case SE_DockWidgetCloseButton:
5733 case SE_DockWidgetFloatButton: {
5734 PseudoElement pe = (se == SE_DockWidgetCloseButton) ? PseudoElement_DockWidgetCloseButton : PseudoElement_DockWidgetFloatButton;
5735 QRenderRule subRule2 = renderRule(w, opt, pe);
5736 if (!subRule2.hasPosition())
5737 break;
5738 QRenderRule subRule = renderRule(w, opt, PseudoElement_DockWidgetTitle);
5739 return positionRect(w, subRule, subRule2, pe, opt->rect, opt->direction);
5740 }
5741
5742 default:
5743 break;
5744 }
5745
5746 return baseStyle()->subElementRect(se, opt, w);
5747}
5748
5749bool QStyleSheetStyle::event(QEvent *e)
5750{
5751 return (baseStyle()->event(e) && e->isAccepted()) || ParentStyle::event(e);
5752}
5753
5754void QStyleSheetStyle::updateStyleSheetFont(QWidget* w) const
5755{
5756 QWidget *container = containerWidget(w);
5757 QRenderRule rule = renderRule(container, PseudoElement_None,
5758 PseudoClass_Active | PseudoClass_Enabled | extendedPseudoClass(container));
5759 QFont font = rule.font.resolve(w->font());
5760
5761 if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
5762 && isNaturalChild(w) && qobject_cast<QWidget *>(w->parent())) {
5763
5764 font = font.resolve(static_cast<QWidget *>(w->parent())->font());
5765 }
5766
5767 if (w->data->fnt == font)
5768 return;
5769
5770#ifdef QT3_SUPPORT
5771 QFont old = w->data->fnt;
5772#endif
5773 w->data->fnt = font;
5774#if defined(Q_WS_X11)
5775 // make sure the font set on this widget is associated with the correct screen
5776 //w->data->fnt.x11SetScreen(w->d_func()->xinfo.screen());
5777#endif
5778
5779 QEvent e(QEvent::FontChange);
5780 QApplication::sendEvent(w, &e);
5781#ifdef QT3_SUPPORT
5782 w->fontChange(old);
5783#endif
5784}
5785
5786void QStyleSheetStyle::saveWidgetFont(QWidget* w, const QFont& font) const
5787{
5788 w->setProperty("_q_styleSheetWidgetFont", font);
5789}
5790
5791void QStyleSheetStyle::clearWidgetFont(QWidget* w) const
5792{
5793 w->setProperty("_q_styleSheetWidgetFont", QVariant(QVariant::Invalid));
5794}
5795
5796// Polish palette that should be used for a particular widget, with particular states
5797// (eg. :focus, :hover, ...)
5798// this is called by widgets that paint themself in their paint event
5799// Returns true if there is a new palette in pal.
5800bool QStyleSheetStyle::styleSheetPalette(const QWidget* w, const QStyleOption* opt, QPalette* pal)
5801{
5802 if (!w || !opt || !pal)
5803 return false;
5804
5805 RECURSION_GUARD(return false)
5806
5807 w = containerWidget(w);
5808
5809 QRenderRule rule = renderRule(w, PseudoElement_None, pseudoClass(opt->state) | extendedPseudoClass(w));
5810 if (!rule.hasPalette())
5811 return false;
5812
5813 rule.configurePalette(pal, QPalette::NoRole, QPalette::NoRole);
5814 return true;
5815}
5816
5817Qt::Alignment QStyleSheetStyle::resolveAlignment(Qt::LayoutDirection layDir, Qt::Alignment src)
5818{
5819 if (layDir == Qt::LeftToRight || src & Qt::AlignAbsolute)
5820 return src;
5821
5822 if (src & Qt::AlignLeft) {
5823 src &= ~Qt::AlignLeft;
5824 src |= Qt::AlignRight;
5825 } else if (src & Qt::AlignRight) {
5826 src &= ~Qt::AlignRight;
5827 src |= Qt::AlignLeft;
5828 }
5829 src |= Qt::AlignAbsolute;
5830 return src;
5831}
5832
5833// Returns whether the given QWidget has a "natural" parent, meaning that
5834// the parent contains this child as part of its normal operation.
5835// An example is the QTabBar inside a QTabWidget.
5836// This does not mean that any QTabBar which is a child of QTabWidget will
5837// match, only the one that was created by the QTabWidget initialization
5838// (and hence has the correct object name).
5839bool QStyleSheetStyle::isNaturalChild(const QWidget *w)
5840{
5841 if (w->objectName().startsWith(QLatin1String("qt_")))
5842 return true;
5843
5844 return false;
5845}
5846
5847QT_END_NAMESPACE
5848
5849#include "moc_qstylesheetstyle_p.cpp"
5850
5851#endif // QT_NO_STYLE_STYLESHEET
Note: See TracBrowser for help on using the repository browser.