color_wheel.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * \file
  3. *
  4. * \author Mattia Basaglia
  5. *
  6. * \copyright Copyright (C) 2013-2020 Mattia Basaglia
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef COLOR_WHEEL_HPP
  23. #define COLOR_WHEEL_HPP
  24. #include <QWidget>
  25. #include "colorwidgets_global.hpp"
  26. namespace color_widgets {
  27. /**
  28. * \brief Display an analog widget that allows the selection of a HSV color
  29. *
  30. * It has an outer wheel to select the Hue and an intenal square to select
  31. * Saturation and Lightness.
  32. */
  33. class QCP_EXPORT ColorWheel : public QWidget
  34. {
  35. Q_OBJECT
  36. Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged DESIGNABLE true STORED false )
  37. Q_PROPERTY(qreal hue READ hue WRITE setHue DESIGNABLE false )
  38. Q_PROPERTY(qreal saturation READ saturation WRITE setSaturation DESIGNABLE false )
  39. Q_PROPERTY(qreal value READ value WRITE setValue DESIGNABLE false )
  40. Q_PROPERTY(unsigned wheelWidth READ wheelWidth WRITE setWheelWidth NOTIFY wheelWidthChanged DESIGNABLE true )
  41. Q_PROPERTY(ShapeEnum selectorShape READ selectorShape WRITE setSelectorShape NOTIFY selectorShapeChanged DESIGNABLE true )
  42. Q_PROPERTY(bool rotatingSelector READ rotatingSelector WRITE setRotatingSelector NOTIFY rotatingSelectorChanged DESIGNABLE true )
  43. Q_PROPERTY(ColorSpaceEnum colorSpace READ colorSpace WRITE setColorSpace NOTIFY colorSpaceChanged DESIGNABLE true )
  44. public:
  45. enum ShapeEnum
  46. {
  47. ShapeTriangle, ///< A triangle
  48. ShapeSquare, ///< A square
  49. };
  50. enum AngleEnum
  51. {
  52. AngleFixed, ///< The inner part doesn't rotate
  53. AngleRotating, ///< The inner part follows the hue selector
  54. };
  55. enum ColorSpaceEnum
  56. {
  57. ColorHSV, ///< Use the HSV color space
  58. ColorHSL, ///< Use the HSL color space
  59. ColorLCH, ///< Use Luma Chroma Hue (Y_601')
  60. };
  61. Q_ENUM(ShapeEnum);
  62. Q_ENUM(AngleEnum);
  63. Q_ENUM(ColorSpaceEnum);
  64. explicit ColorWheel(QWidget *parent = 0);
  65. ~ColorWheel();
  66. /// Get current color
  67. QColor color() const;
  68. virtual QSize sizeHint() const Q_DECL_OVERRIDE;
  69. /// Get current hue in the range [0-1]
  70. qreal hue() const;
  71. /// Get current saturation in the range [0-1]
  72. qreal saturation() const;
  73. /// Get current value in the range [0-1]
  74. qreal value() const;
  75. /// Get the width in pixels of the outer wheel
  76. unsigned int wheelWidth() const;
  77. /// Set the width in pixels of the outer wheel
  78. void setWheelWidth(unsigned int w);
  79. /// Shape of the internal selector
  80. ShapeEnum selectorShape() const;
  81. /// Whether the internal selector should rotare in accordance with the hue
  82. bool rotatingSelector() const;
  83. /// Color space used to preview/edit the color
  84. ColorSpaceEnum colorSpace() const;
  85. public Q_SLOTS:
  86. /// Set current color
  87. void setColor(QColor c);
  88. /**
  89. * @param h Hue [0-1]
  90. */
  91. void setHue(qreal h);
  92. /**
  93. * @param s Saturation [0-1]
  94. */
  95. void setSaturation(qreal s);
  96. /**
  97. * @param v Value [0-1]
  98. */
  99. void setValue(qreal v);
  100. /// Sets the shape of the internal selector
  101. void setSelectorShape(ShapeEnum shape);
  102. /// Sets whether the internal selector should rotare in accordance with the hue
  103. void setRotatingSelector(bool rotating);
  104. /// Sets the color space used to preview/edit the color
  105. void setColorSpace(ColorSpaceEnum space);
  106. Q_SIGNALS:
  107. /**
  108. * Emitted when the user selects a color or setColor is called
  109. */
  110. void colorChanged(QColor);
  111. /**
  112. * Emitted when the user selects a color
  113. */
  114. void colorSelected(QColor);
  115. void wheelWidthChanged(unsigned);
  116. void selectorShapeChanged(ShapeEnum shape);
  117. void rotatingSelectorChanged(bool rotating);
  118. void colorSpaceChanged(ColorSpaceEnum space);
  119. /**
  120. * Emitted when the user releases from dragging
  121. */
  122. void editingFinished();
  123. protected:
  124. void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
  125. void mouseMoveEvent(QMouseEvent *) Q_DECL_OVERRIDE;
  126. void mousePressEvent(QMouseEvent *) Q_DECL_OVERRIDE;
  127. void mouseReleaseEvent(QMouseEvent *) Q_DECL_OVERRIDE;
  128. void resizeEvent(QResizeEvent *) Q_DECL_OVERRIDE;
  129. void dragEnterEvent(QDragEnterEvent* event) Q_DECL_OVERRIDE;
  130. void dropEvent(QDropEvent* event) Q_DECL_OVERRIDE;
  131. protected:
  132. class Private;
  133. ColorWheel(QWidget *parent, Private* data);
  134. Private* data() const { return p; }
  135. private:
  136. Private * const p;
  137. };
  138. } // namespace color_widgets
  139. #endif // COLOR_WHEEL_HPP