ctkCollapsibleGroupBox.cpp 13 KB

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