ctkCollapsibleGroupBox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 <QDebug>
  17. #include <QChildEvent>
  18. #include <QMouseEvent>
  19. #include <QStylePainter>
  20. #include <QStyleOptionGroupBox>
  21. #include <QStyle>
  22. // CTK includes
  23. #include "ctkCollapsibleGroupBox.h"
  24. #if QT_VERSION >= 0x040600
  25. #include "ctkProxyStyle.h"
  26. //-----------------------------------------------------------------------------
  27. class ctkCollapsibleGroupBoxStyle:public ctkProxyStyle
  28. {
  29. public:
  30. typedef ctkProxyStyle Superclass;
  31. ctkCollapsibleGroupBoxStyle(QStyle* style = 0, QObject* parent =0)
  32. : Superclass(style, parent)
  33. {
  34. }
  35. virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption * opt, QPainter * p, const QWidget * widget = 0) const
  36. {
  37. if (pe == QStyle::PE_IndicatorCheckBox)
  38. {
  39. const ctkCollapsibleGroupBox* groupBox= qobject_cast<const ctkCollapsibleGroupBox*>(widget);
  40. if (groupBox)
  41. {
  42. this->Superclass::drawPrimitive(groupBox->isChecked() ? QStyle::PE_IndicatorArrowDown : QStyle::PE_IndicatorArrowRight, opt, p, widget);
  43. return;
  44. }
  45. }
  46. this->Superclass::drawPrimitive(pe, opt, p, widget);
  47. }
  48. virtual int pixelMetric(PixelMetric metric, const QStyleOption * option, const QWidget * widget) const
  49. {
  50. if (metric == QStyle::PM_IndicatorHeight)
  51. {
  52. const ctkCollapsibleGroupBox* groupBox= qobject_cast<const ctkCollapsibleGroupBox*>(widget);
  53. if (groupBox)
  54. {
  55. return groupBox->fontMetrics().height();
  56. }
  57. }
  58. return this->Superclass::pixelMetric(metric, option, widget);
  59. }
  60. };
  61. #endif
  62. //-----------------------------------------------------------------------------
  63. class ctkCollapsibleGroupBoxPrivate
  64. {
  65. Q_DECLARE_PUBLIC(ctkCollapsibleGroupBox);
  66. protected:
  67. ctkCollapsibleGroupBox* const q_ptr;
  68. public:
  69. ctkCollapsibleGroupBoxPrivate(ctkCollapsibleGroupBox& object);
  70. void init();
  71. void setChildVisibility(QWidget* childWidget);
  72. /// Size of the widget for collapsing
  73. QSize OldSize;
  74. /// Maximum allowed height
  75. int MaxHeight;
  76. int CollapsedHeight;
  77. /// We change the visibility of the chidren in setChildrenVisibility
  78. /// and we track when the visibility is changed to force it back to possibly
  79. /// force the child to be hidden. To prevent infinite loop we need to know
  80. /// who is changing children's visibility.
  81. bool ForcingVisibility;
  82. /// Sometimes the creation of the widget is not done inside setVisible,
  83. /// as we need to do special processing the first time the button is
  84. /// setVisible, we track its created state with the variable
  85. bool IsStateCreated;
  86. #if QT_VERSION >= 0x040600
  87. /// Pointer to keep track of the proxy style
  88. ctkCollapsibleGroupBoxStyle* GroupBoxStyle;
  89. #endif
  90. };
  91. //-----------------------------------------------------------------------------
  92. ctkCollapsibleGroupBoxPrivate::ctkCollapsibleGroupBoxPrivate(
  93. ctkCollapsibleGroupBox& object)
  94. :q_ptr(&object)
  95. {
  96. this->ForcingVisibility = false;
  97. this->IsStateCreated = false;
  98. this->MaxHeight = 0;
  99. this->CollapsedHeight = 14;
  100. #if QT_VERSION >= 0x040600
  101. this->GroupBoxStyle = 0;
  102. #endif
  103. }
  104. //-----------------------------------------------------------------------------
  105. void ctkCollapsibleGroupBoxPrivate::init()
  106. {
  107. Q_Q(ctkCollapsibleGroupBox);
  108. q->setCheckable(true);
  109. QObject::connect(q, SIGNAL(toggled(bool)), q, SLOT(expand(bool)));
  110. this->MaxHeight = q->maximumHeight();
  111. #if QT_VERSION >= 0x040600
  112. QWidget* parent = q->parentWidget();
  113. QStyle* parentStyle = (parent) ? parent->style() : qApp->style();
  114. this->GroupBoxStyle = new ctkCollapsibleGroupBoxStyle(parentStyle, qApp);
  115. q->setStyle(this->GroupBoxStyle);
  116. this->GroupBoxStyle->ensureBaseStyle();
  117. #else
  118. this->setStyleSheet(
  119. "ctkCollapsibleGroupBox::indicator:checked{"
  120. "image: url(:/Icons/expand-up.png);}"
  121. "ctkCollapsibleGroupBox::indicator:unchecked{"
  122. "image: url(:/Icons/expand-down.png);}");
  123. #endif
  124. }
  125. //-----------------------------------------------------------------------------
  126. void ctkCollapsibleGroupBoxPrivate::setChildVisibility(QWidget* childWidget)
  127. {
  128. Q_Q(ctkCollapsibleGroupBox);
  129. // Don't hide children while the widget is not yet created (before show() is
  130. // called). If we hide them (but don't set ExplicitShowHide), they would be
  131. // shown anyway when they will be created (because ExplicitShowHide is not set).
  132. // If we set ExplicitShowHide, then calling setVisible(false) on them would
  133. // be a no (because they are already hidden and ExplicitShowHide is set).
  134. // So we don't hide/show the children until the widget is created.
  135. if (!q->testAttribute(Qt::WA_WState_Created))
  136. {
  137. return;
  138. }
  139. this->ForcingVisibility = true;
  140. bool visible= !q->collapsed();
  141. // if the widget has been explicity hidden, then hide it.
  142. if (childWidget->property("visibilityToParent").isValid()
  143. && !childWidget->property("visibilityToParent").toBool())
  144. {
  145. visible = false;
  146. }
  147. childWidget->setVisible(visible);
  148. // setVisible() has set the ExplicitShowHide flag, restore it as we don't want
  149. // to make it like it was an explicit visible set because we want
  150. // to allow the children to be explicitly hidden by the user.
  151. if ((!childWidget->property("visibilityToParent").isValid() ||
  152. childWidget->property("visibilityToParent").toBool()))
  153. {
  154. childWidget->setAttribute(Qt::WA_WState_ExplicitShowHide, false);
  155. }
  156. this->ForcingVisibility = false;
  157. }
  158. //-----------------------------------------------------------------------------
  159. ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(QWidget* _parent)
  160. :QGroupBox(_parent)
  161. , d_ptr(new ctkCollapsibleGroupBoxPrivate(*this))
  162. {
  163. Q_D(ctkCollapsibleGroupBox);
  164. d->init();
  165. }
  166. //-----------------------------------------------------------------------------
  167. ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(const QString& title, QWidget* _parent)
  168. :QGroupBox(title, _parent)
  169. , d_ptr(new ctkCollapsibleGroupBoxPrivate(*this))
  170. {
  171. Q_D(ctkCollapsibleGroupBox);
  172. d->init();
  173. }
  174. //-----------------------------------------------------------------------------
  175. ctkCollapsibleGroupBox::~ctkCollapsibleGroupBox()
  176. {
  177. }
  178. //-----------------------------------------------------------------------------
  179. void ctkCollapsibleGroupBox::setCollapsedHeight(int heightInPixels)
  180. {
  181. Q_D(ctkCollapsibleGroupBox);
  182. d->CollapsedHeight = heightInPixels;
  183. }
  184. //-----------------------------------------------------------------------------
  185. int ctkCollapsibleGroupBox::collapsedHeight()const
  186. {
  187. Q_D(const ctkCollapsibleGroupBox);
  188. return d->CollapsedHeight;
  189. }
  190. //-----------------------------------------------------------------------------
  191. void ctkCollapsibleGroupBox::expand(bool _expand)
  192. {
  193. Q_D(ctkCollapsibleGroupBox);
  194. if (!_expand)
  195. {
  196. d->OldSize = this->size();
  197. }
  198. // Update the visibility of all the children
  199. // We can't use findChildren as it would return the grandchildren
  200. foreach(QObject* childObject, this->children())
  201. {
  202. if (childObject->isWidgetType())
  203. {
  204. d->setChildVisibility(qobject_cast<QWidget*>(childObject));
  205. }
  206. }
  207. if (_expand)
  208. {
  209. this->setMaximumHeight(d->MaxHeight);
  210. this->resize(d->OldSize);
  211. }
  212. else
  213. {
  214. d->MaxHeight = this->maximumHeight();
  215. QStyleOptionGroupBox option;
  216. this->initStyleOption(&option);
  217. QRect labelRect = this->style()->subControlRect(
  218. QStyle::CC_GroupBox, &option, QStyle::SC_GroupBoxLabel, this);
  219. this->setMaximumHeight(labelRect.height() + d->CollapsedHeight);
  220. }
  221. }
  222. #if QT_VERSION < 0x040600
  223. //-----------------------------------------------------------------------------
  224. void ctkCollapsibleGroupBox::paintEvent(QPaintEvent* e)
  225. {
  226. this->QGroupBox::paintEvent(e);
  227. QStylePainter paint(this);
  228. QStyleOptionGroupBox option;
  229. initStyleOption(&option);
  230. option.activeSubControls &= ~QStyle::SC_GroupBoxCheckBox;
  231. paint.drawComplexControl(QStyle::CC_GroupBox, option);
  232. }
  233. //-----------------------------------------------------------------------------
  234. void ctkCollapsibleGroupBox::mousePressEvent(QMouseEvent *event)
  235. {
  236. if (event->button() != Qt::LeftButton) {
  237. event->ignore();
  238. return;
  239. }
  240. // no animation
  241. }
  242. //-----------------------------------------------------------------------------
  243. void ctkCollapsibleGroupBox::mouseReleaseEvent(QMouseEvent *event)
  244. {
  245. if (event->button() != Qt::LeftButton) {
  246. event->ignore();
  247. return;
  248. }
  249. QStyleOptionGroupBox box;
  250. initStyleOption(&box);
  251. box.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
  252. QStyle::SubControl released = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
  253. event->pos(), this);
  254. bool toggle = this->isCheckable() && (released == QStyle::SC_GroupBoxLabel
  255. || released == QStyle::SC_GroupBoxCheckBox);
  256. if (toggle)
  257. {
  258. this->setChecked(!this->isChecked());
  259. }
  260. }
  261. #endif
  262. //-----------------------------------------------------------------------------
  263. void ctkCollapsibleGroupBox::childEvent(QChildEvent* c)
  264. {
  265. Q_D(ctkCollapsibleGroupBox);
  266. QObject* child = c->child();
  267. if (c && c->type() == QEvent::ChildAdded &&
  268. child && child->isWidgetType())
  269. {
  270. QWidget *childWidget = qobject_cast<QWidget*>(c->child());
  271. // Handle the case where the child has already it's visibility set before
  272. // being added to the widget
  273. if (childWidget->testAttribute(Qt::WA_WState_ExplicitShowHide) &&
  274. childWidget->testAttribute(Qt::WA_WState_Hidden))
  275. {
  276. // if the widget has explicitly set to hidden, then mark it as such
  277. childWidget->setProperty("visibilityToParent", false);
  278. }
  279. // We want to catch all the child's Show/Hide events.
  280. child->installEventFilter(this);
  281. // If the child is added while ctkCollapsibleButton is collapsed, then we
  282. // need to hide the child.
  283. d->setChildVisibility(childWidget);
  284. }
  285. this->QGroupBox::childEvent(c);
  286. }
  287. //-----------------------------------------------------------------------------
  288. void ctkCollapsibleGroupBox::setVisible(bool show)
  289. {
  290. Q_D(ctkCollapsibleGroupBox);
  291. // calling QWidget::setVisible() on ctkCollapsibleGroupBox will eventually
  292. // call QWidget::showChildren() or hideChildren() which will generate
  293. // ShowToParent/HideToParent events but we want to ignore that case in
  294. // eventFilter().
  295. d->ForcingVisibility = true;
  296. this->QGroupBox::setVisible(show);
  297. d->ForcingVisibility = false;
  298. // We have been ignoring setChildVisibility() while the collapsible button
  299. // is not yet created, now that it is created, ensure that the children
  300. // are correctly shown/hidden depending on their explicit visibility and
  301. // the collapsed property of the button.
  302. if (!d->IsStateCreated && this->testAttribute(Qt::WA_WState_Created))
  303. {
  304. d->IsStateCreated = true;
  305. foreach(QObject* child, this->children())
  306. {
  307. QWidget* childWidget = qobject_cast<QWidget*>(child);
  308. if (childWidget)
  309. {
  310. d->setChildVisibility(childWidget);
  311. }
  312. }
  313. }
  314. }
  315. //-----------------------------------------------------------------------------
  316. bool ctkCollapsibleGroupBox::eventFilter(QObject* child, QEvent* e)
  317. {
  318. Q_D(ctkCollapsibleGroupBox);
  319. Q_ASSERT(child && e);
  320. // Make sure the Show/QHide events are not generated by one of our
  321. // ctkCollapsibleButton function.
  322. if (d->ForcingVisibility)
  323. {
  324. return false;
  325. }
  326. // When we are here, it's because somewhere (not in ctkCollapsibleButton),
  327. // someone explicitly called setVisible() on a child widget.
  328. // If the collapsible button is collapsed/closed, then even if someone
  329. // request the widget to be visible, we force it back to be hidden because
  330. // they meant to be hidden to its parent, the collapsible button. However the
  331. // child will later be shown when the button will be expanded/opened.
  332. // On the other hand, if the user explicitly hide the child when the button
  333. // is collapsed/closed, then we want to keep it hidden next time the
  334. // collapsible button is expanded/opened.
  335. if (e->type() == QEvent::ShowToParent)
  336. {
  337. child->setProperty("visibilityToParent", true);
  338. Q_ASSERT(qobject_cast<QWidget*>(child));
  339. // force the widget to be hidden if the button is collapsed.
  340. d->setChildVisibility(qobject_cast<QWidget*>(child));
  341. }
  342. else if(e->type() == QEvent::HideToParent)
  343. {
  344. // we don't need to force the widget to be visible here.
  345. child->setProperty("visibilityToParent", false);
  346. }
  347. return this->QGroupBox::eventFilter(child, e);
  348. }