ctkColorPickerButton.h 3.8 KB

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