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++
Python Palette Docker: Fix sorting with groups
Freya Lupen7b77d225801e
diff --git a/libs/libkis/Palette.cpp b/libs/libkis/Palette.cppindex bbd526aed5..82768f1e59 100644--- a/libs/libkis/Palette.cpp+++ b/libs/libkis/Palette.cpp@@ -55,6 +55,29 @@ void Palette::setColumnCount(int columns)d->palette->setColumnCount(columns);}+int Palette::rowCount()+{+ if (!d->palette) return 0;+ return d->palette->rowCount();+}++int Palette::rowCountGroup(QString name)+{+ if (!d->palette) return 0;+ KisSwatchGroupSP group = d->palette->getGroup(name);+ if (!group) return 0;+ return group->rowCount();+}++void Palette::setRowCountGroup(int rows, QString name)+{+ if (!d->palette) return;+ KisSwatchGroupSP group = d->palette->getGroup(name);+ if (!group) return;+ if (rows > 0)+ group->setRowCount(rows);+}+QString Palette::comment(){if (!d->palette) return "";@@ -94,11 +117,24 @@ int Palette::colorsCountTotal()int Palette::colorsCountGroup(QString name){if (!d->palette) return 0;- KisSwatchGroupSP group = d->palette->getGroup(name);+ KisSwatchGroupSP group = d->palette->getGroup(name);if (!group) return 0;return group->colorCount();}+int Palette::slotCount()+{+ if (!d->palette) return 0;+ return d->palette->slotCount();+}++int Palette::slotCountGroup(QString name)+{+ if (!d->palette) return 0;+ KisSwatchGroupSP group = d->palette->getGroup(name);+ if (!group) return 0;+ return group->slotCount();+}Swatch *Palette::colorSetEntryByIndex(int index){@@ -136,7 +172,7 @@ void Palette::addEntry(Swatch entry, QString groupName)d->palette->addSwatch(entry.kisSwatch(), groupName);}-void Palette::removeEntry(int index, const QString &/*groupName*/)+void Palette::removeEntry(int index){int col = index % columnCount();int tmp = index;@@ -150,11 +186,19 @@ void Palette::removeEntry(int index, const QString &/*groupName*/)break;}row -= g->rowCount();-}if (!groupFoundIn) { return; }d->palette->removeSwatch(col, row, groupFoundIn);}+void Palette::removeEntryFromGroup(int index, const QString &groupName)+{+ if (!d->palette) return;+ KisSwatchGroupSP group = d->palette->getGroup(groupName);+ if (!group) return;+ int col = index % columnCount();+ int row = (index - col) / columnCount();+ d->palette->removeSwatch(col, row, group);+}void Palette::changeGroupName(QString oldGroupName, QString newGroupName){diff --git a/libs/libkis/Palette.h b/libs/libkis/Palette.hindex 74e9398e59..3d01f7c285 100644--- a/libs/libkis/Palette.h+++ b/libs/libkis/Palette.h@@ -56,7 +56,7 @@ public:public Q_SLOTS:/**- * @brief number of colors (swatches) in palette+ * @brief number of filled colors (swatches) in palette* NOTE: same as `colorsCountTotal()`** @return total number of colors@@ -80,6 +80,28 @@ public Q_SLOTS:*/void setColumnCount(int columns);+ /**+ * @brief The number of rows in the palette grid.+ * If the palette has groups, the row count is defined by the groups' row count.+ * Otherwise, it's determined by the number of columns and entries.+ * @return the number of rows this palette has.+ */+ int rowCount();++ /**+ * @brief The number of rows defined in the given group.+ * @param name of the group to check.+ * @return the number of rows this group is set to use.+ */+ int rowCountGroup(QString name);++ /**+ * @brief Set the number of rows defined in the given group.+ * @param rows the amount of rows this group should use.+ * @param name of the group to modify.+ */+ void setRowCountGroup(int rows, QString name);+/*** @brief the comment or description associated with the palette.*@@ -113,13 +135,13 @@ public Q_SLOTS:* @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 name The name of the group to remove.* @param keepColors whether or not to delete all the colors inside, or to move them to the default group.*/void removeGroup(QString name, bool keepColors = true);/**- * @brief number of colors (swatches) in palette+ * @brief number of filled colors (swatches) in palette* NOTE: same as `numberOfEntries()`** @return total number of colors@@ -129,10 +151,27 @@ public Q_SLOTS:/*** @brief colorsCountGroup* @param name of the group to check. Empty is the default group.- * @return the amount of colors within that group.+ * @return the amount of filled colors within that group.*/int colorsCountGroup(QString name);+ /**+ * @brief number of slots for swatches in palette+ * This includes any empty slots not filled by a color.+ *+ * @return total number of slots+ */+ int slotCount();++ /**+ * @brief number of slots for swatches in group+ * This includes any empty slots not filled by a color.+ *+ * @param name of the group to check. Empty is the default group.+ * @return number of slots in group+ */+ int slotCountGroup(QString name);+/*** @brief colorSetEntryByIndex* get the colorsetEntry from the global index.@@ -182,12 +221,19 @@ public Q_SLOTS:void addEntry(Swatch entry, QString groupName = QString());/**- * @brief remove from defined group a color entry designed by given index.+ * @brief Remove the color entry at the given index in this palette.+ *+ * @param index index in this palette.+ */+ void removeEntry(int index);++ /**+ * @brief Remove the color entry at the given index in the given group.** @param index index in the group.- * @param groupName the name of the group within which color have to be removed.+ * @param groupName the name of the group to remove the entry from.*/- void removeEntry(int index, const QString &groupName);+ void removeEntryFromGroup(int index, const QString &groupName);/*** @brief changeGroupName@@ -233,7 +279,7 @@ private:/*** @brief colorSet- * @return gives qa KoColorSet object back+ * @return gives a KoColorSet object back*/KoColorSetSP colorSet();diff --git a/libs/pigment/resources/KisSwatchGroup.cpp b/libs/pigment/resources/KisSwatchGroup.cppindex 657cce2655..0afbaac094 100644--- a/libs/pigment/resources/KisSwatchGroup.cpp+++ b/libs/pigment/resources/KisSwatchGroup.cpp@@ -205,6 +205,11 @@ int KisSwatchGroup::colorCount() constreturn d->colorCount;}+int KisSwatchGroup::slotCount() const+{+ return columnCount() * rowCount();+}+QList<KisSwatchGroup::SwatchInfo> KisSwatchGroup::infoList() const{QList<SwatchInfo> res;diff --git a/libs/pigment/resources/KisSwatchGroup.h b/libs/pigment/resources/KisSwatchGroup.hindex 3eea6461aa..38edaae60a 100644--- a/libs/pigment/resources/KisSwatchGroup.h+++ b/libs/pigment/resources/KisSwatchGroup.h@@ -56,6 +56,7 @@ public:int rowCount() const;int colorCount() const;+ int slotCount() const;/*** @brief getColorsdiff --git a/libs/pigment/resources/KoColorSet.cpp b/libs/pigment/resources/KoColorSet.cppindex f86b554993..5ddf264d5f 100644--- a/libs/pigment/resources/KoColorSet.cpp+++ b/libs/pigment/resources/KoColorSet.cpp@@ -915,6 +915,15 @@ quint32 KoColorSet::colorCount() constreturn colorCount;}+quint32 KoColorSet::slotCount() const+{+ int slotCount = 0;+ for (const KisSwatchGroupSP &group : d->swatchGroups) {+ slotCount += group->slotCount();+ }+ return slotCount;+}+KisSwatchGroupSP KoColorSet::getGroup(const QString &name) const{for (KisSwatchGroupSP &group : d->swatchGroups) {diff --git a/libs/pigment/resources/KoColorSet.h b/libs/pigment/resources/KoColorSet.hindex d7fb0b39f5..4f2fb84022 100644--- a/libs/pigment/resources/KoColorSet.h+++ b/libs/pigment/resources/KoColorSet.h@@ -115,6 +115,7 @@ public:int rowCountWithTitles() const;quint32 colorCount() const;+ quint32 slotCount() const;PaletteType paletteType() const;void setPaletteType(PaletteType paletteType);diff --git a/plugins/extensions/pykrita/sip/krita/Palette.sip b/plugins/extensions/pykrita/sip/krita/Palette.sipindex 990bf0697b..772deba176 100644--- a/plugins/extensions/pykrita/sip/krita/Palette.sip+++ b/plugins/extensions/pykrita/sip/krita/Palette.sip@@ -26,6 +26,9 @@ public Q_SLOTS:int numberOfEntries() const;int columnCount();void setColumnCount(int columns);+ int rowCount();+ int rowCountGroup(QString name);+ void setRowCountGroup(int rows, QString name);QString comment();void setComment(QString comment);QStringList groupNames();@@ -33,12 +36,15 @@ public Q_SLOTS:void removeGroup(QString name, bool keepColors);int colorsCountTotal();int colorsCountGroup(QString name);+ int slotCount();+ int slotCountGroup(QString name);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 removeEntry(int index);+ void removeEntryFromGroup(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);diff --git a/plugins/python/palette_docker/palette_exporter_gimppalette.py b/plugins/python/palette_docker/palette_exporter_gimppalette.pyindex b393b813e6..96e1573d76 100644--- a/plugins/python/palette_docker/palette_exporter_gimppalette.py+++ b/plugins/python/palette_docker/palette_exporter_gimppalette.py@@ -43,8 +43,8 @@ class gimpPaletteExporter:groupNames = self.currentPalette.groupNames()for groupName in groupNames:- colorCount = self.currentPalette.colorsCountGroup(groupName)- for i in range(colorCount):+ slotCount = self.currentPalette.slotCountGroup(groupName)+ for i in range(slotCount):entry = self.currentPalette.entryByIndexFromGroup(i, groupName)color = entry.color()# convert to sRGBdiff --git a/plugins/python/palette_docker/palette_exporter_inkscapeSVG.py b/plugins/python/palette_docker/palette_exporter_inkscapeSVG.pyindex 6dd4f3f34e..878b53f5eb 100644--- a/plugins/python/palette_docker/palette_exporter_inkscapeSVG.py+++ b/plugins/python/palette_docker/palette_exporter_inkscapeSVG.py@@ -84,37 +84,44 @@ class inkscapeSVGExporter:groupTitle.appendChild(svgDoc.createTextNode(groupName))svgSwatches.appendChild(groupTitle)Row += 1- colorCount = self.currentPalette.colorsCountGroup(groupName)- for i in range(colorCount):- entry = self.currentPalette.entryByIndexFromGroup(- i, groupName)- color = entry.color()- iccColor = "icc-color(" + color.colorProfile()- for c in range(len(color.componentsOrdered()) - 1):- iccColor = "{col},{c}".format(- col=iccColor, c=color.componentsOrdered()[c])- iccColor = iccColor + ")"- if color.colorProfile() not in iccProfileList:- iccProfileList.append(color.colorProfile())- # convert to sRGB- color.setColorSpace("RGBA", "U8", "sRGB built-in")- red = max(min(int(color.componentsOrdered()[0] * 255), 255), 0)- green = max(min(int(color.componentsOrdered()[1] * 255), 255), 0)- blue = max(min(int(color.componentsOrdered()[2] * 255), 255), 0)- hexcode = "#{red:02x}{green:02x}{blue:02x}".format(- red=red, green=green, blue=blue)+ slotCount = self.currentPalette.slotCountGroup(groupName)+ for i in range(slotCount):+ entry = self.currentPalette.entryByIndexFromGroup(i, groupName)+swatchName = "{i}-{name}".format(i=i, name=entry.name())swatchName = swatchName.replace(" ", "-")swatchName = swatchName.replace("(", "-")swatchName = swatchName.replace(")", "-")+swatchMain = svgDoc.createElement("linearGradient")swatchMain.setAttribute("osb:paint", "solid")swatchMain.setAttribute("id", swatchName)swatchSub = svgDoc.createElement("stop")- swatchSub.setAttribute(- "style",- "stop-color: {hex} {color};stop-opacity:1;".format(- hex=hexcode, color=iccColor))+ if entry.isValid():+ color = entry.color()+ iccColor = "icc-color(" + color.colorProfile()+ for c in range(len(color.componentsOrdered()) - 1):+ iccColor = "{col},{c}".format(+ col=iccColor, c=color.componentsOrdered()[c])+ iccColor = iccColor + ")"+ if color.colorProfile() not in iccProfileList:+ iccProfileList.append(color.colorProfile())+ # convert to sRGB+ color.setColorSpace("RGBA", "U8", "sRGB built-in")+ red = max(min(int(color.componentsOrdered()[0] * 255), 255), 0)+ green = max(min(int(color.componentsOrdered()[1] * 255), 255), 0)+ blue = max(min(int(color.componentsOrdered()[2] * 255), 255), 0)+ hexcode = "#{red:02x}{green:02x}{blue:02x}".format(+ red=red, green=green, blue=blue)++ swatchSub.setAttribute(+ "style",+ "stop-color: {hex} {color};stop-opacity:1;".format(+ hex=hexcode, color=iccColor))+ else: # empty slot- transparent black+ swatchSub.setAttribute(+ "style",+ "stop-color: #000000;stop-opacity:0;")swatchMain.appendChild(swatchSub)svgDefs.appendChild(swatchMain)diff --git a/plugins/python/palette_docker/palette_sortColors.py b/plugins/python/palette_docker/palette_sortColors.pyindex b62327f31d..917176e4d2 100644--- a/plugins/python/palette_docker/palette_sortColors.py+++ b/plugins/python/palette_docker/palette_sortColors.py@@ -19,44 +19,49 @@ class sortColors(object):self.sort_all_groups()def sort_all_groups(self):- self.sort_color_by_name(str())groupNames = self.currentPalette.groupNames()for groupName in groupNames:self.sort_color_by_name(groupName)def sort_color_by_name(self, groupName):d = {}- colorCount = self.currentPalette.colorsCountGroup(groupName)- for i in range(colorCount - 1, -1, -1):+ slotCount = self.currentPalette.slotCountGroup(groupName)+ for i in range(slotCount - 1, -1, -1):entry = self.currentPalette.entryByIndexFromGroup((i), groupName)+ if not entry.isValid():+ continued[entry.name() + str(i)] = entry- self.currentPalette.removeEntry((i), groupName)+ self.currentPalette.removeEntryFromGroup((i), groupName)for s in sorted(d):self.currentPalette.addEntry(d[s], groupName)def sort_color_by_id(self, groupName):d = {}- colorCount = self.currentPalette.colorsCountGroup(groupName)- for i in range(colorCount - 1, -1, -1):+ slotCount = self.currentPalette.slotCountGroup(groupName)+ for i in range(slotCount - 1, -1, -1):entry = self.currentPalette.entryByIndexFromGroup((i), groupName)+ if not entry.isValid():+ continued[entry.id() + " " + str(i)] = entry- self.currentPalette.removeEntry((i), groupName)+ self.currentPalette.removeEntryFromGroup((i), groupName)for s in sorted(d):self.currentPalette.addEntry(d[s], groupName)def sort_by_value(self, groupName):d = {}- colorCount = self.currentPalette.colorsCountGroup(groupName)- for i in range(colorCount - 1, -1, -1):+ slotCount = self.currentPalette.slotCountGroup(groupName)+ for i in range(slotCount - 1, -1, -1):entry = self.currentPalette.entryByIndexFromGroup((i), groupName)+ if not entry.isValid():+ continuecolor = entry.color()color.setColorSpace("RGBA", "U8", "sRGB built-in")d[color.components()[0] +color.components()[1] +color.components()[2]] = entry- self.currentPalette.removeEntry((i), groupName)+ self.currentPalette.removeEntryFromGroup((i), groupName)for s in sorted(d):self.currentPalette.addEntry(d[s], groupName)