color_palette_model.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_MODEL_HPP
  23. #define COLOR_WIDGETS_COLOR_PALETTE_MODEL_HPP
  24. #include <QAbstractListModel>
  25. #include "color_palette.hpp"
  26. namespace color_widgets {
  27. class QCP_EXPORT ColorPaletteModel : public QAbstractListModel
  28. {
  29. Q_OBJECT
  30. /**
  31. * \brief List of directories to be scanned for palette files
  32. */
  33. Q_PROPERTY(QStringList searchPaths READ searchPaths WRITE setSearchPaths NOTIFY searchPathsChanged)
  34. /**
  35. * \brief Default directory to be used when saving a palette
  36. */
  37. Q_PROPERTY(QString savePath READ savePath WRITE setSavePath NOTIFY savePathChanged)
  38. /**
  39. * \brief Size of the icon used for the palette previews
  40. */
  41. Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged)
  42. public:
  43. ColorPaletteModel();
  44. ~ColorPaletteModel();
  45. int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  46. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
  47. bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()) Q_DECL_OVERRIDE;
  48. QString savePath() const;
  49. QStringList searchPaths() const;
  50. QSize iconSize() const;
  51. /**
  52. * \brief Number of palettes
  53. */
  54. int count() const;
  55. /**
  56. * \brief Returns a reference to the first palette with the given name
  57. * \pre hasPalette(name)
  58. */
  59. const ColorPalette& palette(const QString& name) const;
  60. /**
  61. * \brief Whether a palette with the given name exists in the model
  62. */
  63. bool hasPalette(const QString& name) const;
  64. /**
  65. * \brief Get the palette at the given index (row)
  66. * \pre 0 <= index < count()
  67. */
  68. const ColorPalette& palette(int index) const;
  69. /**
  70. * \brief Updates an existing palette
  71. * \param index Palette index
  72. * \param palette New palette
  73. * \param save Whether to save the palette to the filesystem
  74. *
  75. * Saving will try: (in this order)
  76. * * To overwrite the file pointed by the old palette
  77. * * To write to the new palette file name
  78. * * To create a file in the save path
  79. * If all of the above fail, the palette will be replaced interally
  80. * but not on the filesystem
  81. *
  82. * \returns \b true if the palette has been successfully updated (and saved)
  83. */
  84. bool updatePalette(int index, const ColorPalette& palette, bool save = true);
  85. /**
  86. * \brief Remove a palette from the model and optionally from the filesystem
  87. * \returns \b true if the palette has been successfully removed
  88. */
  89. bool removePalette(int index, bool remove_file = true);
  90. /**
  91. * \brief Remove a palette to the model and optionally to the filesystem
  92. * \returns \b true if the palette has been successfully added
  93. */
  94. bool addPalette(const ColorPalette& palette, bool save = true);
  95. /**
  96. * \brief The index of the palette with the given file name
  97. * \returns -1 if none is found
  98. */
  99. int indexFromFile(const QString& filename) const;
  100. public Q_SLOTS:
  101. void setSavePath(const QString& savePath);
  102. void setSearchPaths(const QStringList& searchPaths);
  103. void addSearchPath(const QString& path);
  104. void setIconSize(const QSize& iconSize);
  105. /**
  106. * \brief Load palettes files found in the search paths
  107. */
  108. void load();
  109. Q_SIGNALS:
  110. void savePathChanged(const QString& savePath);
  111. void searchPathsChanged(const QStringList& searchPaths);
  112. void iconSizeChanged(const QSize& iconSize);
  113. private:
  114. class Private;
  115. Private* p;
  116. };
  117. } // namespace color_widgets
  118. #endif // COLOR_WIDGETS_COLOR_PALETTE_MODEL_HPP