ctkColorPickerButton.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // Qt includes
  11. #include <QColorDialog>
  12. #include <QIcon>
  13. #include <QPainter>
  14. #include <QPixmap>
  15. #include <QStyle>
  16. // CTK includes
  17. #include "ctkColorPickerButton.h"
  18. //-----------------------------------------------------------------------------
  19. ctkColorPickerButton::ctkColorPickerButton(QWidget* _parent)
  20. :QPushButton(_parent)
  21. {
  22. connect(this, SIGNAL(toggled(bool)), this, SLOT(changeColor(bool)));
  23. this->setColor(Qt::black);
  24. this->setCheckable(true);
  25. }
  26. //-----------------------------------------------------------------------------
  27. ctkColorPickerButton::ctkColorPickerButton(const QString& _text, QWidget* _parent)
  28. :QPushButton(_text, _parent)
  29. {
  30. connect(this, SIGNAL(clicked), this, SLOT(changeColor));
  31. this->setColor(Qt::black);
  32. // Customize
  33. this->setCheckable(true);
  34. }
  35. //-----------------------------------------------------------------------------
  36. ctkColorPickerButton::ctkColorPickerButton(const QColor& _color,
  37. const QString& _text,
  38. QWidget* _parent)
  39. :QPushButton(_text, _parent)
  40. {
  41. connect(this, SIGNAL(clicked), this, SLOT(changeColor));
  42. this->setColor(_color);
  43. // Customize
  44. this->setCheckable(true);
  45. }
  46. //-----------------------------------------------------------------------------
  47. void ctkColorPickerButton::changeColor(bool change)
  48. {
  49. if (change)
  50. {
  51. this->setColor(QColorDialog::getColor(this->Color));
  52. this->setChecked(false);
  53. }
  54. }
  55. //-----------------------------------------------------------------------------
  56. void ctkColorPickerButton::setColor(const QColor& _color)
  57. {
  58. if (_color == this->Color)
  59. {
  60. return;
  61. }
  62. int _iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
  63. QPixmap pix(_iconSize, _iconSize);
  64. pix.fill(palette().button().color());
  65. QPainter p(&pix);
  66. p.setPen(QPen(Qt::gray));
  67. p.setBrush(_color);
  68. p.drawRect(2, 2, pix.width() - 5, pix.height() - 5);
  69. this->setIcon(QIcon(pix));
  70. this->setText(_color.name());
  71. this->Color = _color;
  72. emit colorChanged(this->Color);
  73. }
  74. //-----------------------------------------------------------------------------
  75. QColor ctkColorPickerButton::color()const
  76. {
  77. return this->Color;
  78. }