123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- /*=========================================================================
- Library: CTK
- Copyright (c) Kitware Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0.txt
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =========================================================================*/
- // Qt includes
- #include <QApplication>
- #include <QPointer>
- #include <QStyleFactory>
- // CTK includes
- #include "ctkProxyStyle.h"
- // ----------------------------------------------------------------------------
- class ctkProxyStylePrivate
- {
- Q_DECLARE_PUBLIC(ctkProxyStyle)
- protected:
- ctkProxyStyle* const q_ptr;
- public:
- void setProxyStyle(QProxyStyle* proxy, QStyle *style)const;
- void setBaseStyle(QProxyStyle* proxyStyle, QStyle* baseStyle)const;
- private:
- ctkProxyStylePrivate(ctkProxyStyle& object);
- mutable QPointer <QStyle> baseStyle;
- };
- // ----------------------------------------------------------------------------
- ctkProxyStylePrivate::ctkProxyStylePrivate(ctkProxyStyle& object)
- : q_ptr(&object)
- {
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStylePrivate::setProxyStyle(QProxyStyle* proxy, QStyle *style)const
- {
- if (style->proxy() == proxy)
- {
- return;
- }
- this->setBaseStyle(proxy, style);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStylePrivate::setBaseStyle(QProxyStyle* proxy, QStyle *style)const
- {
- if (proxy->baseStyle() == style &&
- style->proxy() == proxy)
- {
- return;
- }
- QObject* parent = style->parent();
- QStyle* oldStyle = proxy->baseStyle();
- QObject* oldParent = oldStyle ? oldStyle->parent() : 0;
- if (oldParent == proxy)
- {
- oldStyle->setParent(0);// make sure setBaseStyle doesn't delete baseStyle
- }
- proxy->setBaseStyle(style);
- style->setParent(parent);
- if (oldParent == proxy)
- {
- oldStyle->setParent(oldParent);
- }
- }
- // ----------------------------------------------------------------------------
- ctkProxyStyle::ctkProxyStyle(QStyle *style, QObject* parent)
- : d_ptr(new ctkProxyStylePrivate(*this))
- {
- Q_D(ctkProxyStyle);
- d->baseStyle = style;
- this->setBaseStyle(style);
- this->setParent(parent);
- }
- // ----------------------------------------------------------------------------
- ctkProxyStyle::~ctkProxyStyle()
- {
- Q_D(ctkProxyStyle);
- if (!QApplication::closingDown() && d->baseStyle == QApplication::style())
- {
- d->baseStyle->setParent(qApp); // don't delete the application style.
- }
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::ensureBaseStyle() const
- {
- Q_D(const ctkProxyStyle);
- d->baseStyle = this->baseStyle();
- // Set the proxy to the entire hierarchy.
- QProxyStyle* proxyStyle = const_cast<QProxyStyle*>(qobject_cast<const QProxyStyle*>(
- this->proxy() ? this->proxy() : this));
- QStyle* proxyBaseStyle = proxyStyle->baseStyle(); // calls ensureBaseStyle
- QStyle* baseStyle = proxyBaseStyle;
- while (baseStyle)
- {
- d->setProxyStyle(proxyStyle, baseStyle);// set proxy on itself to all children
- QProxyStyle* proxy = qobject_cast<QProxyStyle*>(baseStyle);
- baseStyle = proxy ? proxy->baseStyle() : 0;
- }
- d->setBaseStyle(proxyStyle, proxyBaseStyle);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->drawPrimitive(element, option, painter, widget);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->drawControl(element, option, painter, widget);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->drawComplexControl(control, option, painter, widget);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled,
- const QString &text, QPalette::ColorRole textRole) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->drawItemText(painter, rect, flags, pal, enabled, text, textRole);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->drawItemPixmap(painter, rect, alignment, pixmap);
- }
- // ----------------------------------------------------------------------------
- QSize ctkProxyStyle::sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->sizeFromContents(type, option, size, widget);
- }
- // ----------------------------------------------------------------------------
- QRect ctkProxyStyle::subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->subElementRect(element, option, widget);
- }
- // ----------------------------------------------------------------------------
- QRect ctkProxyStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *option, SubControl sc, const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->subControlRect(cc, option, sc, widget);
- }
- // ----------------------------------------------------------------------------
- QRect ctkProxyStyle::itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->itemTextRect(fm, r, flags, enabled, text);
- }
- // ----------------------------------------------------------------------------
- QRect ctkProxyStyle::itemPixmapRect(const QRect &r, int flags, const QPixmap &pixmap) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->itemPixmapRect(r, flags, pixmap);
- }
- // ----------------------------------------------------------------------------
- QStyle::SubControl ctkProxyStyle::hitTestComplexControl(ComplexControl control, const QStyleOptionComplex *option, const QPoint &pos, const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->hitTestComplexControl(control, option, pos, widget);
- }
- // ----------------------------------------------------------------------------
- int ctkProxyStyle::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->styleHint(hint, option, widget, returnData);
- }
- // ----------------------------------------------------------------------------
- int ctkProxyStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->pixelMetric(metric, option, widget);
- }
- // ----------------------------------------------------------------------------
- QPixmap ctkProxyStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->standardPixmap(standardPixmap, opt, widget);
- }
- // ----------------------------------------------------------------------------
- QPixmap ctkProxyStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->generatedIconPixmap(iconMode, pixmap, opt);
- }
- // ----------------------------------------------------------------------------
- QPalette ctkProxyStyle::standardPalette() const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->standardPalette();
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::polish(QWidget *widget)
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->polish(widget);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::polish(QPalette &pal)
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->polish(pal);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::polish(QApplication *app)
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->polish(app);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::unpolish(QWidget *widget)
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->unpolish(widget);
- }
- // ----------------------------------------------------------------------------
- void ctkProxyStyle::unpolish(QApplication *app)
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- d->baseStyle->unpolish(app);
- }
- // ----------------------------------------------------------------------------
- bool ctkProxyStyle::event(QEvent *e)
- {
- Q_D(const ctkProxyStyle);
- if (e->type() != QEvent::ParentChange &&
- e->type() != QEvent::ChildRemoved &&
- e->type() != QEvent::ChildAdded)
- {
- this->ensureBaseStyle();
- }
- return !d->baseStyle.isNull() ? d->baseStyle->event(e) : false;
- }
- // ----------------------------------------------------------------------------
- QIcon ctkProxyStyle::standardIconImplementation(StandardPixmap standardIcon,
- const QStyleOption *option,
- const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->standardIcon(standardIcon, option, widget);
- }
- // ----------------------------------------------------------------------------
- int ctkProxyStyle::layoutSpacingImplementation(QSizePolicy::ControlType control1,
- QSizePolicy::ControlType control2,
- Qt::Orientation orientation,
- const QStyleOption *option,
- const QWidget *widget) const
- {
- Q_D(const ctkProxyStyle);
- this->ensureBaseStyle();
- return d->baseStyle->layoutSpacing(control1, control2, orientation, option, widget);
- }
|