ctkProxyStyle.cpp 11 KB

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