1 /*
2 * Copyright (c) 2011 Cyrille Berger <cberger@cberger.net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "kis_color_source_option.h"
21
22 #include <QMap>
23 #include <KoID.h>
24 #include <kis_properties_configuration.h>
25 #include "kis_color_source.h"
26 #include <kis_painter.h>
27 #include <kis_paint_device.h>
28 #include <resources/KoPattern.h>
29
30 struct KisColorSourceOption::Private {
PrivateKisColorSourceOption::Private31 Private()
32 : type(PLAIN)
33 {}
34
35 KisColorSourceOption::Type type;
36
37 static QMap<KisColorSourceOption::Type, KoID> type2id;
38 static QMap<QString, KisColorSourceOption::Type> id2type;
39 static void addType(KisColorSourceOption::Type _type, KoID _id);
40 };
41
42 QMap<KisColorSourceOption::Type, KoID> KisColorSourceOption::Private::type2id;
43 QMap<QString, KisColorSourceOption::Type> KisColorSourceOption::Private::id2type;
44
addType(KisColorSourceOption::Type _type,KoID _id)45 void KisColorSourceOption::Private::addType(KisColorSourceOption::Type _type, KoID _id)
46 {
47 type2id[_type] = _id;
48 id2type[_id.id()] = _type;
49 }
50
51
KisColorSourceOption()52 KisColorSourceOption::KisColorSourceOption() : d(new Private)
53 {
54 if (Private::type2id.isEmpty()) {
55 Private::addType(PLAIN, KoID("plain", i18n("Plain color")));
56 Private::addType(GRADIENT, KoID("gradient", i18n("Gradient")));
57 Private::addType(UNIFORM_RANDOM, KoID("uniform_random", i18n("Uniform random")));
58 Private::addType(TOTAL_RANDOM, KoID("total_random", i18n("Total random")));
59 Private::addType(PATTERN, KoID("pattern", i18n("Pattern")));
60 Private::addType(PATTERN_LOCKED, KoID("lockedpattern", i18n("Locked pattern")));
61 }
62 }
63
~KisColorSourceOption()64 KisColorSourceOption::~KisColorSourceOption()
65 {
66 delete d;
67 }
68
writeOptionSetting(KisPropertiesConfigurationSP setting) const69 void KisColorSourceOption::writeOptionSetting(KisPropertiesConfigurationSP setting) const
70 {
71 setting->setProperty("ColorSource/Type", Private::type2id.value(d->type).id());
72 }
73
readOptionSetting(const KisPropertiesConfigurationSP setting)74 void KisColorSourceOption::readOptionSetting(const KisPropertiesConfigurationSP setting)
75 {
76 QString colorSourceType = setting->getString("ColorSource/Type", "plain");
77 d->type = Private::id2type.value(colorSourceType, PLAIN);
78 }
79
createColorSource(const KisPainter * _painter) const80 KisColorSource* KisColorSourceOption::createColorSource(const KisPainter* _painter) const
81 {
82 Q_ASSERT(_painter);
83
84 switch (d->type) {
85 case PLAIN:
86 return new KisPlainColorSource(_painter->backgroundColor(), _painter->paintColor());
87 case GRADIENT:
88 return new KisGradientColorSource(_painter->gradient(), _painter->paintColor().colorSpace());
89 case UNIFORM_RANDOM:
90 return new KisUniformRandomColorSource();
91 case TOTAL_RANDOM:
92 return new KisTotalRandomColorSource();
93 case PATTERN: {
94 if (_painter->pattern()) {
95 KisPaintDevice* dev = new KisPaintDevice(_painter->paintColor().colorSpace(), _painter->pattern()->name());
96 dev->convertFromQImage(_painter->pattern()->pattern(), 0);
97 return new KoPatternColorSource(dev, _painter->pattern()->width(), _painter->pattern()->height(), false);
98 }
99 break;
100 }
101 case PATTERN_LOCKED: {
102 if (_painter->pattern()) {
103 KisPaintDevice* dev = new KisPaintDevice(_painter->paintColor().colorSpace(), _painter->pattern()->name());
104 dev->convertFromQImage(_painter->pattern()->pattern(), 0);
105 return new KoPatternColorSource(dev, _painter->pattern()->width(), _painter->pattern()->height(), true);
106 }
107
108 }
109 }
110 // Fallback in case the patterns are messed up
111 return new KisPlainColorSource(_painter->backgroundColor(), _painter->paintColor());
112 }
113
colorSourceTypeId() const114 QString KisColorSourceOption::colorSourceTypeId() const
115 {
116 return Private::type2id.value(d->type).id();
117 }
118
setColorSourceType(Type _type)119 void KisColorSourceOption::setColorSourceType(Type _type)
120 {
121 d->type = _type;
122 }
123
setColorSourceType(const QString & _id)124 void KisColorSourceOption::setColorSourceType(const QString& _id)
125 {
126 d->type = Private::id2type[_id];
127 }
128
sourceIds()129 QList<KoID> KisColorSourceOption::sourceIds()
130 {
131 return Private::type2id.values();
132 }
133
type() const134 KisColorSourceOption::Type KisColorSourceOption::type() const
135 {
136 return d->type;
137 }
138