ctkCollapsibleButton.cpp 25 KB

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