ctkProxyStyle.cpp 12 KB

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