ctkCollapsibleButton.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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.commontk.org/LICENSE
  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 <QCleanlooksStyle>
  17. #include <QDebug>
  18. #include <QLayout>
  19. #include <QMouseEvent>
  20. #include <QPainter>
  21. #include <QPushButton>
  22. #include <QStyle>
  23. #include <QStyleOptionButton>
  24. #include <QStyleOptionFrameV3>
  25. // CTK includes
  26. #include "ctkCollapsibleButton.h"
  27. //-----------------------------------------------------------------------------
  28. class ctkCollapsibleButtonPrivate
  29. {
  30. Q_DECLARE_PUBLIC(ctkCollapsibleButton);
  31. protected:
  32. ctkCollapsibleButton* const q_ptr;
  33. public:
  34. ctkCollapsibleButtonPrivate(ctkCollapsibleButton& object);
  35. void init();
  36. void setChildVisibility(QWidget* childWidget);
  37. bool Collapsed;
  38. // Contents frame
  39. QFrame::Shape ContentsFrameShape;
  40. QFrame::Shadow ContentsFrameShadow;
  41. int ContentsLineWidth;
  42. int ContentsMidLineWidth;
  43. int CollapsedHeight;
  44. bool ExclusiveMouseOver;
  45. bool LookOffWhenChecked;
  46. bool ForcingVisibility;
  47. int MaximumHeight; // use carefully
  48. // Tuning of the button look&feel
  49. Qt::Alignment TextAlignment;
  50. Qt::Alignment IndicatorAlignment;
  51. };
  52. //-----------------------------------------------------------------------------
  53. ctkCollapsibleButtonPrivate::ctkCollapsibleButtonPrivate(ctkCollapsibleButton& object)
  54. :q_ptr(&object)
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkCollapsibleButtonPrivate::init()
  59. {
  60. Q_Q(ctkCollapsibleButton);
  61. q->setCheckable(true);
  62. // checked and Collapsed are synchronized: checked != Collapsed
  63. q->setChecked(true);
  64. this->Collapsed = false;
  65. this->ContentsFrameShape = QFrame::NoFrame;
  66. this->ContentsFrameShadow = QFrame::Plain;
  67. this->ContentsLineWidth = 1;
  68. this->ContentsMidLineWidth = 0;
  69. this->CollapsedHeight = 10;
  70. this->ExclusiveMouseOver = false;
  71. this->LookOffWhenChecked = true; // set as a prop ?
  72. this->MaximumHeight = q->maximumHeight();
  73. this->TextAlignment = Qt::AlignLeft | Qt::AlignVCenter;
  74. this->IndicatorAlignment = Qt::AlignLeft | Qt::AlignVCenter;
  75. q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,
  76. QSizePolicy::Preferred,
  77. QSizePolicy::DefaultType));
  78. q->setContentsMargins(0, q->buttonSizeHint().height(),0 , 0);
  79. // by default QAbstractButton changed the background role to Button
  80. // we want a regular background
  81. q->setBackgroundRole(QPalette::Window);
  82. QObject::connect(q, SIGNAL(toggled(bool)),
  83. q, SLOT(onToggled(bool)));
  84. }
  85. //-----------------------------------------------------------------------------
  86. void ctkCollapsibleButtonPrivate::setChildVisibility(QWidget* childWidget)
  87. {
  88. this->ForcingVisibility = true;
  89. bool visible= !this->Collapsed &&
  90. (childWidget->property("visibilityToParent").isValid() ?
  91. childWidget->property("visibilityToParent").toBool() : true);
  92. childWidget->setVisible(visible);
  93. // restore the flag as we don't want to make it like it is an explicit visible set.
  94. if (!childWidget->property("visibilityToParent").isValid() ||
  95. childWidget->property("visibilityToParent").toBool())
  96. {
  97. childWidget->setAttribute(Qt::WA_WState_ExplicitShowHide, false);
  98. }
  99. this->ForcingVisibility = false;
  100. }
  101. //-----------------------------------------------------------------------------
  102. void ctkCollapsibleButton::initStyleOption(QStyleOptionButton* option)const
  103. {
  104. Q_D(const ctkCollapsibleButton);
  105. if (option == 0)
  106. {
  107. return;
  108. }
  109. option->initFrom(this);
  110. if (this->isDown() )
  111. {
  112. option->state |= QStyle::State_Sunken;
  113. }
  114. if (this->isChecked() && !d->LookOffWhenChecked)
  115. {
  116. option->state |= QStyle::State_On;
  117. }
  118. if (!this->isDown())
  119. {
  120. option->state |= QStyle::State_Raised;
  121. }
  122. option->text = this->text();
  123. option->icon = this->icon();
  124. option->iconSize = QSize(this->style()->pixelMetric(QStyle::PM_IndicatorWidth, option, this),
  125. this->style()->pixelMetric(QStyle::PM_IndicatorHeight, option, this));
  126. int buttonHeight = this->buttonSizeHint().height();//qMax(this->fontMetrics().height(), option->iconSize.height());
  127. option->rect.setHeight(buttonHeight);
  128. }
  129. //-----------------------------------------------------------------------------
  130. ctkCollapsibleButton::ctkCollapsibleButton(QWidget* _parent)
  131. :QAbstractButton(_parent)
  132. , d_ptr(new ctkCollapsibleButtonPrivate(*this))
  133. {
  134. Q_D(ctkCollapsibleButton);
  135. d->init();
  136. }
  137. //-----------------------------------------------------------------------------
  138. ctkCollapsibleButton::ctkCollapsibleButton(const QString& title, QWidget* _parent)
  139. :QAbstractButton(_parent)
  140. , d_ptr(new ctkCollapsibleButtonPrivate(*this))
  141. {
  142. Q_D(ctkCollapsibleButton);
  143. d->init();
  144. this->setText(title);
  145. }
  146. //-----------------------------------------------------------------------------
  147. ctkCollapsibleButton::~ctkCollapsibleButton()
  148. {
  149. }
  150. //-----------------------------------------------------------------------------
  151. void ctkCollapsibleButton::setCollapsed(bool c)
  152. {
  153. if (!this->isCheckable())
  154. {
  155. // not sure if one should handle this case...
  156. this->collapse(c);
  157. return;
  158. }
  159. this->setChecked(!c);
  160. }
  161. //-----------------------------------------------------------------------------
  162. bool ctkCollapsibleButton::collapsed()const
  163. {
  164. Q_D(const ctkCollapsibleButton);
  165. return d->Collapsed;
  166. }
  167. //-----------------------------------------------------------------------------
  168. void ctkCollapsibleButton::setCollapsedHeight(int h)
  169. {
  170. Q_D(ctkCollapsibleButton);
  171. d->CollapsedHeight = h;
  172. this->updateGeometry();
  173. }
  174. //-----------------------------------------------------------------------------
  175. int ctkCollapsibleButton::collapsedHeight()const
  176. {
  177. Q_D(const ctkCollapsibleButton);
  178. return d->CollapsedHeight;
  179. }
  180. //-----------------------------------------------------------------------------
  181. void ctkCollapsibleButton::onToggled(bool checked)
  182. {
  183. if (this->isCheckable())
  184. {
  185. this->collapse(!checked);
  186. }
  187. }
  188. //-----------------------------------------------------------------------------
  189. void ctkCollapsibleButton::collapse(bool collapsed)
  190. {
  191. Q_D(ctkCollapsibleButton);
  192. if (collapsed == d->Collapsed)
  193. {
  194. return;
  195. }
  196. d->Collapsed = collapsed;
  197. // we do that here as setVisible calls will correctly refresh the widget
  198. if (collapsed)
  199. {
  200. d->MaximumHeight = this->maximumHeight();
  201. this->setMaximumHeight(this->sizeHint().height());
  202. //this->updateGeometry();
  203. }
  204. else
  205. {
  206. // restore maximumheight
  207. this->setMaximumHeight(d->MaximumHeight);
  208. this->updateGeometry();
  209. }
  210. // update the visibility of all the children
  211. foreach(QWidget* child, this->findChildren<QWidget*>())
  212. {
  213. d->setChildVisibility(child);
  214. }
  215. // this might be too many updates...
  216. QWidget* _parent = this->parentWidget();
  217. if (!d->Collapsed && (!_parent || !_parent->layout()))
  218. {
  219. this->resize(this->sizeHint());
  220. }
  221. else
  222. {
  223. this->updateGeometry();
  224. }
  225. //this->update(QRect(QPoint(0,0), this->sizeHint()));
  226. //this->repaint(QRect(QPoint(0,0), this->sizeHint()));
  227. emit contentsCollapsed(collapsed);
  228. }
  229. //-----------------------------------------------------------------------------
  230. QFrame::Shape ctkCollapsibleButton::contentsFrameShape() const
  231. {
  232. Q_D(const ctkCollapsibleButton);
  233. return d->ContentsFrameShape;
  234. }
  235. //-----------------------------------------------------------------------------
  236. void ctkCollapsibleButton::setContentsFrameShape(QFrame::Shape s)
  237. {
  238. Q_D(ctkCollapsibleButton);
  239. d->ContentsFrameShape = s;
  240. }
  241. //-----------------------------------------------------------------------------
  242. QFrame::Shadow ctkCollapsibleButton::contentsFrameShadow() const
  243. {
  244. Q_D(const ctkCollapsibleButton);
  245. return d->ContentsFrameShadow;
  246. }
  247. //-----------------------------------------------------------------------------
  248. void ctkCollapsibleButton::setContentsFrameShadow(QFrame::Shadow s)
  249. {
  250. Q_D(ctkCollapsibleButton);
  251. d->ContentsFrameShadow = s;
  252. }
  253. //-----------------------------------------------------------------------------
  254. int ctkCollapsibleButton:: contentsLineWidth() const
  255. {
  256. Q_D(const ctkCollapsibleButton);
  257. return d->ContentsLineWidth;
  258. }
  259. //-----------------------------------------------------------------------------
  260. void ctkCollapsibleButton::setContentsLineWidth(int w)
  261. {
  262. Q_D(ctkCollapsibleButton);
  263. d->ContentsLineWidth = w;
  264. }
  265. //-----------------------------------------------------------------------------
  266. int ctkCollapsibleButton::contentsMidLineWidth() const
  267. {
  268. Q_D(const ctkCollapsibleButton);
  269. return d->ContentsMidLineWidth;
  270. }
  271. //-----------------------------------------------------------------------------
  272. void ctkCollapsibleButton::setContentsMidLineWidth(int w)
  273. {
  274. Q_D(ctkCollapsibleButton);
  275. d->ContentsMidLineWidth = w;
  276. }
  277. //-----------------------------------------------------------------------------
  278. void ctkCollapsibleButton::setButtonTextAlignment(Qt::Alignment textAlignment)
  279. {
  280. Q_D(ctkCollapsibleButton);
  281. d->TextAlignment = textAlignment;
  282. this->update();
  283. }
  284. //-----------------------------------------------------------------------------
  285. Qt::Alignment ctkCollapsibleButton::buttonTextAlignment()const
  286. {
  287. Q_D(const ctkCollapsibleButton);
  288. return d->TextAlignment;
  289. }
  290. //-----------------------------------------------------------------------------
  291. void ctkCollapsibleButton::setIndicatorAlignment(Qt::Alignment indicatorAlignment)
  292. {
  293. Q_D(ctkCollapsibleButton);
  294. d->IndicatorAlignment = indicatorAlignment;
  295. this->update();
  296. }
  297. //-----------------------------------------------------------------------------
  298. Qt::Alignment ctkCollapsibleButton::indicatorAlignment()const
  299. {
  300. Q_D(const ctkCollapsibleButton);
  301. return d->IndicatorAlignment;
  302. }
  303. //-----------------------------------------------------------------------------
  304. QSize ctkCollapsibleButton::buttonSizeHint()const
  305. {
  306. int w = 0, h = 0;
  307. QStyleOptionButton opt;
  308. opt.initFrom(this);
  309. // indicator
  310. QSize indicatorSize = QSize(style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, this),
  311. style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, this));
  312. int indicatorSpacing = style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, this);
  313. int ih = indicatorSize.height();
  314. int iw = indicatorSize.width() + indicatorSpacing;
  315. w += iw;
  316. h = qMax(h, ih);
  317. // text
  318. QString string(this->text());
  319. bool empty = string.isEmpty();
  320. if (empty)
  321. {
  322. string = QString::fromLatin1("XXXX");
  323. }
  324. QFontMetrics fm = this->fontMetrics();
  325. QSize sz = fm.size(Qt::TextShowMnemonic, string);
  326. if(!empty || !w)
  327. {
  328. w += sz.width();
  329. }
  330. h = qMax(h, sz.height());
  331. //opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height
  332. QSize buttonSize = (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).
  333. expandedTo(QApplication::globalStrut()));
  334. return buttonSize;
  335. }
  336. //-----------------------------------------------------------------------------
  337. QSize ctkCollapsibleButton::minimumSizeHint()const
  338. {
  339. Q_D(const ctkCollapsibleButton);
  340. QSize buttonSize = this->buttonSizeHint();
  341. if (d->Collapsed)
  342. {
  343. return buttonSize + QSize(0,d->CollapsedHeight);
  344. }
  345. // open
  346. if (this->layout() == 0)
  347. {// no layout, means the button is empty ?
  348. return buttonSize;
  349. }
  350. QSize s = this->QAbstractButton::minimumSizeHint();
  351. return s.expandedTo(buttonSize);
  352. }
  353. //-----------------------------------------------------------------------------
  354. QSize ctkCollapsibleButton::sizeHint()const
  355. {
  356. Q_D(const ctkCollapsibleButton);
  357. QSize buttonSize = this->buttonSizeHint();
  358. if (d->Collapsed)
  359. {
  360. return buttonSize + QSize(0,d->CollapsedHeight);
  361. }
  362. // open
  363. // QAbstractButton works well only if a layout is set
  364. QSize s = this->QAbstractButton::sizeHint();
  365. return s.expandedTo(buttonSize + QSize(0, d->CollapsedHeight));
  366. }
  367. //-----------------------------------------------------------------------------
  368. void ctkCollapsibleButton::paintEvent(QPaintEvent * _event)
  369. {
  370. Q_D(ctkCollapsibleButton);
  371. QPainter p(this);
  372. // Draw Button
  373. QStyleOptionButton opt;
  374. this->initStyleOption(&opt);
  375. // We don't want to have the highlight effect on the button when mouse is
  376. // over a child. We want the highlight effect only when the mouse is just
  377. // over itself.
  378. // same as this->underMouse()
  379. bool exclusiveMouseOver = false;
  380. if (opt.state & QStyle::State_MouseOver)
  381. {
  382. QRect buttonRect = opt.rect;
  383. QList<QWidget*> _children = this->findChildren<QWidget*>();
  384. QList<QWidget*>::ConstIterator it;
  385. for (it = _children.constBegin(); it != _children.constEnd(); ++it )
  386. {
  387. if ((*it)->underMouse())
  388. {
  389. // the mouse has been moved from the collapsible button to one
  390. // of its children. The paint event rect is the child rect, this
  391. // is why we have to request another paint event to redraw the
  392. // button to remove the highlight effect.
  393. if (!_event->rect().contains(buttonRect))
  394. {// repaint the button rect.
  395. this->update(buttonRect);
  396. }
  397. opt.state &= ~QStyle::State_MouseOver;
  398. exclusiveMouseOver = true;
  399. break;
  400. }
  401. }
  402. if (d->ExclusiveMouseOver && !exclusiveMouseOver)
  403. {
  404. // the mouse is over the widget, but not over the children. As it
  405. // has been de-highlighted in the past, we should refresh the button
  406. // rect to re-highlight the button.
  407. if (!_event->rect().contains(buttonRect))
  408. {// repaint the button rect.
  409. this->update(buttonRect);
  410. }
  411. }
  412. }
  413. d->ExclusiveMouseOver = exclusiveMouseOver;
  414. QSize indicatorSize = QSize(style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, this),
  415. style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, this));
  416. opt.iconSize = indicatorSize;
  417. style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &p, this);
  418. // TBD is PE_PanelButtonCommand better ?
  419. //style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &opt, &p, this);
  420. int buttonHeight = opt.rect.height();
  421. uint tf = d->TextAlignment;
  422. if (this->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, this))
  423. {
  424. tf |= Qt::TextShowMnemonic;
  425. }
  426. else
  427. {
  428. tf |= Qt::TextHideMnemonic;
  429. }
  430. int textWidth = opt.fontMetrics.boundingRect(opt.rect, tf, opt.text).width();
  431. int indicatorSpacing = this->style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, this);
  432. int buttonMargin = this->style()->pixelMetric(QStyle::PM_ButtonMargin, &opt, this);
  433. // Draw Indicator
  434. QStyleOption indicatorOpt;
  435. indicatorOpt.init(this);
  436. if (d->IndicatorAlignment & Qt::AlignLeft)
  437. {
  438. indicatorOpt.rect = QRect((buttonHeight - indicatorSize.width()) / 2,
  439. (buttonHeight - indicatorSize.height()) / 2,
  440. indicatorSize.width(), indicatorSize.height());
  441. }
  442. else if (d->IndicatorAlignment & Qt::AlignHCenter)
  443. {
  444. int w = indicatorSize.width();
  445. if (!opt.text.isEmpty() && (d->TextAlignment & Qt::AlignHCenter))
  446. {
  447. w += textWidth + indicatorSpacing;
  448. }
  449. indicatorOpt.rect = QRect(opt.rect.x()+ opt.rect.width() /2 - w / 2,
  450. (buttonHeight - indicatorSize.height()) / 2,
  451. indicatorSize.width(), indicatorSize.height());
  452. if (d->TextAlignment & Qt::AlignLeft &&
  453. indicatorOpt.rect.left() < opt.rect.x() + buttonMargin + textWidth)
  454. {
  455. indicatorOpt.rect.moveLeft(opt.rect.x() + buttonMargin + textWidth);
  456. }
  457. else if (d->TextAlignment & Qt::AlignRight &&
  458. indicatorOpt.rect.right() > opt.rect.right() - buttonMargin - textWidth)
  459. {
  460. indicatorOpt.rect.moveRight(opt.rect.right() - buttonMargin - textWidth);
  461. }
  462. }
  463. else if (d->IndicatorAlignment & Qt::AlignRight)
  464. {
  465. indicatorOpt.rect = QRect(opt.rect.width() - (buttonHeight - indicatorSize.width()) / 2
  466. - indicatorSize.width(),
  467. (buttonHeight - indicatorSize.height()) / 2,
  468. indicatorSize.width(), indicatorSize.height());
  469. }
  470. if (d->Collapsed)
  471. {
  472. style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &indicatorOpt, &p, this);
  473. }
  474. else
  475. {
  476. style()->drawPrimitive(QStyle::PE_IndicatorArrowUp, &indicatorOpt, &p, this);
  477. }
  478. // Draw Text
  479. if (d->TextAlignment & Qt::AlignLeft)
  480. {
  481. if (d->IndicatorAlignment & Qt::AlignLeft)
  482. {
  483. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  484. }
  485. else
  486. {
  487. opt.rect.setLeft(opt.rect.x() + buttonMargin);
  488. }
  489. }
  490. else if (d->TextAlignment & Qt::AlignHCenter)
  491. {
  492. if (d->IndicatorAlignment & Qt::AlignHCenter)
  493. {
  494. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  495. }
  496. else
  497. {
  498. opt.rect.setLeft(opt.rect.x() + opt.rect.width() / 2 - textWidth / 2);
  499. if (d->IndicatorAlignment & Qt::AlignLeft)
  500. {
  501. opt.rect.setLeft( qMax(indicatorOpt.rect.right() + indicatorSpacing, opt.rect.left()) );
  502. }
  503. }
  504. }
  505. else if (d->TextAlignment & Qt::AlignRight)
  506. {
  507. if (d->IndicatorAlignment & Qt::AlignRight)
  508. {
  509. opt.rect.setLeft(indicatorOpt.rect.left() - indicatorSpacing - textWidth);
  510. }
  511. else
  512. {
  513. opt.rect.setLeft(opt.rect.right() - buttonMargin - textWidth);
  514. }
  515. }
  516. // all the computations have been made infering the text would be left oriented
  517. tf &= ~Qt::AlignHCenter & ~Qt::AlignRight;
  518. tf |= Qt::AlignLeft;
  519. style()->drawItemText(&p, opt.rect, tf, opt.palette, (opt.state & QStyle::State_Enabled),
  520. opt.text, QPalette::ButtonText);
  521. // Draw Frame around contents
  522. QStyleOptionFrameV3 fopt;
  523. fopt.init(this);
  524. // HACK: on some styles, the frame doesn't exactly touch the button.
  525. // this is because the button has some kind of extra border.
  526. if (qobject_cast<QCleanlooksStyle*>(this->style()) != 0)
  527. {
  528. fopt.rect.setTop(buttonHeight - 1);
  529. }
  530. else
  531. {
  532. fopt.rect.setTop(buttonHeight);
  533. }
  534. fopt.frameShape = d->ContentsFrameShape;
  535. switch (d->ContentsFrameShadow)
  536. {
  537. case QFrame::Sunken:
  538. fopt.state |= QStyle::State_Sunken;
  539. break;
  540. case QFrame::Raised:
  541. fopt.state |= QStyle::State_Raised;
  542. break;
  543. default:
  544. case QFrame::Plain:
  545. break;
  546. }
  547. fopt.lineWidth = d->ContentsLineWidth;
  548. fopt.midLineWidth = d->ContentsMidLineWidth;
  549. style()->drawControl(QStyle::CE_ShapedFrame, &fopt, &p, this);
  550. }
  551. //-----------------------------------------------------------------------------
  552. bool ctkCollapsibleButton::hitButton(const QPoint & _pos)const
  553. {
  554. QStyleOptionButton opt;
  555. this->initStyleOption(&opt);
  556. return opt.rect.contains(_pos);
  557. }
  558. //-----------------------------------------------------------------------------
  559. void ctkCollapsibleButton::childEvent(QChildEvent* c)
  560. {
  561. Q_D(ctkCollapsibleButton);
  562. if (c && c->type() == QEvent::ChildAdded &&
  563. c->child() && c->child()->isWidgetType())
  564. {
  565. // We want to catch all the child's Show/Hide events.
  566. c->child()->installEventFilter(this);
  567. // If the child is added while ctkCollapsibleButton is collapsed, then we
  568. // need to hide the child.
  569. QWidget *w = static_cast<QWidget*>(c->child());
  570. d->setChildVisibility(w);
  571. }
  572. this->QWidget::childEvent(c);
  573. }
  574. //-----------------------------------------------------------------------------
  575. void ctkCollapsibleButton::setVisible(bool show)
  576. {
  577. Q_D(ctkCollapsibleButton);
  578. // calling QWidget::setVisible() on ctkCollapsibleButton will eventually
  579. // call QWidget::showChildren() or hideChildren() which will generate
  580. // ShowToParent/HideToParent events but we want to ignore that case in
  581. // eventFilter().
  582. d->ForcingVisibility = true;
  583. this->QWidget::setVisible(show);
  584. d->ForcingVisibility = false;
  585. }
  586. //-----------------------------------------------------------------------------
  587. bool ctkCollapsibleButton::eventFilter(QObject* child, QEvent* e)
  588. {
  589. Q_D(ctkCollapsibleButton);
  590. Q_ASSERT(child && e);
  591. // Make sure the Show/QHide events are not generated by one of our
  592. // ctkCollapsibleButton function.
  593. if (d->ForcingVisibility)
  594. {
  595. return false;
  596. }
  597. // When we are here, it's because somewhere (not in ctkCollapsibleButton),
  598. // someone explicitly called setVisible() on a child widget.
  599. // If the collapsible button is collapsed/closed, then even if someone
  600. // request the widget to be visible, we force it back to be hidden because
  601. // they meant to be hidden to its parent, the collapsible button. However the
  602. // child will later be shown when the button will be expanded/opened.
  603. // On the other hand, if the user explicitly hide the child when the button
  604. // is collapsed/closed, then we want to keep it hidden next time the
  605. // collapsible button is expanded/opened.
  606. if (e->type() == QEvent::ShowToParent)
  607. {
  608. child->setProperty("visibilityToParent", true);
  609. Q_ASSERT(qobject_cast<QWidget*>(child));
  610. // force the widget to be hidden if the button is collapsed.
  611. d->setChildVisibility(qobject_cast<QWidget*>(child));
  612. }
  613. else if(e->type() == QEvent::HideToParent)
  614. {
  615. // we don't need to force the widget to be visible here.
  616. child->setProperty("visibilityToParent", false);
  617. }
  618. return this->QWidget::eventFilter(child, e);
  619. }
  620. //-----------------------------------------------------------------------------
  621. bool ctkCollapsibleButton::event(QEvent *event)
  622. {
  623. if (event->type() == QEvent::StyleChange
  624. || event->type() == QEvent::FontChange
  625. #ifdef Q_WS_MAC
  626. || event->type() == QEvent::MacSizeChange
  627. #endif
  628. )
  629. {
  630. this->setContentsMargins(0, this->buttonSizeHint().height(),0 , 0);
  631. if (this->collapsed())
  632. {
  633. this->setMaximumHeight(this->sizeHint().height());
  634. }
  635. }
  636. return QAbstractButton::event(event);
  637. }