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 25 #include <QWidget> 26 27 #include "colorwidgets_global.hpp" 28 29 30 namespace color_widgets { 31 32 /** 33 * \brief Display an analog widget that allows the selection of a HSV color 34 * 35 * It has an outer wheel to select the Hue and an intenal square to select 36 * Saturation and Lightness. 37 */ 38 class QCP_EXPORT ColorWheel : public QWidget 39 { 40 Q_OBJECT 41 42 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged DESIGNABLE true STORED false ) 43 Q_PROPERTY(qreal hue READ hue WRITE setHue DESIGNABLE false ) 44 Q_PROPERTY(qreal saturation READ saturation WRITE setSaturation DESIGNABLE false ) 45 Q_PROPERTY(qreal value READ value WRITE setValue DESIGNABLE false ) 46 Q_PROPERTY(unsigned wheelWidth READ wheelWidth WRITE setWheelWidth NOTIFY wheelWidthChanged DESIGNABLE true ) 47 Q_PROPERTY(ShapeEnum selectorShape READ selectorShape WRITE setSelectorShape NOTIFY selectorShapeChanged DESIGNABLE true ) 48 Q_PROPERTY(bool rotatingSelector READ rotatingSelector WRITE setRotatingSelector NOTIFY rotatingSelectorChanged DESIGNABLE true ) 49 Q_PROPERTY(ColorSpaceEnum colorSpace READ colorSpace WRITE setColorSpace NOTIFY colorSpaceChanged DESIGNABLE true ) 50 51 public: 52 enum ShapeEnum 53 { 54 ShapeTriangle, ///< A triangle 55 ShapeSquare, ///< A square 56 }; 57 58 enum AngleEnum 59 { 60 AngleFixed, ///< The inner part doesn't rotate 61 AngleRotating, ///< The inner part follows the hue selector 62 }; 63 64 enum ColorSpaceEnum 65 { 66 ColorHSV, ///< Use the HSV color space 67 ColorHSL, ///< Use the HSL color space 68 ColorLCH, ///< Use Luma Chroma Hue (Y_601') 69 }; 70 71 Q_ENUM(ShapeEnum); 72 Q_ENUM(AngleEnum); 73 Q_ENUM(ColorSpaceEnum); 74 75 explicit ColorWheel(QWidget *parent = 0); 76 ~ColorWheel(); 77 78 /// Get current color 79 QColor color() const; 80 81 virtual QSize sizeHint() const Q_DECL_OVERRIDE; 82 83 /// Get current hue in the range [0-1] 84 qreal hue() const; 85 86 /// Get current saturation in the range [0-1] 87 qreal saturation() const; 88 89 /// Get current value in the range [0-1] 90 qreal value() const; 91 92 /// Get the width in pixels of the outer wheel 93 unsigned int wheelWidth() const; 94 95 /// Set the width in pixels of the outer wheel 96 void setWheelWidth(unsigned int w); 97 98 /// Shape of the internal selector 99 ShapeEnum selectorShape() const; 100 101 /// Whether the internal selector should rotare in accordance with the hue 102 bool rotatingSelector() const; 103 104 /// Color space used to preview/edit the color 105 ColorSpaceEnum colorSpace() const; 106 107 public Q_SLOTS: 108 109 /// Set current color 110 void setColor(QColor c); 111 112 /** 113 * @param h Hue [0-1] 114 */ 115 void setHue(qreal h); 116 117 /** 118 * @param s Saturation [0-1] 119 */ 120 void setSaturation(qreal s); 121 122 /** 123 * @param v Value [0-1] 124 */ 125 void setValue(qreal v); 126 127 /// Sets the shape of the internal selector 128 void setSelectorShape(ShapeEnum shape); 129 130 /// Sets whether the internal selector should rotare in accordance with the hue 131 void setRotatingSelector(bool rotating); 132 133 /// Sets the color space used to preview/edit the color 134 void setColorSpace(ColorSpaceEnum space); 135 136 Q_SIGNALS: 137 /** 138 * Emitted when the user selects a color or setColor is called 139 */ 140 void colorChanged(QColor); 141 142 /** 143 * Emitted when the user selects a color 144 */ 145 void colorSelected(QColor); 146 147 void wheelWidthChanged(unsigned); 148 149 void selectorShapeChanged(ShapeEnum shape); 150 151 void rotatingSelectorChanged(bool rotating); 152 153 void colorSpaceChanged(ColorSpaceEnum space); 154 155 /** 156 * Emitted when the user releases from dragging 157 */ 158 void editingFinished(); 159 160 protected: 161 void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE; 162 void mouseMoveEvent(QMouseEvent *) Q_DECL_OVERRIDE; 163 void mousePressEvent(QMouseEvent *) Q_DECL_OVERRIDE; 164 void mouseReleaseEvent(QMouseEvent *) Q_DECL_OVERRIDE; 165 void resizeEvent(QResizeEvent *) Q_DECL_OVERRIDE; 166 void dragEnterEvent(QDragEnterEvent* event) Q_DECL_OVERRIDE; 167 void dropEvent(QDropEvent* event) Q_DECL_OVERRIDE; 168 169 protected: 170 class Private; 171 ColorWheel(QWidget *parent, Private* data); data() const172 Private* data() const { return p; } 173 174 private: 175 Private * const p; 176 177 }; 178 179 } // namespace color_widgets 180 181 #endif // COLOR_WHEEL_HPP 182