ctkCheckablePushButton.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 <QDesktopWidget>
  19. #include <QLayout>
  20. #include <QMouseEvent>
  21. #include <QMenu>
  22. #include <QPainter>
  23. #include <QPointer>
  24. #include <QPushButton>
  25. #include <QStyle>
  26. #include <QStyleOptionButton>
  27. #include <QStylePainter>
  28. #include <QToolBar>
  29. // CTK includes
  30. #include "ctkCheckablePushButton.h"
  31. //-----------------------------------------------------------------------------
  32. class ctkCheckablePushButtonPrivate
  33. {
  34. Q_DECLARE_PUBLIC(ctkCheckablePushButton);
  35. protected:
  36. ctkCheckablePushButton* const q_ptr;
  37. public:
  38. ctkCheckablePushButtonPrivate(ctkCheckablePushButton& object);
  39. void init();
  40. QRect checkboxRect() const;
  41. QSize buttonSizeHint()const;
  42. // Tuning of the button look&feel
  43. Qt::Alignment TextAlignment;
  44. Qt::Alignment IndicatorAlignment;
  45. Qt::ItemFlags CheckBoxFlags;
  46. Qt::CheckState CheckState;
  47. };
  48. //-----------------------------------------------------------------------------
  49. ctkCheckablePushButtonPrivate::ctkCheckablePushButtonPrivate(ctkCheckablePushButton& object)
  50. :q_ptr(&object)
  51. {
  52. this->TextAlignment = Qt::AlignLeft | Qt::AlignVCenter;
  53. this->IndicatorAlignment = Qt::AlignLeft | Qt::AlignVCenter;
  54. this->CheckBoxFlags = Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
  55. this->CheckState = Qt::Unchecked;
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkCheckablePushButtonPrivate::init()
  59. {
  60. }
  61. //-----------------------------------------------------------------------------
  62. QRect ctkCheckablePushButtonPrivate::checkboxRect()const
  63. {
  64. Q_Q(const ctkCheckablePushButton);
  65. QRect rect;
  66. QStyleOptionButton opt;
  67. q->initStyleOption(&opt);
  68. QSize indicatorSize = QSize(q->style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, q),
  69. q->style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, q));
  70. int buttonHeight = opt.rect.height();
  71. uint tf = this->TextAlignment;
  72. if (q->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, q))
  73. {
  74. tf |= Qt::TextShowMnemonic;
  75. }
  76. else
  77. {
  78. tf |= Qt::TextHideMnemonic;
  79. }
  80. int textWidth = opt.fontMetrics.boundingRect(opt.rect, tf, opt.text).width();
  81. int indicatorSpacing = q->style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, q);
  82. int buttonMargin = q->style()->pixelMetric(QStyle::PM_ButtonMargin, &opt, q);
  83. if (this->IndicatorAlignment & Qt::AlignLeft)
  84. {
  85. rect = QRect((buttonHeight - indicatorSize.width()) / 2,
  86. (buttonHeight - indicatorSize.height()) / 2,
  87. indicatorSize.width(), indicatorSize.height());
  88. }
  89. else if (this->IndicatorAlignment & Qt::AlignHCenter)
  90. {
  91. int w = indicatorSize.width();
  92. if (!opt.text.isEmpty() && (this->TextAlignment & Qt::AlignHCenter))
  93. {
  94. w += textWidth + indicatorSpacing;
  95. }
  96. rect = QRect(opt.rect.x()+ opt.rect.width() /2 - w / 2,
  97. (buttonHeight - indicatorSize.height()) / 2,
  98. indicatorSize.width(), indicatorSize.height());
  99. if (this->TextAlignment & Qt::AlignLeft &&
  100. rect.left() < opt.rect.x() + buttonMargin + textWidth)
  101. {
  102. rect.moveLeft(opt.rect.x() + buttonMargin + textWidth);
  103. }
  104. else if (this->TextAlignment & Qt::AlignRight &&
  105. rect.right() > opt.rect.right() - buttonMargin - textWidth)
  106. {
  107. rect.moveRight(opt.rect.right() - buttonMargin - textWidth);
  108. }
  109. }
  110. else if (this->IndicatorAlignment & Qt::AlignRight)
  111. {
  112. rect = QRect(opt.rect.width() - (buttonHeight - indicatorSize.width()) / 2
  113. - indicatorSize.width(),
  114. (buttonHeight - indicatorSize.height()) / 2,
  115. indicatorSize.width(), indicatorSize.height());
  116. }
  117. return rect;
  118. }
  119. //-----------------------------------------------------------------------------
  120. QSize ctkCheckablePushButtonPrivate::buttonSizeHint()const
  121. {
  122. Q_Q(const ctkCheckablePushButton);
  123. int w = 0, h = 0;
  124. QStyleOptionButton opt;
  125. opt.initFrom(q);
  126. // indicator
  127. QSize indicatorSize = QSize(q->style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, q),
  128. q->style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, q));
  129. int indicatorSpacing = q->style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, q);
  130. int ih = indicatorSize.height();
  131. int iw = indicatorSize.width() + indicatorSpacing;
  132. w += iw;
  133. h = qMax(h, ih);
  134. // text
  135. QString string(q->text());
  136. bool empty = string.isEmpty();
  137. if (empty)
  138. {
  139. string = QString::fromLatin1("XXXX");
  140. }
  141. QFontMetrics fm = q->fontMetrics();
  142. QSize sz = fm.size(Qt::TextShowMnemonic, string);
  143. if(!empty || !w)
  144. {
  145. w += sz.width();
  146. }
  147. h = qMax(h, sz.height());
  148. //opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height
  149. QSize buttonSize = (q->style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), q).
  150. expandedTo(QApplication::globalStrut()));
  151. return buttonSize;
  152. }
  153. //-----------------------------------------------------------------------------
  154. ctkCheckablePushButton::ctkCheckablePushButton(QWidget* _parent)
  155. :QPushButton(_parent)
  156. , d_ptr(new ctkCheckablePushButtonPrivate(*this))
  157. {
  158. Q_D(ctkCheckablePushButton);
  159. d->init();
  160. }
  161. //-----------------------------------------------------------------------------
  162. ctkCheckablePushButton::ctkCheckablePushButton(const QString& title, QWidget* _parent)
  163. :QPushButton(title, _parent)
  164. , d_ptr(new ctkCheckablePushButtonPrivate(*this))
  165. {
  166. }
  167. //-----------------------------------------------------------------------------
  168. ctkCheckablePushButton::~ctkCheckablePushButton()
  169. {
  170. }
  171. //-----------------------------------------------------------------------------
  172. void ctkCheckablePushButton::setButtonTextAlignment(Qt::Alignment textAlignment)
  173. {
  174. Q_D(ctkCheckablePushButton);
  175. d->TextAlignment = textAlignment;
  176. this->update();
  177. }
  178. //-----------------------------------------------------------------------------
  179. Qt::Alignment ctkCheckablePushButton::buttonTextAlignment()const
  180. {
  181. Q_D(const ctkCheckablePushButton);
  182. return d->TextAlignment;
  183. }
  184. //-----------------------------------------------------------------------------
  185. void ctkCheckablePushButton::setIndicatorAlignment(Qt::Alignment indicatorAlignment)
  186. {
  187. Q_D(ctkCheckablePushButton);
  188. d->IndicatorAlignment = indicatorAlignment;
  189. this->update();
  190. }
  191. //-----------------------------------------------------------------------------
  192. Qt::Alignment ctkCheckablePushButton::indicatorAlignment()const
  193. {
  194. Q_D(const ctkCheckablePushButton);
  195. return d->IndicatorAlignment;
  196. }
  197. //-----------------------------------------------------------------------------
  198. void ctkCheckablePushButton::setCheckState(Qt::CheckState checkState)
  199. {
  200. Q_D(ctkCheckablePushButton);
  201. Qt::CheckState oldCheckState = d->CheckState;
  202. if (checkState == oldCheckState)
  203. {
  204. return;
  205. }
  206. d->CheckState = checkState;
  207. bool emitToggled = false;
  208. if (d->CheckBoxFlags & Qt::ItemIsEnabled)
  209. {
  210. bool wasChecked = this->isChecked();
  211. // QCheckBox::setCheckable() doesn't emit toggled signal
  212. this->setCheckable(checkState == Qt::Checked);
  213. emitToggled = (wasChecked != this->isChecked());
  214. }
  215. if (emitToggled)
  216. {
  217. emit toggled(this->isChecked());
  218. }
  219. emit checkStateChanged(d->CheckState);
  220. emit checkBoxToggled(d->CheckState == Qt::Checked);
  221. this->update();
  222. }
  223. //-----------------------------------------------------------------------------
  224. Qt::CheckState ctkCheckablePushButton::checkState()const
  225. {
  226. Q_D(const ctkCheckablePushButton);
  227. return d->CheckState;
  228. }
  229. //-----------------------------------------------------------------------------
  230. void ctkCheckablePushButton::setCheckBoxControlsButton(bool b)
  231. {
  232. Q_D(ctkCheckablePushButton);
  233. if (b)
  234. {
  235. d->CheckBoxFlags |= Qt::ItemIsEnabled;
  236. // synchronize checkstate with the checkable property.
  237. this->setCheckState(
  238. this->isCheckable() ? Qt::Checked : Qt::Unchecked);
  239. }
  240. else
  241. {
  242. d->CheckBoxFlags &= ~Qt::ItemIsEnabled;
  243. }
  244. this->update();
  245. }
  246. //-----------------------------------------------------------------------------
  247. bool ctkCheckablePushButton::checkBoxControlsButton()const
  248. {
  249. Q_D(const ctkCheckablePushButton);
  250. return d->CheckBoxFlags & Qt::ItemIsEnabled;
  251. }
  252. //-----------------------------------------------------------------------------
  253. void ctkCheckablePushButton::setCheckBoxUserCheckable(bool b)
  254. {
  255. Q_D(ctkCheckablePushButton);
  256. if (b)
  257. {
  258. d->CheckBoxFlags |= Qt::ItemIsUserCheckable;
  259. }
  260. else
  261. {
  262. d->CheckBoxFlags &= ~Qt::ItemIsUserCheckable;
  263. }
  264. this->update();
  265. }
  266. //-----------------------------------------------------------------------------
  267. bool ctkCheckablePushButton::isCheckBoxUserCheckable()const
  268. {
  269. Q_D(const ctkCheckablePushButton);
  270. return d->CheckBoxFlags & Qt::ItemIsUserCheckable;
  271. }
  272. //-----------------------------------------------------------------------------
  273. QSize ctkCheckablePushButton::minimumSizeHint()const
  274. {
  275. Q_D(const ctkCheckablePushButton);
  276. return d->buttonSizeHint();
  277. }
  278. //-----------------------------------------------------------------------------
  279. QSize ctkCheckablePushButton::sizeHint()const
  280. {
  281. return this->minimumSizeHint();
  282. }
  283. //-----------------------------------------------------------------------------
  284. void ctkCheckablePushButton::paintEvent(QPaintEvent * _event)
  285. {
  286. Q_UNUSED(_event);
  287. Q_D(ctkCheckablePushButton);
  288. QPainter p(this);
  289. // Draw Button
  290. QStyleOptionButton opt;
  291. this->initStyleOption(&opt);
  292. // Checkbox size
  293. QSize indicatorSize = QSize(style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, this),
  294. style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, this));
  295. // Replace the icon size by the checkbox size
  296. opt.iconSize = indicatorSize;
  297. // Draw the panel of the button (no text, no icon)
  298. style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &p, this);
  299. // TBD is PE_PanelButtonCommand better ?
  300. //style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &opt, &p, this);
  301. //int buttonHeight = opt.rect.height();
  302. uint tf = d->TextAlignment;
  303. if (this->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, this))
  304. {
  305. tf |= Qt::TextShowMnemonic;
  306. }
  307. else
  308. {
  309. tf |= Qt::TextHideMnemonic;
  310. }
  311. int textWidth = opt.fontMetrics.boundingRect(opt.rect, tf, opt.text).width();
  312. // Spacing between the text and the checkbox
  313. int indicatorSpacing = this->style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, this);
  314. int buttonMargin = this->style()->pixelMetric(QStyle::PM_ButtonMargin, &opt, this);
  315. // Draw Indicator
  316. QStyleOptionButton indicatorOpt;
  317. indicatorOpt.init(this);
  318. if (!(d->CheckBoxFlags & Qt::ItemIsUserCheckable))
  319. {
  320. indicatorOpt.state &= ~QStyle::State_Enabled;
  321. }
  322. if (this->checkBoxControlsButton())
  323. {
  324. // Hack: calling setCheckable() instead of setCheckState while being in a
  325. // control button mode leads to an inconsistent state, we need to make
  326. // synchronize the 2 properties.
  327. this->setCheckState(this->isCheckable() ? Qt::Checked : Qt::Unchecked);
  328. }
  329. switch (d->CheckState)
  330. {
  331. case Qt::Checked:
  332. indicatorOpt.state |= QStyle::State_On;
  333. break;
  334. case Qt::PartiallyChecked:
  335. indicatorOpt.state |= QStyle::State_NoChange;
  336. break;
  337. default:
  338. case Qt::Unchecked:
  339. indicatorOpt.state |= QStyle::State_Off;
  340. break;
  341. }
  342. indicatorOpt.rect = d->checkboxRect();
  343. this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &indicatorOpt, &p, 0);
  344. // Draw Text
  345. if (d->TextAlignment & Qt::AlignLeft)
  346. {
  347. if (d->IndicatorAlignment & Qt::AlignLeft)
  348. {
  349. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  350. }
  351. else
  352. {
  353. opt.rect.setLeft(opt.rect.x() + buttonMargin);
  354. }
  355. }
  356. else if (d->TextAlignment & Qt::AlignHCenter)
  357. {
  358. if (d->IndicatorAlignment & Qt::AlignHCenter)
  359. {
  360. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  361. }
  362. else
  363. {
  364. opt.rect.setLeft(opt.rect.x() + opt.rect.width() / 2 - textWidth / 2);
  365. if (d->IndicatorAlignment & Qt::AlignLeft)
  366. {
  367. opt.rect.setLeft( qMax(indicatorOpt.rect.right() + indicatorSpacing, opt.rect.left()) );
  368. }
  369. }
  370. }
  371. else if (d->TextAlignment & Qt::AlignRight)
  372. {
  373. if (d->IndicatorAlignment & Qt::AlignRight)
  374. {
  375. opt.rect.setLeft(indicatorOpt.rect.left() - indicatorSpacing - textWidth);
  376. }
  377. else
  378. {
  379. opt.rect.setLeft(opt.rect.right() - buttonMargin - textWidth);
  380. }
  381. }
  382. // all the computations have been made infering the text would be left oriented
  383. tf &= ~Qt::AlignHCenter & ~Qt::AlignRight;
  384. tf |= Qt::AlignLeft;
  385. this->style()->drawItemText(&p, opt.rect, tf, opt.palette, (opt.state & QStyle::State_Enabled),
  386. opt.text, QPalette::ButtonText);
  387. }
  388. //-----------------------------------------------------------------------------
  389. bool ctkCheckablePushButton::hitButton(const QPoint & _pos)const
  390. {
  391. Q_D(const ctkCheckablePushButton);
  392. return !d->checkboxRect().contains(_pos)
  393. && this->QPushButton::hitButton(_pos);
  394. }
  395. //-----------------------------------------------------------------------------
  396. void ctkCheckablePushButton::initStyleOption(QStyleOptionButton* option)const
  397. {
  398. this->QPushButton::initStyleOption(option);
  399. option->iconSize = QSize(this->style()->pixelMetric(QStyle::PM_IndicatorWidth, option, this),
  400. this->style()->pixelMetric(QStyle::PM_IndicatorHeight, option, this));
  401. }
  402. //-----------------------------------------------------------------------------
  403. void ctkCheckablePushButton::mousePressEvent(QMouseEvent *e)
  404. {
  405. Q_D(ctkCheckablePushButton);
  406. this->QPushButton::mousePressEvent(e);
  407. if (e->isAccepted())
  408. {
  409. return;
  410. }
  411. if (d->checkboxRect().contains(e->pos()) &&
  412. (d->CheckBoxFlags & Qt::ItemIsUserCheckable))
  413. {
  414. Qt::CheckState newCheckState;
  415. switch (d->CheckState)
  416. {
  417. case Qt::Unchecked:
  418. case Qt::PartiallyChecked:
  419. newCheckState = Qt::Checked;
  420. break;
  421. default:
  422. case Qt::Checked:
  423. newCheckState = Qt::Unchecked;
  424. break;
  425. }
  426. this->setCheckState(newCheckState);
  427. e->accept();
  428. }
  429. }