ctkCheckBox.cpp 6.4 KB

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