ctkCheckBox.cpp 6.4 KB

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