gradient_list_model.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_GRADIENT_LIST_MODEL_HPP
  23. #define COLOR_WIDGETS_GRADIENT_LIST_MODEL_HPP
  24. #include "colorwidgets_global.hpp"
  25. #include <memory>
  26. #include <QAbstractListModel>
  27. #include <QLinearGradient>
  28. namespace color_widgets {
  29. class QCP_EXPORT GradientListModel : public QAbstractListModel
  30. {
  31. Q_OBJECT
  32. /**
  33. * \brief Size of the icon used for the gradient previews
  34. */
  35. Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged)
  36. Q_PROPERTY(ItemEditMode editMode READ editMode WRITE setEditMode NOTIFY editModeChanged)
  37. public:
  38. enum ItemEditMode
  39. {
  40. EditNone = 0,
  41. EditName,
  42. EditGradient,
  43. };
  44. Q_ENUM(ItemEditMode);
  45. GradientListModel(QObject *parent = nullptr);
  46. ~GradientListModel();
  47. int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  48. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
  49. Qt::ItemFlags flags(const QModelIndex & index) const Q_DECL_OVERRIDE;
  50. bool setData(const QModelIndex & index, const QVariant & value, int role) Q_DECL_OVERRIDE;
  51. QSize iconSize() const;
  52. /**
  53. * \brief Number of gradients
  54. */
  55. int count() const;
  56. /**
  57. * \brief Remove all gradients
  58. */
  59. void clear();
  60. /**
  61. * \brief Returns a reference to the first gradient with the given name
  62. * \pre hasGradient(name)
  63. */
  64. const QLinearGradient& gradient(const QString& name) const;
  65. /**
  66. * \brief Returns a reference to the first gradient with the given name
  67. * \pre hasGradient(name)
  68. */
  69. QGradientStops gradientStops(const QString& name) const;
  70. /**
  71. * \brief Whether a gradient with the given name exists in the model
  72. */
  73. bool hasGradient(const QString& name) const;
  74. /**
  75. * \brief Get the gradient at the given index (row)
  76. * \pre 0 <= index < count()
  77. */
  78. const QLinearGradient& gradient(int index) const;
  79. /**
  80. * \brief Get the gradient stops at the given index (row)
  81. * \pre 0 <= index < count()
  82. */
  83. QGradientStops gradientStops(int index) const;
  84. /**
  85. * \brief Inserts or updates a gradient
  86. * \returns The index for the new gradient
  87. */
  88. int setGradient(const QString& name, const QGradient& gradient);
  89. int setGradient(const QString& name, const QGradientStops& gradient);
  90. /**
  91. * \brief Updates the gradient at \p index
  92. */
  93. bool setGradient(int index, const QGradient& gradient);
  94. bool setGradient(int index, const QGradientStops& gradient);
  95. /**
  96. * \brief Renames the gradient at \p index
  97. * \returns \b true on success
  98. */
  99. bool rename(int index, const QString& new_name);
  100. /**
  101. * \brief Renames a gradient
  102. * \returns \b true on success
  103. */
  104. bool rename(const QString& old_name, const QString& new_name);
  105. /**
  106. * \brief Remove a gradient from the model
  107. * \returns \b true if the gradient has been successfully removed
  108. */
  109. bool removeGradient(const QString& name);
  110. bool removeGradient(int index);
  111. /**
  112. * \brief The index of the gradient with the given name
  113. * \returns -1 if none is found
  114. */
  115. int indexFromName(const QString& name) const;
  116. /**
  117. * \brief Name of the gradient at index
  118. */
  119. QString nameFromIndex(int index) const;
  120. ItemEditMode editMode() const;
  121. /**
  122. * \brief Brush for a gradient
  123. * \pre 0 <= \p index < count()
  124. */
  125. QBrush gradientBrush(int index) const;
  126. public Q_SLOTS:
  127. void setIconSize(const QSize& iconSize);
  128. void setEditMode(ItemEditMode mode);
  129. Q_SIGNALS:
  130. void iconSizeChanged(const QSize& iconSize);
  131. void editModeChanged(ItemEditMode mode);
  132. private:
  133. class Private;
  134. std::unique_ptr<Private> d;
  135. };
  136. } // namespace color_widgets
  137. #endif // COLOR_WIDGETS_GRADIENT_LIST_MODEL_HPP