color_palette_widget.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_COLOR_PALETTE_WIDGET_HPP
  23. #define COLOR_WIDGETS_COLOR_PALETTE_WIDGET_HPP
  24. #include <memory>
  25. #include <QWidget>
  26. #include "color_palette_model.hpp"
  27. #include "swatch.hpp"
  28. namespace color_widgets {
  29. /**
  30. * \brief A widget to use and modify palettes
  31. */
  32. class QCP_EXPORT ColorPaletteWidget : public QWidget
  33. {
  34. Q_OBJECT
  35. /**
  36. * \brief Model used to store the palettes
  37. */
  38. Q_PROPERTY(ColorPaletteModel* model READ model WRITE setModel NOTIFY modelChanged)
  39. /**
  40. * \brief Size of a single color in the swatch widget
  41. */
  42. Q_PROPERTY(QSize colorSize READ colorSize WRITE setColorSize NOTIFY colorSizeChanged)
  43. /**
  44. * \brief Policy for colorSize
  45. **/
  46. Q_PROPERTY(color_widgets::Swatch::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 palettes can be modified via user interaction
  75. */
  76. Q_PROPERTY(bool readOnly READ readOnly WRITE setReadOnly NOTIFY readOnlyChanged)
  77. /**
  78. * \brief Currently selected color
  79. */
  80. Q_PROPERTY(QColor currentColor READ currentColor WRITE setCurrentColor NOTIFY currentColorChanged)
  81. /**
  82. * \brief Currently selected model row
  83. */
  84. Q_PROPERTY(int currentRow READ currentRow WRITE setCurrentRow NOTIFY currentRowChanged)
  85. /**
  86. * \brief Palette shown by the widget
  87. */
  88. Q_PROPERTY(const ColorPalette& currentPalette READ currentPalette NOTIFY currentPaletteChanged)
  89. /**
  90. * \brief If a valid color, it's used when adding new colors to palettes
  91. */
  92. Q_PROPERTY(QColor defaultColor READ defaultColor WRITE setDefaultColor NOTIFY defaultColorChanged)
  93. public:
  94. ColorPaletteWidget(QWidget* parent = nullptr);
  95. ~ColorPaletteWidget();
  96. ColorPaletteModel* model() const;
  97. /**
  98. * \brief Currently selected palette
  99. * \pre model() != nullptr and there is a selected palette
  100. */
  101. const ColorPalette& currentPalette() const;
  102. QSize colorSize() const;
  103. Swatch::ColorSizePolicy colorSizePolicy() const;
  104. QPen border() const;
  105. int forcedRows() const;
  106. int forcedColumns() const;
  107. bool readOnly() const;
  108. QColor currentColor() const;
  109. int currentRow() const;
  110. /**
  111. * \brief Default color when adding a color to the current palette
  112. */
  113. QColor defaultColor() const;
  114. public Q_SLOTS:
  115. void setModel(ColorPaletteModel* model);
  116. void setColorSize(const QSize& colorSize);
  117. void setColorSizePolicy(Swatch::ColorSizePolicy colorSizePolicy);
  118. void setBorder(const QPen& border);
  119. void setForcedRows(int forcedRows);
  120. void setForcedColumns(int forcedColumns);
  121. void setReadOnly(bool readOnly);
  122. /**
  123. * \brief Clear the selected color
  124. */
  125. void clearCurrentColor();
  126. /**
  127. * \brief Attempt to select a color
  128. *
  129. * If the given color is available in the current palete, it will be selected
  130. * \return \b true on success
  131. */
  132. bool setCurrentColor(const QColor& color);
  133. /**
  134. * \brief Attempt to select a color by name
  135. *
  136. * If the given color is available in the current palete, it will be selected
  137. * \return \b true on success
  138. */
  139. bool setCurrentColor(const QString& name);
  140. /**
  141. * \brief Attempt to select a color by index
  142. *
  143. * If the given color is available in the current palete, it will be selected
  144. * \return \b true on success
  145. */
  146. bool setCurrentColor(int index);
  147. /**
  148. * \brief Set the selected row in the model
  149. */
  150. void setCurrentRow(int currentRow);
  151. /**
  152. * \brief Sets the default for new colors
  153. * If invalid, it will show a dialog
  154. */
  155. void setDefaultColor(const QColor& color);
  156. Q_SIGNALS:
  157. void modelChanged(ColorPaletteModel* model);
  158. void colorSizeChanged(const QSize& colorSize);
  159. void colorSizePolicyChanged(Swatch::ColorSizePolicy colorSizePolicy);
  160. void forcedRowsChanged(int forcedRows);
  161. void forcedColumnsChanged(int forcedColumns);
  162. void readOnlyChanged(bool readOnly);
  163. void currentColorChanged(const QColor& currentColor);
  164. void currentColorChanged(int index);
  165. void borderChanged(const QPen& border);
  166. void currentRowChanged(int currentRow);
  167. void currentPaletteChanged(const ColorPalette& palette);
  168. void defaultColorChanged(const QColor& color);
  169. private Q_SLOTS:
  170. void on_palette_list_currentIndexChanged(int index);
  171. void on_swatch_doubleClicked(int index);
  172. private:
  173. class Private;
  174. std::unique_ptr<Private> p;
  175. };
  176. } // namespace color_widgets
  177. #endif // COLOR_WIDGETS_COLOR_PALETTE_WIDGET_HPP