ctkCheckBox.cpp 6.0 KB

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