ctkColorPickerButton.cpp 6.2 KB

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