ctkColorPickerButton.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. QString text()const;
  37. QIcon Icon;
  38. QColor Color;
  39. QString ColorName;
  40. bool DisplayColorName;
  41. ctkColorPickerButton::ColorDialogOptions DialogOptions;
  42. mutable QSize CachedSizeHint;
  43. };
  44. //-----------------------------------------------------------------------------
  45. ctkColorPickerButtonPrivate::ctkColorPickerButtonPrivate(ctkColorPickerButton& object)
  46. : q_ptr(&object)
  47. {
  48. this->Color = Qt::black;
  49. this->ColorName = QString();
  50. this->DisplayColorName = true;
  51. this->DialogOptions = 0;
  52. }
  53. //-----------------------------------------------------------------------------
  54. void ctkColorPickerButtonPrivate::init()
  55. {
  56. Q_Q(ctkColorPickerButton);
  57. q->setCheckable(true);
  58. QObject::connect(q, SIGNAL(toggled(bool)),
  59. q, SLOT(onToggled(bool)));
  60. this->computeIcon();
  61. }
  62. //-----------------------------------------------------------------------------
  63. void ctkColorPickerButtonPrivate::computeIcon()
  64. {
  65. Q_Q(ctkColorPickerButton);
  66. int _iconSize = q->style()->pixelMetric(QStyle::PM_SmallIconSize);
  67. QPixmap pix(_iconSize, _iconSize);
  68. pix.fill(q->palette().button().color());
  69. QPainter p(&pix);
  70. p.setPen(QPen(Qt::gray));
  71. p.setBrush(this->Color);
  72. p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
  73. this->Icon = QIcon(pix);
  74. }
  75. //-----------------------------------------------------------------------------
  76. QString ctkColorPickerButtonPrivate::text()const
  77. {
  78. Q_Q(const ctkColorPickerButton);
  79. if (!this->DisplayColorName)
  80. {
  81. return q->text();
  82. }
  83. if (this->ColorName.isEmpty())
  84. {
  85. return this->Color.name();
  86. }
  87. else
  88. {
  89. return this->ColorName;
  90. }
  91. }
  92. //-----------------------------------------------------------------------------
  93. ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
  94. : QPushButton(_parent)
  95. , d_ptr(new ctkColorPickerButtonPrivate(*this))
  96. {
  97. Q_D(ctkColorPickerButton);
  98. d->init();
  99. }
  100. //-----------------------------------------------------------------------------
  101. ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
  102. : QPushButton(_text, _parent)
  103. , d_ptr(new ctkColorPickerButtonPrivate(*this))
  104. {
  105. Q_D(ctkColorPickerButton);
  106. d->init();
  107. }
  108. //-----------------------------------------------------------------------------
  109. ctkColorPickerButton::ctkColorPickerButton(const QColor& _color,
  110. const QString& _text,
  111. QWidget* _parent)
  112. : QPushButton(_text, _parent)
  113. , d_ptr(new ctkColorPickerButtonPrivate(*this))
  114. {
  115. Q_D(ctkColorPickerButton);
  116. d->init();
  117. this->setColor(_color);
  118. }
  119. //-----------------------------------------------------------------------------
  120. ctkColorPickerButton::~ctkColorPickerButton()
  121. {
  122. }
  123. //-----------------------------------------------------------------------------
  124. void ctkColorPickerButton::changeColor()
  125. {
  126. Q_D(ctkColorPickerButton);
  127. QColor newColor;
  128. QString newColorName;
  129. QColorDialog::ColorDialogOptions options;
  130. options |= QColorDialog::ColorDialogOption(
  131. static_cast<int>(d->DialogOptions & ShowAlphaChannel));
  132. options |= QColorDialog::ColorDialogOption(
  133. static_cast<int>(d->DialogOptions & NoButtons));
  134. options |= QColorDialog::ColorDialogOption(
  135. static_cast<int>(d->DialogOptions & DontUseNativeDialog));
  136. if (d->DialogOptions & UseCTKColorDialog)
  137. {
  138. newColor = ctkColorDialog::getColor(d->Color, this, QString(""),options);
  139. newColorName = ctkColorDialog::getColorName();
  140. }
  141. else
  142. {
  143. newColor = QColorDialog::getColor(d->Color, this, QString(""), options);
  144. }
  145. if (newColor.isValid())
  146. {
  147. this->setColor(newColor);
  148. this->setColorName(newColorName);
  149. }
  150. }
  151. //-----------------------------------------------------------------------------
  152. void ctkColorPickerButton::onToggled(bool change)
  153. {
  154. if (change)
  155. {
  156. this->changeColor();
  157. this->setChecked(false);
  158. }
  159. }
  160. //-----------------------------------------------------------------------------
  161. void ctkColorPickerButton::setDisplayColorName(bool displayColorName)
  162. {
  163. Q_D(ctkColorPickerButton);
  164. d->DisplayColorName = displayColorName;
  165. d->CachedSizeHint = QSize();
  166. this->update();
  167. this->updateGeometry();
  168. }
  169. //-----------------------------------------------------------------------------
  170. bool ctkColorPickerButton::displayColorName()const
  171. {
  172. Q_D(const ctkColorPickerButton);
  173. return d->DisplayColorName;
  174. }
  175. //-----------------------------------------------------------------------------
  176. void ctkColorPickerButton::setDialogOptions(const ColorDialogOptions& options)
  177. {
  178. Q_D(ctkColorPickerButton);
  179. d->DialogOptions = options;
  180. }
  181. //-----------------------------------------------------------------------------
  182. const ctkColorPickerButton::ColorDialogOptions& ctkColorPickerButton::dialogOptions()const
  183. {
  184. Q_D(const ctkColorPickerButton);
  185. return d->DialogOptions;
  186. }
  187. //-----------------------------------------------------------------------------
  188. void ctkColorPickerButton::setColor(const QColor& newColor)
  189. {
  190. Q_D(ctkColorPickerButton);
  191. if (newColor == d->Color)
  192. {
  193. return;
  194. }
  195. d->Color = newColor;
  196. d->computeIcon();
  197. this->update();
  198. emit colorChanged(d->Color);
  199. }
  200. //-----------------------------------------------------------------------------
  201. QColor ctkColorPickerButton::color()const
  202. {
  203. Q_D(const ctkColorPickerButton);
  204. return d->Color;
  205. }
  206. //-----------------------------------------------------------------------------
  207. void ctkColorPickerButton::setColorName(const QString& newColorName)
  208. {
  209. Q_D(ctkColorPickerButton);
  210. if (newColorName == d->ColorName)
  211. {
  212. return;
  213. }
  214. d->ColorName = newColorName;
  215. d->CachedSizeHint = QSize();
  216. this->update();
  217. this->updateGeometry();
  218. emit colorNameChanged(d->ColorName);
  219. }
  220. //-----------------------------------------------------------------------------
  221. QString ctkColorPickerButton::colorName()const
  222. {
  223. Q_D(const ctkColorPickerButton);
  224. return d->ColorName;
  225. }
  226. //-----------------------------------------------------------------------------
  227. void ctkColorPickerButton::paintEvent(QPaintEvent *)
  228. {
  229. Q_D(ctkColorPickerButton);
  230. QStylePainter p(this);
  231. QStyleOptionButton option;
  232. this->initStyleOption(&option);
  233. option.text = d->text();
  234. option.icon = d->Icon;
  235. p.drawControl(QStyle::CE_PushButton, option);
  236. }
  237. //-----------------------------------------------------------------------------
  238. QSize ctkColorPickerButton::sizeHint()const
  239. {
  240. Q_D(const ctkColorPickerButton);
  241. if (!d->DisplayColorName && !this->text().isEmpty())
  242. {
  243. return this->QPushButton::sizeHint();
  244. }
  245. if (d->CachedSizeHint.isValid())
  246. {
  247. return d->CachedSizeHint;
  248. }
  249. // If no text, the sizehint is a QToolButton sizeHint
  250. QStyleOptionButton pushButtonOpt;
  251. this->initStyleOption(&pushButtonOpt);
  252. pushButtonOpt.text = d->text();
  253. int iconSize = this->style()->pixelMetric(QStyle::PM_SmallIconSize);
  254. if (pushButtonOpt.text == QString())
  255. {
  256. QStyleOptionToolButton opt;
  257. (&opt)->QStyleOption::operator=(pushButtonOpt);
  258. opt.arrowType = Qt::NoArrow;
  259. opt.icon = d->Icon;
  260. opt.iconSize = QSize(iconSize, iconSize);
  261. opt.rect.setSize(opt.iconSize); // PM_MenuButtonIndicator depends on the height
  262. d->CachedSizeHint = this->style()->sizeFromContents(
  263. QStyle::CT_ToolButton, &opt, opt.iconSize, this).
  264. expandedTo(QApplication::globalStrut());
  265. }
  266. else
  267. {
  268. pushButtonOpt.icon = d->Icon;
  269. pushButtonOpt.iconSize = QSize(iconSize, iconSize);
  270. pushButtonOpt.rect.setSize(pushButtonOpt.iconSize); // PM_MenuButtonIndicator depends on the height
  271. d->CachedSizeHint = (style()->sizeFromContents(
  272. QStyle::CT_PushButton, &pushButtonOpt, pushButtonOpt.iconSize, this).
  273. expandedTo(QApplication::globalStrut()));
  274. }
  275. return d->CachedSizeHint;
  276. }