ctkCollapsibleButton.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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(QObject* child, this->children())
  212. {
  213. QWidget* childWidget = qobject_cast<QWidget*>(child);
  214. if (childWidget)
  215. {
  216. d->setChildVisibility(childWidget);
  217. }
  218. }
  219. // this might be too many updates...
  220. QWidget* _parent = this->parentWidget();
  221. if (!d->Collapsed && (!_parent || !_parent->layout()))
  222. {
  223. this->resize(this->sizeHint());
  224. }
  225. else
  226. {
  227. this->updateGeometry();
  228. }
  229. //this->update(QRect(QPoint(0,0), this->sizeHint()));
  230. //this->repaint(QRect(QPoint(0,0), this->sizeHint()));
  231. emit contentsCollapsed(collapsed);
  232. }
  233. //-----------------------------------------------------------------------------
  234. QFrame::Shape ctkCollapsibleButton::contentsFrameShape() const
  235. {
  236. Q_D(const ctkCollapsibleButton);
  237. return d->ContentsFrameShape;
  238. }
  239. //-----------------------------------------------------------------------------
  240. void ctkCollapsibleButton::setContentsFrameShape(QFrame::Shape s)
  241. {
  242. Q_D(ctkCollapsibleButton);
  243. d->ContentsFrameShape = s;
  244. }
  245. //-----------------------------------------------------------------------------
  246. QFrame::Shadow ctkCollapsibleButton::contentsFrameShadow() const
  247. {
  248. Q_D(const ctkCollapsibleButton);
  249. return d->ContentsFrameShadow;
  250. }
  251. //-----------------------------------------------------------------------------
  252. void ctkCollapsibleButton::setContentsFrameShadow(QFrame::Shadow s)
  253. {
  254. Q_D(ctkCollapsibleButton);
  255. d->ContentsFrameShadow = s;
  256. }
  257. //-----------------------------------------------------------------------------
  258. int ctkCollapsibleButton:: contentsLineWidth() const
  259. {
  260. Q_D(const ctkCollapsibleButton);
  261. return d->ContentsLineWidth;
  262. }
  263. //-----------------------------------------------------------------------------
  264. void ctkCollapsibleButton::setContentsLineWidth(int w)
  265. {
  266. Q_D(ctkCollapsibleButton);
  267. d->ContentsLineWidth = w;
  268. }
  269. //-----------------------------------------------------------------------------
  270. int ctkCollapsibleButton::contentsMidLineWidth() const
  271. {
  272. Q_D(const ctkCollapsibleButton);
  273. return d->ContentsMidLineWidth;
  274. }
  275. //-----------------------------------------------------------------------------
  276. void ctkCollapsibleButton::setContentsMidLineWidth(int w)
  277. {
  278. Q_D(ctkCollapsibleButton);
  279. d->ContentsMidLineWidth = w;
  280. }
  281. //-----------------------------------------------------------------------------
  282. void ctkCollapsibleButton::setButtonTextAlignment(Qt::Alignment textAlignment)
  283. {
  284. Q_D(ctkCollapsibleButton);
  285. d->TextAlignment = textAlignment;
  286. this->update();
  287. }
  288. //-----------------------------------------------------------------------------
  289. Qt::Alignment ctkCollapsibleButton::buttonTextAlignment()const
  290. {
  291. Q_D(const ctkCollapsibleButton);
  292. return d->TextAlignment;
  293. }
  294. //-----------------------------------------------------------------------------
  295. void ctkCollapsibleButton::setIndicatorAlignment(Qt::Alignment indicatorAlignment)
  296. {
  297. Q_D(ctkCollapsibleButton);
  298. d->IndicatorAlignment = indicatorAlignment;
  299. this->update();
  300. }
  301. //-----------------------------------------------------------------------------
  302. Qt::Alignment ctkCollapsibleButton::indicatorAlignment()const
  303. {
  304. Q_D(const ctkCollapsibleButton);
  305. return d->IndicatorAlignment;
  306. }
  307. //-----------------------------------------------------------------------------
  308. QSize ctkCollapsibleButton::buttonSizeHint()const
  309. {
  310. int w = 0, h = 0;
  311. QStyleOptionButton opt;
  312. opt.initFrom(this);
  313. // indicator
  314. QSize indicatorSize = QSize(style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, this),
  315. style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, this));
  316. int indicatorSpacing = style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, this);
  317. int ih = indicatorSize.height();
  318. int iw = indicatorSize.width() + indicatorSpacing;
  319. w += iw;
  320. h = qMax(h, ih);
  321. // text
  322. QString string(this->text());
  323. bool empty = string.isEmpty();
  324. if (empty)
  325. {
  326. string = QString::fromLatin1("XXXX");
  327. }
  328. QFontMetrics fm = this->fontMetrics();
  329. QSize sz = fm.size(Qt::TextShowMnemonic, string);
  330. if(!empty || !w)
  331. {
  332. w += sz.width();
  333. }
  334. h = qMax(h, sz.height());
  335. //opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height
  336. QSize buttonSize = (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).
  337. expandedTo(QApplication::globalStrut()));
  338. return buttonSize;
  339. }
  340. //-----------------------------------------------------------------------------
  341. QSize ctkCollapsibleButton::minimumSizeHint()const
  342. {
  343. Q_D(const ctkCollapsibleButton);
  344. QSize buttonSize = this->buttonSizeHint();
  345. if (d->Collapsed)
  346. {
  347. return buttonSize + QSize(0,d->CollapsedHeight);
  348. }
  349. // open
  350. if (this->layout() == 0)
  351. {// no layout, means the button is empty ?
  352. return buttonSize;
  353. }
  354. QSize s = this->QAbstractButton::minimumSizeHint();
  355. return s.expandedTo(buttonSize);
  356. }
  357. //-----------------------------------------------------------------------------
  358. QSize ctkCollapsibleButton::sizeHint()const
  359. {
  360. Q_D(const ctkCollapsibleButton);
  361. QSize buttonSize = this->buttonSizeHint();
  362. if (d->Collapsed)
  363. {
  364. return buttonSize + QSize(0,d->CollapsedHeight);
  365. }
  366. // open
  367. // QAbstractButton works well only if a layout is set
  368. QSize s = this->QAbstractButton::sizeHint();
  369. return s.expandedTo(buttonSize + QSize(0, d->CollapsedHeight));
  370. }
  371. //-----------------------------------------------------------------------------
  372. void ctkCollapsibleButton::paintEvent(QPaintEvent * _event)
  373. {
  374. Q_D(ctkCollapsibleButton);
  375. QPainter p(this);
  376. // Draw Button
  377. QStyleOptionButton opt;
  378. this->initStyleOption(&opt);
  379. // We don't want to have the highlight effect on the button when mouse is
  380. // over a child. We want the highlight effect only when the mouse is just
  381. // over itself.
  382. // same as this->underMouse()
  383. bool exclusiveMouseOver = false;
  384. if (opt.state & QStyle::State_MouseOver)
  385. {
  386. QRect buttonRect = opt.rect;
  387. QList<QWidget*> _children = this->findChildren<QWidget*>();
  388. QList<QWidget*>::ConstIterator it;
  389. for (it = _children.constBegin(); it != _children.constEnd(); ++it )
  390. {
  391. if ((*it)->underMouse())
  392. {
  393. // the mouse has been moved from the collapsible button to one
  394. // of its children. The paint event rect is the child rect, this
  395. // is why we have to request another paint event to redraw the
  396. // button to remove the highlight effect.
  397. if (!_event->rect().contains(buttonRect))
  398. {// repaint the button rect.
  399. this->update(buttonRect);
  400. }
  401. opt.state &= ~QStyle::State_MouseOver;
  402. exclusiveMouseOver = true;
  403. break;
  404. }
  405. }
  406. if (d->ExclusiveMouseOver && !exclusiveMouseOver)
  407. {
  408. // the mouse is over the widget, but not over the children. As it
  409. // has been de-highlighted in the past, we should refresh the button
  410. // rect to re-highlight the button.
  411. if (!_event->rect().contains(buttonRect))
  412. {// repaint the button rect.
  413. this->update(buttonRect);
  414. }
  415. }
  416. }
  417. d->ExclusiveMouseOver = exclusiveMouseOver;
  418. QSize indicatorSize = QSize(style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, this),
  419. style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, this));
  420. opt.iconSize = indicatorSize;
  421. style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &p, this);
  422. // TBD is PE_PanelButtonCommand better ?
  423. //style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &opt, &p, this);
  424. int buttonHeight = opt.rect.height();
  425. uint tf = d->TextAlignment;
  426. if (this->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, this))
  427. {
  428. tf |= Qt::TextShowMnemonic;
  429. }
  430. else
  431. {
  432. tf |= Qt::TextHideMnemonic;
  433. }
  434. int textWidth = opt.fontMetrics.boundingRect(opt.rect, tf, opt.text).width();
  435. int indicatorSpacing = this->style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, this);
  436. int buttonMargin = this->style()->pixelMetric(QStyle::PM_ButtonMargin, &opt, this);
  437. // Draw Indicator
  438. QStyleOption indicatorOpt;
  439. indicatorOpt.init(this);
  440. if (d->IndicatorAlignment & Qt::AlignLeft)
  441. {
  442. indicatorOpt.rect = QRect((buttonHeight - indicatorSize.width()) / 2,
  443. (buttonHeight - indicatorSize.height()) / 2,
  444. indicatorSize.width(), indicatorSize.height());
  445. }
  446. else if (d->IndicatorAlignment & Qt::AlignHCenter)
  447. {
  448. int w = indicatorSize.width();
  449. if (!opt.text.isEmpty() && (d->TextAlignment & Qt::AlignHCenter))
  450. {
  451. w += textWidth + indicatorSpacing;
  452. }
  453. indicatorOpt.rect = QRect(opt.rect.x()+ opt.rect.width() /2 - w / 2,
  454. (buttonHeight - indicatorSize.height()) / 2,
  455. indicatorSize.width(), indicatorSize.height());
  456. if (d->TextAlignment & Qt::AlignLeft &&
  457. indicatorOpt.rect.left() < opt.rect.x() + buttonMargin + textWidth)
  458. {
  459. indicatorOpt.rect.moveLeft(opt.rect.x() + buttonMargin + textWidth);
  460. }
  461. else if (d->TextAlignment & Qt::AlignRight &&
  462. indicatorOpt.rect.right() > opt.rect.right() - buttonMargin - textWidth)
  463. {
  464. indicatorOpt.rect.moveRight(opt.rect.right() - buttonMargin - textWidth);
  465. }
  466. }
  467. else if (d->IndicatorAlignment & Qt::AlignRight)
  468. {
  469. indicatorOpt.rect = QRect(opt.rect.width() - (buttonHeight - indicatorSize.width()) / 2
  470. - indicatorSize.width(),
  471. (buttonHeight - indicatorSize.height()) / 2,
  472. indicatorSize.width(), indicatorSize.height());
  473. }
  474. if (d->Collapsed)
  475. {
  476. style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &indicatorOpt, &p, this);
  477. }
  478. else
  479. {
  480. style()->drawPrimitive(QStyle::PE_IndicatorArrowUp, &indicatorOpt, &p, this);
  481. }
  482. // Draw Text
  483. if (d->TextAlignment & Qt::AlignLeft)
  484. {
  485. if (d->IndicatorAlignment & Qt::AlignLeft)
  486. {
  487. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  488. }
  489. else
  490. {
  491. opt.rect.setLeft(opt.rect.x() + buttonMargin);
  492. }
  493. }
  494. else if (d->TextAlignment & Qt::AlignHCenter)
  495. {
  496. if (d->IndicatorAlignment & Qt::AlignHCenter)
  497. {
  498. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  499. }
  500. else
  501. {
  502. opt.rect.setLeft(opt.rect.x() + opt.rect.width() / 2 - textWidth / 2);
  503. if (d->IndicatorAlignment & Qt::AlignLeft)
  504. {
  505. opt.rect.setLeft( qMax(indicatorOpt.rect.right() + indicatorSpacing, opt.rect.left()) );
  506. }
  507. }
  508. }
  509. else if (d->TextAlignment & Qt::AlignRight)
  510. {
  511. if (d->IndicatorAlignment & Qt::AlignRight)
  512. {
  513. opt.rect.setLeft(indicatorOpt.rect.left() - indicatorSpacing - textWidth);
  514. }
  515. else
  516. {
  517. opt.rect.setLeft(opt.rect.right() - buttonMargin - textWidth);
  518. }
  519. }
  520. // all the computations have been made infering the text would be left oriented
  521. tf &= ~Qt::AlignHCenter & ~Qt::AlignRight;
  522. tf |= Qt::AlignLeft;
  523. style()->drawItemText(&p, opt.rect, tf, opt.palette, (opt.state & QStyle::State_Enabled),
  524. opt.text, QPalette::ButtonText);
  525. // Draw Frame around contents
  526. QStyleOptionFrameV3 fopt;
  527. fopt.init(this);
  528. // HACK: on some styles, the frame doesn't exactly touch the button.
  529. // this is because the button has some kind of extra border.
  530. if (qobject_cast<QCleanlooksStyle*>(this->style()) != 0)
  531. {
  532. fopt.rect.setTop(buttonHeight - 1);
  533. }
  534. else
  535. {
  536. fopt.rect.setTop(buttonHeight);
  537. }
  538. fopt.frameShape = d->ContentsFrameShape;
  539. switch (d->ContentsFrameShadow)
  540. {
  541. case QFrame::Sunken:
  542. fopt.state |= QStyle::State_Sunken;
  543. break;
  544. case QFrame::Raised:
  545. fopt.state |= QStyle::State_Raised;
  546. break;
  547. default:
  548. case QFrame::Plain:
  549. break;
  550. }
  551. fopt.lineWidth = d->ContentsLineWidth;
  552. fopt.midLineWidth = d->ContentsMidLineWidth;
  553. style()->drawControl(QStyle::CE_ShapedFrame, &fopt, &p, this);
  554. }
  555. //-----------------------------------------------------------------------------
  556. bool ctkCollapsibleButton::hitButton(const QPoint & _pos)const
  557. {
  558. QStyleOptionButton opt;
  559. this->initStyleOption(&opt);
  560. return opt.rect.contains(_pos);
  561. }
  562. //-----------------------------------------------------------------------------
  563. void ctkCollapsibleButton::childEvent(QChildEvent* c)
  564. {
  565. Q_D(ctkCollapsibleButton);
  566. if (c && c->type() == QEvent::ChildAdded &&
  567. c->child() && c->child()->isWidgetType())
  568. {
  569. // We want to catch all the child's Show/Hide events.
  570. c->child()->installEventFilter(this);
  571. // If the child is added while ctkCollapsibleButton is collapsed, then we
  572. // need to hide the child.
  573. QWidget *w = static_cast<QWidget*>(c->child());
  574. d->setChildVisibility(w);
  575. }
  576. this->QAbstractButton::childEvent(c);
  577. }
  578. //-----------------------------------------------------------------------------
  579. void ctkCollapsibleButton::setVisible(bool show)
  580. {
  581. Q_D(ctkCollapsibleButton);
  582. // calling QWidget::setVisible() on ctkCollapsibleButton will eventually
  583. // call QWidget::showChildren() or hideChildren() which will generate
  584. // ShowToParent/HideToParent events but we want to ignore that case in
  585. // eventFilter().
  586. d->ForcingVisibility = true;
  587. this->QWidget::setVisible(show);
  588. d->ForcingVisibility = false;
  589. }
  590. //-----------------------------------------------------------------------------
  591. bool ctkCollapsibleButton::eventFilter(QObject* child, QEvent* e)
  592. {
  593. Q_D(ctkCollapsibleButton);
  594. Q_ASSERT(child && e);
  595. // Make sure the Show/QHide events are not generated by one of our
  596. // ctkCollapsibleButton function.
  597. if (d->ForcingVisibility)
  598. {
  599. return false;
  600. }
  601. // When we are here, it's because somewhere (not in ctkCollapsibleButton),
  602. // someone explicitly called setVisible() on a child widget.
  603. // If the collapsible button is collapsed/closed, then even if someone
  604. // request the widget to be visible, we force it back to be hidden because
  605. // they meant to be hidden to its parent, the collapsible button. However the
  606. // child will later be shown when the button will be expanded/opened.
  607. // On the other hand, if the user explicitly hide the child when the button
  608. // is collapsed/closed, then we want to keep it hidden next time the
  609. // collapsible button is expanded/opened.
  610. if (e->type() == QEvent::ShowToParent)
  611. {
  612. child->setProperty("visibilityToParent", true);
  613. Q_ASSERT(qobject_cast<QWidget*>(child));
  614. // force the widget to be hidden if the button is collapsed.
  615. d->setChildVisibility(qobject_cast<QWidget*>(child));
  616. }
  617. else if(e->type() == QEvent::HideToParent)
  618. {
  619. // we don't need to force the widget to be visible here.
  620. child->setProperty("visibilityToParent", false);
  621. }
  622. return this->QWidget::eventFilter(child, e);
  623. }
  624. //-----------------------------------------------------------------------------
  625. bool ctkCollapsibleButton::event(QEvent *event)
  626. {
  627. if (event->type() == QEvent::StyleChange
  628. || event->type() == QEvent::FontChange
  629. #ifdef Q_WS_MAC
  630. || event->type() == QEvent::MacSizeChange
  631. #endif
  632. )
  633. {
  634. this->setContentsMargins(0, this->buttonSizeHint().height(),0 , 0);
  635. if (this->collapsed())
  636. {
  637. this->setMaximumHeight(this->sizeHint().height());
  638. }
  639. }
  640. return QAbstractButton::event(event);
  641. }