File indexing completed on 2024-05-19 04:29:28
0001 /* 0002 * SPDX-FileCopyrightText: 2013 Sven Langkamp <sven.langkamp@gmail.com> 0003 * SPDX-FileCopyrightText: 2022 Halla Rempt <halla@valdyas.org> 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #ifndef KIS_PALETTEMODEL_H 0007 #define KIS_PALETTEMODEL_H 0008 0009 #include <QPointer> 0010 #include <QModelIndex> 0011 #include <QMap> 0012 0013 #include <KoColorDisplayRendererInterface.h> 0014 0015 #include "kritawidgets_export.h" 0016 #include <KoColorSet.h> 0017 #include <KisSwatchGroup.h> 0018 #include <QScopedPointer> 0019 0020 class KisPaletteView; 0021 0022 /** 0023 * @brief The KisPaletteModel class 0024 * This, together with KisPaletteView and KisPaletteDelegate forms a mvc way to access kocolorsets. 0025 * A display renderer is given to this model to convert KoColor to QColor when 0026 * colors are requested 0027 */ 0028 class KRITAWIDGETS_EXPORT KisPaletteModel : public QAbstractTableModel 0029 { 0030 Q_OBJECT 0031 public: 0032 explicit KisPaletteModel(QObject* parent = 0); 0033 ~KisPaletteModel() override; 0034 0035 /** 0036 * Installs a display renderer object for a palette that will 0037 * convert the KoColor to the displayable QColor. Default is the 0038 * dumb renderer. 0039 */ 0040 void setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer); 0041 0042 0043 enum AdditionalRoles { 0044 IsGroupNameRole = Qt::UserRole + 1, 0045 CheckSlotRole, 0046 GroupNameRole, 0047 RowInGroupRole 0048 }; 0049 0050 public: // QAbstractTableModel 0051 0052 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; 0053 0054 int rowCount(const QModelIndex& parent = QModelIndex()) const override; 0055 0056 int columnCount(const QModelIndex& parent = QModelIndex()) const override; 0057 0058 QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override; 0059 0060 Qt::ItemFlags flags(const QModelIndex& index) const override; 0061 0062 bool dropMimeData(const QMimeData *data, Qt::DropAction action, 0063 int row, int column, const QModelIndex &parent) override; 0064 0065 QMimeData *mimeData(const QModelIndexList &indexes) const override; 0066 0067 QStringList mimeTypes() const override; 0068 0069 Qt::DropActions supportedDropActions() const override; 0070 0071 Q_SIGNALS: 0072 0073 /** 0074 * @brief sigPaletteModified 0075 * emitted when palette associated with the model is modified 0076 */ 0077 void sigPaletteModified(); 0078 0079 /** 0080 * @brief sigPaletteChanged 0081 * emitted when the palette associated with the model is changed for another palette 0082 */ 0083 void sigPaletteChanged(); 0084 0085 public: 0086 0087 void setColorSet(KoColorSetSP colorSet); 0088 0089 KoColorSetSP colorSet() const; 0090 0091 void addSwatch(const KisSwatch &entry, 0092 const QString &groupName = KoColorSet::GLOBAL_GROUP_NAME); 0093 0094 void setSwatch(const KisSwatch &entry, const QModelIndex &index); 0095 0096 void removeSwatch(const QModelIndex &index, bool keepColors=true); 0097 0098 void changeGroupName(const QString &groupName, const QString &newName); 0099 0100 void removeGroup(const QString &groupName, bool keepColors); 0101 0102 KisSwatchGroupSP addGroup(const QString &groupName, int columnCount = KisSwatchGroup::DEFAULT_COLUMN_COUNT, int rowCount = KisSwatchGroup::DEFAULT_ROW_COUNT); 0103 0104 void setRowCountForGroup(const QString &groupName, int rowCount); 0105 0106 void setColumnCount(int colCount); 0107 0108 void clear(); 0109 0110 void clear(int defaultColumnsCount); 0111 0112 KisSwatch getSwatch(const QModelIndex &index) const; 0113 0114 QModelIndex indexForClosest(const KoColor &compare); 0115 0116 0117 void slotExternalPaletteModified(QSharedPointer<KoColorSet> resource); 0118 0119 private Q_SLOTS: 0120 0121 void slotDisplayConfigurationChanged(); 0122 0123 void slotPaletteModified(); 0124 0125 private: 0126 0127 friend class TestKisPaletteModel; 0128 0129 int rowNumberInGroup(int rowInModel) const; 0130 int indexRowForInfo(const KisSwatchGroup::SwatchInfo &info); 0131 0132 QVariant dataForGroupNameRow(const QModelIndex &idx, int role) const; 0133 QVariant dataForSwatch(const QModelIndex &idx, int role) const; 0134 0135 private /* member variables */: 0136 KoColorSetSP m_colorSet; 0137 QPointer<const KoColorDisplayRendererInterface> m_displayRenderer; 0138 0139 }; 0140 0141 #endif