swatch.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_WIDGETS_SWATCH_HPP
  23. #define COLOR_WIDGETS_SWATCH_HPP
  24. #include <QWidget>
  25. #include <QPen>
  26. #include "color_palette.hpp"
  27. namespace color_widgets {
  28. /**
  29. * \brief A widget drawing a palette
  30. */
  31. class QCP_EXPORT Swatch : public QWidget
  32. {
  33. Q_OBJECT
  34. /**
  35. * \brief Palette shown by the widget
  36. */
  37. Q_PROPERTY(const ColorPalette& palette READ palette WRITE setPalette NOTIFY paletteChanged)
  38. /**
  39. * \brief Currently selected color (-1 if no color is selected)
  40. */
  41. Q_PROPERTY(int selected READ selected WRITE setSelected NOTIFY selectedChanged)
  42. /**
  43. * \brief Preferred size for a color square
  44. */
  45. Q_PROPERTY(QSize colorSize READ colorSize WRITE setColorSize NOTIFY colorSizeChanged)
  46. Q_PROPERTY(ColorSizePolicy colorSizePolicy READ colorSizePolicy WRITE setColorSizePolicy NOTIFY colorSizePolicyChanged)
  47. /**
  48. * \brief Border around the colors
  49. */
  50. Q_PROPERTY(QPen border READ border WRITE setBorder NOTIFY borderChanged)
  51. /**
  52. * \brief Forces the Swatch to display that many rows of colors
  53. *
  54. * If there are too few elements, the widget will display less than this
  55. * many rows.
  56. *
  57. * A value of0 means that the number of rows is automatic.
  58. *
  59. * \note Conflicts with forcedColumns
  60. */
  61. Q_PROPERTY(int forcedRows READ forcedRows WRITE setForcedRows NOTIFY forcedRowsChanged)
  62. /**
  63. * \brief Forces the Swatch to display that many columns of colors
  64. *
  65. * If there are too few elements, the widget will display less than this
  66. * many columns.
  67. *
  68. * A value of 0 means that the number of columns is automatic.
  69. *
  70. * \note Conflicts with forcedRows
  71. */
  72. Q_PROPERTY(int forcedColumns READ forcedColumns WRITE setForcedColumns NOTIFY forcedColumnsChanged)
  73. /**
  74. * \brief Whether the palette can be modified via user interaction
  75. * \note Even when this is \b false, it can still be altered programmatically
  76. */
  77. Q_PROPERTY(bool readOnly READ readOnly WRITE setReadOnly NOTIFY readOnlyChanged)
  78. /**
  79. * \brief Maximum size a color square can have
  80. */
  81. Q_PROPERTY(QSize maxColorSize READ maxColorSize WRITE setMaxColorSize NOTIFY maxColorSizeChanged)
  82. /**
  83. * \brief Whether to show an extra color to perform a "clear" operation.
  84. *
  85. * Clicking on this extra pseudo-color will emit signals like clicked() etc with an index of -1.
  86. */
  87. Q_PROPERTY(bool showClearColor READ showClearColor WRITE setShowClearColor NOTIFY showClearColorChanged)
  88. public:
  89. enum ColorSizePolicy
  90. {
  91. Hint, ///< The size is just a hint
  92. Minimum, ///< Can expand but not contract
  93. Fixed ///< Must be exactly as specified
  94. };
  95. Q_ENUMS(ColorSizePolicy)
  96. Swatch(QWidget* parent = 0);
  97. ~Swatch();
  98. QSize sizeHint() const Q_DECL_OVERRIDE;
  99. QSize minimumSizeHint() const Q_DECL_OVERRIDE;
  100. const ColorPalette& palette() const;
  101. ColorPalette& palette();
  102. int selected() const;
  103. /**
  104. * \brief Color at the currently selected index
  105. */
  106. QColor selectedColor() const;
  107. /**
  108. * \brief Color index at the given position within the widget
  109. * \param p Point in local coordinates
  110. * \returns -1 if the position doesn't represent any color
  111. */
  112. int indexAt(const QPoint& p);
  113. /**
  114. * \brief Color at the given position within the widget
  115. * \param p Point in local coordinates
  116. */
  117. QColor colorAt(const QPoint& p);
  118. QSize colorSize() const;
  119. QSize maxColorSize() const;
  120. ColorSizePolicy colorSizePolicy() const;
  121. QPen border() const;
  122. int forcedRows() const;
  123. int forcedColumns() const;
  124. bool readOnly() const;
  125. bool showClearColor() const;
  126. public Q_SLOTS:
  127. void setPalette(const ColorPalette& palette);
  128. void setSelected(int selected);
  129. void clearSelection();
  130. void setColorSize(const QSize& colorSize);
  131. void setMaxColorSize(const QSize& colorSize);
  132. void setColorSizePolicy(ColorSizePolicy colorSizePolicy);
  133. void setBorder(const QPen& border);
  134. void setForcedRows(int forcedRows);
  135. void setForcedColumns(int forcedColumns);
  136. void setReadOnly(bool readOnly);
  137. /**
  138. * \brief Remove the currently seleceted color
  139. **/
  140. void removeSelected();
  141. void setShowClearColor(bool show);
  142. Q_SIGNALS:
  143. void paletteChanged(const ColorPalette& palette);
  144. void selectedChanged(int selected);
  145. void colorSelected(const QColor& color);
  146. void colorSizeChanged(const QSize& colorSize);
  147. void maxColorSizeChanged(const QSize& colorSize);
  148. void colorSizePolicyChanged(ColorSizePolicy colorSizePolicy);
  149. void doubleClicked(int index, Qt::KeyboardModifiers modifiers);
  150. void rightClicked(int index, Qt::KeyboardModifiers modifiers);
  151. void clicked(int index, Qt::KeyboardModifiers modifiers);
  152. void forcedRowsChanged(int forcedRows);
  153. void forcedColumnsChanged(int forcedColumns);
  154. void readOnlyChanged(bool readOnly);
  155. void borderChanged(const QPen& border);
  156. void showClearColorChanged(bool show);
  157. protected:
  158. bool event(QEvent* event) Q_DECL_OVERRIDE;
  159. void paintEvent(QPaintEvent* event) Q_DECL_OVERRIDE;
  160. void keyPressEvent(QKeyEvent* event) Q_DECL_OVERRIDE;
  161. void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  162. void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  163. void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  164. void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  165. void wheelEvent(QWheelEvent* event) Q_DECL_OVERRIDE;
  166. void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
  167. void dragMoveEvent(QDragMoveEvent* event) Q_DECL_OVERRIDE;
  168. void dragLeaveEvent(QDragLeaveEvent *event) Q_DECL_OVERRIDE;
  169. void dropEvent(QDropEvent* event) Q_DECL_OVERRIDE;
  170. protected Q_SLOTS:
  171. /**
  172. * \brief Connected to the internal palette object to keep eveything consistent
  173. */
  174. void paletteModified();
  175. private:
  176. class Private;
  177. Private* p;
  178. };
  179. } // namespace color_widgets
  180. #endif // COLOR_WIDGETS_SWATCH_HPP