File indexing completed on 2024-11-24 03:56:05
0001 /* 0002 * SPDX-FileCopyrightText: 2013-2020 Mattia Basaglia 0003 * 0004 * SPDX-License-Identifier: LGPL-3.0-or-later 0005 */ 0006 0007 #ifndef COLOR_WIDGETS_SWATCH_HPP 0008 #define COLOR_WIDGETS_SWATCH_HPP 0009 0010 #include <QWidget> 0011 #include <QPen> 0012 #include "color_palette.hpp" 0013 0014 namespace color_widgets { 0015 0016 /** 0017 * \brief A widget drawing a palette 0018 */ 0019 class QCP_EXPORT Swatch : public QWidget 0020 { 0021 Q_OBJECT 0022 0023 /** 0024 * \brief Palette shown by the widget 0025 */ 0026 Q_PROPERTY(const ColorPalette& palette READ palette WRITE setPalette NOTIFY paletteChanged) 0027 /** 0028 * \brief Currently selected color (-1 if no color is selected) 0029 */ 0030 Q_PROPERTY(int selected READ selected WRITE setSelected NOTIFY selectedChanged) 0031 0032 /** 0033 * \brief Preferred size for a color square 0034 */ 0035 Q_PROPERTY(QSize colorSize READ colorSize WRITE setColorSize NOTIFY colorSizeChanged) 0036 0037 Q_PROPERTY(ColorSizePolicy colorSizePolicy READ colorSizePolicy WRITE setColorSizePolicy NOTIFY colorSizePolicyChanged) 0038 0039 /** 0040 * \brief Border around the colors 0041 */ 0042 Q_PROPERTY(QPen border READ border WRITE setBorder NOTIFY borderChanged) 0043 0044 /** 0045 * \brief Selection rectangle for selected color 0046 */ 0047 Q_PROPERTY(QPen selection READ selectionPen WRITE setSelectionPen) 0048 0049 /** 0050 * \brief Forces the Swatch to display that many rows of colors 0051 * 0052 * If there are too few elements, the widget will display less than this 0053 * many rows. 0054 * 0055 * A value of0 means that the number of rows is automatic. 0056 * 0057 * \note Conflicts with forcedColumns 0058 */ 0059 Q_PROPERTY(int forcedRows READ forcedRows WRITE setForcedRows NOTIFY forcedRowsChanged) 0060 0061 /** 0062 * \brief Forces the Swatch to display that many columns of colors 0063 * 0064 * If there are too few elements, the widget will display less than this 0065 * many columns. 0066 * 0067 * A value of 0 means that the number of columns is automatic. 0068 * 0069 * \note Conflicts with forcedRows 0070 */ 0071 Q_PROPERTY(int forcedColumns READ forcedColumns WRITE setForcedColumns NOTIFY forcedColumnsChanged) 0072 0073 /** 0074 * \brief Whether the palette can be modified via user interaction 0075 * \note Even when this is \b false, it can still be altered programmatically 0076 */ 0077 Q_PROPERTY(bool readOnly READ readOnly WRITE setReadOnly NOTIFY readOnlyChanged) 0078 0079 0080 /** 0081 * \brief Maximum size a color square can have 0082 */ 0083 Q_PROPERTY(QSize maxColorSize READ maxColorSize WRITE setMaxColorSize NOTIFY maxColorSizeChanged) 0084 0085 /** 0086 * \brief Whether to show an extra color to perform a "clear" operation. 0087 * 0088 * Clicking on this extra pseudo-color will emit signals like clicked() etc with an index of -1. 0089 */ 0090 Q_PROPERTY(bool showClearColor READ showClearColor WRITE setShowClearColor NOTIFY showClearColorChanged) 0091 0092 public: 0093 enum ColorSizePolicy 0094 { 0095 Hint, ///< The size is just a hint 0096 Minimum, ///< Can expand but not contract 0097 Fixed ///< Must be exactly as specified 0098 }; 0099 Q_ENUMS(ColorSizePolicy) 0100 0101 Swatch(QWidget* parent = nullptr); 0102 ~Swatch(); 0103 0104 QSize sizeHint() const Q_DECL_OVERRIDE; 0105 QSize minimumSizeHint() const Q_DECL_OVERRIDE; 0106 0107 const ColorPalette& palette() const; 0108 ColorPalette& palette(); 0109 int selected() const; 0110 /** 0111 * \brief Color at the currently selected index 0112 */ 0113 QColor selectedColor() const; 0114 0115 /** 0116 * \brief Color index at the given position within the widget 0117 * \param p Point in local coordinates 0118 * \returns -1 if the position doesn't represent any color 0119 */ 0120 int indexAt(const QPoint& p); 0121 0122 /** 0123 * \brief Color at the given position within the widget 0124 * \param p Point in local coordinates 0125 */ 0126 QColor colorAt(const QPoint& p); 0127 0128 QSize colorSize() const; 0129 QSize maxColorSize() const; 0130 ColorSizePolicy colorSizePolicy() const; 0131 QPen border() const; 0132 QPen selectionPen() const; 0133 0134 int forcedRows() const; 0135 int forcedColumns() const; 0136 0137 bool readOnly() const; 0138 0139 bool showClearColor() const; 0140 0141 public Q_SLOTS: 0142 void setPalette(const ColorPalette& palette); 0143 void setSelected(int selected); 0144 0145 /** 0146 * Sets the given color as selected color. 0147 * If the given color is not in the palette, the function returns 0148 * false 0149 */ 0150 bool setSelectedColor(const QColor& color); 0151 void clearSelection(); 0152 void setColorSize(const QSize& colorSize); 0153 void setMaxColorSize(const QSize& colorSize); 0154 void setColorSizePolicy(ColorSizePolicy colorSizePolicy); 0155 void setBorder(const QPen& border); 0156 void setSelectionPen(const QPen& selected); 0157 void setForcedRows(int forcedRows); 0158 void setForcedColumns(int forcedColumns); 0159 void setReadOnly(bool readOnly); 0160 0161 /** 0162 * \brief Remove the currently seleceted color 0163 **/ 0164 void removeSelected(); 0165 void setShowClearColor(bool show); 0166 0167 Q_SIGNALS: 0168 void paletteChanged(const ColorPalette& palette); 0169 void selectedChanged(int selected); 0170 void colorSelected(const QColor& color); 0171 void colorSizeChanged(const QSize& colorSize); 0172 void maxColorSizeChanged(const QSize& colorSize); 0173 void colorSizePolicyChanged(ColorSizePolicy colorSizePolicy); 0174 void doubleClicked(int index, Qt::KeyboardModifiers modifiers); 0175 void rightClicked(int index, Qt::KeyboardModifiers modifiers); 0176 void clicked(int index, Qt::KeyboardModifiers modifiers); 0177 void forcedRowsChanged(int forcedRows); 0178 void forcedColumnsChanged(int forcedColumns); 0179 void readOnlyChanged(bool readOnly); 0180 void borderChanged(const QPen& border); 0181 void showClearColorChanged(bool show); 0182 0183 protected: 0184 bool event(QEvent* event) Q_DECL_OVERRIDE; 0185 0186 void paintEvent(QPaintEvent* event) Q_DECL_OVERRIDE; 0187 0188 void keyPressEvent(QKeyEvent* event) Q_DECL_OVERRIDE; 0189 0190 void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; 0191 void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; 0192 void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; 0193 void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE; 0194 void wheelEvent(QWheelEvent* event) Q_DECL_OVERRIDE; 0195 0196 void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE; 0197 void dragMoveEvent(QDragMoveEvent* event) Q_DECL_OVERRIDE; 0198 void dragLeaveEvent(QDragLeaveEvent *event) Q_DECL_OVERRIDE; 0199 void dropEvent(QDropEvent* event) Q_DECL_OVERRIDE; 0200 0201 protected Q_SLOTS: 0202 /** 0203 * \brief Connected to the internal palette object to keep eveything consistent 0204 */ 0205 void paletteModified(); 0206 0207 private: 0208 class Private; 0209 Private* p; 0210 }; 0211 0212 0213 } // namespace color_widgets 0214 #endif // COLOR_WIDGETS_SWATCH_HPP