ctkCollapsibleButton.cpp 25 KB

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