ctkCollapsibleButton.cpp 25 KB

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