File indexing completed on 2024-12-22 04:11:44

0001 /*  This file is part of the KDE project
0002  *  SPDX-FileCopyrightText: 2005 Boudewijn Rempt <boud@valdyas.org>
0003  *  SPDX-FileCopyrightText: 2016 L. E. Segovia <amy@amyspark.me>
0004  *  SPDX-FileCopyrightText: 2022 Halla Rempt <halla@valdyas.org>
0005  *  SPDX-License-Identifier: LGPL-2.1-or-later
0006  *
0007  */
0008 #ifndef KOCOLORSET
0009 #define KOCOLORSET
0010 
0011 #include <QObject>
0012 #include <QColor>
0013 #include <QVector>
0014 #include <QScopedPointer>
0015 #include <QSharedPointer>
0016 
0017 #include <KoResource.h>
0018 #include "KoColor.h"
0019 #include "KisSwatch.h"
0020 #include "KisSwatchGroup.h"
0021 
0022 class KUndo2Stack;
0023 
0024 /**
0025  * Also called palette.
0026  * Open Gimp, Photoshop or RIFF palette files. This is a straight port
0027  * from the Gimp.
0028  */
0029 class KRITAPIGMENT_EXPORT KoColorSet : public QObject, public KoResource
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034     static const QString GLOBAL_GROUP_NAME;
0035     static const QString KPL_VERSION_ATTR;
0036     static const QString KPL_GROUP_ROW_COUNT_ATTR;
0037     static const QString KPL_PALETTE_COLUMN_COUNT_ATTR;
0038     static const QString KPL_PALETTE_NAME_ATTR;
0039     static const QString KPL_PALETTE_COMMENT_ATTR;
0040     static const QString KPL_PALETTE_FILENAME_ATTR;
0041     static const QString KPL_PALETTE_READONLY_ATTR;
0042     static const QString KPL_COLOR_MODEL_ID_ATTR;
0043     static const QString KPL_COLOR_DEPTH_ID_ATTR;
0044     static const QString KPL_GROUP_NAME_ATTR;
0045     static const QString KPL_SWATCH_ROW_ATTR;
0046     static const QString KPL_SWATCH_COL_ATTR;
0047     static const QString KPL_SWATCH_NAME_ATTR;
0048     static const QString KPL_SWATCH_SPOT_ATTR;
0049     static const QString KPL_SWATCH_ID_ATTR;
0050     static const QString KPL_SWATCH_BITDEPTH_ATTR;
0051     static const QString KPL_PALETTE_PROFILE_TAG;
0052     static const QString KPL_SWATCH_POS_TAG;
0053     static const QString KPL_SWATCH_TAG;
0054     static const QString KPL_GROUP_TAG;
0055     static const QString KPL_PALETTE_TAG;
0056 
0057 public:
0058     enum PaletteType {
0059         UNKNOWN = 0,
0060         GPL,                // GIMP
0061         RIFF_PAL,           // RIFF
0062         ACT,                // Photoshop binary
0063         PSP_PAL,            // PaintShop Pro
0064         ACO,                // Photoshop Swatches
0065         XML,                // XML palette (Scribus)
0066         KPL,                // KoColor-based XML palette
0067         SBZ,                // SwatchBooker
0068         ASE,                // Adobe swatch exchange
0069         ACB                 // Adobe Color Book.
0070     };
0071 
0072 
0073     /**
0074      * Load a color set from a file. This can be a Gimp
0075      * palette, a RIFF palette, a Photoshop palette,
0076      * a Krita palette,
0077      * a Scribus palette or a SwatchBooker palette.
0078      */
0079     explicit KoColorSet(const QString &filename = QString());
0080 
0081     KoColorSet(const KoColorSet& rhs);
0082 
0083     ~KoColorSet() override;
0084 
0085     KoColorSet &operator=(const KoColorSet &rhs) = delete;
0086 
0087     KoResourceSP clone() const override;
0088 
0089     bool loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP resourcesInterface) override;
0090     bool saveToDevice(QIODevice* dev) const override;
0091     QString defaultFileExtension() const override;
0092     QPair<QString, QString> resourceType() const override
0093     {
0094         return QPair<QString, QString>(ResourceType::Palettes, "");
0095     }
0096 
0097     KUndo2Stack *undoStack() const;
0098 
0099     void updateThumbnail() override;
0100 
0101     void setLocked(bool lock);
0102     bool isLocked() const;
0103 
0104     void setColumnCount(int columns);
0105     int columnCount() const;
0106 
0107     void setComment(QString comment);
0108     QString comment();
0109 
0110     // Total number of rows, including empty rows in the groups, excluding rows
0111     // a model might use to show group headings
0112     int rowCount() const;
0113 
0114     int rowCountWithTitles() const;
0115 
0116     quint32 colorCount() const;
0117 
0118     PaletteType paletteType() const;
0119     void setPaletteType(PaletteType paletteType);
0120 
0121     bool fromByteArray(QByteArray &data, KisResourcesInterfaceSP resourcesInterface);
0122 
0123     /**
0124      * @brief Add a color to the palette.
0125      * @param c the swatch
0126      * @param groupName color to add the group to. If empty, it will be added to the unsorted.
0127      * @param column. The column in the group
0128      * @param row. The row in the group
0129      */
0130     void addSwatch(const KisSwatch &swatch, const QString &groupName = GLOBAL_GROUP_NAME, int column = -1, int row = -1);
0131 
0132     /**
0133      * @brief remove the swatch from the given group at column and row
0134      */
0135     void removeSwatch(int column, int row, KisSwatchGroupSP group);
0136 
0137     /**
0138      * @brief getGroup
0139      * @param name
0140      * @return the group with the name given; global group if no parameter is given
0141      * null pointer if not found.
0142      */
0143     KisSwatchGroupSP getGroup(const QString &name) const;
0144 
0145     /**
0146      * @brief getGroup get the group that covers this row
0147      * @param row
0148      * @return a swatch group
0149      */
0150     KisSwatchGroupSP getGroup(int row) const;
0151 
0152     /**
0153      * @brief getGlobalGroup
0154      * @return
0155      */
0156     KisSwatchGroupSP getGlobalGroup() const;
0157 
0158     /**
0159      * @brief changeGroupName
0160      * @param oldGroupName
0161      * @param newGroupName
0162      */
0163     void changeGroupName(const QString &oldGroupName, const QString &newGroupName);
0164 
0165     /**
0166      * @brief addGroup
0167      * Adds a new group.
0168      * @param groupName the name of the new group. When not specified, this will fail.
0169      * @return whether the group was made.
0170      */
0171     void addGroup(const QString &groupName, int columnCount = KisSwatchGroup::DEFAULT_COLUMN_COUNT, int rowCount = KisSwatchGroup::DEFAULT_ROW_COUNT);
0172 
0173     /**
0174      * @brief moveGroup
0175      * Move a group in the internal stringlist.
0176      * @param groupName the groupname to move.
0177      * @param groupNameInsertBefore the groupname to insert before. Empty means it will be added to the end.
0178      * @return
0179      */
0180     void moveGroup(const QString &groupName, const QString &groupNameInsertBefore = GLOBAL_GROUP_NAME);
0181 
0182     /**
0183      * @brief removeGroup
0184      * Remove a group from the KoColorSet
0185      * @param groupName the name of the group you want to remove.
0186      * @param keepColors Whether you wish to keep the colorsetentries. These will be added to the unsorted.
0187      * @return whether it could find the group to remove.
0188      */
0189     void removeGroup(const QString &groupName, bool keepColors = true);
0190 
0191     /**
0192      * @brief clears the complete colorset
0193      */
0194     void clear();
0195 
0196     /**
0197      * @brief getIndexClosestColor
0198      * function that matches the color to all colors in the colorset, and returns the index
0199      * of the closest match.
0200      * @param compare the color you wish to compare.
0201      * @param useGivenColorSpace whether to use the color space of the color given
0202      * when the two colors' colorspaces don't match. Else it'll use the entry's colorspace.
0203      * @return returns the int of the closest match.
0204      */
0205     KisSwatchGroup::SwatchInfo getClosestSwatchInfo(KoColor compare, bool useGivenColorSpace = true) const;
0206 
0207     /**
0208      * @brief getColorGlobal
0209      * A function for getting a color based on a global index. Useful for iterating through all color entries.
0210      * @param x the global x index over the whole palette.
0211      * @param y the global y index over the whole palette.
0212      * @return the entry.
0213      */
0214     KisSwatch getColorGlobal(quint32 column, quint32 row) const;
0215 
0216     /**
0217      * @brief getColorGroup
0218      * A function for getting the color from a specific group.
0219      * @param x the x index over the group.
0220      * @param y the y index over the group.
0221      * @param groupName the name of the group, will give unsorted when not defined.
0222      * @return the entry
0223      */
0224     KisSwatch getSwatchFromGroup(quint32 column, quint32 row, QString groupName = KoColorSet::GLOBAL_GROUP_NAME) const;
0225 
0226     /**
0227      * @brief getGroupNames
0228      * @return returns a list of group names, excluding the unsorted group.
0229      */
0230     QStringList swatchGroupNames() const;
0231 
0232     /**
0233      * @brief isGroupRow checks whether the current row is a group title
0234      * @param row the row to check
0235      * @return true if this is a group row
0236      */
0237     bool isGroupTitleRow(int row) const;
0238 
0239     /**
0240      * @brief rowForNamedGroup returns the row the group's title is on
0241      * @param groupName
0242      * @return
0243      */
0244     int startRowForGroup(const QString &groupName) const;
0245 
0246     /**
0247      * @brief rowNumberInGroup calculates the row number in the group from the global rownumber
0248      * @param rowNumber this is a row in rowCountWithTitles
0249      * @return -1 if the row is a group title row.
0250      */
0251     int rowNumberInGroup(int rowNumber) const;
0252 
0253 Q_SIGNALS:
0254 
0255     void modified();
0256 
0257 
0258 private Q_SLOTS:
0259 
0260     void canUndoChanged(bool canUndo);
0261     void canRedoChanged(bool canRedo);
0262 
0263 
0264 private:
0265 
0266     void setModified(bool);
0267 
0268     friend struct AddSwatchCommand;
0269     friend struct RemoveSwatchCommand;
0270     friend struct ChangeGroupNameCommand;
0271     friend struct AddGroupCommand;
0272     friend struct RemoveGroupCommand;
0273     friend struct ClearCommand;
0274     friend struct SetColumnCountCommand;
0275     friend struct SetCommentCommand;
0276     friend struct SetPaletteTypeCommand;
0277     friend struct MoveGroupCommand;
0278     friend class TestKoColorSet;
0279     friend class TestKisPaletteModel;
0280 
0281     class Private;
0282     const QScopedPointer<Private> d;
0283 
0284 };
0285 
0286 typedef QSharedPointer<KoColorSet> KoColorSetSP;
0287 
0288 #endif // KOCOLORSET