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

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

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

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