ctkCheckablePushButton.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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.commontk.org/LICENSE
  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. };
  46. //-----------------------------------------------------------------------------
  47. ctkCheckablePushButtonPrivate::ctkCheckablePushButtonPrivate(ctkCheckablePushButton& object)
  48. :q_ptr(&object)
  49. {
  50. this->TextAlignment = Qt::AlignLeft | Qt::AlignVCenter;
  51. this->IndicatorAlignment = Qt::AlignLeft | Qt::AlignVCenter;
  52. }
  53. //-----------------------------------------------------------------------------
  54. void ctkCheckablePushButtonPrivate::init()
  55. {
  56. Q_Q(ctkCheckablePushButton);
  57. }
  58. //-----------------------------------------------------------------------------
  59. QRect ctkCheckablePushButtonPrivate::checkboxRect()const
  60. {
  61. Q_Q(const ctkCheckablePushButton);
  62. QRect rect;
  63. QStyleOptionButton opt;
  64. q->initStyleOption(&opt);
  65. QSize indicatorSize = QSize(q->style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, q),
  66. q->style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, q));
  67. int buttonHeight = opt.rect.height();
  68. uint tf = this->TextAlignment;
  69. if (q->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, q))
  70. {
  71. tf |= Qt::TextShowMnemonic;
  72. }
  73. else
  74. {
  75. tf |= Qt::TextHideMnemonic;
  76. }
  77. int textWidth = opt.fontMetrics.boundingRect(opt.rect, tf, opt.text).width();
  78. int indicatorSpacing = q->style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, q);
  79. int buttonMargin = q->style()->pixelMetric(QStyle::PM_ButtonMargin, &opt, q);
  80. if (this->IndicatorAlignment & Qt::AlignLeft)
  81. {
  82. rect = QRect((buttonHeight - indicatorSize.width()) / 2,
  83. (buttonHeight - indicatorSize.height()) / 2,
  84. indicatorSize.width(), indicatorSize.height());
  85. }
  86. else if (this->IndicatorAlignment & Qt::AlignHCenter)
  87. {
  88. int w = indicatorSize.width();
  89. if (!opt.text.isEmpty() && (this->TextAlignment & Qt::AlignHCenter))
  90. {
  91. w += textWidth + indicatorSpacing;
  92. }
  93. rect = QRect(opt.rect.x()+ opt.rect.width() /2 - w / 2,
  94. (buttonHeight - indicatorSize.height()) / 2,
  95. indicatorSize.width(), indicatorSize.height());
  96. if (this->TextAlignment & Qt::AlignLeft &&
  97. rect.left() < opt.rect.x() + buttonMargin + textWidth)
  98. {
  99. rect.moveLeft(opt.rect.x() + buttonMargin + textWidth);
  100. }
  101. else if (this->TextAlignment & Qt::AlignRight &&
  102. rect.right() > opt.rect.right() - buttonMargin - textWidth)
  103. {
  104. rect.moveRight(opt.rect.right() - buttonMargin - textWidth);
  105. }
  106. }
  107. else if (this->IndicatorAlignment & Qt::AlignRight)
  108. {
  109. rect = QRect(opt.rect.width() - (buttonHeight - indicatorSize.width()) / 2
  110. - indicatorSize.width(),
  111. (buttonHeight - indicatorSize.height()) / 2,
  112. indicatorSize.width(), indicatorSize.height());
  113. }
  114. return rect;
  115. }
  116. //-----------------------------------------------------------------------------
  117. QSize ctkCheckablePushButtonPrivate::buttonSizeHint()const
  118. {
  119. Q_Q(const ctkCheckablePushButton);
  120. int w = 0, h = 0;
  121. QStyleOptionButton opt;
  122. opt.initFrom(q);
  123. // indicator
  124. QSize indicatorSize = QSize(q->style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, q),
  125. q->style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, q));
  126. int indicatorSpacing = q->style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, q);
  127. int ih = indicatorSize.height();
  128. int iw = indicatorSize.width() + indicatorSpacing;
  129. w += iw;
  130. h = qMax(h, ih);
  131. // text
  132. QString string(q->text());
  133. bool empty = string.isEmpty();
  134. if (empty)
  135. {
  136. string = QString::fromLatin1("XXXX");
  137. }
  138. QFontMetrics fm = q->fontMetrics();
  139. QSize sz = fm.size(Qt::TextShowMnemonic, string);
  140. if(!empty || !w)
  141. {
  142. w += sz.width();
  143. }
  144. h = qMax(h, sz.height());
  145. //opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height
  146. QSize buttonSize = (q->style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), q).
  147. expandedTo(QApplication::globalStrut()));
  148. return buttonSize;
  149. }
  150. //-----------------------------------------------------------------------------
  151. ctkCheckablePushButton::ctkCheckablePushButton(QWidget* _parent)
  152. :QPushButton(_parent)
  153. , d_ptr(new ctkCheckablePushButtonPrivate(*this))
  154. {
  155. Q_D(ctkCheckablePushButton);
  156. d->init();
  157. }
  158. //-----------------------------------------------------------------------------
  159. ctkCheckablePushButton::ctkCheckablePushButton(const QString& title, QWidget* _parent)
  160. :QPushButton(title, _parent)
  161. , d_ptr(new ctkCheckablePushButtonPrivate(*this))
  162. {
  163. }
  164. //-----------------------------------------------------------------------------
  165. ctkCheckablePushButton::~ctkCheckablePushButton()
  166. {
  167. }
  168. //-----------------------------------------------------------------------------
  169. void ctkCheckablePushButton::setButtonTextAlignment(Qt::Alignment textAlignment)
  170. {
  171. Q_D(ctkCheckablePushButton);
  172. d->TextAlignment = textAlignment;
  173. this->update();
  174. }
  175. //-----------------------------------------------------------------------------
  176. Qt::Alignment ctkCheckablePushButton::buttonTextAlignment()const
  177. {
  178. Q_D(const ctkCheckablePushButton);
  179. return d->TextAlignment;
  180. }
  181. //-----------------------------------------------------------------------------
  182. void ctkCheckablePushButton::setIndicatorAlignment(Qt::Alignment indicatorAlignment)
  183. {
  184. Q_D(ctkCheckablePushButton);
  185. d->IndicatorAlignment = indicatorAlignment;
  186. this->update();
  187. }
  188. //-----------------------------------------------------------------------------
  189. Qt::Alignment ctkCheckablePushButton::indicatorAlignment()const
  190. {
  191. Q_D(const ctkCheckablePushButton);
  192. return d->IndicatorAlignment;
  193. }
  194. //-----------------------------------------------------------------------------
  195. QSize ctkCheckablePushButton::minimumSizeHint()const
  196. {
  197. Q_D(const ctkCheckablePushButton);
  198. return d->buttonSizeHint();
  199. }
  200. //-----------------------------------------------------------------------------
  201. QSize ctkCheckablePushButton::sizeHint()const
  202. {
  203. return this->minimumSizeHint();
  204. }
  205. //-----------------------------------------------------------------------------
  206. void ctkCheckablePushButton::paintEvent(QPaintEvent * _event)
  207. {
  208. Q_UNUSED(_event);
  209. Q_D(ctkCheckablePushButton);
  210. QPainter p(this);
  211. // Draw Button
  212. QStyleOptionButton opt;
  213. this->initStyleOption(&opt);
  214. QSize indicatorSize = QSize(style()->pixelMetric(QStyle::PM_IndicatorWidth, &opt, this),
  215. style()->pixelMetric(QStyle::PM_IndicatorHeight, &opt, this));
  216. opt.iconSize = indicatorSize;
  217. style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &p, this);
  218. // TBD is PE_PanelButtonCommand better ?
  219. //style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &opt, &p, this);
  220. int buttonHeight = opt.rect.height();
  221. uint tf = d->TextAlignment;
  222. if (this->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, this))
  223. {
  224. tf |= Qt::TextShowMnemonic;
  225. }
  226. else
  227. {
  228. tf |= Qt::TextHideMnemonic;
  229. }
  230. int textWidth = opt.fontMetrics.boundingRect(opt.rect, tf, opt.text).width();
  231. int indicatorSpacing = this->style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing, &opt, this);
  232. int buttonMargin = this->style()->pixelMetric(QStyle::PM_ButtonMargin, &opt, this);
  233. // Draw Indicator
  234. QStyleOption indicatorOpt;
  235. indicatorOpt.init(this);
  236. if (this->isCheckable())
  237. {
  238. indicatorOpt.state |= QStyle::State_On;
  239. }
  240. else
  241. {
  242. indicatorOpt.state |= QStyle::State_Off;
  243. }
  244. indicatorOpt.rect = d->checkboxRect();
  245. this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &indicatorOpt, &p, 0);
  246. // Draw Text
  247. if (d->TextAlignment & Qt::AlignLeft)
  248. {
  249. if (d->IndicatorAlignment & Qt::AlignLeft)
  250. {
  251. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  252. }
  253. else
  254. {
  255. opt.rect.setLeft(opt.rect.x() + buttonMargin);
  256. }
  257. }
  258. else if (d->TextAlignment & Qt::AlignHCenter)
  259. {
  260. if (d->IndicatorAlignment & Qt::AlignHCenter)
  261. {
  262. opt.rect.setLeft(indicatorOpt.rect.right() + indicatorSpacing);
  263. }
  264. else
  265. {
  266. opt.rect.setLeft(opt.rect.x() + opt.rect.width() / 2 - textWidth / 2);
  267. if (d->IndicatorAlignment & Qt::AlignLeft)
  268. {
  269. opt.rect.setLeft( qMax(indicatorOpt.rect.right() + indicatorSpacing, opt.rect.left()) );
  270. }
  271. }
  272. }
  273. else if (d->TextAlignment & Qt::AlignRight)
  274. {
  275. if (d->IndicatorAlignment & Qt::AlignRight)
  276. {
  277. opt.rect.setLeft(indicatorOpt.rect.left() - indicatorSpacing - textWidth);
  278. }
  279. else
  280. {
  281. opt.rect.setLeft(opt.rect.right() - buttonMargin - textWidth);
  282. }
  283. }
  284. // all the computations have been made infering the text would be left oriented
  285. tf &= ~Qt::AlignHCenter & ~Qt::AlignRight;
  286. tf |= Qt::AlignLeft;
  287. this->style()->drawItemText(&p, opt.rect, tf, opt.palette, (opt.state & QStyle::State_Enabled),
  288. opt.text, QPalette::ButtonText);
  289. }
  290. //-----------------------------------------------------------------------------
  291. bool ctkCheckablePushButton::hitButton(const QPoint & _pos)const
  292. {
  293. Q_D(const ctkCheckablePushButton);
  294. return !d->checkboxRect().contains(_pos)
  295. && this->QPushButton::hitButton(_pos);
  296. }
  297. //-----------------------------------------------------------------------------
  298. void ctkCheckablePushButton::initStyleOption(QStyleOptionButton* option)const
  299. {
  300. this->QPushButton::initStyleOption(option);
  301. option->iconSize = QSize(this->style()->pixelMetric(QStyle::PM_IndicatorWidth, option, this),
  302. this->style()->pixelMetric(QStyle::PM_IndicatorHeight, option, this));
  303. }
  304. //-----------------------------------------------------------------------------
  305. void ctkCheckablePushButton::mousePressEvent(QMouseEvent *e)
  306. {
  307. Q_D(ctkCheckablePushButton);
  308. this->QPushButton::mousePressEvent(e);
  309. if (e->isAccepted())
  310. {
  311. return;
  312. }
  313. if (d->checkboxRect().contains(e->pos()))
  314. {
  315. //check the checkbox
  316. this->setCheckable(!this->isCheckable());
  317. this->update();
  318. e->accept();
  319. }
  320. }