color_palette.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_HPP
  23. #define COLOR_WIDGETS_COLOR_PALETTE_HPP
  24. #include <QColor>
  25. #include <QString>
  26. #include <QVector>
  27. #include <QObject>
  28. #include <QPair>
  29. #include <QPixmap>
  30. #include "colorwidgets_global.hpp"
  31. namespace color_widgets {
  32. class QCP_EXPORT ColorPalette : public QObject
  33. {
  34. Q_OBJECT
  35. /**
  36. * \brief The list of colors
  37. */
  38. Q_PROPERTY(QVector<value_type> colors READ colors WRITE setColors NOTIFY colorsChanged)
  39. /**
  40. * \brief Name of the palette
  41. */
  42. Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
  43. /**
  44. * \brief Number of colors to display in a row, if 0 unspecified
  45. */
  46. Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged)
  47. /**
  48. * \brief Number of colors
  49. */
  50. Q_PROPERTY(int count READ count)
  51. /**
  52. * \brief Name of the file the palette has been read from
  53. */
  54. Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
  55. /**
  56. * \brief Whether it has been modified and it might be advisable to save it
  57. */
  58. Q_PROPERTY(bool dirty READ dirty WRITE setDirty NOTIFY dirtyChanged)
  59. public:
  60. typedef QPair<QColor,QString> value_type;
  61. ColorPalette(const QVector<QColor>& colors, const QString& name = QString(), int columns = 0);
  62. ColorPalette(const QVector<QPair<QColor,QString> >& colors, const QString& name = QString(), int columns = 0);
  63. explicit ColorPalette(const QString& name = QString());
  64. ColorPalette(const ColorPalette& other);
  65. ColorPalette& operator=(const ColorPalette& other);
  66. ~ColorPalette();
  67. ColorPalette(ColorPalette&& other);
  68. ColorPalette& operator=(ColorPalette&& other);
  69. /**
  70. * \brief Color at the given index
  71. */
  72. Q_INVOKABLE QColor colorAt(int index) const;
  73. /**
  74. * \brief Color name at the given index
  75. */
  76. Q_INVOKABLE QString nameAt(int index) const;
  77. QVector<QPair<QColor,QString> > colors() const;
  78. QVector<QColor> onlyColors() const;
  79. int count() const;
  80. int columns();
  81. QString name() const;
  82. /**
  83. * \brief Use a color table to set the colors
  84. */
  85. Q_INVOKABLE void loadColorTable(const QVector<QRgb>& color_table);
  86. /**
  87. * \brief Convert to a color table
  88. */
  89. Q_INVOKABLE QVector<QRgb> colorTable() const;
  90. /**
  91. * \brief Creates a ColorPalette from a color table
  92. */
  93. static ColorPalette fromColorTable(const QVector<QRgb>& table);
  94. /**
  95. * \brief Use the pixels on an image to set the palette colors
  96. */
  97. Q_INVOKABLE bool loadImage(const QImage& image);
  98. /**
  99. * \brief Creates a ColorPalette from a Gimp palette (gpl) file
  100. */
  101. static ColorPalette fromImage(const QImage& image);
  102. /**
  103. * \brief Load contents from a Gimp palette (gpl) file
  104. * \returns \b true On Success
  105. * \note If this function returns \b false, the palette will become empty
  106. */
  107. Q_INVOKABLE bool load(const QString& name);
  108. /**
  109. * \brief Creates a ColorPalette from a Gimp palette (gpl) file
  110. */
  111. static ColorPalette fromFile(const QString& name);
  112. QString fileName() const;
  113. bool dirty() const;
  114. /**
  115. * \brief Returns a preview image of the colors in the palette
  116. */
  117. QPixmap preview(const QSize& size, const QColor& background=Qt::transparent) const;
  118. public Q_SLOTS:
  119. void setColumns(int columns);
  120. void setColors(const QVector<QColor>& colors);
  121. void setColors(const QVector<QPair<QColor,QString> >& colors);
  122. /**
  123. * \brief Change the color at the given index
  124. */
  125. void setColorAt(int index, const QColor& color);
  126. /**
  127. * \brief Change the color at the given index
  128. */
  129. void setColorAt(int index, const QColor& color, const QString& name);
  130. /**
  131. * \brief Change the name of a color
  132. */
  133. void setNameAt(int index, const QString& name = QString());
  134. /**
  135. * \brief Append a color at the end
  136. */
  137. void appendColor(const QColor& color, const QString& name = QString());
  138. /**
  139. * \brief Insert a color in an arbitrary location
  140. */
  141. void insertColor(int index, const QColor& color, const QString& name = QString());
  142. /**
  143. * \brief Remove the color at the given index
  144. */
  145. void eraseColor(int index);
  146. /**
  147. * \brief Change file name and save
  148. * \returns \b true on success
  149. */
  150. bool save(const QString& filename);
  151. /**
  152. * \brief save to file, the filename is \c fileName or determined automatically
  153. * \returns \b true on success
  154. */
  155. bool save();
  156. void setName(const QString& name);
  157. void setFileName(const QString& name);
  158. void setDirty(bool dirty);
  159. Q_SIGNALS:
  160. /**
  161. * \brief Emitted when all the colors have changed
  162. */
  163. void colorsChanged(const QVector<QPair<QColor,QString> >&);
  164. void columnsChanged(int);
  165. void nameChanged(const QString&);
  166. void fileNameChanged(const QString&);
  167. void dirtyChanged(bool);
  168. /**
  169. * \brief Emitted when the color or the name at the given index has been modified
  170. */
  171. void colorChanged(int index);
  172. /**
  173. * \brief Emitted when the color at the given index has been removed
  174. */
  175. void colorRemoved(int index);
  176. /**
  177. * \brief Emitted when a single color has been added
  178. */
  179. void colorAdded(int index);
  180. /**
  181. * \brief Emitted when the colors have been modified with a simple operation (set, append etc.)
  182. */
  183. void colorsUpdated(const QVector<QPair<QColor,QString>>&);
  184. private:
  185. /**
  186. * \brief Returns \c name if it isn't null, otherwise a default value
  187. */
  188. QString unnamed(const QString& name = QString()) const;
  189. /**
  190. * \brief Emit all the necessary signals when the palette has been completely overwritten
  191. */
  192. void emitUpdate();
  193. class Private;
  194. Private *p;
  195. };
  196. } // namespace color_widgets
  197. #endif // COLOR_WIDGETS_COLOR_PALETTE_HPP