ctkColorPickerButton.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_ENUMS(ColorDialogOption)
  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 DESIGNABLE true)
  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(ColorDialogOptions options);
  64. ColorDialogOptions dialogOptions() const;
  65. public slots:
  66. ///
  67. /// Set a new current color without opening a dialog
  68. void setColor(const QColor& color);
  69. ///
  70. /// Opens a color dialog to select a new current color.
  71. void changeColor();
  72. ///
  73. /// Toggle the display of the color name after color selection.
  74. /// By default, this is activated.
  75. void setDisplayColorName(bool displayColorName);
  76. signals:
  77. /// colorChanged is fired anytime a new color is set. Programatically or
  78. /// by the user when choosing a color from the color dialog
  79. void colorChanged(QColor);
  80. protected slots:
  81. void onToggled(bool change = true);
  82. protected:
  83. virtual void paintEvent(QPaintEvent* event);
  84. QScopedPointer<ctkColorPickerButtonPrivate> d_ptr;
  85. private :
  86. Q_DECLARE_PRIVATE(ctkColorPickerButton);
  87. Q_DISABLE_COPY(ctkColorPickerButton);
  88. };
  89. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkColorPickerButton::ColorDialogOptions)
  90. #endif