1 /*
2 * Copyright (c) 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "Palette.h"
20 #include <KoColorSet.h>
21 #include <KisSwatch.h>
22 #include <KisSwatchGroup.h>
23 #include <ManagedColor.h>
24 #include <KisPaletteModel.h>
25
26 struct Palette::Private {
27 KoColorSet *palette {0};
28 };
29
Palette(Resource * resource)30 Palette::Palette(Resource *resource): d(new Private()) {
31 d->palette = dynamic_cast<KoColorSet*>(resource->resource());
32 }
33
~Palette()34 Palette::~Palette()
35 {
36 delete d;
37 }
38
numberOfEntries() const39 int Palette::numberOfEntries() const
40 {
41 if (!d->palette) return 0;
42 return d->palette->colorCount();
43 }
44
columnCount()45 int Palette::columnCount()
46 {
47 if (!d->palette) return 0;
48 return d->palette->columnCount();
49 }
50
setColumnCount(int columns)51 void Palette::setColumnCount(int columns)
52 {
53 if (d->palette && columns > 0)
54 d->palette->setColumnCount(columns);
55 }
56
comment()57 QString Palette::comment()
58 {
59 if (!d->palette) return "";
60 return d->palette->comment();
61 }
62
setComment(QString comment)63 void Palette::setComment(QString comment)
64 {
65 if (!d->palette) return;
66 return d->palette->setComment(comment);
67 }
68
groupNames() const69 QStringList Palette::groupNames() const
70 {
71 if (!d->palette) return QStringList();
72 return d->palette->getGroupNames();
73 }
74
addGroup(QString name)75 bool Palette::addGroup(QString name)
76 {
77 if (!d->palette) return false;
78 return d->palette->addGroup(name);
79 }
80
removeGroup(QString name,bool keepColors)81 bool Palette::removeGroup(QString name, bool keepColors)
82 {
83 if (!d->palette) return false;
84 return d->palette->removeGroup(name, keepColors);
85 }
86
colorsCountTotal()87 int Palette::colorsCountTotal()
88 {
89 if (!d->palette) return 0;
90 return d->palette->colorCount();
91 }
92
colorSetEntryByIndex(int index)93 Swatch *Palette::colorSetEntryByIndex(int index)
94 {
95 if (!d->palette || columnCount() == 0) {
96 return new Swatch();
97 }
98 int col = index % columnCount();
99 int row = (index - col) / columnCount();
100 return new Swatch(d->palette->getColorGlobal(col, row));
101 }
102
colorSetEntryFromGroup(int index,const QString & groupName)103 Swatch *Palette::colorSetEntryFromGroup(int index, const QString &groupName)
104 {
105 if (!d->palette || columnCount() == 0) {
106 return new Swatch();
107 }
108 int row = index % columnCount();
109 return new Swatch(d->palette->getColorGroup((index - row) / columnCount(), row, groupName));
110 }
111
addEntry(Swatch entry,QString groupName)112 void Palette::addEntry(Swatch entry, QString groupName)
113 {
114 d->palette->add(entry.kisSwatch(), groupName);
115 }
116
removeEntry(int index,const QString &)117 void Palette::removeEntry(int index, const QString &/*groupName*/)
118 {
119 int col = index % columnCount();
120 int tmp = index;
121 int row = (index - col) / columnCount();
122 KisSwatchGroup *groupFoundIn = 0;
123 Q_FOREACH(const QString &name, groupNames()) {
124 KisSwatchGroup *g = d->palette->getGroup(name);
125 tmp -= g->rowCount() * columnCount();
126 if (tmp < 0) {
127 groupFoundIn = g;
128 break;
129 }
130 row -= g->rowCount();
131
132 }
133 if (!groupFoundIn) { return; }
134 groupFoundIn->removeEntry(col, row);
135 }
136
changeGroupName(QString oldGroupName,QString newGroupName)137 bool Palette::changeGroupName(QString oldGroupName, QString newGroupName)
138 {
139 return d->palette->changeGroupName(oldGroupName, newGroupName);
140 }
141
moveGroup(const QString & groupName,const QString & groupNameInsertBefore)142 bool Palette::moveGroup(const QString &groupName, const QString &groupNameInsertBefore)
143 {
144 return d->palette->moveGroup(groupName, groupNameInsertBefore);
145 }
146
save()147 bool Palette::save()
148 {
149 if (d->palette->filename().size()>0) {
150 return d->palette->save();
151 }
152 //if there's no filename the palette proly doesn't even exist...
153 return false;
154 }
155
colorSet()156 KoColorSet *Palette::colorSet()
157 {
158 return d->palette;
159 }
160