1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtGui module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
24 #include <QStyleFactory>
25 #include "qxtproxystyle.h"
28 \class QxtProxyStyle QxtProxyStyle
32 A technique called "proxy style" is a common way for creating
33 cross-platform custom styles. Developers often want to do slight
34 adjustments in some specific values returned by QStyle. A proxy
35 style is the solution to avoid subclassing any platform specific
36 style (eg. QPlastiqueStyle, QWindowsXPStyle, or QMacStyle) and
37 to retain the native look on all supported platforms.
39 The subject has been discussed in Qt Quarterly 9:
40 http://doc.trolltech.com/qq/qq09-q-and-a.html#style (just notice
41 that there are a few noteworthy spelling mistakes in the article).
43 Proxy styles are becoming obsolete thanks to style sheets
44 introduced in Qt 4.2. However, style sheets still is a new
45 concept and only a portion of features are supported yet. Both -
46 style sheets and proxy styles - have their pros and cons.
50 Implement the custom behaviour in a subclass of QxtProxyStyle:
52 class MyCustomStyle : public QxtProxyStyle
55 MyCustomStyle(const QString& baseStyle) : QxtProxyStyle(baseStyle)
59 int pixelMetric(PixelMetric metric, const QStyleOption* option = 0, const QWidget* widget = 0) const
61 if (metric == QStyle::PM_ButtonMargin)
63 return QxtProxyStyle::pixelMetric(metric, option, widget);
68 Using the custom style for the whole application:
70 QString defaultStyle = QApplication::style()->objectName();
71 QApplication::setStyle(new MyCustomStyle(defaultStyle));
74 Using the custom style for a single widget:
76 QString defaultStyle = widget->style()->objectName();
77 widget->setStyle(new MyCustomStyle(defaultStyle));
82 Constructs a new QxtProxyStyle for \a style.
83 See QStyleFactory::keys() for supported styles.
85 \sa QStyleFactory::keys()
87 QxtProxyStyle::QxtProxyStyle(const QString& baseStyle)
88 : QStyle(), style(QStyleFactory::create(baseStyle))
90 setObjectName(QLatin1String("proxy"));
94 Destructs the proxy style.
96 QxtProxyStyle::~QxtProxyStyle()
101 void QxtProxyStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget) const
103 style->drawComplexControl(control, option, painter, widget);
106 void QxtProxyStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
108 style->drawControl(element, option, painter, widget);
111 void QxtProxyStyle::drawItemPixmap(QPainter* painter, const QRect& rect, int alignment, const QPixmap& pixmap) const
113 style->drawItemPixmap(painter, rect, alignment, pixmap);
116 void QxtProxyStyle::drawItemText(QPainter* painter, const QRect& rect, int alignment, const QPalette& pal, bool enabled, const QString& text, QPalette::ColorRole textRole) const
118 style->drawItemText(painter, rect, alignment, pal, enabled, text, textRole);
121 void QxtProxyStyle::drawPrimitive(PrimitiveElement elem, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
123 style->drawPrimitive(elem, option, painter, widget);
126 QPixmap QxtProxyStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap& pixmap, const QStyleOption* option) const
128 return style->generatedIconPixmap(iconMode, pixmap, option);
131 QStyle::SubControl QxtProxyStyle::hitTestComplexControl(ComplexControl control, const QStyleOptionComplex* option, const QPoint& pos, const QWidget* widget) const
133 return style->hitTestComplexControl(control, option, pos, widget);
136 QRect QxtProxyStyle::itemPixmapRect(const QRect& rect, int alignment, const QPixmap& pixmap) const
138 return style->itemPixmapRect(rect, alignment, pixmap);
141 QRect QxtProxyStyle::itemTextRect(const QFontMetrics& metrics, const QRect& rect, int alignment, bool enabled, const QString& text) const
143 return style->itemTextRect(metrics, rect, alignment, enabled, text);
146 int QxtProxyStyle::pixelMetric(PixelMetric metric, const QStyleOption* option, const QWidget* widget) const
148 return style->pixelMetric(metric, option, widget);
151 void QxtProxyStyle::polish(QWidget* widget)
153 style->polish(widget);
156 void QxtProxyStyle::polish(QApplication* app)
161 void QxtProxyStyle::polish(QPalette& pal)
166 QSize QxtProxyStyle::sizeFromContents(ContentsType type, const QStyleOption* option, const QSize& contentsSize, const QWidget* widget) const
168 return style->sizeFromContents(type, option, contentsSize, widget);
171 QIcon QxtProxyStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption* option, const QWidget* widget) const
173 return style->standardIcon(standardIcon, option, widget);
176 QPalette QxtProxyStyle::standardPalette() const
178 return style->standardPalette();
181 QPixmap QxtProxyStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption* option, const QWidget* widget) const
183 return style->standardPixmap(standardPixmap, option, widget);
186 int QxtProxyStyle::styleHint(StyleHint hint, const QStyleOption* option, const QWidget* widget, QStyleHintReturn* returnData) const
188 return style->styleHint(hint, option, widget, returnData);
191 QRect QxtProxyStyle::subControlRect(ComplexControl control, const QStyleOptionComplex* option, SubControl subControl, const QWidget* widget) const
193 return style->subControlRect(control, option, subControl, widget);
196 QRect QxtProxyStyle::subElementRect(SubElement element, const QStyleOption* option, const QWidget* widget) const
198 return style->subElementRect(element, option, widget);
201 void QxtProxyStyle::unpolish(QWidget* widget)
203 style->unpolish(widget);
206 void QxtProxyStyle::unpolish(QApplication* app)
208 style->unpolish(app);