ctkColorPickerButton.cpp 9.0 KB

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