ctkCollapsibleGroupBox.cpp 13 KB

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