ctkCheckBox.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <QProxyStyle>
  17. #include <QStyleOption>
  18. // CTK includes
  19. #include "ctkCheckBox.h"
  20. #include <iostream>
  21. // ----------------------------------------------------------------------------
  22. class ctkCheckBoxStyle : public QProxyStyle
  23. {
  24. public:
  25. ctkCheckBoxStyle(QStyle *parentStyle);
  26. virtual void drawPrimitive(QStyle::PrimitiveElement pe,
  27. const QStyleOption * opt,
  28. QPainter * p,
  29. const QWidget * widget = 0) const;
  30. virtual int pixelMetric(QStyle::PixelMetric metric,
  31. const QStyleOption *option,
  32. const QWidget *widget = 0) const;
  33. QIcon indicatorIcon;
  34. QSize indicatorSize;
  35. };
  36. // ----------------------------------------------------------------------------
  37. // Methods ctkCheckBoxStyle
  38. // ----------------------------------------------------------------------------
  39. ctkCheckBoxStyle::ctkCheckBoxStyle(QStyle *parentStyle)
  40. : QProxyStyle(parentStyle)
  41. {
  42. }
  43. // ----------------------------------------------------------------------------
  44. void ctkCheckBoxStyle::drawPrimitive(QStyle::PrimitiveElement pe,
  45. const QStyleOption * opt,
  46. QPainter * p, const QWidget * widget) const
  47. {
  48. if (pe == QStyle::PE_IndicatorCheckBox)
  49. {
  50. const ctkCheckBox* checkBox= qobject_cast<const ctkCheckBox*>(widget);
  51. if (checkBox && !indicatorIcon.isNull())
  52. {
  53. QIcon::Mode mode =
  54. (opt->state & QStyle::State_MouseOver) == QStyle::State_MouseOver
  55. ? QIcon::Active : QIcon::Normal;
  56. mode = (opt->state & QStyle::State_Sunken) == QStyle::State_Sunken
  57. ? QIcon::Selected : mode;
  58. mode = (opt->state & QStyle::State_Enabled) == QStyle::State_Enabled
  59. ? mode : QIcon::Disabled;
  60. QIcon::State state =
  61. (opt->state & QStyle::State_On) == QStyle::State_On
  62. ? QIcon::Off : QIcon::On;
  63. state = (opt->state & QStyle::State_Sunken) == QStyle::State_Sunken
  64. ? QIcon::Off : state ;
  65. this->drawItemPixmap(p, opt->rect, Qt::AlignHCenter,
  66. this->indicatorIcon.pixmap(
  67. opt->rect.width(), opt->rect.height(),
  68. mode,
  69. state));
  70. return;
  71. }
  72. }
  73. this->QProxyStyle::drawPrimitive(pe, opt, p, widget);
  74. }
  75. // ----------------------------------------------------------------------------
  76. int ctkCheckBoxStyle::pixelMetric(QStyle::PixelMetric metric,
  77. const QStyleOption *option,
  78. const QWidget *widget) const
  79. {
  80. const ctkCheckBox* checkBox= qobject_cast<const ctkCheckBox*>(widget);
  81. if (checkBox && !indicatorIcon.isNull())
  82. {
  83. if(metric == QStyle::PM_IndicatorHeight && !this->indicatorSize.isEmpty())
  84. {
  85. return this->indicatorIcon.actualSize(this->indicatorSize).height();
  86. }
  87. if(metric == QStyle::PM_IndicatorWidth && !this->indicatorSize.isEmpty())
  88. {
  89. return this->indicatorIcon.actualSize(this->indicatorSize).width();
  90. }
  91. }
  92. return this->QProxyStyle::pixelMetric(metric, option, widget);
  93. }
  94. // ----------------------------------------------------------------------------
  95. class ctkCheckBoxPrivate
  96. {
  97. Q_DECLARE_PUBLIC(ctkCheckBox);
  98. protected:
  99. ctkCheckBox* const q_ptr;
  100. public:
  101. ctkCheckBoxPrivate(ctkCheckBox& object);
  102. void init();
  103. ctkCheckBoxStyle* iconStyle;
  104. };
  105. // ----------------------------------------------------------------------------
  106. // Methods ctkCheckBoxPrivate
  107. // ----------------------------------------------------------------------------
  108. ctkCheckBoxPrivate::ctkCheckBoxPrivate(ctkCheckBox &object)
  109. : q_ptr(&object)
  110. {
  111. this->iconStyle = 0;
  112. }
  113. // ----------------------------------------------------------------------------
  114. void ctkCheckBoxPrivate::init()
  115. {
  116. Q_Q(ctkCheckBox);
  117. QWidget* parent = q->parentWidget();
  118. QStyle* parentStyle = (parent) ? parent->style() : QApplication::style();
  119. this->iconStyle = new ctkCheckBoxStyle(parentStyle);
  120. q->setStyle(this->iconStyle);
  121. }
  122. // ----------------------------------------------------------------------------
  123. // Methods ctkCheckBox
  124. // ----------------------------------------------------------------------------
  125. ctkCheckBox::ctkCheckBox(QWidget *_parent)
  126. :Superclass(_parent)
  127. , d_ptr(new ctkCheckBoxPrivate(*this))
  128. {
  129. Q_D(ctkCheckBox);
  130. d->init();
  131. }
  132. // ----------------------------------------------------------------------------
  133. ctkCheckBox::~ctkCheckBox()
  134. {
  135. }
  136. // ----------------------------------------------------------------------------
  137. void ctkCheckBox::setIndicatorIcon(const QIcon& newIcon)
  138. {
  139. Q_D(ctkCheckBox);
  140. d->iconStyle->indicatorIcon = newIcon;
  141. this->update();
  142. }
  143. // ----------------------------------------------------------------------------
  144. QIcon ctkCheckBox::indicatorIcon() const
  145. {
  146. Q_D(const ctkCheckBox);
  147. return d->iconStyle->indicatorIcon;
  148. }
  149. // ----------------------------------------------------------------------------
  150. void ctkCheckBox::setIndicatorIconSize(const QSize& newSize)
  151. {
  152. Q_D(ctkCheckBox);
  153. d->iconStyle->indicatorSize = newSize;
  154. this->update();
  155. }
  156. // ----------------------------------------------------------------------------
  157. QSize ctkCheckBox::indicatorIconSize() const
  158. {
  159. Q_D(const ctkCheckBox);
  160. return d->iconStyle->indicatorSize;
  161. }