ctkCollapsibleGroupBox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 groupBox 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. // Setting Qt::WA_WState_Visible to true during child construction can have
  148. // undesirable side effects.
  149. if (childWidget->testAttribute(Qt::WA_WState_Created) ||
  150. !visible)
  151. {
  152. childWidget->setVisible(visible);
  153. }
  154. // setVisible() has set the ExplicitShowHide flag, restore it as we don't want
  155. // to make it like it was an explicit visible set because we want
  156. // to allow any children to be explicitly hidden by the user.
  157. if ((!childWidget->property("visibilityToParent").isValid() ||
  158. childWidget->property("visibilityToParent").toBool()))
  159. {
  160. childWidget->setAttribute(Qt::WA_WState_ExplicitShowHide, false);
  161. }
  162. this->ForcingVisibility = false;
  163. }
  164. //-----------------------------------------------------------------------------
  165. ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(QWidget* _parent)
  166. :QGroupBox(_parent)
  167. , d_ptr(new ctkCollapsibleGroupBoxPrivate(*this))
  168. {
  169. Q_D(ctkCollapsibleGroupBox);
  170. d->init();
  171. }
  172. //-----------------------------------------------------------------------------
  173. ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(const QString& title, QWidget* _parent)
  174. :QGroupBox(title, _parent)
  175. , d_ptr(new ctkCollapsibleGroupBoxPrivate(*this))
  176. {
  177. Q_D(ctkCollapsibleGroupBox);
  178. d->init();
  179. }
  180. //-----------------------------------------------------------------------------
  181. ctkCollapsibleGroupBox::~ctkCollapsibleGroupBox()
  182. {
  183. }
  184. //-----------------------------------------------------------------------------
  185. void ctkCollapsibleGroupBox::setCollapsedHeight(int heightInPixels)
  186. {
  187. Q_D(ctkCollapsibleGroupBox);
  188. d->CollapsedHeight = heightInPixels;
  189. }
  190. //-----------------------------------------------------------------------------
  191. int ctkCollapsibleGroupBox::collapsedHeight()const
  192. {
  193. Q_D(const ctkCollapsibleGroupBox);
  194. return d->CollapsedHeight;
  195. }
  196. //-----------------------------------------------------------------------------
  197. void ctkCollapsibleGroupBox::expand(bool _expand)
  198. {
  199. Q_D(ctkCollapsibleGroupBox);
  200. if (!_expand)
  201. {
  202. d->OldSize = this->size();
  203. }
  204. // Update the visibility of all the children
  205. // We can't use findChildren as it would return the grandchildren
  206. foreach(QObject* childObject, this->children())
  207. {
  208. if (childObject->isWidgetType())
  209. {
  210. d->setChildVisibility(qobject_cast<QWidget*>(childObject));
  211. }
  212. }
  213. if (_expand)
  214. {
  215. this->setMaximumHeight(d->MaxHeight);
  216. this->resize(d->OldSize);
  217. }
  218. else
  219. {
  220. d->MaxHeight = this->maximumHeight();
  221. QStyleOptionGroupBox option;
  222. this->initStyleOption(&option);
  223. QRect labelRect = this->style()->subControlRect(
  224. QStyle::CC_GroupBox, &option, QStyle::SC_GroupBoxLabel, this);
  225. this->setMaximumHeight(labelRect.height() + d->CollapsedHeight);
  226. }
  227. }
  228. #if QT_VERSION < 0x040600
  229. //-----------------------------------------------------------------------------
  230. void ctkCollapsibleGroupBox::paintEvent(QPaintEvent* e)
  231. {
  232. this->QGroupBox::paintEvent(e);
  233. QStylePainter paint(this);
  234. QStyleOptionGroupBox option;
  235. initStyleOption(&option);
  236. option.activeSubControls &= ~QStyle::SC_GroupBoxCheckBox;
  237. paint.drawComplexControl(QStyle::CC_GroupBox, option);
  238. }
  239. //-----------------------------------------------------------------------------
  240. void ctkCollapsibleGroupBox::mousePressEvent(QMouseEvent *event)
  241. {
  242. if (event->button() != Qt::LeftButton) {
  243. event->ignore();
  244. return;
  245. }
  246. // no animation
  247. }
  248. //-----------------------------------------------------------------------------
  249. void ctkCollapsibleGroupBox::mouseReleaseEvent(QMouseEvent *event)
  250. {
  251. if (event->button() != Qt::LeftButton) {
  252. event->ignore();
  253. return;
  254. }
  255. QStyleOptionGroupBox box;
  256. initStyleOption(&box);
  257. box.activeSubControls &= !QStyle::SC_GroupBoxCheckBox;
  258. QStyle::SubControl released = style()->hitTestComplexControl(QStyle::CC_GroupBox, &box,
  259. event->pos(), this);
  260. bool toggle = this->isCheckable() && (released == QStyle::SC_GroupBoxLabel
  261. || released == QStyle::SC_GroupBoxCheckBox);
  262. if (toggle)
  263. {
  264. this->setChecked(!this->isChecked());
  265. }
  266. }
  267. #endif
  268. //-----------------------------------------------------------------------------
  269. void ctkCollapsibleGroupBox::childEvent(QChildEvent* c)
  270. {
  271. Q_D(ctkCollapsibleGroupBox);
  272. QObject* child = c->child();
  273. if (c && c->type() == QEvent::ChildAdded &&
  274. child && child->isWidgetType())
  275. {
  276. QWidget *childWidget = qobject_cast<QWidget*>(c->child());
  277. // Handle the case where the child has already it's visibility set before
  278. // being added to the widget
  279. if (childWidget->testAttribute(Qt::WA_WState_ExplicitShowHide) &&
  280. childWidget->testAttribute(Qt::WA_WState_Hidden))
  281. {
  282. // if the widget has explicitly set to hidden, then mark it as such
  283. childWidget->setProperty("visibilityToParent", false);
  284. }
  285. // We want to catch all the child's Show/Hide events.
  286. child->installEventFilter(this);
  287. // If the child is added while ctkCollapsibleButton is collapsed, then we
  288. // need to hide the child.
  289. d->setChildVisibility(childWidget);
  290. }
  291. this->QGroupBox::childEvent(c);
  292. }
  293. //-----------------------------------------------------------------------------
  294. void ctkCollapsibleGroupBox::setVisible(bool show)
  295. {
  296. Q_D(ctkCollapsibleGroupBox);
  297. // calling QWidget::setVisible() on ctkCollapsibleGroupBox will eventually
  298. // call QWidget::showChildren() or hideChildren() which will generate
  299. // ShowToParent/HideToParent events but we want to ignore that case in
  300. // eventFilter().
  301. d->ForcingVisibility = true;
  302. this->QGroupBox::setVisible(show);
  303. d->ForcingVisibility = false;
  304. // We have been ignoring setChildVisibility() while the collapsible button
  305. // is not yet created, now that it is created, ensure that the children
  306. // are correctly shown/hidden depending on their explicit visibility and
  307. // the collapsed property of the button.
  308. if (!d->IsStateCreated && this->testAttribute(Qt::WA_WState_Created))
  309. {
  310. d->IsStateCreated = true;
  311. foreach(QObject* child, this->children())
  312. {
  313. QWidget* childWidget = qobject_cast<QWidget*>(child);
  314. if (childWidget)
  315. {
  316. d->setChildVisibility(childWidget);
  317. }
  318. }
  319. }
  320. }
  321. //-----------------------------------------------------------------------------
  322. bool ctkCollapsibleGroupBox::eventFilter(QObject* child, QEvent* e)
  323. {
  324. Q_D(ctkCollapsibleGroupBox);
  325. Q_ASSERT(child && e);
  326. // Make sure the Show/QHide events are not generated by one of our
  327. // ctkCollapsibleButton function.
  328. if (d->ForcingVisibility)
  329. {
  330. return false;
  331. }
  332. // When we are here, it's because somewhere (not in ctkCollapsibleButton),
  333. // someone explicitly called setVisible() on a child widget.
  334. // If the collapsible button is collapsed/closed, then even if someone
  335. // request the widget to be visible, we force it back to be hidden because
  336. // they meant to be hidden to its parent, the collapsible button. However the
  337. // child will later be shown when the button will be expanded/opened.
  338. // On the other hand, if the user explicitly hide the child when the button
  339. // is collapsed/closed, then we want to keep it hidden next time the
  340. // collapsible button is expanded/opened.
  341. if (e->type() == QEvent::ShowToParent)
  342. {
  343. child->setProperty("visibilityToParent", true);
  344. Q_ASSERT(qobject_cast<QWidget*>(child));
  345. // force the widget to be hidden if the button is collapsed.
  346. d->setChildVisibility(qobject_cast<QWidget*>(child));
  347. }
  348. else if(e->type() == QEvent::HideToParent)
  349. {
  350. // we don't need to force the widget to be visible here.
  351. child->setProperty("visibilityToParent", false);
  352. }
  353. return this->QGroupBox::eventFilter(child, e);
  354. }