ctkCheckBox.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. : QProxyStyle(parentStyle)
  27. {}
  28. void setCheckIcon(const QIcon& newIcon)
  29. {
  30. this->checkBoxIcon = newIcon;
  31. }
  32. QIcon checkIcon() const
  33. {
  34. return this->checkBoxIcon;
  35. }
  36. virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption * opt, QPainter * p, const QWidget * widget = 0) const
  37. {
  38. if (pe == QStyle::PE_IndicatorCheckBox)
  39. {
  40. const ctkCheckBox* checkBox= qobject_cast<const ctkCheckBox*>(widget);
  41. if (checkBox && !checkBoxIcon.isNull())
  42. {
  43. QIcon::Mode mode =
  44. (checkBox->testAttribute(Qt::WA_Hover) && checkBox->underMouse()) ?
  45. QIcon::Active : QIcon::Normal;
  46. mode = checkBox->isDown() ? QIcon::Selected : mode;
  47. mode = checkBox->isEnabled() ? mode : QIcon::Disabled;
  48. this->drawItemPixmap(p, opt->rect, Qt::AlignHCenter,
  49. checkBoxIcon.pixmap(
  50. opt->rect.width(), opt->rect.height(),
  51. mode,
  52. checkBox->isChecked() ? QIcon::Off : QIcon::On));
  53. return;
  54. }
  55. }
  56. this->QProxyStyle::drawPrimitive(pe, opt, p, widget);
  57. }
  58. protected:
  59. QIcon checkBoxIcon;
  60. };
  61. // ----------------------------------------------------------------------------
  62. class ctkCheckBoxPrivate
  63. {
  64. Q_DECLARE_PUBLIC(ctkCheckBox);
  65. protected:
  66. ctkCheckBox* const q_ptr;
  67. public:
  68. ctkCheckBoxPrivate(ctkCheckBox& object);
  69. void init();
  70. ctkCheckBoxStyle* iconStyle;
  71. };
  72. // ----------------------------------------------------------------------------
  73. ctkCheckBoxPrivate::ctkCheckBoxPrivate(ctkCheckBox &object)
  74. : q_ptr(&object)
  75. {
  76. this->iconStyle = 0;
  77. }
  78. // ----------------------------------------------------------------------------
  79. void ctkCheckBoxPrivate::init()
  80. {
  81. Q_Q(ctkCheckBox);
  82. this->iconStyle = new ctkCheckBoxStyle(0);
  83. this->iconStyle->setParent(q);
  84. q->setStyle(this->iconStyle);
  85. }
  86. // ----------------------------------------------------------------------------
  87. // Methods ctkCheckBox
  88. // ----------------------------------------------------------------------------
  89. ctkCheckBox::ctkCheckBox(QWidget *_parent)
  90. :Superclass(_parent)
  91. , d_ptr(new ctkCheckBoxPrivate(*this))
  92. {
  93. Q_D(ctkCheckBox);
  94. d->init();
  95. }
  96. // ----------------------------------------------------------------------------
  97. ctkCheckBox::~ctkCheckBox()
  98. {
  99. }
  100. // ----------------------------------------------------------------------------
  101. void ctkCheckBox::setCheckIcon(const QIcon& newIcon)
  102. {
  103. Q_D(ctkCheckBox);
  104. d->iconStyle->setCheckIcon(newIcon);
  105. this->update();
  106. }
  107. // ----------------------------------------------------------------------------
  108. QIcon ctkCheckBox::checkIcon() const
  109. {
  110. Q_D(const ctkCheckBox);
  111. return d->iconStyle->checkIcon();
  112. }