ctkColorPickerButton.cpp 7.2 KB

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