ctkCheckBoxPixmaps.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.commontk.org/LICENSE
  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. /*=========================================================================
  15. Program: ParaView
  16. Module: pqCheckBoxPixMaps.cxx
  17. Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
  18. All rights reserved.
  19. ParaView is a free software; you can redistribute it and/or modify it
  20. under the terms of the ParaView license version 1.2.
  21. See License_v1.2.txt for the full ParaView license.
  22. A copy of this license can be obtained by contacting
  23. Kitware Inc.
  24. 28 Corporate Drive
  25. Clifton Park, NY 12065
  26. USA
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. ========================================================================*/
  39. // Qt includes
  40. #include <QApplication>
  41. #include <QPainter>
  42. #include <QPixmap>
  43. #include <QStyle>
  44. #include <QStyleOptionButton>
  45. #include <QWidget>
  46. // CTK includes
  47. #include "ctkCheckBoxPixmaps.h"
  48. class ctkCheckBoxPixmapsPrivate
  49. {
  50. public:
  51. enum PixmapStateIndex
  52. {
  53. Checked = 0,
  54. PartiallyChecked = 1,
  55. UnChecked = 2,
  56. // All active states in lower half
  57. Checked_Active = 3,
  58. PartiallyChecked_Active = 4,
  59. UnChecked_Active = 5,
  60. PixmapCount = 6
  61. };
  62. QPixmap Pixmaps[7];
  63. };
  64. //-----------------------------------------------------------------------------
  65. ctkCheckBoxPixmaps::ctkCheckBoxPixmaps(QWidget* parentWidget)
  66. : Superclass(parentWidget)
  67. , d_ptr(new ctkCheckBoxPixmapsPrivate)
  68. {
  69. Q_D(ctkCheckBoxPixmaps);
  70. // Initialize the pixmaps. The following style array should
  71. // correspond to the PixmapStateIndex enum.
  72. const QStyle::State PixmapStyle[] =
  73. {
  74. QStyle::State_On | QStyle::State_Enabled,
  75. QStyle::State_NoChange | QStyle::State_Enabled,
  76. QStyle::State_Off | QStyle::State_Enabled,
  77. QStyle::State_On | QStyle::State_Enabled | QStyle::State_Active,
  78. QStyle::State_NoChange | QStyle::State_Enabled | QStyle::State_Active,
  79. QStyle::State_Off | QStyle::State_Enabled | QStyle::State_Active
  80. };
  81. QStyleOptionButton option;
  82. QStyle* style = parentWidget ? parentWidget->style() : qApp->style();
  83. QRect r = style->subElementRect(
  84. QStyle::SE_CheckBoxIndicator, &option, parentWidget);
  85. option.rect = QRect(QPoint(0,0), r.size());
  86. for(int i = 0; i < ctkCheckBoxPixmapsPrivate::PixmapCount; i++)
  87. {
  88. d->Pixmaps[i] = QPixmap(r.size());
  89. d->Pixmaps[i].fill(QColor(0, 0, 0, 0));
  90. QPainter painter(&d->Pixmaps[i]);
  91. option.state = PixmapStyle[i];
  92. style->drawPrimitive(
  93. QStyle::PE_IndicatorCheckBox, &option, &painter, parentWidget);
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. ctkCheckBoxPixmaps::~ctkCheckBoxPixmaps()
  98. {
  99. }
  100. //-----------------------------------------------------------------------------
  101. const QPixmap& ctkCheckBoxPixmaps::pixmap(Qt::CheckState state, bool active) const
  102. {
  103. Q_D(const ctkCheckBoxPixmaps);
  104. int offset = active ? 3 : 0;
  105. switch (state)
  106. {
  107. case Qt::Checked:
  108. return d->Pixmaps[offset + ctkCheckBoxPixmapsPrivate::Checked];
  109. case Qt::Unchecked:
  110. return d->Pixmaps[offset + ctkCheckBoxPixmapsPrivate::UnChecked];
  111. case Qt::PartiallyChecked:
  112. return d->Pixmaps[offset + ctkCheckBoxPixmapsPrivate::PartiallyChecked];
  113. default:
  114. return d->Pixmaps[ctkCheckBoxPixmapsPrivate::PixmapCount];
  115. }
  116. return d->Pixmaps[ctkCheckBoxPixmapsPrivate::PixmapCount];
  117. }