ctkCollapsibleGroupBox.cpp 12 KB

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