ctkCheckablePushButton.cpp 14 KB

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