ctkCollapsibleGroupBox.cpp 11 KB

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