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

0001 /*  This file is part of the KDE project
0002  *
0003  *  SPDX-FileCopyrightText: 2005..2022 Halla Rempt <halla@valdyas.org>
0004  *  SPDX-FileCopyrightText: 2016 L. E. Segovia <amy@amyspark.me>
0005  *  SPDX-FileCopyrightText: 2018 Michael Zhou <simerixh@gmail.com>
0006  *  SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 
0009 #ifndef KISSWATCHGROUP_H
0010 #define KISSWATCHGROUP_H
0011 
0012 #include "KisSwatch.h"
0013 
0014 #include "kritapigment_export.h"
0015 
0016 #include <QSharedPointer>
0017 #include <QVector>
0018 #include <QList>
0019 #include <QMap>
0020 #include <QScopedPointer>
0021 
0022 #include <KisPropagateConstWrapper.h>
0023 
0024 /**
0025  * @brief The KisSwatchGroup class stores a matrix of color swatches
0026  * swatches can accessed using (x, y) coordinates.
0027  * x is the column number from left to right and y is the row number from top
0028  * to bottom.
0029  * Both x and y start at 0
0030  * there could be empty entries, so the checkEntry(int, int) method must used
0031  * whenever you want to get an entry from the matrix
0032  */
0033 class KRITAPIGMENT_EXPORT KisSwatchGroup
0034 {
0035 public /* struct */:
0036     struct SwatchInfo {
0037         QString group;
0038         KisSwatch swatch;
0039         int row;
0040         int column;
0041     };
0042 
0043 public:
0044 
0045     static int DEFAULT_COLUMN_COUNT;
0046     static int DEFAULT_ROW_COUNT;
0047 
0048     ~KisSwatchGroup();
0049     KisSwatchGroup(const KisSwatchGroup &rhs);
0050     KisSwatchGroup &operator=(const KisSwatchGroup &rhs);
0051 
0052     void setName(const QString &name);
0053     QString name() const;
0054 
0055     void setRowCount(int newRowCount);
0056     int rowCount() const;
0057 
0058     int colorCount() const;
0059 
0060     /**
0061      * @brief getColors
0062      * @return the list of colors in this SwatchGroup, in no specific order.
0063      */
0064     QList<SwatchInfo> infoList() const;
0065 
0066     /**
0067      * @brief checkSwatch
0068      * checks if position @p column and @p row has a valid entry
0069      * both @p column and @p row start from 0
0070      * @param column
0071      * @param row
0072      * @return true if there is a valid entry at position (column, row)
0073      */
0074     bool checkSwatchExists(int column, int row) const;
0075 
0076     /**
0077      * @brief getSwatch
0078      * used to get the swatch entry at position (@p column, @p row)
0079      * there is an assertion to make sure that this position isn't empty,
0080      * so checkEntry(int, int) must be used before this method to ensure
0081      * a valid entry can be found
0082      * @param column
0083      * @param row
0084      * @return the swatch entry at position (column, row)
0085      */
0086     KisSwatch getSwatch(int column, int row) const;
0087 
0088 private:
0089 
0090     friend class KoColorSet;
0091     friend struct AddSwatchCommand;
0092     friend struct RemoveGroupCommand;
0093     friend struct RemoveSwatchCommand;
0094     friend struct AddGroupCommand;
0095     friend struct ClearCommand;
0096     friend struct SetColumnCountCommand;
0097     friend class TestKisSwatchGroup;
0098     friend class TestKoColorSet;
0099 
0100     // Hidden, you're supposed to go through KoColorSet or KisPaletteModel
0101     KisSwatchGroup();
0102 
0103     friend class KisPaletteEditor; // Ew, gross! Refactor this when you understand what the PaletteEditor does...
0104 
0105     /**
0106      * @brief setSwatch
0107      * sets the entry at position (@p column, @p row) to be @p e
0108      * @param e
0109      * @param column
0110      * @param row
0111      */
0112     void setSwatch(const KisSwatch &e, int column, int row);
0113 
0114     /**
0115      * @brief removeSwatch
0116      * removes the entry at position (@p column, @p row)
0117      * @param column
0118      * @param row
0119      * @return true if these is an entry at (column, row)
0120      */
0121     bool removeSwatch(int column, int row);
0122 
0123     /**
0124      * @brief addEntry
0125      * adds the entry e to the right of the rightmost entry in the last row
0126      * if the rightmost entry in the last row is in the right most column,
0127      * add e to the leftmost column of a new row
0128      *
0129      * when column is set to 0, resize number of columns to default
0130      * @param e
0131      */
0132     QPair<int, int> addSwatch(const KisSwatch &e);
0133 
0134     void clear();
0135 
0136 
0137     void setColumnCount(int columnCount);
0138     int columnCount() const;
0139 
0140     /**
0141      * The usage of propagate_const makes all 'const' methods of the
0142      * class reentrant. Which is necessary for, e.g., palettize filter.
0143      */
0144     struct Private;
0145     std::experimental::propagate_const<std::unique_ptr<Private>> d;
0146 };
0147 
0148 inline QDebug operator<<(QDebug dbg, const KisSwatchGroup group)
0149 {
0150     dbg.nospace() << "[Group] Name: " << group.name();
0151     return dbg.space();
0152 }
0153 
0154 typedef QSharedPointer<KisSwatchGroup> KisSwatchGroupSP;
0155 
0156 
0157 #endif // KISSWATCHGROUP_H