ctkCollapsibleButton.cpp 25 KB

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