ctkProxyStyle.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. mutable bool ensureBaseStyleInProgress;
  33. };
  34. // ----------------------------------------------------------------------------
  35. ctkProxyStylePrivate::ctkProxyStylePrivate(ctkProxyStyle& object)
  36. : q_ptr(&object)
  37. , ensureBaseStyleInProgress(false)
  38. {
  39. }
  40. // ----------------------------------------------------------------------------
  41. void ctkProxyStylePrivate::setProxyStyle(QProxyStyle* proxy, QStyle *style)const
  42. {
  43. if (style->proxy() == proxy)
  44. {
  45. return;
  46. }
  47. this->setBaseStyle(proxy, style);
  48. }
  49. // ----------------------------------------------------------------------------
  50. void ctkProxyStylePrivate::setBaseStyle(QProxyStyle* proxy, QStyle *style)const
  51. {
  52. if (proxy->baseStyle() == style &&
  53. style->proxy() == proxy)
  54. {
  55. return;
  56. }
  57. QObject* parent = style->parent();
  58. QStyle* oldStyle = proxy->baseStyle();
  59. QObject* oldParent = oldStyle ? oldStyle->parent() : 0;
  60. if (oldParent == proxy)
  61. {
  62. oldStyle->setParent(0);// make sure setBaseStyle doesn't delete baseStyle
  63. }
  64. proxy->setBaseStyle(style);
  65. style->setParent(parent);
  66. if (oldParent == proxy)
  67. {
  68. oldStyle->setParent(oldParent);
  69. }
  70. }
  71. // ----------------------------------------------------------------------------
  72. ctkProxyStyle::ctkProxyStyle(QStyle *style, QObject* parent)
  73. : d_ptr(new ctkProxyStylePrivate(*this))
  74. {
  75. Q_D(ctkProxyStyle);
  76. d->baseStyle = style;
  77. this->setBaseStyle(style);
  78. this->setParent(parent);
  79. }
  80. // ----------------------------------------------------------------------------
  81. ctkProxyStyle::~ctkProxyStyle()
  82. {
  83. Q_D(ctkProxyStyle);
  84. if (!QApplication::closingDown() && d->baseStyle == QApplication::style())
  85. {
  86. d->baseStyle->setParent(qApp); // don't delete the application style.
  87. }
  88. }
  89. // ----------------------------------------------------------------------------
  90. void ctkProxyStyle::ensureBaseStyle() const
  91. {
  92. Q_D(const ctkProxyStyle);
  93. if (d->ensureBaseStyleInProgress)
  94. {
  95. // avoid infinite loop
  96. return;
  97. }
  98. d->ensureBaseStyleInProgress = true;
  99. d->baseStyle = this->baseStyle();
  100. // Set the proxy to the entire hierarchy.
  101. QProxyStyle* proxyStyle = const_cast<QProxyStyle*>(qobject_cast<const QProxyStyle*>(
  102. this->proxy() ? this->proxy() : this));
  103. QStyle* proxyBaseStyle = proxyStyle->baseStyle(); // calls ensureBaseStyle
  104. QStyle* baseStyle = proxyBaseStyle;
  105. while (baseStyle)
  106. {
  107. d->setProxyStyle(proxyStyle, baseStyle);// set proxy on itself to all children
  108. QProxyStyle* proxy = qobject_cast<QProxyStyle*>(baseStyle);
  109. baseStyle = proxy ? proxy->baseStyle() : 0;
  110. }
  111. d->setBaseStyle(proxyStyle, proxyBaseStyle);
  112. d->ensureBaseStyleInProgress = false;
  113. }
  114. // ----------------------------------------------------------------------------
  115. void ctkProxyStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
  116. {
  117. Q_D(const ctkProxyStyle);
  118. this->ensureBaseStyle();
  119. d->baseStyle->drawPrimitive(element, option, painter, widget);
  120. }
  121. // ----------------------------------------------------------------------------
  122. void ctkProxyStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
  123. {
  124. Q_D(const ctkProxyStyle);
  125. this->ensureBaseStyle();
  126. d->baseStyle->drawControl(element, option, painter, widget);
  127. }
  128. // ----------------------------------------------------------------------------
  129. void ctkProxyStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
  130. {
  131. Q_D(const ctkProxyStyle);
  132. this->ensureBaseStyle();
  133. d->baseStyle->drawComplexControl(control, option, painter, widget);
  134. }
  135. // ----------------------------------------------------------------------------
  136. void ctkProxyStyle::drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled,
  137. const QString &text, QPalette::ColorRole textRole) const
  138. {
  139. Q_D(const ctkProxyStyle);
  140. this->ensureBaseStyle();
  141. d->baseStyle->drawItemText(painter, rect, flags, pal, enabled, text, textRole);
  142. }
  143. // ----------------------------------------------------------------------------
  144. void ctkProxyStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const
  145. {
  146. Q_D(const ctkProxyStyle);
  147. this->ensureBaseStyle();
  148. d->baseStyle->drawItemPixmap(painter, rect, alignment, pixmap);
  149. }
  150. // ----------------------------------------------------------------------------
  151. QSize ctkProxyStyle::sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const
  152. {
  153. Q_D(const ctkProxyStyle);
  154. this->ensureBaseStyle();
  155. return d->baseStyle->sizeFromContents(type, option, size, widget);
  156. }
  157. // ----------------------------------------------------------------------------
  158. QRect ctkProxyStyle::subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const
  159. {
  160. Q_D(const ctkProxyStyle);
  161. this->ensureBaseStyle();
  162. return d->baseStyle->subElementRect(element, option, widget);
  163. }
  164. // ----------------------------------------------------------------------------
  165. QRect ctkProxyStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *option, SubControl sc, const QWidget *widget) const
  166. {
  167. Q_D(const ctkProxyStyle);
  168. this->ensureBaseStyle();
  169. return d->baseStyle->subControlRect(cc, option, sc, widget);
  170. }
  171. // ----------------------------------------------------------------------------
  172. QRect ctkProxyStyle::itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const
  173. {
  174. Q_D(const ctkProxyStyle);
  175. this->ensureBaseStyle();
  176. return d->baseStyle->itemTextRect(fm, r, flags, enabled, text);
  177. }
  178. // ----------------------------------------------------------------------------
  179. QRect ctkProxyStyle::itemPixmapRect(const QRect &r, int flags, const QPixmap &pixmap) const
  180. {
  181. Q_D(const ctkProxyStyle);
  182. this->ensureBaseStyle();
  183. return d->baseStyle->itemPixmapRect(r, flags, pixmap);
  184. }
  185. // ----------------------------------------------------------------------------
  186. QStyle::SubControl ctkProxyStyle::hitTestComplexControl(ComplexControl control, const QStyleOptionComplex *option, const QPoint &pos, const QWidget *widget) const
  187. {
  188. Q_D(const ctkProxyStyle);
  189. this->ensureBaseStyle();
  190. return d->baseStyle->hitTestComplexControl(control, option, pos, widget);
  191. }
  192. // ----------------------------------------------------------------------------
  193. int ctkProxyStyle::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
  194. {
  195. Q_D(const ctkProxyStyle);
  196. this->ensureBaseStyle();
  197. return d->baseStyle->styleHint(hint, option, widget, returnData);
  198. }
  199. // ----------------------------------------------------------------------------
  200. int ctkProxyStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
  201. {
  202. Q_D(const ctkProxyStyle);
  203. this->ensureBaseStyle();
  204. return d->baseStyle->pixelMetric(metric, option, widget);
  205. }
  206. // ----------------------------------------------------------------------------
  207. QPixmap ctkProxyStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, const QWidget *widget) const
  208. {
  209. Q_D(const ctkProxyStyle);
  210. this->ensureBaseStyle();
  211. return d->baseStyle->standardPixmap(standardPixmap, opt, widget);
  212. }
  213. // ----------------------------------------------------------------------------
  214. QPixmap ctkProxyStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const
  215. {
  216. Q_D(const ctkProxyStyle);
  217. this->ensureBaseStyle();
  218. return d->baseStyle->generatedIconPixmap(iconMode, pixmap, opt);
  219. }
  220. // ----------------------------------------------------------------------------
  221. QPalette ctkProxyStyle::standardPalette() const
  222. {
  223. Q_D(const ctkProxyStyle);
  224. this->ensureBaseStyle();
  225. return d->baseStyle->standardPalette();
  226. }
  227. // ----------------------------------------------------------------------------
  228. void ctkProxyStyle::polish(QWidget *widget)
  229. {
  230. Q_D(const ctkProxyStyle);
  231. this->ensureBaseStyle();
  232. d->baseStyle->polish(widget);
  233. }
  234. // ----------------------------------------------------------------------------
  235. void ctkProxyStyle::polish(QPalette &pal)
  236. {
  237. Q_D(const ctkProxyStyle);
  238. this->ensureBaseStyle();
  239. d->baseStyle->polish(pal);
  240. }
  241. // ----------------------------------------------------------------------------
  242. void ctkProxyStyle::polish(QApplication *app)
  243. {
  244. Q_D(const ctkProxyStyle);
  245. this->ensureBaseStyle();
  246. d->baseStyle->polish(app);
  247. }
  248. // ----------------------------------------------------------------------------
  249. void ctkProxyStyle::unpolish(QWidget *widget)
  250. {
  251. Q_D(const ctkProxyStyle);
  252. this->ensureBaseStyle();
  253. d->baseStyle->unpolish(widget);
  254. }
  255. // ----------------------------------------------------------------------------
  256. void ctkProxyStyle::unpolish(QApplication *app)
  257. {
  258. Q_D(const ctkProxyStyle);
  259. this->ensureBaseStyle();
  260. d->baseStyle->unpolish(app);
  261. }
  262. // ----------------------------------------------------------------------------
  263. bool ctkProxyStyle::event(QEvent *e)
  264. {
  265. Q_D(const ctkProxyStyle);
  266. if (e->type() != QEvent::ParentChange &&
  267. e->type() != QEvent::ChildRemoved &&
  268. e->type() != QEvent::ChildAdded)
  269. {
  270. this->ensureBaseStyle();
  271. }
  272. return !d->baseStyle.isNull() ? d->baseStyle->event(e) : false;
  273. }
  274. // ----------------------------------------------------------------------------
  275. QIcon ctkProxyStyle::standardIconImplementation(StandardPixmap standardIcon,
  276. const QStyleOption *option,
  277. const QWidget *widget) const
  278. {
  279. Q_D(const ctkProxyStyle);
  280. this->ensureBaseStyle();
  281. return d->baseStyle->standardIcon(standardIcon, option, widget);
  282. }
  283. // ----------------------------------------------------------------------------
  284. int ctkProxyStyle::layoutSpacingImplementation(QSizePolicy::ControlType control1,
  285. QSizePolicy::ControlType control2,
  286. Qt::Orientation orientation,
  287. const QStyleOption *option,
  288. const QWidget *widget) const
  289. {
  290. Q_D(const ctkProxyStyle);
  291. this->ensureBaseStyle();
  292. return d->baseStyle->layoutSpacing(control1, control2, orientation, option, widget);
  293. }