File indexing completed on 2024-06-16 04:15:59

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_BASE_H
0008 #define KIS_COLOR_SELECTOR_BASE_H
0009 
0010 #include <QWidget>
0011 #include <QRgb>
0012 #include <QPointer>
0013 #include <kis_canvas2.h>
0014 #include "kis_acs_types.h"
0015 #include "kis_signal_compressor_with_param.h"
0016 
0017 
0018 class KoColor;
0019 class QTimer;
0020 class KoColorSpace;
0021 class KisCanvas2;
0022 class KisColorPreviewPopup;
0023 class KisDisplayColorConverter;
0024 class KoGamutMask;
0025 
0026 
0027 /// Base class for all color selectors, that should support color management and zooming.
0028 class KisColorSelectorBase : public QWidget
0029 {
0030 Q_OBJECT
0031 public:
0032     enum Move {MoveToMousePosition, DontMove};
0033     explicit KisColorSelectorBase(QWidget *parent = 0);
0034     ~KisColorSelectorBase() override;
0035 
0036     void setPopupBehaviour(bool onMouseOver, bool onMouseClick);
0037     void setColorSpace(const KoColorSpace* colorSpace);
0038     virtual void setCanvas(KisCanvas2* canvas);
0039     virtual void unsetCanvas();
0040     const KoColorSpace* colorSpace() const;
0041 
0042     KisDisplayColorConverter* converter() const;
0043 
0044     void tryHideAllPopups();
0045 
0046 public:
0047     void updateColor(const KoColor &color, Acs::ColorRole role, bool needsExplicitColorReset);
0048     void updateColorPreview(const KoColor &color);
0049     void showColorPreview();
0050     void updateBaseColorPreview(const KoColor &color);
0051     void updatePreviousColorPreview();
0052 
0053     virtual void setColor(const KoColor& color);
0054 
0055 public Q_SLOTS:
0056     /**
0057      * Flushes caches and redraws the selectors
0058      */
0059     virtual void reset();
0060 
0061     virtual void updateSettings();
0062     virtual void showPopup(Move move=MoveToMousePosition);
0063 
0064 public:
0065     void enterEvent(QEvent *e) override;
0066     void leaveEvent(QEvent *e) override;
0067 
0068     void mousePressEvent(QMouseEvent *) override;
0069     void mouseReleaseEvent(QMouseEvent *) override;
0070 
0071 protected:
0072     void keyPressEvent(QKeyEvent *) override;
0073     virtual KisColorSelectorBase* createPopup() const = 0;
0074     void dragEnterEvent(QDragEnterEvent *) override;
0075     void dropEvent(QDropEvent *) override;
0076     void setHidingTime(int time);
0077     bool isPopup() const { return m_isPopup; }
0078     void mouseMoveEvent(QMouseEvent *event) override;
0079     void changeEvent(QEvent *event) override;
0080     void showEvent(QShowEvent *event) override;
0081     void requestUpdateColorAndPreview(const KoColor &color, Acs::ColorRole role);
0082 
0083 private:
0084     void commitColor(const KoColor& koColor, Acs::ColorRole role);
0085 
0086 
0087 protected Q_SLOTS:
0088     void hidePopup();
0089 
0090     /// if you overwrite this, keep in mind, that you should set the color only, if m_colorUpdateAllowed is true
0091     virtual void canvasResourceChanged(int key, const QVariant& v);
0092 
0093     void updateLastUsedColorPreview(const KoColor &color);
0094 
0095 public:
0096     // This is a private interface for signal compressor, don't use it.
0097     // Use requestUpdateColorAndPreview() instead
0098     void slotUpdateColorAndPreview(QPair<KoColor, Acs::ColorRole> color);
0099 
0100 private:
0101     void lazyCreatePopup();
0102 
0103 protected:
0104     QPointer<KisCanvas2> m_canvas;
0105     KisColorSelectorBase* m_popup;
0106     QWidget* m_parent;
0107     bool m_colorUpdateAllowed;
0108 
0109     // This boolean here is to check if the color selector is updating the resource, so it won't update itself when the resource is updated.
0110     bool m_colorUpdateSelf;
0111 
0112 private:
0113     QTimer* m_hideTimer;
0114     bool m_popupOnMouseOver;
0115     bool m_popupOnMouseClick;
0116     mutable const KoColorSpace* m_colorSpace;
0117     bool m_isPopup; //this instance is a popup
0118     bool m_hideOnMouseClick;
0119     KisColorPreviewPopup* m_colorPreviewPopup;
0120 
0121     typedef KisSignalCompressorWithParam<QPair<KoColor, Acs::ColorRole>> ColorCompressorType;
0122     QScopedPointer<ColorCompressorType> m_updateColorCompressor;
0123 };
0124 
0125 #endif