Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Improve Python API - Palette

Grum 99936b46f610895
diff --git a/libs/libkis/Palette.cpp b/libs/libkis/Palette.cpp
index afc8f2d880..ac7e0aabe2 100644
--- a/libs/libkis/Palette.cpp
+++ b/libs/libkis/Palette.cpp
@@ -15,7 +15,9 @@ struct Palette::Private {
KoColorSetSP palette {0};
};
-Palette::Palette(Resource *resource): d(new Private()) {
+Palette::Palette(Resource *resource, QObject *parent)
+ : QObject(parent)
+ , d(new Private()) {
d->palette = resource->resource().dynamicCast<KoColorSet>();
}
@@ -24,6 +26,17 @@ Palette::~Palette()
delete d;
}
+bool Palette::operator==(const Palette &other) const
+{
+ return (d->palette == other.d->palette);
+}
+
+bool Palette::operator!=(const Palette &other) const
+{
+ return !(operator==(other));
+}
+
+
int Palette::numberOfEntries() const
{
if (!d->palette) return 0;
@@ -79,6 +92,12 @@ int Palette::colorsCountTotal()
}
Swatch *Palette::colorSetEntryByIndex(int index)
+{
+ warnScript << "DEPRECATED Palette.colorSetEntryByIndex() - use Palette.entryByIndex() instead";
+ return entryByIndex(index);
+}
+
+Swatch *Palette::entryByIndex(int index)
{
if (!d->palette || columnCount() == 0) {
return new Swatch();
@@ -89,6 +108,12 @@ Swatch *Palette::colorSetEntryByIndex(int index)
}
Swatch *Palette::colorSetEntryFromGroup(int index, const QString &groupName)
+{
+ warnScript << "DEPRECATED Palette.colorSetEntryFromGroup() - use Palette.entryByIndexFromGroup() instead";
+ return entryByIndexFromGroup(index, groupName);
+}
+
+Swatch *Palette::entryByIndexFromGroup(int index, const QString &groupName)
{
if (!d->palette || columnCount() == 0) {
return new Swatch();
@@ -123,6 +148,12 @@ void Palette::removeEntry(int index, const QString &/*groupName*/)
}
void Palette::changeGroupName(QString oldGroupName, QString newGroupName)
+{
+ warnScript << "DEPRECATED Palette.changeGroupName() - use Palette.renameGroup() instead";
+ return renameGroup(oldGroupName, newGroupName);
+}
+
+void Palette::renameGroup(QString oldGroupName, QString newGroupName)
{
d->palette->changeGroupName(oldGroupName, newGroupName);
}
diff --git a/libs/libkis/Palette.h b/libs/libkis/Palette.h
index 9faefc36b9..2401a26628 100644
--- a/libs/libkis/Palette.h
+++ b/libs/libkis/Palette.h
@@ -37,118 +37,185 @@ for (k, v) in resources.items():
palette = Palette(v)
for x in range(palette.numberOfEntries()):
entry = palette.colorSetEntryByIndex(x)
- c = palette.colorForEntry(entry);
- print(x, entry.name(), entry.id(), entry.spotColor(), c.toQString())
+ print(x, entry.name(), entry.id(), entry.spotColor())
* @endcode
*/
class KRITALIBKIS_EXPORT Palette : public QObject
{
+ Q_OBJECT
+ Q_DISABLE_COPY(Palette)
+
public:
- Palette(Resource *resource);
+ explicit Palette(Resource *resource, QObject *parent = 0);
~Palette() override;
+ bool operator==(const Palette &other) const;
+ bool operator!=(const Palette &other) const;
+
+public Q_SLOTS:
+
/**
- * @brief numberOfEntries
- * @return
+ * @brief number of colors (swatches) in palette
+ * NOTE: same as `colorsCountTotal()`
+ *
+ * @return total number of colors
*/
int numberOfEntries() const;
/**
- * @brief columnCount
- * @return the amount of columns this palette is set to use.
+ * @brief Palettes are defined in grids.
+ * The number of column define grid width.
+ * The number of rows will depend of columns and total number of entries.
+ *
+ * @return the number of columns this palette is set to use.
*/
int columnCount();
/**
- * @brief setColumnCount
- * Set the amount of columns this palette should use.
+ * @brief Palettes are defined in grids.
+ * The number of column define grid width, this value can be defined.
+ * The number of rows will depend of columns and total number of entries.
+ *
+ * @param columns Set the amount of columns this palette should use.
*/
void setColumnCount(int columns);
+
/**
- * @brief comment
- * @return the comment or description associated with the palette.
+ * @brief the comment or description associated with the palette.
+ *
+ * @return A string for which value contains the comment/description of palette.
*/
QString comment();
+
/**
- * @brief setComment
- * set the comment or description associated with the palette.
- * @param comment
+ * @brief the comment or description associated with the palette.
+ *
+ * @param comment set the comment or description associated with the palette.
*/
void setComment(QString comment);
+
/**
- * @brief groupNames
- * @return the list of group names. This is list is in the order these groups are in the file.
+ * @brief Palette content can be organized in groups.
+ *
+ * @return The list of group names (list of string). This list follow the order the groups are defined in palette.
*/
QStringList groupNames() const;
+
/**
- * @brief addGroup
- * @param name of the new group
- * @return whether adding the group was successful.
+ * @brief Palette content can be organized in groups.
+ * This method allows to add a new group in palette.
+ *
+ * @param name The name of the new group to add.
*/
void addGroup(QString name);
+
/**
- * @brief removeGroup
- * @param name the name of the group to remove.
+ * @brief Palette content can be organized in groups.
+ * This method allows to remove an existing group from palette.
+ *
+ * @param name The name of the new group to remve.
* @param keepColors whether or not to delete all the colors inside, or to move them to the default group.
- * @return
*/
void removeGroup(QString name, bool keepColors = true);
/**
- * @brief colorsCountTotal
- * @return the total amount of entries in the whole group
+ * @brief number of colors (swatches) in palette
+ * NOTE: same as `numberOfEntries()`
+ *
+ * @return total number of colors
*/
int colorsCountTotal();
/**
* @brief colorSetEntryByIndex
* get the colorsetEntry from the global index.
+ *
+ * DEPRECATED: use `entryByIndex()` instead
+ *
* @param index the global index
* @return the colorset entry
*/
- Swatch *colorSetEntryByIndex(int index);
+ Q_DECL_DEPRECATED Swatch *colorSetEntryByIndex(int index);
+
+ /**
+ * @brief get color (swatch) from the global index.
+ *
+ * @param index the global index
+ * @return The Swatch color for given index.
+ */
+ Swatch *entryByIndex(int index);
+
/**
* @brief colorSetEntryFromGroup
+ *
+ * DEPRECATED: use `entryByIndexFromGroup()` instead
+ *
* @param index index in the group.
* @param groupName the name of the group to get the color from.
* @return the colorsetentry.
*/
- Swatch *colorSetEntryFromGroup(int index, const QString &groupName);
+ Q_DECL_DEPRECATED Swatch *colorSetEntryFromGroup(int index, const QString &groupName);
/**
- * @brief addEntry
- * add an entry to a group. Gets appended to the end.
+ * @brief get color (swatch) from the given group index.
+ *
+ * @param index index in the group.
+ * @param groupName the name of the group to get the color from.
+ * @return The Swatch color for given index within given group name.
+ */
+ Swatch *entryByIndexFromGroup(int index, const QString &groupName);
+
+ /**
+ * @brief add a color entry to a group.
+ * Color is appended to the end.
+ *
* @param entry the entry
* @param groupName the name of the group to add to.
*/
void addEntry(Swatch entry, QString groupName = QString());
+
/**
- * @brief removeEntry
- * remove the entry at @p index from the group @p groupName.
+ * @brief remove from defined group a color entry designed by given index.
+ *
+ * @param index index in the group.
+ * @param groupName the name of the group within which color have to be removed.
*/
void removeEntry(int index, const QString &groupName);
/**
* @brief changeGroupName
* change the group name.
+ *
+ * DEPRECATED: use `renameGroup()` instead
+ *
* @param oldGroupName the old groupname to change.
* @param newGroupName the new name to change it into.
* @return whether successful. Reasons for failure include not knowing have oldGroupName
*/
- void changeGroupName(QString oldGroupName, QString newGroupName);
+ Q_DECL_DEPRECATED void changeGroupName(QString oldGroupName, QString newGroupName);
+
+ /**
+ * @brief rename a group
+ *
+ * @param oldGroupName the old groupname to change.
+ * @param newGroupName the new name to change it into.
+ */
+ void renameGroup(QString oldGroupName, QString newGroupName);
+
/**
- * @brief moveGroup
- * move the group to before groupNameInsertBefore.
+ * @brief Move the group `groupName` to position before group `groupNameInsertBefore`.
+ *
* @param groupName group to move.
- * @param groupNameInsertBefore group to inset before.
- * @return whether successful. Reasons for failure include either group not existing.
+ * @param groupNameInsertBefore reference group for which `groupName` have to be moved before.
*/
void moveGroup(const QString &groupName, const QString &groupNameInsertBefore = QString());
/**
- * @brief save
- * save the palette
- * @return whether it was successful.
+ * @brief save the palette
+ *
+ * WARNING: this method does nothing and need to be implemented!
+ *
+ * @return always False
*/
bool save();
diff --git a/plugins/extensions/pykrita/sip/krita/Palette.sip b/plugins/extensions/pykrita/sip/krita/Palette.sip
index ffeca577b6..8b4e8b3c27 100644
--- a/plugins/extensions/pykrita/sip/krita/Palette.sip
+++ b/plugins/extensions/pykrita/sip/krita/Palette.sip
@@ -3,9 +3,26 @@ class Palette : QObject
%TypeHeaderCode
#include "Palette.h"
%End
+
+%ConvertToSubClassCode
+ // Need to add sub class conversion, otherwise a Palette passed to a Signal is provided as a QObject instead of
+ // a Palette object
+ // (not sure why, but explicit conversion here fix the problem)
+ Palette* mc = qobject_cast<Palette*>(sipCpp);
+ sipType = 0;
+ if (mc) {
+ sipType = sipType_Palette;
+ }
+%End
+
Palette(const Palette & __0);
public:
Palette(Resource *resource);
+
+ bool operator==(const Palette &other) const;
+ bool operator!=(const Palette &other) const;
+
+public Q_SLOTS:
int numberOfEntries() const;
int columnCount();
void setColumnCount(int columns);
@@ -16,10 +33,13 @@ public:
void removeGroup(QString name, bool keepColors);
int colorsCountTotal();
Swatch *colorSetEntryByIndex(int index) /Factory/;
+ Swatch *entryByIndex(int index) /Factory/;
Swatch *colorSetEntryFromGroup(int index, const QString &groupName) /Factory/;
+ Swatch *entryByIndexFromGroup(int index, const QString &groupName) /Factory/;
void addEntry(Swatch entry, QString groupName);
void removeEntry(int index, const QString &groupName);
void changeGroupName(QString oldGroupName, QString newGroupName);
+ void renameGroup(QString oldGroupName, QString newGroupName);
void moveGroup(const QString &groupName, const QString &groupNameInsertBefore);
bool save();
private: