1 /*
2  * Copyright (C) 2021 Damir Porobic <damir.porobic@gmx.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 3 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,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "AnnotationSettingsAdapter.h"
21 
22 namespace kImageAnnotator {
23 
AnnotationSettingsAdapter(AnnotationGeneralSettings * generalSettings,AnnotationItemSettings * itemSettings,AnnotationToolSelection * toolSettings,AnnotationImageSettings * imageSettings,Config * config)24 AnnotationSettingsAdapter::AnnotationSettingsAdapter(
25 		AnnotationGeneralSettings *generalSettings,
26 		AnnotationItemSettings *itemSettings,
27 		AnnotationToolSelection *toolSettings,
28 		AnnotationImageSettings *imageSettings,
29 		Config *config) :
30 	mGeneralSettings(generalSettings),
31 	mItemSettings(itemSettings),
32 	mToolSettings(toolSettings),
33 	mImageSettings(imageSettings),
34 	mConfig(config)
35 {
36 	connect(mToolSettings, &AnnotationToolSelection::toolTypeChanged, this, &AnnotationSettingsAdapter::toolTypeChanged);
37 
38 	connect(mImageSettings, &AnnotationImageSettings::effectChanged, this, &AnnotationSettingsAdapter::effectChanged);
39 
40 	connect(mGeneralSettings, &AnnotationGeneralSettings::zoomValueChanged, this, &AnnotationSettingsAdapter::zoomValueChanged);
41 
42 	connect(mItemSettings, &AnnotationItemSettings::toolColorChanged, this, &AnnotationSettingsAdapter::toolColorChanged);
43 	connect(mItemSettings, &AnnotationItemSettings::toolWidthChanged, this, &AnnotationSettingsAdapter::toolWidthChanged);
44 	connect(mItemSettings, &AnnotationItemSettings::toolTextColorChanged, this, &AnnotationSettingsAdapter::toolTextColorChanged);
45 	connect(mItemSettings, &AnnotationItemSettings::toolFillTypeChanged, this, &AnnotationSettingsAdapter::toolFillTypeChanged);
46 	connect(mItemSettings, &AnnotationItemSettings::notifyNumberToolSeedChanged, this, &AnnotationSettingsAdapter::notifyNumberToolSeedChanged);
47 	connect(mItemSettings, &AnnotationItemSettings::obfuscateFactorChanged, this, &AnnotationSettingsAdapter::obfuscateFactorChanged);
48 	connect(mItemSettings, &AnnotationItemSettings::stickerChanged, this, &AnnotationSettingsAdapter::stickerChanged);
49 	connect(mItemSettings, &AnnotationItemSettings::shadowEnabledChanged, this, &AnnotationSettingsAdapter::shadowEnabledChanged);
50 	connect(mItemSettings, &AnnotationItemSettings::fontChanged, this, &AnnotationSettingsAdapter::fontChanged);
51 
52 	reloadConfig();
53 }
54 
editItem(AbstractAnnotationItem * item)55 void AnnotationSettingsAdapter::editItem(AbstractAnnotationItem *item)
56 {
57 	activateSelectTool();
58 	loadFromItem(item);
59 	mExistingItemEditInfo.startEdit(item->toolType());
60 }
61 
activateSelectTool()62 void AnnotationSettingsAdapter::activateSelectTool()
63 {
64 	mToolSettings->setToolType(Tools::Select);
65 	mItemSettings->setUpForTool(Tools::Select);
66 }
67 
toolType() const68 Tools AnnotationSettingsAdapter::toolType() const
69 {
70 	return mToolSettings->toolType();
71 }
72 
toolColor() const73 QColor AnnotationSettingsAdapter::toolColor() const
74 {
75 	return mItemSettings->toolColor();
76 }
77 
textColor() const78 QColor AnnotationSettingsAdapter::textColor() const
79 {
80 	return mItemSettings->textColor();
81 }
82 
toolWidth() const83 int AnnotationSettingsAdapter::toolWidth() const
84 {
85 	return mItemSettings->toolWidth();
86 }
87 
font() const88 QFont AnnotationSettingsAdapter::font() const
89 {
90 	return mItemSettings->font();
91 }
92 
fillType() const93 FillModes AnnotationSettingsAdapter::fillType() const
94 {
95 	return mItemSettings->fillMode();
96 }
97 
obfuscationFactor() const98 int AnnotationSettingsAdapter::obfuscationFactor() const
99 {
100 	return mItemSettings->obfuscationFactor();
101 }
102 
sticker() const103 QString AnnotationSettingsAdapter::sticker() const
104 {
105 	return mItemSettings->sticker();
106 }
107 
effect() const108 ImageEffects AnnotationSettingsAdapter::effect() const
109 {
110 	return mImageSettings->effect();
111 }
112 
shadowEnabled() const113 bool AnnotationSettingsAdapter::shadowEnabled() const
114 {
115 	return mItemSettings->shadowEnabled();
116 }
117 
updateNumberToolSeed(int numberToolSeed)118 void AnnotationSettingsAdapter::updateNumberToolSeed(int numberToolSeed)
119 {
120 	mItemSettings->updateNumberToolSeed(numberToolSeed);
121 }
122 
updateZoomLevel(double value)123 void AnnotationSettingsAdapter::updateZoomLevel(double value)
124 {
125 	mGeneralSettings->updateZoomLevel(value);
126 }
127 
reloadConfig()128 void AnnotationSettingsAdapter::reloadConfig()
129 {
130 	mToolSettings->setToolType(mConfig->selectedTool());
131 	mImageSettings->setEffect(ImageEffects::NoEffect);
132 	mItemSettings->setUpForTool(toolType());
133 }
134 
effectChanged(ImageEffects effect)135 void AnnotationSettingsAdapter::effectChanged(ImageEffects effect)
136 {
137 	AbstractSettingsProvider::effectChanged(effect);
138 }
139 
zoomValueChanged(double value)140 void AnnotationSettingsAdapter::zoomValueChanged(double value)
141 {
142 	AbstractSettingsProvider::zoomValueChanged(value);
143 }
144 
loadFromConfig(Tools tool)145 void AnnotationSettingsAdapter::loadFromConfig(Tools tool)
146 {
147 	mItemSettings->setUpForTool(tool);
148 	mItemSettings->setToolColor(mConfig->toolColor(tool));
149 	mItemSettings->setTextColor(mConfig->toolTextColor(tool));
150 	mItemSettings->setToolWidth(mConfig->toolWidth(tool));
151 	mItemSettings->setFillMode(mConfig->toolFillType(tool));
152 	mItemSettings->setFont(mConfig->toolFont(tool));
153 	mItemSettings->setObfuscationFactor(mConfig->obfuscationFactor(tool));
154 	mItemSettings->setShadowEnabled(mConfig->shadowEnabled(tool));
155 }
156 
loadFromItem(const AbstractAnnotationItem * item)157 void AnnotationSettingsAdapter::loadFromItem(const AbstractAnnotationItem *item)
158 {
159 	auto properties = item->properties();
160 	mItemSettings->setUpForTool(item->toolType());
161 	mItemSettings->setToolColor(properties->color());
162 	mItemSettings->setTextColor(properties->textColor());
163 	mItemSettings->setToolWidth(properties->width());
164 	mItemSettings->setFillMode(properties->fillType());
165 	mItemSettings->setShadowEnabled(properties->shadowEnabled());
166 
167 	auto textProperties = properties.dynamicCast<AnnotationTextProperties>();
168 	if(textProperties != nullptr) {
169 		mItemSettings->setFont(textProperties->font());
170 	}
171 
172 	auto obfuscateProperties = properties.dynamicCast<AnnotationObfuscateProperties>();
173 	if(obfuscateProperties != nullptr) {
174 		mItemSettings->setObfuscationFactor(obfuscateProperties->factor());
175 	}
176 }
177 
toolTypeChanged(Tools toolType)178 void AnnotationSettingsAdapter::toolTypeChanged(Tools toolType)
179 {
180 	mExistingItemEditInfo.stopEdit();
181 	mConfig->setSelectedToolType(toolType);
182 	if(!mExistingItemEditInfo.isEdit()) {
183 		loadFromConfig(toolType);
184 	}
185 	toolChanged(toolType);
186 }
187 
toolColorChanged(const QColor & color)188 void AnnotationSettingsAdapter::toolColorChanged(const QColor &color)
189 {
190 	configChanged([&](Tools tool){
191 		mConfig->setToolColor(color, tool);
192 	});
193 }
194 
toolTextColorChanged(const QColor & color)195 void AnnotationSettingsAdapter::toolTextColorChanged(const QColor &color)
196 {
197 	configChanged([&](Tools tool){
198 		mConfig->setToolTextColor(color, tool);
199 	});
200 }
201 
toolWidthChanged(int width)202 void AnnotationSettingsAdapter::toolWidthChanged(int width)
203 {
204 	configChanged([&](Tools tool){
205 		mConfig->setToolWidth(width, tool);
206 	});
207 }
208 
toolFillTypeChanged(FillModes fill)209 void AnnotationSettingsAdapter::toolFillTypeChanged(FillModes fill)
210 {
211 	configChanged([&](Tools tool){
212 		mConfig->setToolFillType(fill, tool);
213 	});
214 }
215 
notifyNumberToolSeedChanged(int newNumberToolSeed)216 void AnnotationSettingsAdapter::notifyNumberToolSeedChanged(int newNumberToolSeed)
217 {
218 	numberToolSeedChanged(newNumberToolSeed);
219 }
220 
obfuscateFactorChanged(int factor)221 void AnnotationSettingsAdapter::obfuscateFactorChanged(int factor)
222 {
223 	configChanged([&](Tools tool){
224 		mConfig->setObfuscationFactor(factor, tool);
225 	});
226 }
227 
stickerChanged(const QString & sticker)228 void AnnotationSettingsAdapter::stickerChanged(const QString &sticker)
229 {
230 	if(mExistingItemEditInfo.isEdit()) {
231 		itemSettingChanged();
232 	}
233 }
234 
shadowEnabledChanged(bool enabled)235 void AnnotationSettingsAdapter::shadowEnabledChanged(bool enabled)
236 {
237 	configChanged([&](Tools tool){
238 		mConfig->setShadowEnabled(enabled, tool);
239 	});
240 }
241 
fontChanged(const QFont & font)242 void AnnotationSettingsAdapter::fontChanged(const QFont &font)
243 {
244 	configChanged([&](Tools tool){
245 		mConfig->setToolFont(font, tool);
246 	});
247 }
248 
configChanged(const std::function<void (Tools)> & configChangedMethod)249 void AnnotationSettingsAdapter::configChanged(const std::function<void(Tools)> &configChangedMethod)
250 {
251 	if(mExistingItemEditInfo.isEdit()) {
252 		itemSettingChanged();
253 		configChangedMethod(mExistingItemEditInfo.toolType());
254 	} else {
255 		configChangedMethod(toolType());
256 	}
257 }
258 
259 } // namespace kImageAnnotator
260