| 1 | /* This file is part of the KDE project |
| 2 | * |
| 3 | * SPDX-FileCopyrightText: 2005..2022 Halla Rempt <[email protected]> |
| 4 | * SPDX-FileCopyrightText: 2016 L. E. Segovia <[email protected]> |
| 5 | * SPDX-FileCopyrightText: 2018 Michael Zhou <[email protected]> |
| 6 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef KISSWATCHGROUP_H |
| 10 | #define KISSWATCHGROUP_H |
| 11 | |
| 12 | #include "KisSwatch.h" |
| 13 | |
| 14 | #include "kritapigment_export.h" |
| 15 | |
| 16 | #include <QSharedPointer> |
| 17 | #include <QList> |
| 18 | #include <QMap> |
| 19 | |
| 20 | #include <KisPropagateConstWrapper.h> |
| 21 | |
| 22 | /** |
| 23 | * @brief The KisSwatchGroup class stores a matrix of color swatches |
| 24 | * swatches can accessed using (x, y) coordinates. |
| 25 | * x is the column number from left to right and y is the row number from top |
| 26 | * to bottom. |
| 27 | * Both x and y start at 0 |
| 28 | * there could be empty entries, so the checkEntry(int, int) method must used |
| 29 | * whenever you want to get an entry from the matrix |
| 30 | */ |
| 31 | class KRITAPIGMENT_EXPORT KisSwatchGroup |
| 32 | { |
| 33 | public /* struct */: |
| 34 | struct SwatchInfo { |
| 35 | QString group; |
| 36 | KisSwatch swatch; |
| 37 | int row; |
| 38 | int column; |
| 39 | }; |
| 40 | |
| 41 | public: |
| 42 | |
| 43 | static int DEFAULT_COLUMN_COUNT; |
| 44 | static int DEFAULT_ROW_COUNT; |
| 45 | |
| 46 | ~KisSwatchGroup(); |
| 47 | KisSwatchGroup(const KisSwatchGroup &rhs); |
| 48 | KisSwatchGroup &operator=(const KisSwatchGroup &rhs); |
| 49 | |
| 50 | void setName(const QString &name); |
| 51 | QString name() const; |
| 52 | |
| 53 | void setRowCount(int newRowCount); |
| 54 | int rowCount() const; |
| 55 | |
| 56 | int colorCount() const; |
| 57 | int slotCount() const; |
| 58 | |
| 59 | /** |
| 60 | * @brief getColors |
| 61 | * @return the list of colors in this SwatchGroup, in no specific order. |
| 62 | */ |
| 63 | QList<SwatchInfo> infoList() const; |
| 64 | |
| 65 | /** |
| 66 | * @brief checkSwatch |
| 67 | * checks if position @p column and @p row has a valid entry |
| 68 | * both @p column and @p row start from 0 |
| 69 | * @param column |
| 70 | * @param row |
| 71 | * @return true if there is a valid entry at position (column, row) |
| 72 | */ |
| 73 | bool checkSwatchExists(int column, int row) const; |
| 74 | |
| 75 | /** |
| 76 | * @brief getSwatch |
| 77 | * used to get the swatch entry at position (@p column, @p row) |
| 78 | * there is an assertion to make sure that this position isn't empty, |
| 79 | * so checkEntry(int, int) must be used before this method to ensure |
| 80 | * a valid entry can be found |
| 81 | * @param column |
| 82 | * @param row |
| 83 | * @return the swatch entry at position (column, row) |
| 84 | */ |
| 85 | KisSwatch getSwatch(int column, int row) const; |
| 86 | |
| 87 | private: |
| 88 | |
| 89 | friend class KoColorSet; |
| 90 | friend struct AddSwatchCommand; |
| 91 | friend struct RemoveGroupCommand; |
| 92 | friend struct RemoveSwatchCommand; |
| 93 | friend struct AddGroupCommand; |
| 94 | friend struct ClearCommand; |
| 95 | friend struct SetColumnCountCommand; |
| 96 | friend class TestKisSwatchGroup; |
| 97 | friend class TestKoColorSet; |
| 98 | |
| 99 | // Hidden, you're supposed to go through KoColorSet or KisPaletteModel |
| 100 | KisSwatchGroup(); |
| 101 | |
| 102 | friend class KisPaletteEditor; // Ew, gross! Refactor this when you understand what the PaletteEditor does... |
| 103 | |
| 104 | /** |
| 105 | * @brief setSwatch |
| 106 | * sets the entry at position (@p column, @p row) to be @p e |
| 107 | * @param e |
| 108 | * @param column |
| 109 | * @param row |
| 110 | */ |
| 111 | void setSwatch(const KisSwatch &e, int column, int row); |
| 112 | |
| 113 | /** |
| 114 | * @brief removeSwatch |
| 115 | * removes the entry at position (@p column, @p row) |
| 116 | * @param column |
| 117 | * @param row |
| 118 | * @return true if these is an entry at (column, row) |
| 119 | */ |
| 120 | bool removeSwatch(int column, int row); |
| 121 | |
| 122 | /** |
| 123 | * @brief addEntry |
| 124 | * adds the entry e to the right of the rightmost entry in the last row |
| 125 | * if the rightmost entry in the last row is in the right most column, |
| 126 | * add e to the leftmost column of a new row |
| 127 | * |
| 128 | * when column is set to 0, resize number of columns to default |
| 129 | * @param e |
| 130 | */ |
| 131 | QPair<int, int> addSwatch(const KisSwatch &e); |
| 132 | |
| 133 | void clear(); |
| 134 | |
| 135 | |
| 136 | void setColumnCount(int columnCount); |
| 137 | int columnCount() const; |
| 138 | |
| 139 | /** |
| 140 | * The usage of propagate_const makes all 'const' methods of the |
| 141 | * class reentrant. Which is necessary for, e.g., palettize filter. |
| 142 | */ |
| 143 | struct Private; |
| 144 | std::experimental::propagate_const<std::unique_ptr<Private>> d; |
| 145 | }; |
| 146 | |
| 147 | inline QDebug operator<<(QDebug dbg, const KisSwatchGroup group) |
| 148 | { |
| 149 | dbg.nospace() << "[Group] Name: " << group.name(); |
| 150 | return dbg.space(); |
| 151 | } |
| 152 | |
| 153 | typedef QSharedPointer<KisSwatchGroup> KisSwatchGroupSP; |
| 154 | |
| 155 | |
| 156 | #endif // KISSWATCHGROUP_H |