Krita

Palette.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "Palette.h"
8 #include <KoColorSet.h>
9 #include <KisSwatch.h>
10 #include <KisSwatchGroup.h>
11 #include <ManagedColor.h>
12 #include <KisPaletteModel.h>
13 
14 struct Palette::Private {
15  KoColorSetSP palette {0};
16 };
17 
18 Palette::Palette(Resource *resource): d(new Private()) {
19  d->palette = resource->resource().dynamicCast<KoColorSet>();
20 }
21 
22 Palette::~Palette()
23 {
24  delete d;
25 }
26 
28 {
29  if (!d->palette) return 0;
30  return d->palette->colorCount();
31 }
32 
34 {
35  if (!d->palette) return 0;
36  return d->palette->columnCount();
37 }
38 
39 void Palette::setColumnCount(int columns)
40 {
41  if (d->palette && columns > 0)
42  d->palette->setColumnCount(columns);
43 }
44 
46 {
47  if (!d->palette) return "";
48  return d->palette->comment();
49 }
50 
52 {
53  if (!d->palette) return;
54  return d->palette->setComment(comment);
55 }
56 
58 {
59  if (!d->palette) return QStringList();
60  return d->palette->swatchGroupNames();
61 }
62 
64 {
65  if (!d->palette) return;
66  d->palette->addGroup(name);
67 }
68 
69 void Palette::removeGroup(QString name, bool keepColors)
70 {
71  if (!d->palette) return;
72  return d->palette->removeGroup(name, keepColors);
73 }
74 
76 {
77  if (!d->palette) return 0;
78  return d->palette->colorCount();
79 }
80 
82 {
83  if (!d->palette || columnCount() == 0) {
84  return new Swatch();
85  }
86  int col = index % columnCount();
87  int row = (index - col) / columnCount();
88  return new Swatch(d->palette->getColorGlobal(col, row));
89 }
90 
91 Swatch *Palette::colorSetEntryFromGroup(int index, const QString &groupName)
92 {
93  if (!d->palette || columnCount() == 0) {
94  return new Swatch();
95  }
96  int row = index % columnCount();
97  return new Swatch(d->palette->getSwatchFromGroup((index - row) / columnCount(), row, groupName));
98 }
99 
100 void Palette::addEntry(Swatch entry, QString groupName)
101 {
102  d->palette->addSwatch(entry.kisSwatch(), groupName);
103 }
104 
105 void Palette::removeEntry(int index, const QString &/*groupName*/)
106 {
107  int col = index % columnCount();
108  int tmp = index;
109  int row = (index - col) / columnCount();
110  KisSwatchGroupSP groupFoundIn;
111  Q_FOREACH(const QString &name, groupNames()) {
112  KisSwatchGroupSP g = d->palette->getGroup(name);
113  tmp -= g->rowCount() * columnCount();
114  if (tmp < 0) {
115  groupFoundIn = g;
116  break;
117  }
118  row -= g->rowCount();
119 
120  }
121  if (!groupFoundIn) { return; }
122  d->palette->removeSwatch(col, row, groupFoundIn);
123 }
124 
125 void Palette::changeGroupName(QString oldGroupName, QString newGroupName)
126 {
127  d->palette->changeGroupName(oldGroupName, newGroupName);
128 }
129 
130 void Palette::moveGroup(const QString &groupName, const QString &groupNameInsertBefore)
131 {
132  return d->palette->moveGroup(groupName, groupNameInsertBefore);
133 }
134 
136 {
137  return false;
138 }
139 
140 KoColorSetSP Palette::colorSet()
141 {
142  return d->palette;
143 }
void setColumnCount(int columns)
setColumnCount Set the amount of columns this palette should use.
Definition: Palette.cpp:39
QStringList groupNames() const
groupNames
Definition: Palette.cpp:57
void removeGroup(QString name, bool keepColors=true)
removeGroup
Definition: Palette.cpp:69
QString comment()
comment
Definition: Palette.cpp:45
A Resource represents a gradient, pattern, brush tip, brush preset, palette or workspace definition.
Definition: Resource.h:30
int numberOfEntries() const
numberOfEntries
Definition: Palette.cpp:27
void moveGroup(const QString &groupName, const QString &groupNameInsertBefore=QString())
moveGroup move the group to before groupNameInsertBefore.
Definition: Palette.cpp:130
void changeGroupName(QString oldGroupName, QString newGroupName)
changeGroupName change the group name.
Definition: Palette.cpp:125
int colorsCountTotal()
colorsCountTotal
Definition: Palette.cpp:75
The Swatch class is a thin wrapper around the KisSwatch class.
Definition: Swatch.h:21
bool save()
save save the palette
Definition: Palette.cpp:135
Swatch * colorSetEntryFromGroup(int index, const QString &groupName)
colorSetEntryFromGroup
Definition: Palette.cpp:91
int columnCount()
columnCount
Definition: Palette.cpp:33
void addEntry(Swatch entry, QString groupName=QString())
addEntry add an entry to a group.
Definition: Palette.cpp:100
void setComment(QString comment)
setComment set the comment or description associated with the palette.
Definition: Palette.cpp:51
Swatch * colorSetEntryByIndex(int index)
colorSetEntryByIndex get the colorsetEntry from the global index.
Definition: Palette.cpp:81
void removeEntry(int index, const QString &groupName)
removeEntry remove the entry at index from the group groupName.
Definition: Palette.cpp:105
void addGroup(QString name)
addGroup
Definition: Palette.cpp:63
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Sep 29 2023 04:08:11 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.