ctkColorPickerButton.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /// This property controls the name of the color.
  33. /// Black (0,0,0) by default.
  34. Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged USER true)
  35. /// This property controls the name of the color.
  36. /// If empty (default), the color in the format "#RRGGBB" is displayed in the
  37. /// button if \a displayColorName is true, otherwise, the color name is used.
  38. Q_PROPERTY(QString colorName READ colorName WRITE setColorName NOTIFY colorNameChanged)
  39. /// This properties controls whether the name of the color is shown on the
  40. /// button if true or the button text instead. True by default.
  41. /// \sa colorName, QPushButton::text
  42. Q_PROPERTY(bool displayColorName READ displayColorName WRITE setDisplayColorName DESIGNABLE true)
  43. /// This property controls the properties of the dialog used in \a changeColor
  44. Q_PROPERTY(ColorDialogOptions dialogOptions READ dialogOptions WRITE setDialogOptions)
  45. public:
  46. enum ColorDialogOption {
  47. ShowAlphaChannel = 0x00000001,
  48. NoButtons = 0x00000002,
  49. DontUseNativeDialog = 0x00000004,
  50. UseCTKColorDialog = 0x0000000C
  51. };
  52. Q_DECLARE_FLAGS(ColorDialogOptions, ColorDialogOption)
  53. /// By default, the color is black
  54. explicit ctkColorPickerButton(QWidget* parent = 0);
  55. /// By default, the color is black. The text will be shown on the button if
  56. /// displayColorName is false, otherwise the color name is shown.
  57. /// \sa QPushButton::setText
  58. explicit ctkColorPickerButton(const QString& text, QWidget* parent = 0 );
  59. /// The text will be shown on the button if
  60. /// displayColorName is false, otherwise the color name is shown.
  61. /// \sa setColor, QPushButton::setText
  62. explicit ctkColorPickerButton(const QColor& color, const QString & text, QWidget* parent = 0 );
  63. virtual ~ctkColorPickerButton();
  64. /// Current selected color
  65. QColor color()const;
  66. /// Current selected color name.
  67. /// Returns the name of the color in the format "#RRGGBB" or the string set
  68. /// by setColorName().
  69. /// \sa color(), setColorName()
  70. QString colorName()const;
  71. /// Set the current color name.
  72. /// This allows you to give name other than the default "#RRGGBB"
  73. /// Set an invalid QString to restore the default color names
  74. void setColorName(const QString& name);
  75. ///
  76. /// Display the color name after color selection
  77. bool displayColorName()const;
  78. ///
  79. /// Set the color dialog options to configure the color dialog.
  80. /// \sa QColorDialog::setOptions QColorDialog::ColorDialogOption
  81. void setDialogOptions(const ColorDialogOptions& options);
  82. const ColorDialogOptions& dialogOptions() const;
  83. ///
  84. /// Reimplemented to return a toolbutton sizehint when no text is displayed
  85. /// in the button.
  86. virtual QSize sizeHint()const;
  87. public Q_SLOTS:
  88. ///
  89. /// Set a new current color without opening a dialog
  90. void setColor(const QColor& color);
  91. /// Opens a color dialog to select a new current color.
  92. /// If the CTK color dialog (\a UseCTKColorDialog) is used, then the color
  93. /// name is also set if the user selects a named color.
  94. /// \sa ctkColorDialog, color, colorName
  95. void changeColor();
  96. ///
  97. /// Toggle the display of the color name after color selection.
  98. /// By default, this is activated.
  99. void setDisplayColorName(bool displayColorName);
  100. Q_SIGNALS:
  101. /// colorChanged is fired anytime a new color is set. Programatically or
  102. /// by the user when choosing a color from the color dialog
  103. void colorChanged(QColor);
  104. /// This signaled is fired anytime a new color name is set.
  105. void colorNameChanged(QString);
  106. protected Q_SLOTS:
  107. void onToggled(bool change = true);
  108. protected:
  109. virtual void paintEvent(QPaintEvent* event);
  110. QScopedPointer<ctkColorPickerButtonPrivate> d_ptr;
  111. private :
  112. Q_DECLARE_PRIVATE(ctkColorPickerButton);
  113. Q_DISABLE_COPY(ctkColorPickerButton);
  114. };
  115. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkColorPickerButton::ColorDialogOptions)
  116. #endif