ctkProxyStyle.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QPointer>
  17. #include <QStyleFactory>
  18. // CTK includes
  19. #include "ctkProxyStyle.h"
  20. // ----------------------------------------------------------------------------
  21. class ctkProxyStylePrivate
  22. {
  23. Q_DECLARE_PUBLIC(ctkProxyStyle)
  24. protected:
  25. ctkProxyStyle* const q_ptr;
  26. public:
  27. void setProxyStyle(QProxyStyle* proxy, QStyle *style)const;
  28. void setBaseStyle(QProxyStyle* proxyStyle, QStyle* baseStyle)const;
  29. private:
  30. ctkProxyStylePrivate(ctkProxyStyle& object);
  31. mutable QPointer <QStyle> baseStyle;
  32. };
  33. // ----------------------------------------------------------------------------
  34. ctkProxyStylePrivate::ctkProxyStylePrivate(ctkProxyStyle& object)
  35. : q_ptr(&object)
  36. {
  37. }
  38. // ----------------------------------------------------------------------------
  39. void ctkProxyStylePrivate::setProxyStyle(QProxyStyle* proxy, QStyle *style)const
  40. {
  41. if (style->proxy() == proxy)
  42. {
  43. return;
  44. }
  45. this->setBaseStyle(proxy, style);
  46. }
  47. // ----------------------------------------------------------------------------
  48. void ctkProxyStylePrivate::setBaseStyle(QProxyStyle* proxy, QStyle *style)const
  49. {
  50. if (proxy->baseStyle() == style &&
  51. style->proxy() == proxy)
  52. {
  53. return;
  54. }
  55. QObject* parent = style->parent();
  56. QStyle* oldStyle = proxy->baseStyle();
  57. QObject* oldParent = oldStyle ? oldStyle->parent() : 0;
  58. if (oldParent == proxy)
  59. {
  60. oldStyle->setParent(0);// make sure setBaseStyle doesn't delete baseStyle
  61. }
  62. proxy->setBaseStyle(style);
  63. style->setParent(parent);
  64. if (oldParent == proxy)
  65. {
  66. oldStyle->setParent(oldParent);
  67. }
  68. }
  69. // ----------------------------------------------------------------------------
  70. ctkProxyStyle::ctkProxyStyle(QStyle *style, QObject* parent)
  71. : d_ptr(new ctkProxyStylePrivate(*this))
  72. {
  73. Q_D(ctkProxyStyle);
  74. d->baseStyle = style;
  75. this->setBaseStyle(style);
  76. this->setParent(parent);
  77. }
  78. // ----------------------------------------------------------------------------
  79. ctkProxyStyle::~ctkProxyStyle()
  80. {
  81. Q_D(ctkProxyStyle);
  82. if (!QApplication::closingDown() && d->baseStyle == QApplication::style())
  83. {
  84. d->baseStyle->setParent(qApp); // don't delete the application style.
  85. }
  86. }
  87. // ----------------------------------------------------------------------------
  88. void ctkProxyStyle::ensureBaseStyle() const
  89. {
  90. Q_D(const ctkProxyStyle);
  91. d->baseStyle = this->baseStyle();
  92. // Set the proxy to the entire hierarchy.
  93. QProxyStyle* proxyStyle = const_cast<QProxyStyle*>(qobject_cast<const QProxyStyle*>(
  94. this->proxy() ? this->proxy() : this));
  95. QStyle* proxyBaseStyle = proxyStyle->baseStyle(); // calls ensureBaseStyle
  96. QStyle* baseStyle = proxyBaseStyle;
  97. while (baseStyle)
  98. {
  99. d->setProxyStyle(proxyStyle, baseStyle);// set proxy on itself to all children
  100. QProxyStyle* proxy = qobject_cast<QProxyStyle*>(baseStyle);
  101. baseStyle = proxy ? proxy->baseStyle() : 0;
  102. }
  103. d->setBaseStyle(proxyStyle, proxyBaseStyle);
  104. }
  105. // ----------------------------------------------------------------------------
  106. void ctkProxyStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
  107. {
  108. Q_D(const ctkProxyStyle);
  109. this->ensureBaseStyle();
  110. d->baseStyle->drawPrimitive(element, option, painter, widget);
  111. }
  112. // ----------------------------------------------------------------------------
  113. void ctkProxyStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
  114. {
  115. Q_D(const ctkProxyStyle);
  116. this->ensureBaseStyle();
  117. d->baseStyle->drawControl(element, option, painter, widget);
  118. }
  119. // ----------------------------------------------------------------------------
  120. void ctkProxyStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
  121. {
  122. Q_D(const ctkProxyStyle);
  123. this->ensureBaseStyle();
  124. d->baseStyle->drawComplexControl(control, option, painter, widget);
  125. }
  126. // ----------------------------------------------------------------------------
  127. void ctkProxyStyle::drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled,
  128. const QString &text, QPalette::ColorRole textRole) const
  129. {
  130. Q_D(const ctkProxyStyle);
  131. this->ensureBaseStyle();
  132. d->baseStyle->drawItemText(painter, rect, flags, pal, enabled, text, textRole);
  133. }
  134. // ----------------------------------------------------------------------------
  135. void ctkProxyStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const
  136. {
  137. Q_D(const ctkProxyStyle);
  138. this->ensureBaseStyle();
  139. d->baseStyle->drawItemPixmap(painter, rect, alignment, pixmap);
  140. }
  141. // ----------------------------------------------------------------------------
  142. QSize ctkProxyStyle::sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const
  143. {
  144. Q_D(const ctkProxyStyle);
  145. this->ensureBaseStyle();
  146. return d->baseStyle->sizeFromContents(type, option, size, widget);
  147. }
  148. // ----------------------------------------------------------------------------
  149. QRect ctkProxyStyle::subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const
  150. {
  151. Q_D(const ctkProxyStyle);
  152. this->ensureBaseStyle();
  153. return d->baseStyle->subElementRect(element, option, widget);
  154. }
  155. // ----------------------------------------------------------------------------
  156. QRect ctkProxyStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *option, SubControl sc, const QWidget *widget) const
  157. {
  158. Q_D(const ctkProxyStyle);
  159. this->ensureBaseStyle();
  160. return d->baseStyle->subControlRect(cc, option, sc, widget);
  161. }
  162. // ----------------------------------------------------------------------------
  163. QRect ctkProxyStyle::itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const
  164. {
  165. Q_D(const ctkProxyStyle);
  166. this->ensureBaseStyle();
  167. return d->baseStyle->itemTextRect(fm, r, flags, enabled, text);
  168. }
  169. // ----------------------------------------------------------------------------
  170. QRect ctkProxyStyle::itemPixmapRect(const QRect &r, int flags, const QPixmap &pixmap) const
  171. {
  172. Q_D(const ctkProxyStyle);
  173. this->ensureBaseStyle();
  174. return d->baseStyle->itemPixmapRect(r, flags, pixmap);
  175. }
  176. // ----------------------------------------------------------------------------
  177. QStyle::SubControl ctkProxyStyle::hitTestComplexControl(ComplexControl control, const QStyleOptionComplex *option, const QPoint &pos, const QWidget *widget) const
  178. {
  179. Q_D(const ctkProxyStyle);
  180. this->ensureBaseStyle();
  181. return d->baseStyle->hitTestComplexControl(control, option, pos, widget);
  182. }
  183. // ----------------------------------------------------------------------------
  184. int ctkProxyStyle::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
  185. {
  186. Q_D(const ctkProxyStyle);
  187. this->ensureBaseStyle();
  188. return d->baseStyle->styleHint(hint, option, widget, returnData);
  189. }
  190. // ----------------------------------------------------------------------------
  191. int ctkProxyStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
  192. {
  193. Q_D(const ctkProxyStyle);
  194. this->ensureBaseStyle();
  195. return d->baseStyle->pixelMetric(metric, option, widget);
  196. }
  197. // ----------------------------------------------------------------------------
  198. QPixmap ctkProxyStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, const QWidget *widget) const
  199. {
  200. Q_D(const ctkProxyStyle);
  201. this->ensureBaseStyle();
  202. return d->baseStyle->standardPixmap(standardPixmap, opt, widget);
  203. }
  204. // ----------------------------------------------------------------------------
  205. QPixmap ctkProxyStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const
  206. {
  207. Q_D(const ctkProxyStyle);
  208. this->ensureBaseStyle();
  209. return d->baseStyle->generatedIconPixmap(iconMode, pixmap, opt);
  210. }
  211. // ----------------------------------------------------------------------------
  212. QPalette ctkProxyStyle::standardPalette() const
  213. {
  214. Q_D(const ctkProxyStyle);
  215. this->ensureBaseStyle();
  216. return d->baseStyle->standardPalette();
  217. }
  218. // ----------------------------------------------------------------------------
  219. void ctkProxyStyle::polish(QWidget *widget)
  220. {
  221. Q_D(const ctkProxyStyle);
  222. this->ensureBaseStyle();
  223. d->baseStyle->polish(widget);
  224. }
  225. // ----------------------------------------------------------------------------
  226. void ctkProxyStyle::polish(QPalette &pal)
  227. {
  228. Q_D(const ctkProxyStyle);
  229. this->ensureBaseStyle();
  230. d->baseStyle->polish(pal);
  231. }
  232. // ----------------------------------------------------------------------------
  233. void ctkProxyStyle::polish(QApplication *app)
  234. {
  235. Q_D(const ctkProxyStyle);
  236. this->ensureBaseStyle();
  237. d->baseStyle->polish(app);
  238. }
  239. // ----------------------------------------------------------------------------
  240. void ctkProxyStyle::unpolish(QWidget *widget)
  241. {
  242. Q_D(const ctkProxyStyle);
  243. this->ensureBaseStyle();
  244. d->baseStyle->unpolish(widget);
  245. }
  246. // ----------------------------------------------------------------------------
  247. void ctkProxyStyle::unpolish(QApplication *app)
  248. {
  249. Q_D(const ctkProxyStyle);
  250. this->ensureBaseStyle();
  251. d->baseStyle->unpolish(app);
  252. }
  253. // ----------------------------------------------------------------------------
  254. bool ctkProxyStyle::event(QEvent *e)
  255. {
  256. Q_D(const ctkProxyStyle);
  257. if (e->type() != QEvent::ParentChange &&
  258. e->type() != QEvent::ChildRemoved &&
  259. e->type() != QEvent::ChildAdded)
  260. {
  261. this->ensureBaseStyle();
  262. }
  263. return !d->baseStyle.isNull() ? d->baseStyle->event(e) : false;
  264. }
  265. // ----------------------------------------------------------------------------
  266. QIcon ctkProxyStyle::standardIconImplementation(StandardPixmap standardIcon,
  267. const QStyleOption *option,
  268. const QWidget *widget) const
  269. {
  270. Q_D(const ctkProxyStyle);
  271. this->ensureBaseStyle();
  272. return d->baseStyle->standardIcon(standardIcon, option, widget);
  273. }
  274. // ----------------------------------------------------------------------------
  275. int ctkProxyStyle::layoutSpacingImplementation(QSizePolicy::ControlType control1,
  276. QSizePolicy::ControlType control2,
  277. Qt::Orientation orientation,
  278. const QStyleOption *option,
  279. const QWidget *widget) const
  280. {
  281. Q_D(const ctkProxyStyle);
  282. this->ensureBaseStyle();
  283. return d->baseStyle->layoutSpacing(control1, control2, orientation, option, widget);
  284. }