File indexing completed on 2025-01-26 04:09:17

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Halla Rempt <halla@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "TestKisPaletteModel.h"
0008 
0009 #include <QVariant>
0010 #include <QBrush>
0011 #include <QColor>
0012 #include <QList>
0013 
0014 #include <simpletest.h>
0015 #include <kundo2stack.h>
0016 
0017 #include <KisSwatch.h>
0018 #include <KisSwatchGroup.h>
0019 #include <KisPaletteModel.h>
0020 
0021 void TestKisPaletteModel::testSetColorSet()
0022 {
0023     KoColorSetSP cs = createColorSet();
0024     KisPaletteModel model;
0025     model.setColorSet(cs);
0026     QVERIFY(model.colorSet());
0027 }
0028 
0029 void TestKisPaletteModel::testAddSwatch()
0030 {
0031     KoColorSetSP cs = createColorSet();
0032     KisPaletteModel model;
0033     model.setColorSet(cs);
0034 
0035     QCOMPARE(model.rowCount(), 20);
0036     QCOMPARE(cs->rowCount(), 20);
0037 
0038     KisSwatch sw(red(), "red");
0039     model.addSwatch(sw);
0040 
0041     QCOMPARE(model.rowCount(), 20);
0042     QCOMPARE(cs->colorCount(), 1);
0043     QCOMPARE(cs->getGroup(QString())->colorCount(), 1);
0044 
0045     KisSwatchGroupSP grp = model.addGroup("newgroup");
0046 
0047     QCOMPARE(model.rowCount(), 41);
0048     QCOMPARE(cs->rowCount(), 40);
0049     QCOMPARE(cs->rowCountWithTitles(), 41);
0050 
0051     KisSwatch sw2(blue(), "blue");
0052     model.addSwatch(sw2, "newgroup");
0053     QCOMPARE(cs->colorCount(), 2);
0054     QCOMPARE(cs->getGroup("newgroup")->colorCount(), 1);
0055     QCOMPARE(grp->colorCount(), 1);
0056     QCOMPARE(cs->getGroup(QString())->colorCount(), 1);
0057 
0058     cs->undoStack()->undo(); // addSwatch
0059     cs->undoStack()->undo(); // addGroup
0060     QCOMPARE(model.rowCount(), 20);
0061 }
0062 
0063 
0064 void TestKisPaletteModel::testSetSwatch()
0065 {
0066     KoColorSetSP cs = createColorSet();
0067     KisPaletteModel model;
0068     model.setColorSet(cs);
0069 
0070     KisSwatch sw(red(), "red");
0071     QModelIndex idx = model.index(10, 10, QModelIndex());
0072     model.setSwatch(sw, idx);
0073 
0074     QCOMPARE(cs->colorCount(), 1);
0075 
0076     KisSwatch sw2(blue(), "blue");
0077     idx = model.index(10, 10, QModelIndex());
0078     model.setSwatch(sw2, idx);
0079 
0080     QCOMPARE(cs->colorCount(), 1);
0081 
0082     idx = model.index(1, 1, QModelIndex());
0083     model.setSwatch(sw, idx);
0084 
0085     QCOMPARE(cs->colorCount(), 2);
0086 }
0087 
0088 
0089 void TestKisPaletteModel::testRemoveSwatch()
0090 {
0091     KoColorSetSP cs = createColorSet();
0092     KisPaletteModel model;
0093     model.setColorSet(cs);
0094 
0095     KisSwatch sw(red(), "red");
0096     QModelIndex idx = model.index(10, 10, QModelIndex());
0097     model.setSwatch(sw, idx);
0098 
0099     QCOMPARE(cs->colorCount(), 1);
0100 
0101     model.removeSwatch(idx);
0102     QCOMPARE(cs->colorCount(), 0);
0103 }
0104 
0105 
0106 void TestKisPaletteModel::testChangeGroupName()
0107 {
0108     KoColorSetSP cs = createColorSet();
0109     KisPaletteModel model;
0110     model.setColorSet(cs);
0111     model.addGroup("newgroup1");
0112     model.changeGroupName("newgroup1", "newnewgroup1");
0113     QVERIFY(cs->swatchGroupNames().contains("newnewgroup1"));
0114     QVERIFY(!cs->swatchGroupNames().contains("newgroup1"));
0115 
0116 }
0117 
0118 void TestKisPaletteModel::testRemoveGroup()
0119 {
0120     KoColorSetSP cs = createColorSet();
0121     KisPaletteModel model;
0122     model.setColorSet(cs);
0123     model.addGroup("newgroup1");
0124     QVERIFY(cs->swatchGroupNames().contains("newgroup1"));
0125     model.removeGroup("newgroup1", false);
0126     QVERIFY(!cs->swatchGroupNames().contains("newgroup1"));
0127 }
0128 
0129 
0130 void TestKisPaletteModel::testAddGroup()
0131 {
0132     KoColorSetSP cs = createColorSet();
0133     KisPaletteModel model;
0134     model.setColorSet(cs);
0135 
0136     QCOMPARE(model.rowCount(), 20);
0137     QCOMPARE(cs->rowCount(), 20);
0138 
0139     KisSwatchGroupSP grp1 = model.addGroup("newgroup");
0140 
0141     QCOMPARE(model.rowCount(), 41);
0142     QCOMPARE(cs->rowCount(), 40);
0143     QCOMPARE(cs->rowCountWithTitles(), 41);
0144     QCOMPARE(grp1->colorCount(), 0);
0145 
0146     KisSwatchGroupSP grp2 = model.addGroup("newgroup2");
0147 
0148     QCOMPARE(model.rowCount(), 62);
0149     QCOMPARE(cs->rowCount(), 60);
0150     QCOMPARE(cs->rowCountWithTitles(), 62);
0151     QCOMPARE(grp2->colorCount(), 0);
0152 
0153     cs->undoStack()->undo();
0154     QCOMPARE(model.rowCount(), 41);
0155 
0156     cs->undoStack()->undo();
0157     QCOMPARE(model.rowCount(), 20);
0158 
0159 }
0160 
0161 void TestKisPaletteModel::testSetRowCountForGroup()
0162 {
0163     KoColorSetSP cs = createColorSet();
0164     KisPaletteModel model;
0165     model.setColorSet(cs);
0166 
0167     KisSwatchGroupSP group = model.addGroup("newgroup", 5, 5);
0168     QCOMPARE(group->rowCount(), 5);
0169     QCOMPARE(cs->rowCount(), 25);
0170     QCOMPARE(cs->rowCountWithTitles(), 26);
0171 
0172     model.setRowCountForGroup("newgroup", 10);
0173     QCOMPARE(group->rowCount(), 10);
0174     QCOMPARE(cs->rowCount(), 30);
0175     QCOMPARE(cs->rowCountWithTitles(), 31);
0176 }
0177 
0178 
0179 void TestKisPaletteModel::testClear()
0180 {
0181     KoColorSetSP cs = createColorSet();
0182     KisPaletteModel model;
0183     model.setColorSet(cs);
0184 
0185     KisSwatchGroupSP grp = model.addGroup("newgroup");
0186 
0187     QCOMPARE(model.rowCount(), 41);
0188     QCOMPARE(cs->rowCount(), 40);
0189     QCOMPARE(cs->rowCountWithTitles(), 41);
0190 
0191     KisSwatch sw2(blue(), "blue");
0192     model.addSwatch(sw2, "newgroup");
0193     QCOMPARE(cs->colorCount(), 1);
0194     QCOMPARE(cs->getGroup("newgroup")->colorCount(), 1);
0195     QCOMPARE(grp->colorCount(), 1);
0196     QVERIFY(cs->swatchGroupNames().contains("newgroup"));
0197 
0198     model.clear();
0199 
0200     QVERIFY(!cs->swatchGroupNames().contains("newgroup"));
0201     QCOMPARE(cs->colorCount(), 0);
0202     QCOMPARE(model.rowCount(), KisSwatchGroup::DEFAULT_ROW_COUNT);
0203 }
0204 
0205 void TestKisPaletteModel::testIndexRowForInfo()
0206 {
0207     KoColorSetSP cs = createColorSet();
0208     cs->getGlobalGroup()->setRowCount(7);
0209     cs->addGroup("group1", KisSwatchGroup::DEFAULT_COLUMN_COUNT, 6);
0210     cs->addGroup("group2", KisSwatchGroup::DEFAULT_COLUMN_COUNT, 5);
0211     cs->addGroup("group3", KisSwatchGroup::DEFAULT_COLUMN_COUNT, 4);
0212 
0213     KisPaletteModel model;
0214     model.setColorSet(cs);
0215 
0216     KisSwatch sw(red(), "red");
0217     model.addGroup("group");
0218 
0219     QCOMPARE(model.rowCount(), cs->rowCountWithTitles());
0220 
0221     QModelIndex idx = model.index(10, 10);
0222     model.setSwatch(sw, idx);
0223 
0224     QVERIFY(cs->getGroup("group1"));
0225     QVERIFY(cs->getGroup("group1")->infoList().size() > 0);
0226     QCOMPARE(cs->getGroup("group1")->infoList().size(), 1);
0227     QCOMPARE(cs->rowNumberInGroup(10), 2);
0228 
0229     KisSwatchGroup::SwatchInfo info = cs->getGroup("group1")->infoList().first();
0230 
0231     QCOMPARE(info.row, 2);
0232     QCOMPARE(info.column, 10);
0233     QCOMPARE(info.swatch.color(), red());
0234 
0235     int row = model.indexRowForInfo(info);
0236     QCOMPARE(row, 10);
0237 
0238 }
0239 
0240 void TestKisPaletteModel::testIndexForClosest()
0241 {
0242     KoColorSetSP cs = createColorSet();
0243     KisPaletteModel model;
0244     model.setColorSet(cs);
0245 
0246     KisSwatch sw(red(), "red");
0247     QModelIndex idx = model.index(5, 10, QModelIndex());
0248     model.setSwatch(sw, idx);
0249 
0250     QColor c;
0251     c.setRgb(255, 10, 10);
0252     KoColor kc(c, KoColorSpaceRegistry::instance()->rgb8());
0253 
0254     KisSwatchGroup::SwatchInfo info = cs->getClosestSwatchInfo(kc);
0255     QCOMPARE(info.row, 5);
0256     QCOMPARE(info.column, 10);
0257     QCOMPARE(info.swatch.color().toQColor(), red().toQColor());
0258 
0259     QModelIndex idx2 = model.indexForClosest(kc);
0260     QCOMPARE(idx2.row(), 5);
0261     QCOMPARE(idx2.column(), 10);
0262     QColor c2 = idx2.data(Qt::BackgroundRole).value<QBrush>().color();
0263     QCOMPARE(c2, red().toQColor());
0264 }
0265 
0266 
0267 
0268 void TestKisPaletteModel::testData()
0269 {
0270     KoColorSetSP cs = createColorSet();
0271     cs->getGlobalGroup()->setRowCount(7);
0272     cs->setColumnCount(3);
0273 
0274     KisPaletteModel model;
0275     model.setColorSet(cs);
0276 
0277     model.addGroup("group1", 3, 6);
0278     model.addGroup("group2", 3, 5);
0279 
0280     KisSwatch sw(red(), "red");
0281     QModelIndex idx = model.index(5, 1, QModelIndex());
0282     model.setSwatch(sw, idx);
0283 
0284     KisSwatch sw2 = cs->getSwatchFromGroup(1, 5);
0285     QCOMPARE(sw2.color(), sw.color());
0286 
0287     QCOMPARE(model.rowCount(), cs->rowCountWithTitles());
0288     QCOMPARE(model.columnCount(), 3);
0289     QCOMPARE(cs->columnCount(), 3);
0290 
0291     int rowCount = model.rowCount();
0292     for (int row = 0; row < rowCount; ++row) {
0293         if (row == 7 || row == 14 || row == 20) {
0294             QVERIFY(cs->isGroupTitleRow(row));
0295         }
0296         else {
0297             QVERIFY(!cs->isGroupTitleRow(row));
0298             for (int column = 0; column < model.columnCount(); ++column) {
0299                 QModelIndex idx = model.index(row, column);
0300                 Q_ASSERT(idx.column() < model.columnCount());
0301                 QColor c(row, column, 0);
0302                 sw.setColor(KoColor(c, KoColorSpaceRegistry::instance()->rgb8()));
0303                 model.setSwatch(sw, idx);
0304                 KisSwatch sw2 = model.getSwatch(idx);
0305                 QCOMPARE(model.getSwatch(idx).color().toQColor(), sw2.color().toQColor());
0306             }
0307 
0308         }
0309     }
0310 
0311     for (int row = 0; row < rowCount; ++row) {
0312         if (cs->isGroupTitleRow(row)) {
0313             QModelIndex idx = model.index(row, 0);
0314             KisSwatchGroupSP grp = cs->getGroup(row);
0315             QCOMPARE(grp->name(), idx.data());
0316             QVERIFY(idx.data(KisPaletteModel::GroupNameRole).toBool());
0317         }
0318         else {
0319             for (int column = 0; column < model.columnCount(); ++column) {
0320                 QModelIndex idx = model.index(row, column);
0321                 QColor c(row, column, 0);
0322                 QCOMPARE(model.getSwatch(idx).color().toQColor(), c);
0323             }
0324         }
0325     }
0326 }
0327 
0328 void TestKisPaletteModel::testRowNumberInGroup()
0329 {
0330     KoColorSetSP cs = createColorSet();
0331     cs->getGlobalGroup()->setRowCount(7);
0332     cs->addGroup("group1", KisSwatchGroup::DEFAULT_COLUMN_COUNT, 6);
0333     cs->addGroup("group2", KisSwatchGroup::DEFAULT_COLUMN_COUNT, 5);
0334     cs->addGroup("group3", KisSwatchGroup::DEFAULT_COLUMN_COUNT, 4);
0335 
0336     KisPaletteModel model;
0337     model.setColorSet(cs);
0338 
0339 
0340     QCOMPARE(model.rowCount(), 25);
0341 
0342     QVector<int> rowCountsInGroup {    0, 1, 2, 3, 4, 5, 6,
0343                                    -1, 0, 1, 2, 3, 4, 5,
0344                                    -1, 0, 1, 2, 3, 4,
0345                                    -1, 0, 1, 2, 3};
0346 
0347 
0348     QCOMPARE(model.rowCount(), rowCountsInGroup.size());
0349 
0350     for (int i = 0; i < model.rowCount(); ++i) {
0351         QCOMPARE(model.rowNumberInGroup(i), rowCountsInGroup[i]);
0352     }
0353 }
0354 
0355 
0356 KoColorSetSP TestKisPaletteModel::createColorSet()
0357 {
0358     KoColorSetSP colorSet(new KoColorSet());
0359     colorSet->setPaletteType(KoColorSet::KPL);
0360     colorSet->setName("Dummy");
0361     colorSet->setFilename("dummy.kpl");
0362     colorSet->setModified(false);
0363     colorSet->undoStack()->clear();
0364     colorSet->setValid(true);
0365     return colorSet;
0366 }
0367 
0368 KoColor TestKisPaletteModel::blue()
0369 {
0370     QColor c(Qt::blue);
0371     KoColor  kc(c, KoColorSpaceRegistry::instance()->rgb8());
0372     return kc;
0373 }
0374 
0375 KoColor TestKisPaletteModel::red()
0376 {
0377     QColor c(Qt::red);
0378     KoColor  kc(c, KoColorSpaceRegistry::instance()->rgb8());
0379     return kc;
0380 }
0381 
0382 
0383 SIMPLE_TEST_MAIN(TestKisPaletteModel)