File indexing completed on 2024-05-12 16:02:04

0001 /*
0002  *  SPDX-FileCopyrightText: 2013 Sven Langkamp <sven.langkamp@gmail.com>
0003  *
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 <QScopedPointer>
0018 
0019 class KoColorSet;
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     enum AdditionalRoles {
0036         IsGroupNameRole = Qt::UserRole + 1,
0037         CheckSlotRole,
0038         GroupNameRole,
0039         RowInGroupRole
0040     };
0041 
0042 public /* overridden methods */: // QAbstractTableModel
0043     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
0044     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0045     int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0046     /**
0047      * @brief index
0048      * @param row
0049      * @param column
0050      * @param parent
0051      * @return the index of for the data at row, column
0052      * if the data is a color entry, the internal pointer points to the group
0053      * the entry belongs to, and the row and column are row number and column
0054      * number inside the group.
0055      * if the data is a group, the row number and group number is Q_INFINIFY,
0056      * and the internal pointer also points to the group
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     /**
0063      * @brief dropMimeData
0064      * This is an overridden function that handles dropped mimedata.
0065      * right now only colorsetentries and colorsetgroups are handled.
0066      * @return
0067      */
0068     bool dropMimeData(const QMimeData *data, Qt::DropAction action,
0069                       int row, int column, const QModelIndex &parent) override;
0070     /**
0071      * @brief mimeData
0072      * gives the mimedata for a kocolorsetentry or a kocolorsetgroup.
0073      * @param indexes
0074      * @return the mimedata for the given indices
0075      */
0076     QMimeData *mimeData(const QModelIndexList &indexes) const override;
0077 
0078     QStringList mimeTypes() const override;
0079 
0080     Qt::DropActions supportedDropActions() const override;
0081     /**
0082      * @brief setData
0083      * setData is not used as KoColor is not a QVariant
0084      * use setEntry, addEntry and removeEntry instead
0085      */
0086     // TODO Used QVariant::setValue and QVariant.value<KoColor> to implement this
0087     // bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0088 
0089 Q_SIGNALS:
0090     /**
0091      * @brief sigPaletteModified
0092      * emitted when palette associated with the model is modified
0093      */
0094     void sigPaletteModified();
0095     /**
0096      * @brief sigPaletteChanged
0097      * emitted when the palette associated with the model is made another one
0098      */
0099     void sigPaletteChanged();
0100 
0101 public /* methods */:
0102     /**
0103      * @brief addEntry
0104      * proper function to handle adding entries.
0105      * @return whether successful.
0106      */
0107     bool addEntry(const KisSwatch &entry,
0108                   const QString &groupName = KoColorSet::GLOBAL_GROUP_NAME);
0109 
0110     void setEntry(const KisSwatch &entry, const QModelIndex &index);
0111 
0112     /**
0113      * @brief removeEntry
0114      * proper function to remove the colorsetentry at the given index.
0115      * The consolidates both removeentry and removegroup.
0116      * @param index the given index
0117      * @param keepColors This bool determines whether, when deleting a group,
0118      * the colors should be added to the default group. This is usually desirable,
0119      * so hence the default is true.
0120      * @return if successful
0121      */
0122     bool removeEntry(const QModelIndex &index, bool keepColors=true);
0123     void removeGroup(const QString &groupName, bool keepColors);
0124     bool renameGroup(const QString &groupName, const QString &newName);
0125     void addGroup(const KisSwatchGroup &group);
0126     void setRowNumber(const QString &groupName, int rowCount);
0127     void clear();
0128     void clear(int defaultColumnsCount);
0129 
0130     KisSwatch getEntry(const QModelIndex &index) const;
0131 
0132     void setPalette(KoColorSetSP colorSet);
0133     KoColorSetSP colorSet() const;
0134 
0135     QModelIndex indexForClosest(const KoColor &compare);
0136     int indexRowForInfo(const KisSwatchGroup::SwatchInfo &info);
0137 
0138 public Q_SLOTS:
0139 
0140     void slotExternalPaletteModified(QSharedPointer<KoColorSet> resource);
0141 
0142 private Q_SLOTS:
0143     void slotDisplayConfigurationChanged();
0144     void slotPaletteModified();
0145 
0146 private /* methods */:
0147     QVariant dataForGroupNameRow(const QModelIndex &idx, int role) const;
0148     QVariant dataForSwatch(const QModelIndex &idx, int role) const;
0149     int rowNumberInGroup(int rowInModel) const;
0150     int groupNameRowForRow(int rowInModel) const;
0151     int groupNameRowForName(const QString &groupName);
0152     void resetGroupNameRows();
0153     /**
0154      * Installs a display renderer object for a palette that will
0155      * convert the KoColor to the displayable QColor. Default is the
0156      * dumb renderer.
0157      */
0158     void setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer);
0159 
0160 
0161 private /* member variables */:
0162     QSharedPointer<KoColorSet> m_colorSet;
0163     QPointer<const KoColorDisplayRendererInterface> m_displayRenderer;
0164     QMap<int, QString> m_rowGroupNameMap;
0165 
0166 friend class KisPaletteView;
0167 };
0168 
0169 #endif