File indexing completed on 2024-05-12 15:59:07

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef LIBKIS_PALETTE_H
0008 #define LIBKIS_PALETTE_H
0009 
0010 #include <QObject>
0011 #include <QList>
0012 
0013 #include "kritalibkis_export.h"
0014 #include "libkis.h"
0015 #include "Resource.h"
0016 #include "KoColorSet.h"
0017 #include <Swatch.h>
0018 
0019 class ManagedColor;
0020 
0021 
0022 /**
0023  * @brief The Palette class
0024  * Palette is a resource object that stores organised color data.
0025  * It's purpose is to allow artists to save colors and store them.
0026  *
0027  * An example for printing all the palettes and the entries:
0028  *
0029  * @code
0030 import sys
0031 from krita import *
0032 
0033 resources = Application.resources("palette")
0034 
0035 for (k, v) in resources.items():
0036     print(k)
0037     palette = Palette(v)
0038     for x in range(palette.numberOfEntries()):
0039         entry = palette.colorSetEntryByIndex(x)
0040         c = palette.colorForEntry(entry);
0041         print(x, entry.name(), entry.id(), entry.spotColor(), c.toQString())
0042  * @endcode
0043  */
0044 
0045 class KRITALIBKIS_EXPORT Palette : public QObject
0046 {
0047 public:
0048     Palette(Resource *resource);
0049     ~Palette() override;
0050 
0051     /**
0052      * @brief numberOfEntries
0053      * @return
0054      */
0055     int numberOfEntries() const;
0056 
0057     /**
0058      * @brief columnCount
0059      * @return the amount of columns this palette is set to use.
0060      */
0061     int columnCount();
0062     /**
0063      * @brief setColumnCount
0064      * Set the amount of columns this palette should use.
0065      */
0066     void setColumnCount(int columns);
0067     /**
0068      * @brief comment
0069      * @return the comment or description associated with the palette.
0070      */
0071     QString comment();
0072     /**
0073      * @brief setComment
0074      * set the comment or description associated with the palette.
0075      * @param comment
0076      */
0077     void setComment(QString comment);
0078     /**
0079      * @brief groupNames
0080      * @return the list of group names. This is list is in the order these groups are in the file.
0081      */
0082     QStringList groupNames() const;
0083     /**
0084      * @brief addGroup
0085      * @param name of the new group
0086      * @return whether adding the group was successful.
0087      */
0088     bool addGroup(QString name);
0089     /**
0090      * @brief removeGroup
0091      * @param name the name of the group to remove.
0092      * @param keepColors whether or not to delete all the colors inside, or to move them to the default group.
0093      * @return
0094      */
0095     bool removeGroup(QString name, bool keepColors = true);
0096 
0097     /**
0098      * @brief colorsCountTotal
0099      * @return the total amount of entries in the whole group
0100      */
0101     int colorsCountTotal();
0102 
0103     /**
0104      * @brief colorSetEntryByIndex
0105      * get the colorsetEntry from the global index.
0106      * @param index the global index
0107      * @return the colorset entry
0108      */
0109     Swatch *colorSetEntryByIndex(int index);
0110     /**
0111      * @brief colorSetEntryFromGroup
0112      * @param index index in the group.
0113      * @param groupName the name of the group to get the color from.
0114      * @return the colorsetentry.
0115      */
0116     Swatch *colorSetEntryFromGroup(int index, const QString &groupName);
0117 
0118     /**
0119      * @brief addEntry
0120      * add an entry to a group. Gets appended to the end.
0121      * @param entry the entry
0122      * @param groupName the name of the group to add to.
0123      */
0124     void addEntry(Swatch entry, QString groupName = QString());
0125     /**
0126      * @brief removeEntry
0127      * remove the entry at @p index from the group @p groupName.
0128      */
0129     void removeEntry(int index, const QString &groupName);
0130 
0131     /**
0132      * @brief changeGroupName
0133      * change the group name.
0134      * @param oldGroupName the old groupname to change.
0135      * @param newGroupName the new name to change it into.
0136      * @return whether successful. Reasons for failure include not knowing have oldGroupName
0137      */
0138     bool changeGroupName(QString oldGroupName, QString newGroupName);
0139     /**
0140      * @brief moveGroup
0141      * move the group to before groupNameInsertBefore.
0142      * @param groupName group to move.
0143      * @param groupNameInsertBefore group to inset before.
0144      * @return whether successful. Reasons for failure include either group not existing.
0145      */
0146     bool moveGroup(const QString &groupName, const QString &groupNameInsertBefore = QString());
0147 
0148     /**
0149      * @brief save
0150      * save the palette
0151      * @return whether it was successful.
0152      */
0153     bool save();
0154 
0155 private:
0156     friend class PaletteView;
0157     struct Private;
0158     Private *const d;
0159 
0160     /**
0161      * @brief colorSet
0162      * @return gives qa KoColorSet object back
0163      */
0164     KoColorSetSP colorSet();
0165 
0166 };
0167 
0168 #endif // LIBKIS_PALETTE_H