ctkCollapsibleButton.cpp 24 KB

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