ctkColorPickerButton.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <QIcon>
  17. #include <QPainter>
  18. #include <QPixmap>
  19. #include <QStyle>
  20. // CTK includes
  21. #include "ctkColorPickerButton.h"
  22. //-----------------------------------------------------------------------------
  23. ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
  24. :QPushButton(_parent)
  25. {
  26. connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
  27. this->setColor(Qt::black);
  28. this->setCheckable(true);
  29. this->setDisplayColorName(true);
  30. }
  31. //-----------------------------------------------------------------------------
  32. ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
  33. :QPushButton(_text, _parent)
  34. {
  35. connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
  36. this->setColor(Qt::black);
  37. // Customize
  38. this->setCheckable(true);
  39. this->setDisplayColorName(true);
  40. }
  41. //-----------------------------------------------------------------------------
  42. ctkColorPickerButton::ctkColorPickerButton(const QColor& _color,
  43. const QString& _text,
  44. QWidget* _parent)
  45. :QPushButton(_text, _parent)
  46. {
  47. connect(this, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
  48. this->setColor(_color);
  49. // Customize
  50. this->setCheckable(true);
  51. this->setDisplayColorName(true);
  52. }
  53. //-----------------------------------------------------------------------------
  54. ctkColorPickerButton::~ctkColorPickerButton()
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkColorPickerButton::changeColor()
  59. {
  60. this->setColor(QColorDialog::getColor(this->Color));
  61. }
  62. //-----------------------------------------------------------------------------
  63. void ctkColorPickerButton::onToggled(bool change)
  64. {
  65. if (change)
  66. {
  67. this->changeColor();
  68. this->setChecked(false);
  69. }
  70. }
  71. //-----------------------------------------------------------------------------
  72. void ctkColorPickerButton::setDisplayColorName(bool displayColorName)
  73. {
  74. this->DisplayColorName = displayColorName;
  75. }
  76. //-----------------------------------------------------------------------------
  77. void ctkColorPickerButton::setColor(const QColor& newColor)
  78. {
  79. if (newColor == this->Color)
  80. {
  81. return;
  82. }
  83. int _iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
  84. QPixmap pix(_iconSize, _iconSize);
  85. pix.fill(palette().button().color());
  86. QPainter p(&pix);
  87. p.setPen(QPen(Qt::gray));
  88. p.setBrush(newColor);
  89. p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
  90. this->setIcon(QIcon(pix));
  91. // Update the button text to the color name, if selected
  92. if (this->DisplayColorName)
  93. {
  94. this->setText(newColor.name());
  95. }
  96. this->Color = newColor;
  97. emit colorChanged(this->Color);
  98. }
  99. //-----------------------------------------------------------------------------
  100. QColor ctkColorPickerButton::color()const
  101. {
  102. return this->Color;
  103. }
  104. //-----------------------------------------------------------------------------
  105. bool ctkColorPickerButton::displayColorName()const
  106. {
  107. return this->DisplayColorName;
  108. }