File indexing completed on 2024-05-12 15:59:07

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "Palette.h"
0008 #include <KoColorSet.h>
0009 #include <KisSwatch.h>
0010 #include <KisSwatchGroup.h>
0011 #include <ManagedColor.h>
0012 #include <KisPaletteModel.h>
0013 
0014 struct Palette::Private {
0015     KoColorSetSP palette {0};
0016 };
0017 
0018 Palette::Palette(Resource *resource): d(new Private()) {
0019     d->palette = resource->resource().dynamicCast<KoColorSet>();
0020 }
0021 
0022 Palette::~Palette()
0023 {
0024     delete d;
0025 }
0026 
0027 int Palette::numberOfEntries() const
0028 {
0029     if (!d->palette) return 0;
0030     return d->palette->colorCount();
0031 }
0032 
0033 int Palette::columnCount()
0034 {
0035     if (!d->palette) return 0;
0036     return d->palette->columnCount();
0037 }
0038 
0039 void Palette::setColumnCount(int columns)
0040 {
0041     if (d->palette && columns > 0)
0042         d->palette->setColumnCount(columns);
0043 }
0044 
0045 QString Palette::comment()
0046 {
0047     if (!d->palette) return "";
0048     return d->palette->comment();
0049 }
0050 
0051 void Palette::setComment(QString comment)
0052 {
0053     if (!d->palette) return;
0054     return d->palette->setComment(comment);
0055 }
0056 
0057 QStringList Palette::groupNames() const
0058 {
0059     if (!d->palette) return QStringList();
0060     return d->palette->getGroupNames();
0061 }
0062 
0063 bool Palette::addGroup(QString name)
0064 {
0065     if (!d->palette) return false;
0066     return d->palette->addGroup(name);
0067 }
0068 
0069 bool Palette::removeGroup(QString name, bool keepColors)
0070 {
0071     if (!d->palette) return false;
0072     return d->palette->removeGroup(name, keepColors);
0073 }
0074 
0075 int Palette::colorsCountTotal()
0076 {
0077     if (!d->palette) return 0;
0078     return d->palette->colorCount();
0079 }
0080 
0081 Swatch *Palette::colorSetEntryByIndex(int index)
0082 {
0083     if (!d->palette || columnCount() == 0) {
0084         return new Swatch();
0085     }
0086     int col = index % columnCount();
0087     int row = (index - col) / columnCount();
0088     return new Swatch(d->palette->getColorGlobal(col, row));
0089 }
0090 
0091 Swatch *Palette::colorSetEntryFromGroup(int index, const QString &groupName)
0092 {
0093     if (!d->palette || columnCount() == 0) {
0094         return new Swatch();
0095     }
0096     int row = index % columnCount();
0097     return new Swatch(d->palette->getColorGroup((index - row) / columnCount(), row, groupName));
0098 }
0099 
0100 void Palette::addEntry(Swatch entry, QString groupName)
0101 {
0102     d->palette->add(entry.kisSwatch(), groupName);
0103 }
0104 
0105 void Palette::removeEntry(int index, const QString &/*groupName*/)
0106 {
0107     int col = index % columnCount();
0108     int tmp = index;
0109     int row = (index - col) / columnCount();
0110     KisSwatchGroup *groupFoundIn = 0;
0111     Q_FOREACH(const QString &name, groupNames()) {
0112         KisSwatchGroup *g = d->palette->getGroup(name);
0113         tmp -= g->rowCount() * columnCount();
0114         if (tmp < 0) {
0115             groupFoundIn = g;
0116             break;
0117         }
0118         row -= g->rowCount();
0119 
0120     }
0121     if (!groupFoundIn) { return; }
0122     groupFoundIn->removeEntry(col, row);
0123 }
0124 
0125 bool Palette::changeGroupName(QString oldGroupName, QString newGroupName)
0126 {
0127     return d->palette->changeGroupName(oldGroupName, newGroupName);
0128 }
0129 
0130 bool Palette::moveGroup(const QString &groupName, const QString &groupNameInsertBefore)
0131 {
0132     return d->palette->moveGroup(groupName, groupNameInsertBefore);
0133 }
0134 
0135 bool Palette::save()
0136 {
0137     return false;
0138 }
0139 
0140 KoColorSetSP Palette::colorSet()
0141 {
0142     return d->palette;
0143 }