Pencil2D Animation
Download Community News Docs Contribute

core_lib/src/managers/toolmanager.cpp Source File

  • Main Page
  • Related Pages
  • Classes
  • Files
  •  
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • managers
toolmanager.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#include "toolmanager.h"
18
19#include <cmath>
20#include "pentool.h"
21#include "penciltool.h"
22#include "brushtool.h"
23#include "buckettool.h"
24#include "erasertool.h"
25#include "eyedroppertool.h"
26#include "handtool.h"
27#include "movetool.h"
28#include "polylinetool.h"
29#include "selecttool.h"
30#include "smudgetool.h"
31#include "cameratool.h"
32#include "editor.h"
33
34
35ToolManager::ToolManager(Editor* editor) : BaseManager(editor, __FUNCTION__)
36{
37}
38
39bool ToolManager::init()
40{
41 mToolSetHash.insert(PEN, new PenTool(this));
42 mToolSetHash.insert(PENCIL, new PencilTool(this));
43 mToolSetHash.insert(BRUSH, new BrushTool(this));
44 mToolSetHash.insert(ERASER, new EraserTool(this));
45 mToolSetHash.insert(BUCKET, new BucketTool(this));
46 mToolSetHash.insert(EYEDROPPER, new EyedropperTool(this));
47 mToolSetHash.insert(HAND, new HandTool(this));
48 mToolSetHash.insert(MOVE, new MoveTool(this));
49 mToolSetHash.insert(POLYLINE, new PolylineTool(this));
50 mToolSetHash.insert(SELECT, new SelectTool(this));
51 mToolSetHash.insert(SMUDGE, new SmudgeTool(this));
52 mToolSetHash.insert(CAMERA, new CameraTool(this));
53
54 foreach(BaseTool* pTool, mToolSetHash.values())
55 {
56 pTool->initialize(editor());
57 }
58
59 setDefaultTool();
60
61 return true;
62}
63
64Status ToolManager::load(Object*)
65{
66 setDefaultTool();
67 return Status::OK;
68}
69
70Status ToolManager::save(Object*)
71{
72 return Status::OK;
73}
74
75BaseTool* ToolManager::currentTool() const
76{
77 if (mTemporaryTool != nullptr)
78 {
79 return mTemporaryTool;
80 }
81 else if (mTabletEraserTool != nullptr)
82 {
83 return mTabletEraserTool;
84 }
85 return mCurrentTool;
86}
87
88BaseTool* ToolManager::getTool(ToolType eToolType)
89{
90 return mToolSetHash[eToolType];
91}
92
93void ToolManager::setDefaultTool()
94{
95 // Set default tool
96 // (called by the main window init)
97 ToolType defaultToolType = PENCIL;
98
99 setCurrentTool(defaultToolType);
100 mTabletEraserTool = nullptr;
101 mTemporaryTool = nullptr;
102}
103
104void ToolManager::setCurrentTool(ToolType eToolType)
105{
106 // We're already using this tool
107 if (mCurrentTool == getTool(eToolType)) { return; }
108
109 if (mCurrentTool != nullptr)
110 {
111 mCurrentTool->leavingThisTool();
112 }
113
114 mCurrentTool = getTool(eToolType);
115 if (mTemporaryTool == nullptr && mTabletEraserTool == nullptr)
116 {
117 emit toolChanged(eToolType);
118 }
119}
120
121bool ToolManager::leavingThisTool()
122{
123 return currentTool()->leavingThisTool();
124}
125
126void ToolManager::cleanupAllToolsData()
127{
128 foreach(BaseTool* tool, mToolSetHash)
129 {
130 tool->clearToolData();
131 }
132}
133
134void ToolManager::resetAllTools()
135{
136 // Reset can be useful to solve some pencil settings problems.
137 // Beta-testers should be recommended to reset before sending tool related issues.
138 // This can prevent from users to stop working on their project.
139
140 foreach(BaseTool* tool, mToolSetHash)
141 {
142 tool->resetToDefault();
143 }
144 qDebug("tools restored to default settings");
145}
146
147void ToolManager::setWidth(float newWidth)
148{
149 if (std::isnan(newWidth) || newWidth < 0)
150 {
151 newWidth = 1.f;
152 }
153
154 currentTool()->setWidth(static_cast<qreal>(newWidth));
155 emit penWidthValueChanged(newWidth);
156 emit toolPropertyChanged(currentTool()->type(), WIDTH);
157}
158
159void ToolManager::setFeather(float newFeather)
160{
161 if (std::isnan(newFeather) || newFeather < 0)
162 {
163 newFeather = 0.f;
164 }
165
166 currentTool()->setFeather(static_cast<qreal>(newFeather));
167 emit penFeatherValueChanged(newFeather);
168 emit toolPropertyChanged(currentTool()->type(), FEATHER);
169}
170
171void ToolManager::setUseFeather(bool usingFeather)
172{
173 int usingAA = currentTool()->properties.useAA;
174 int value = propertySwitch(usingFeather, usingAA);
175
176 currentTool()->setAA(value);
177 currentTool()->setUseFeather(usingFeather);
178 emit toolPropertyChanged(currentTool()->type(), USEFEATHER);
179 emit toolPropertyChanged(currentTool()->type(), ANTI_ALIASING);
180}
181
182void ToolManager::setInvisibility(bool isInvisible)
183{
184 currentTool()->setInvisibility(isInvisible);
185 emit toolPropertyChanged(currentTool()->type(), INVISIBILITY);
186}
187
188void ToolManager::setPreserveAlpha(bool isPreserveAlpha)
189{
190 currentTool()->setPreserveAlpha(isPreserveAlpha);
191 emit toolPropertyChanged(currentTool()->type(), PRESERVEALPHA);
192}
193
194void ToolManager::setVectorMergeEnabled(bool isVectorMergeEnabled)
195{
196 currentTool()->setVectorMergeEnabled(isVectorMergeEnabled);
197 emit toolPropertyChanged(currentTool()->type(), VECTORMERGE);
198}
199
200void ToolManager::setBezier(bool isBezierOn)
201{
202 currentTool()->setBezier(isBezierOn);
203 emit toolPropertyChanged(currentTool()->type(), BEZIER);
204}
205
206void ToolManager::setPressure(bool isPressureOn)
207{
208 currentTool()->setPressure(isPressureOn);
209 emit toolPropertyChanged(currentTool()->type(), PRESSURE);
210}
211
212void ToolManager::setAA(int usingAA)
213{
214 currentTool()->setAA(usingAA);
215 emit toolPropertyChanged(currentTool()->type(), ANTI_ALIASING);
216}
217
218void ToolManager::setFillMode(int mode)
219{
220 currentTool()->setFillMode(mode);
221 emit toolPropertyChanged(currentTool()->type(), FILL_MODE);
222}
223
224void ToolManager::setStabilizerLevel(int level)
225{
226 currentTool()->setStabilizerLevel(level);
227 emit toolPropertyChanged(currentTool()->type(), STABILIZATION);
228}
229
230void ToolManager::setTolerance(int newTolerance)
231{
232 newTolerance = qMax(0, newTolerance);
233
234 currentTool()->setTolerance(newTolerance);
235 emit toleranceValueChanged(newTolerance);
236 emit toolPropertyChanged(currentTool()->type(), TOLERANCE);
237}
238
239void ToolManager::setBucketColorToleranceEnabled(bool enabled)
240{
241 currentTool()->setToleranceEnabled(enabled);
242 emit toolPropertyChanged(currentTool()->type(), USETOLERANCE);
243}
244
245void ToolManager::setBucketFillExpandEnabled(bool expandValue)
246{
247 currentTool()->setFillExpandEnabled(expandValue);
248 emit toolPropertyChanged(currentTool()->type(), USEBUCKETFILLEXPAND);
249}
250
251void ToolManager::setBucketFillExpand(int expandValue)
252{
253 currentTool()->setFillExpand(expandValue);
254 emit toolPropertyChanged(currentTool()->type(), BUCKETFILLEXPAND);
255}
256
257void ToolManager::setBucketFillReferenceMode(int referenceMode)
258{
259 currentTool()->setFillReferenceMode(referenceMode);
260 emit toolPropertyChanged(currentTool()->type(), BUCKETFILLLAYERREFERENCEMODE);
261}
262
263void ToolManager::setUseFillContour(bool useFillContour)
264{
265 currentTool()->setUseFillContour(useFillContour);
266 emit toolPropertyChanged(currentTool()->type(), FILLCONTOUR);
267}
268
269void ToolManager::setShowSelectionInfo(bool b)
270{
271 currentTool()->setShowSelectionInfo(b);
272}
273
274void ToolManager::setShowCameraPath(bool enabled)
275{
276 CameraTool* cameraTool = static_cast<CameraTool*>(getTool(CAMERA));
277 cameraTool->setShowCameraPath(enabled);
278 emit toolPropertyChanged(cameraTool->type(), CAMERAPATH);
279}
280
281void ToolManager::resetCameraPath()
282{
283 CameraTool* cameraTool = static_cast<CameraTool*>(getTool(CAMERA));
284 cameraTool->resetCameraPath();
285 emit toolPropertyChanged(cameraTool->type(), CAMERAPATH);
286}
287
288void ToolManager::resetCameraTransform(CameraFieldOption option)
289{
290 CameraTool* cameraTool = static_cast<CameraTool*>(getTool(CAMERA));
291 cameraTool->resetTransform(option);
292}
293
294void ToolManager::setCameraPathDotColor(int dotColorNum)
295{
296 CameraTool* cameraTool = static_cast<CameraTool*>(getTool(CAMERA));
297 cameraTool->setPathDotColorType(static_cast<DotColorType>(dotColorNum));
298 emit toolPropertyChanged(cameraTool->type(), CAMERAPATH);
299}
300
301bool ToolManager::bucketReferenceModeIsCurrentLayer(int referenceMode) const
302{
303 return referenceMode == 0;
304}
305
306// Switches on/off two actions
307// eg. if x = true, then y = false
308int ToolManager::propertySwitch(bool condition, int tool)
309{
310 int value = 0;
311 int newValue = 0;
312
313 if (condition == true)
314 {
315 value = -1;
316 newValue = mOldValue;
317 mOldValue = tool;
318 }
319 else if (condition == false)
320 {
321 value = (newValue == 1) ? 1 : mOldValue;
322 }
323 return value;
324}
325
326void ToolManager::tabletSwitchToEraser()
327{
328 mTabletEraserTool = getTool(ERASER);
329
330 // We should only notify a tool change if we're positive that the state has changed and it should only happen once
331 // if the user for some reason is using another temporary tool at the same time, that takes first priority
332 if (mTemporaryTool == nullptr)
333 {
334 emit toolChanged(ERASER);
335 }
336}
337
338void ToolManager::tabletRestorePrevTool()
339{
340 if (mTemporaryTool == nullptr && mTabletEraserTool != nullptr)
341 {
342 mTabletEraserTool = nullptr;
343 emit toolChanged(currentTool()->type());
344 }
345}
346
347bool ToolManager::setTemporaryTool(ToolType eToolType, QFlags<Qt::Key> keys, Qt::KeyboardModifiers modifiers)
348{
349 if (mTemporaryTool != nullptr) return false;
350 mTemporaryTriggerKeys = keys;
351 mTemporaryTriggerModifiers = modifiers;
352 mTemporaryTriggerMouseButtons = Qt::NoButton;
353 setTemporaryTool(eToolType);
354 return true;
355}
356
357bool ToolManager::setTemporaryTool(ToolType eToolType, Qt::MouseButtons buttons)
358{
359 if (mTemporaryTool != nullptr) return false;
360 mTemporaryTriggerKeys = {};
361 mTemporaryTriggerModifiers = Qt::NoModifier;
362 mTemporaryTriggerMouseButtons = buttons;
363 setTemporaryTool(eToolType);
364 return true;
365}
366
367bool ToolManager::tryClearTemporaryTool(Qt::Key key)
368{
369 Qt::KeyboardModifier modifier = Qt::NoModifier;
370 switch(key)
371 {
372 case Qt::Key_Control:
373 modifier = Qt::ControlModifier;
374 break;
375 case Qt::Key_Shift:
376 modifier = Qt::ShiftModifier;
377 break;
378 case Qt::Key_Alt:
379 modifier = Qt::AltModifier;
380 break;
381 case Qt::Key_Meta:
382 modifier = Qt::MetaModifier;
383 break;
384 default:
385 break;
386 }
387
388 if (mTemporaryTriggerKeys.testFlag(key) ||
389 mTemporaryTriggerModifiers.testFlag(modifier))
390 {
391 clearTemporaryTool();
392 return true;
393 }
394 return false;
395}
396
397bool ToolManager::tryClearTemporaryTool(Qt::MouseButton button)
398{
399 if (mTemporaryTriggerMouseButtons != Qt::NoButton && mTemporaryTriggerMouseButtons.testFlag(button))
400 {
401 clearTemporaryTool();
402 return true;
403 }
404 return false;
405}
406
407void ToolManager::setTemporaryTool(ToolType eToolType)
408{
409 mTemporaryTool = getTool(eToolType);
410 emit toolChanged(eToolType);
411}
412
413void ToolManager::clearTemporaryTool()
414{
415 mTemporaryTool = nullptr;
416 mTemporaryTriggerKeys = {};
417 mTemporaryTriggerModifiers = Qt::NoModifier;
418 mTemporaryTriggerMouseButtons = Qt::NoButton;
419 emit toolChanged(currentTool()->type());
420}
BaseManager
Definition: basemanager.h:29
BaseTool
Definition: basetool.h:69
BrushTool
Definition: brushtool.h:27
BucketTool
Definition: buckettool.h:30
CameraTool
Definition: cameratool.h:45
Editor
Definition: editor.h:68
EraserTool
Definition: erasertool.h:24
EyedropperTool
Definition: eyedroppertool.h:27
HandTool
Definition: handtool.h:26
MoveTool
Definition: movetool.h:30
Object
Definition: object.h:42
PenTool
Definition: pentool.h:26
PencilTool
Definition: penciltool.h:27
PolylineTool
Definition: polylinetool.h:26
SelectTool
Definition: selecttool.h:30
SmudgeTool
Definition: smudgetool.h:24
Status
Definition: pencilerror.h:40
ToolManager::bucketReferenceModeIsCurrentLayer
bool bucketReferenceModeIsCurrentLayer(int referenceMode) const
Layer mode will be enforced by the the choice the reference mode selected.
Definition: toolmanager.cpp:301
QFlags
QFlags::testFlag
bool testFlag(Enum flag) const const
QHash::insert
QHash::iterator insert(const Key &key, const T &value)
QHash::values
QList< T > values() const const
Qt::Key
Key
Qt::KeyboardModifiers
typedef KeyboardModifiers
Qt::NoButton
NoButton
Generated on Sat Nov 25 2023 13:00:01 for Pencil2D by doxygen 1.9.6 based on revision 18c5494f61f228a0f7b8820420627d4ac76231a8