KisPaletteModel.h 6.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *  Copyright (c) 2013 Sven Langkamp <sven.langkamp@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */


#ifndef KIS_PALETTEMODEL_H
#define KIS_PALETTEMODEL_H

#include <QPointer>
#include <QModelIndex>
25
#include <QMap>
26 27 28 29 30 31 32 33

#include <KoColorDisplayRendererInterface.h>

#include "kritawidgets_export.h"
#include <KoColorSet.h>
#include <QScopedPointer>

class KoColorSet;
34
class KisPaletteView;
35 36 37

/**
 * @brief The KisPaletteModel class
38 39 40
 * This, together with KisPaletteView and KisPaletteDelegate forms a mvc way to access kocolorsets.
 * A display renderer is given to this model to convert KoColor to QColor when
 * colors are requested
41 42 43 44 45
 */
class KRITAWIDGETS_EXPORT KisPaletteModel : public QAbstractTableModel
{
    Q_OBJECT
public:
46
    explicit KisPaletteModel(QObject* parent = 0);
47 48 49
    ~KisPaletteModel() override;

    enum AdditionalRoles {
Michael Zhou's avatar
Michael Zhou committed
50
        IsGroupNameRole = Qt::UserRole + 1,
51 52 53
        CheckSlotRole,
        GroupNameRole,
        RowInGroupRole
54 55
    };

56
public /* overridden methods */: // QAbstractTableModel
57
    QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    int rowCount(const QModelIndex& parent = QModelIndex()) const override;
    int columnCount(const QModelIndex& parent = QModelIndex()) const override;
    /**
     * @brief index
     * @param row
     * @param column
     * @param parent
     * @return the index of for the data at row, column
     * if the data is a color entry, the internal pointer points to the group
     * the entry belongs to, and the row and column are row number and column
     * number inside the group.
     * if the data is a group, the row number and group number is Q_INFINIFY,
     * and the internal pointer also points to the group
     */
    QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;

    Qt::ItemFlags flags(const QModelIndex& index) const override;

    /**
     * @brief dropMimeData
     * This is an overridden function that handles dropped mimedata.
     * right now only colorsetentries and colorsetgroups are handled.
     * @return
     */
    bool dropMimeData(const QMimeData *data, Qt::DropAction action,
                      int row, int column, const QModelIndex &parent) override;
    /**
     * @brief mimeData
     * gives the mimedata for a kocolorsetentry or a kocolorsetgroup.
     * @param indexes
     * @return the mimedata for the given indices
     */
    QMimeData *mimeData(const QModelIndexList &indexes) const override;

    QStringList mimeTypes() const override;

    Qt::DropActions supportedDropActions() const override;
95 96 97 98 99
    /**
     * @brief setData
     * setData is not used as KoColor is not a QVariant
     * use setEntry, addEntry and removeEntry instead
     */
Michael Zhou's avatar
Michael Zhou committed
100 101
    // TODO Used QVariant::setValue and QVariant.value<KoColor> to implement this
    // bool setData(const QModelIndex &index, const QVariant &value, int role) override;
102

103
Q_SIGNALS:
104 105 106 107 108 109 110 111 112
    /**
     * @brief sigPaletteModified
     * emitted when palette associated with the model is modified
     */
    void sigPaletteModified();
    /**
     * @brief sigPaletteChanged
     * emitted when the palette associated with the model is made another one
     */
113
    void sigPaletteChanged();
114

115
public /* methods */:
116 117 118 119 120
    /**
     * @brief addEntry
     * proper function to handle adding entries.
     * @return whether successful.
     */
Michael Zhou's avatar
Michael Zhou committed
121 122
    bool addEntry(const KisSwatch &entry,
                  const QString &groupName = KoColorSet::GLOBAL_GROUP_NAME);
123 124 125 126 127 128

    void setEntry(const KisSwatch &entry, const QModelIndex &index);

    /**
     * @brief removeEntry
     * proper function to remove the colorsetentry at the given index.
Yuri Chornoivan's avatar
Yuri Chornoivan committed
129
     * The consolidates both removeentry and removegroup.
130 131
     * @param index the given index
     * @param keepColors This bool determines whether, when deleting a group,
132 133 134 135
     * the colors should be added to the default group. This is usually desirable,
     * so hence the default is true.
     * @return if successful
     */
136
    bool removeEntry(const QModelIndex &index, bool keepColors=true);
137 138 139
    void removeGroup(const QString &groupName, bool keepColors);
    bool renameGroup(const QString &groupName, const QString &newName);
    void addGroup(const KisSwatchGroup &group);
140
    void setRowNumber(const QString &groupName, int rowCount);
141
    void clear();
Michael Zhou's avatar
Michael Zhou committed
142

143
    KisSwatch getEntry(const QModelIndex &index) const;
144

145
    void setPalette(KoColorSet* colorSet);
146 147
    KoColorSet* colorSet() const;

148
    QModelIndex indexForClosest(const KoColor &compare);
149
    int indexRowForInfo(const KisSwatchGroup::SwatchInfo &info);
150

151 152
public Q_SLOTS:

153 154
private Q_SLOTS:
    void slotDisplayConfigurationChanged();
155
    void slotPaletteModified();
156

157
private /* methods */:
158 159
    QVariant dataForGroupNameRow(const QModelIndex &idx, int role) const;
    QVariant dataForSwatch(const QModelIndex &idx, int role) const;
160
    int rowNumberInGroup(int rowInModel) const;
Michael Zhou's avatar
Michael Zhou committed
161
    int groupNameRowForRow(int rowInModel) const;
162
    int groupNameRowForName(const QString &groupName);
163
    void resetGroupNameRows();
164 165 166 167 168 169 170
    /**
     * Installs a display renderer object for a palette that will
     * convert the KoColor to the displayable QColor. Default is the
     * dumb renderer.
     */
    void setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer);

171

172
private /* member variables */:
173 174
    QPointer<KoColorSet> m_colorSet;
    QPointer<const KoColorDisplayRendererInterface> m_displayRenderer;
175
    QMap<int, QString> m_rowGroupNameMap;
176 177

friend class KisPaletteView;
178 179 180
};

#endif