ctkColorPickerButton.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef __ctkColorPickerButton_h
  15. #define __ctkColorPickerButton_h
  16. // Qt includes
  17. #include <QPushButton>
  18. #include <QColor>
  19. // CTK includes
  20. #include "ctkWidgetsExport.h"
  21. class ctkColorPickerButtonPrivate;
  22. ///
  23. /// ctkColorPickerButton is a QPushButton that refers to a color. The color
  24. /// and the name of the color (i.e. #FFFFFF) are displayed on the button.
  25. /// When clicked, a color dialog pops up to select a new color
  26. /// for the QPushButton.
  27. class CTK_WIDGETS_EXPORT ctkColorPickerButton : public QPushButton
  28. {
  29. Q_OBJECT
  30. Q_FLAGS(ColorDialogOption ColorDialogOptions)
  31. Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged USER true)
  32. Q_PROPERTY(bool displayColorName READ displayColorName WRITE setDisplayColorName DESIGNABLE true)
  33. Q_PROPERTY(ColorDialogOptions dialogOptions READ dialogOptions WRITE setDialogOptions)
  34. public:
  35. enum ColorDialogOption {
  36. ShowAlphaChannel = 0x00000001,
  37. NoButtons = 0x00000002,
  38. DontUseNativeDialog = 0x00000004,
  39. UseCTKColorDialog = 0x0000000C
  40. };
  41. Q_DECLARE_FLAGS(ColorDialogOptions, ColorDialogOption)
  42. /// By default, the color is black
  43. explicit ctkColorPickerButton(QWidget* parent = 0);
  44. /// By default, the color is black. The text will be shown on the button if
  45. /// displayColorName is false, otherwise the color name is shown.
  46. /// \sa QPushButton::setText
  47. explicit ctkColorPickerButton(const QString& text, QWidget* parent = 0 );
  48. /// The text will be shown on the button if
  49. /// displayColorName is false, otherwise the color name is shown.
  50. /// \sa setColor, QPushButton::setText
  51. explicit ctkColorPickerButton(const QColor& color, const QString & text, QWidget* parent = 0 );
  52. virtual ~ctkColorPickerButton();
  53. ///
  54. /// Current selected color
  55. QColor color()const;
  56. ///
  57. /// Display the color name after color selection
  58. bool displayColorName()const;
  59. ///
  60. /// Set the color dialog options to configure the color dialog.
  61. /// \sa QColorDialog::setOptions QColorDialog::ColorDialogOption
  62. void setDialogOptions(const ColorDialogOptions& options);
  63. const ColorDialogOptions& dialogOptions() const;
  64. public slots:
  65. ///
  66. /// Set a new current color without opening a dialog
  67. void setColor(const QColor& color);
  68. ///
  69. /// Opens a color dialog to select a new current color.
  70. void changeColor();
  71. ///
  72. /// Toggle the display of the color name after color selection.
  73. /// By default, this is activated.
  74. void setDisplayColorName(bool displayColorName);
  75. signals:
  76. /// colorChanged is fired anytime a new color is set. Programatically or
  77. /// by the user when choosing a color from the color dialog
  78. void colorChanged(QColor);
  79. protected slots:
  80. void onToggled(bool change = true);
  81. protected:
  82. virtual void paintEvent(QPaintEvent* event);
  83. QScopedPointer<ctkColorPickerButtonPrivate> d_ptr;
  84. private :
  85. Q_DECLARE_PRIVATE(ctkColorPickerButton);
  86. Q_DISABLE_COPY(ctkColorPickerButton);
  87. };
  88. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkColorPickerButton::ColorDialogOptions)
  89. #endif