ctkColorPickerButton.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // Qt includes
  15. #include <QColorDialog>
  16. #include <QDebug>
  17. #include <QIcon>
  18. #include <QPainter>
  19. #include <QPixmap>
  20. #include <QStyle>
  21. #include <QStyleOptionButton>
  22. #include <QStylePainter>
  23. // CTK includes
  24. #include "ctkColorPickerButton.h"
  25. class ctkColorPickerButtonPrivate
  26. {
  27. Q_DECLARE_PUBLIC(ctkColorPickerButton);
  28. protected:
  29. ctkColorPickerButton* const q_ptr;
  30. public:
  31. ctkColorPickerButtonPrivate(ctkColorPickerButton& object);
  32. void init();
  33. void computeIcon();
  34. QIcon Icon;
  35. QColor Color;
  36. bool DisplayColorName;
  37. bool ShowAlpha;
  38. };
  39. //-----------------------------------------------------------------------------
  40. ctkColorPickerButtonPrivate::ctkColorPickerButtonPrivate(ctkColorPickerButton& object)
  41. : q_ptr(&object)
  42. {
  43. this->Color = Qt::black;
  44. this->DisplayColorName = true;
  45. this->ShowAlpha = false;
  46. }
  47. //-----------------------------------------------------------------------------
  48. void ctkColorPickerButtonPrivate::init()
  49. {
  50. Q_Q(ctkColorPickerButton);
  51. q->setCheckable(true);
  52. QObject::connect(q, SIGNAL(toggled(bool)),
  53. q, SLOT(onToggled(bool)));
  54. this->computeIcon();
  55. }
  56. //-----------------------------------------------------------------------------
  57. void ctkColorPickerButtonPrivate::computeIcon()
  58. {
  59. Q_Q(ctkColorPickerButton);
  60. int _iconSize = q->style()->pixelMetric(QStyle::PM_SmallIconSize);
  61. QPixmap pix(_iconSize, _iconSize);
  62. pix.fill(q->palette().button().color());
  63. QPainter p(&pix);
  64. p.setPen(QPen(Qt::gray));
  65. p.setBrush(this->Color);
  66. p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
  67. this->Icon = QIcon(pix);
  68. }
  69. //-----------------------------------------------------------------------------
  70. ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
  71. : QPushButton(_parent)
  72. , d_ptr(new ctkColorPickerButtonPrivate(*this))
  73. {
  74. Q_D(ctkColorPickerButton);
  75. d->init();
  76. }
  77. //-----------------------------------------------------------------------------
  78. ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
  79. : QPushButton(_text, _parent)
  80. , d_ptr(new ctkColorPickerButtonPrivate(*this))
  81. {
  82. Q_D(ctkColorPickerButton);
  83. d->init();
  84. }
  85. //-----------------------------------------------------------------------------
  86. ctkColorPickerButton::ctkColorPickerButton(const QColor& _color,
  87. const QString& _text,
  88. QWidget* _parent)
  89. : QPushButton(_text, _parent)
  90. , d_ptr(new ctkColorPickerButtonPrivate(*this))
  91. {
  92. Q_D(ctkColorPickerButton);
  93. d->init();
  94. this->setColor(_color);
  95. }
  96. //-----------------------------------------------------------------------------
  97. ctkColorPickerButton::~ctkColorPickerButton()
  98. {
  99. }
  100. //-----------------------------------------------------------------------------
  101. void ctkColorPickerButton::changeColor()
  102. {
  103. Q_D(ctkColorPickerButton);
  104. QColor res;
  105. if (d->ShowAlpha)
  106. {
  107. res = QColorDialog::getColor(
  108. d->Color, this, QString(""), QColorDialog::ShowAlphaChannel );
  109. }
  110. else
  111. {
  112. res = QColorDialog::getColor(d->Color);
  113. }
  114. if (res.isValid())
  115. {
  116. this->setColor(res);
  117. }
  118. }
  119. //-----------------------------------------------------------------------------
  120. void ctkColorPickerButton::onToggled(bool change)
  121. {
  122. if (change)
  123. {
  124. this->changeColor();
  125. this->setChecked(false);
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. void ctkColorPickerButton::setDisplayColorName(bool displayColorName)
  130. {
  131. Q_D(ctkColorPickerButton);
  132. d->DisplayColorName = displayColorName;
  133. this->update();
  134. }
  135. //-----------------------------------------------------------------------------
  136. bool ctkColorPickerButton::displayColorName()const
  137. {
  138. Q_D(const ctkColorPickerButton);
  139. return d->DisplayColorName;
  140. }
  141. //-----------------------------------------------------------------------------
  142. void ctkColorPickerButton::setShowAlpha(bool show)
  143. {
  144. Q_D(ctkColorPickerButton);
  145. d->ShowAlpha = show;
  146. }
  147. //-----------------------------------------------------------------------------
  148. bool ctkColorPickerButton::showAlpha()const
  149. {
  150. Q_D(const ctkColorPickerButton);
  151. return d->ShowAlpha;
  152. }
  153. //-----------------------------------------------------------------------------
  154. void ctkColorPickerButton::setColor(const QColor& newColor)
  155. {
  156. Q_D(ctkColorPickerButton);
  157. if (newColor == d->Color)
  158. {
  159. return;
  160. }
  161. d->Color = newColor;
  162. d->computeIcon();
  163. this->update();
  164. emit colorChanged(d->Color);
  165. }
  166. //-----------------------------------------------------------------------------
  167. QColor ctkColorPickerButton::color()const
  168. {
  169. Q_D(const ctkColorPickerButton);
  170. return d->Color;
  171. }
  172. //-----------------------------------------------------------------------------
  173. void ctkColorPickerButton::paintEvent(QPaintEvent *)
  174. {
  175. Q_D(ctkColorPickerButton);
  176. QStylePainter p(this);
  177. QStyleOptionButton option;
  178. initStyleOption(&option);
  179. if (d->DisplayColorName)
  180. {
  181. option.text = d->Color.name();
  182. }
  183. option.icon = d->Icon;
  184. p.drawControl(QStyle::CE_PushButton, option);
  185. }