ctkCheckablePushButton.cpp 11 KB

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