File indexing completed on 2024-05-26 04:32:17

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Adam Celarek <kdedev at xibo dot at>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KIS_COLOR_SELECTOR_COMPONENT_H
0008 #define KIS_COLOR_SELECTOR_COMPONENT_H
0009 
0010 #include <QObject>
0011 #include <QColor>
0012 
0013 #include <resources/KoGamutMask.h>
0014 
0015 #include "kis_color_selector.h"
0016 
0017 class KoColorSpace;
0018 
0019 class QPainter;
0020 
0021 
0022 class KisColorSelectorComponent : public QObject
0023 {
0024     Q_OBJECT
0025 public:
0026     typedef KisColorSelectorConfiguration::Parameters Parameter;
0027     typedef KisColorSelectorConfiguration::Type Type;
0028 
0029     explicit KisColorSelectorComponent(KisColorSelector* parent);
0030     void setGeometry(int x, int y, int width, int height);
0031     void paintEvent(QPainter*);
0032 
0033     /// saves the mouse position, so that a blip can be created.
0034     virtual void mouseEvent(int x, int y);
0035 
0036     /// return the color, that was selected by calling mouseEvent
0037     KoColor currentColor();
0038 
0039     int width() const;
0040     int height() const;
0041 
0042     /// setConfiguration can be ignored (for instance ring and triangle, as they can have only one config)
0043     void setConfiguration(Parameter param, Type type);
0044 
0045     /// set the color, blibs etc
0046     virtual void setColor(const KoColor& color);
0047 
0048     /// force subsequent redraw of the component
0049     void setDirty();
0050 
0051     /// returns true, if this component wants to grab the mouse (normally true, if containsPoint returns true)
0052     bool wantsGrab(int x, int y) {return containsPointInComponentCoords(x-m_x, y-m_y);}
0053 
0054     void setGamutMask(KoGamutMaskSP gamutMask);
0055     void unsetGamutMask();
0056     void updateGamutMaskPreview();
0057     void toggleGamutMask(bool state);
0058 
0059 public Q_SLOTS:
0060     /// set hue, saturation, value or/and lightness
0061     /// unused parameters should be set to -1
0062     void setParam(qreal hue, qreal hsvSaturation, qreal value, qreal hslSaturation, qreal lightness, qreal hsiSaturation, qreal intensity, qreal hsySaturation, qreal luma);
0063 Q_SIGNALS:
0064     /// request for repaint, for instance, if the hue changes.
0065     void update();
0066     /// -1, if unaffected
0067     void paramChanged(qreal hue, qreal hsvSaturation, qreal value, qreal hslSaturation, qreal lightness, qreal hsiSaturation, qreal intensity, qreal hsySaturation, qreal luma);
0068 protected:
0069     const KoColorSpace* colorSpace() const;
0070     /// returns true, if ether the color space, the size or the parameters have changed since the last paint event
0071     bool isDirty() const;
0072 
0073     /// this method must be overloaded to return the color at position x/y and draw a marker on that position
0074     virtual KoColor selectColor(int x, int y) = 0;
0075 
0076     /// paint component using given painter
0077     /// the component should respect width() and height() (eg. scale to width and height), but doesn't
0078     /// have to care about x/y coordinates (top left corner)
0079     virtual void paint(QPainter*) = 0;
0080 
0081     /// a subclass can implement this method, the default returns true if the coordinates are in the component rect
0082     /// values for the subclasses are provided in component coordinates, eg (0,0) is top left of component
0083     virtual bool containsPointInComponentCoords(int x, int y) const;
0084 
0085     /// a subclass can implement this method to note that the point, although it is in
0086     /// containsPointInComponentCoords area, still cannot be selected as a color (e.g.
0087     /// it is masked out). Default implementation always returns true.
0088     virtual bool allowsColorSelectionAtPoint(const QPoint &) const;
0089 
0090     // Workaround for Bug 287001
0091     void setLastMousePosition(int x, int y);
0092 
0093     qreal m_hue {0.0};
0094     qreal m_hsvSaturation {0.0};
0095     qreal m_value {0.0};
0096     qreal m_hslSaturation {0.0};
0097     qreal m_lightness {0.0};
0098     qreal m_hsiSaturation {0.0};
0099     qreal m_intensity {0.0};
0100     qreal m_hsySaturation {0.0};
0101     qreal m_luma {0.0};
0102     Parameter m_parameter;
0103     Type m_type;
0104     KisColorSelector* m_parent {0};
0105     bool m_gamutMaskOn {false};
0106     KoGamutMaskSP m_currentGamutMask;
0107     bool m_maskPreviewActive {false};
0108     qreal m_lastX {0.0};
0109     qreal m_lastY {0.0};
0110     int m_x {0};
0111     int m_y {0};
0112 private:
0113     int m_width {0};
0114     int m_height {0};
0115     bool m_dirty {false};
0116     const KoColorSpace* m_lastColorSpace {0};
0117     KoColor m_lastSelectedColor;
0118 };
0119 
0120 #endif // KIS_COLOR_SELECTOR_COMPONENT_H