ctkCheckablePushButton.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. d->CheckState = checkState;
  205. if (d->CheckBoxFlags & Qt::ItemIsEnabled)
  206. {
  207. setCheckable(checkState == Qt::Checked);
  208. }
  209. this->update();
  210. emit checkStateChanged(checkState);
  211. }
  212. }
  213. //-----------------------------------------------------------------------------
  214. Qt::CheckState ctkCheckablePushButton::checkState()const
  215. {
  216. Q_D(const ctkCheckablePushButton);
  217. return d->CheckState;
  218. }
  219. //-----------------------------------------------------------------------------
  220. void ctkCheckablePushButton::setCheckBoxControlsButton(bool b)
  221. {
  222. Q_D(ctkCheckablePushButton);
  223. if (b)
  224. {
  225. d->CheckBoxFlags |= Qt::ItemIsEnabled;
  226. }
  227. else
  228. {
  229. d->CheckBoxFlags &= ~Qt::ItemIsEnabled;
  230. }
  231. this->update();
  232. }
  233. //-----------------------------------------------------------------------------
  234. bool ctkCheckablePushButton::checkBoxControlsButton()const
  235. {
  236. Q_D(const ctkCheckablePushButton);
  237. return d->CheckBoxFlags & Qt::ItemIsEnabled;
  238. }
  239. //-----------------------------------------------------------------------------
  240. void ctkCheckablePushButton::setCheckBoxUserCheckable(bool b)
  241. {
  242. Q_D(ctkCheckablePushButton);
  243. if (b)
  244. {
  245. d->CheckBoxFlags |= Qt::ItemIsUserCheckable;
  246. }
  247. else
  248. {
  249. d->CheckBoxFlags &= ~Qt::ItemIsUserCheckable;
  250. }
  251. this->update();
  252. }
  253. //-----------------------------------------------------------------------------
  254. bool ctkCheckablePushButton::isCheckBoxUserCheckable()const
  255. {
  256. Q_D(const ctkCheckablePushButton);
  257. return d->CheckBoxFlags & Qt::ItemIsUserCheckable;
  258. }
  259. //-----------------------------------------------------------------------------
  260. QSize ctkCheckablePushButton::minimumSizeHint()const
  261. {
  262. Q_D(const ctkCheckablePushButton);
  263. return d->buttonSizeHint();
  264. }
  265. //-----------------------------------------------------------------------------
  266. QSize ctkCheckablePushButton::sizeHint()const
  267. {
  268. return this->minimumSizeHint();
  269. }
  270. //-----------------------------------------------------------------------------
  271. void ctkCheckablePushButton::paintEvent(QPaintEvent * _event)
  272. {
  273. Q_UNUSED(_event);
  274. Q_D(ctkCheckablePushButton);
  275. QPainter p(this);
  276. // Draw Button
  277. QStyleOptionButton opt;
  278. this->initStyleOption(&opt);
  279. // Checkbox size
  280. QSize indicatorSize = QSize(style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, this),
  281. style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, this));
  282. // Replace the icon size by the checkbox size
  283. opt.iconSize = indicatorSize;
  284. // Draw the panel of the button (no text, no icon)
  285. style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &p, this);
  286. // TBD is PE_PanelButtonCommand better ?
  287. //style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &opt, &p, this);
  288. //int buttonHeight = opt.rect.height();
  289. uint tf = d->TextAlignment;
  290. if (this->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, this))
  291. {
  292. tf |= Qt::TextShowMnemonic;
  293. }
  294. else
  295. {
  296. tf |= Qt::TextHideMnemonic;
  297. }
  298. int textWidth = opt.fontMetrics.boundingRect(opt.rect, tf, opt.text).width();
  299. // Spacing between the text and the checkbox
  300. int indicatorSpacing = this->style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, this);
  301. int buttonMargin = this->style()->pixelMetric(QStyle::PM_ButtonMargin, &opt, this);
  302. // Draw Indicator
  303. QStyleOption indicatorOpt;
  304. indicatorOpt.init(this);
  305. if (!(d->CheckBoxFlags & Qt::ItemIsUserCheckable))
  306. {
  307. indicatorOpt.state &= ~QStyle::State_Enabled;
  308. }
  309. if (d->CheckBoxFlags & Qt::ItemIsEnabled)
  310. {
  311. if (this->isCheckable())
  312. {
  313. indicatorOpt.state |= QStyle::State_On;
  314. }
  315. else
  316. {
  317. indicatorOpt.state |= QStyle::State_Off;
  318. }
  319. }
  320. else
  321. {
  322. if (d->CheckState == Qt::Checked)
  323. {
  324. indicatorOpt.state |= QStyle::State_On;
  325. }
  326. else if (d->CheckState == Qt::PartiallyChecked)
  327. {
  328. indicatorOpt.state |= QStyle::State_NoChange;
  329. }
  330. else
  331. {
  332. indicatorOpt.state |= QStyle::State_Off;
  333. }
  334. }
  335. indicatorOpt.rect = d->checkboxRect();
  336. this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &indicatorOpt, &p, 0);
  337. // Draw Text
  338. if (d->TextAlignment & Qt::AlignLeft)
  339. {
  340. if (d->IndicatorAlignment & Qt::AlignLeft)
  341. {
  342. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  343. }
  344. else
  345. {
  346. opt.rect.setLeft(opt.rect.x() + buttonMargin);
  347. }
  348. }
  349. else if (d->TextAlignment & Qt::AlignHCenter)
  350. {
  351. if (d->IndicatorAlignment & Qt::AlignHCenter)
  352. {
  353. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  354. }
  355. else
  356. {
  357. opt.rect.setLeft(opt.rect.x() + opt.rect.width() / 2 - textWidth / 2);
  358. if (d->IndicatorAlignment & Qt::AlignLeft)
  359. {
  360. opt.rect.setLeft( qMax(indicatorOpt.rect.right() + indicatorSpacing, opt.rect.left()) );
  361. }
  362. }
  363. }
  364. else if (d->TextAlignment & Qt::AlignRight)
  365. {
  366. if (d->IndicatorAlignment & Qt::AlignRight)
  367. {
  368. opt.rect.setLeft(indicatorOpt.rect.left() - indicatorSpacing - textWidth);
  369. }
  370. else
  371. {
  372. opt.rect.setLeft(opt.rect.right() - buttonMargin - textWidth);
  373. }
  374. }
  375. // all the computations have been made infering the text would be left oriented
  376. tf &= ~Qt::AlignHCenter & ~Qt::AlignRight;
  377. tf |= Qt::AlignLeft;
  378. this->style()->drawItemText(&p, opt.rect, tf, opt.palette, (opt.state & QStyle::State_Enabled),
  379. opt.text, QPalette::ButtonText);
  380. }
  381. //-----------------------------------------------------------------------------
  382. bool ctkCheckablePushButton::hitButton(const QPoint & _pos)const
  383. {
  384. Q_D(const ctkCheckablePushButton);
  385. return !d->checkboxRect().contains(_pos)
  386. && this->QPushButton::hitButton(_pos);
  387. }
  388. //-----------------------------------------------------------------------------
  389. void ctkCheckablePushButton::initStyleOption(QStyleOptionButton* option)const
  390. {
  391. this->QPushButton::initStyleOption(option);
  392. option->iconSize = QSize(this->style()->pixelMetric(QStyle::PM_IndicatorWidth, option, this),
  393. this->style()->pixelMetric(QStyle::PM_IndicatorHeight, option, this));
  394. }
  395. //-----------------------------------------------------------------------------
  396. void ctkCheckablePushButton::mousePressEvent(QMouseEvent *e)
  397. {
  398. Q_D(ctkCheckablePushButton);
  399. this->QPushButton::mousePressEvent(e);
  400. if (e->isAccepted())
  401. {
  402. return;
  403. }
  404. if (d->checkboxRect().contains(e->pos()) &&
  405. (d->CheckBoxFlags & Qt::ItemIsUserCheckable))
  406. {
  407. if (d->CheckBoxFlags & Qt::ItemIsEnabled)
  408. {
  409. //check the checkbox
  410. this->setCheckable(!this->isCheckable());
  411. }
  412. else
  413. {
  414. switch (d->CheckState)
  415. {
  416. case Qt::Unchecked:
  417. case Qt::PartiallyChecked:
  418. d->CheckState = Qt::Checked;
  419. break;
  420. case Qt::Checked:
  421. d->CheckState = Qt::Unchecked;
  422. break;
  423. }
  424. }
  425. this->update();
  426. e->accept();
  427. }
  428. }