ctkCollapsibleButton.cpp 25 KB

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