All Classes Namespaces Functions Variables Enumerations Properties Pages
toolmanager.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU 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 "editor.h"
32 
33 
34 ToolManager::ToolManager(Editor* editor) : BaseManager(editor)
35 {
36 }
37 
38 bool ToolManager::init()
39 {
40  mIsSwitchedToEraser = false;
41 
42  mToolSetHash.insert(PEN, new PenTool(this));
43  mToolSetHash.insert(PENCIL, new PencilTool(this));
44  mToolSetHash.insert(BRUSH, new BrushTool(this));
45  mToolSetHash.insert(ERASER, new EraserTool(this));
46  mToolSetHash.insert(BUCKET, new BucketTool(this));
47  mToolSetHash.insert(EYEDROPPER, new EyedropperTool(this));
48  mToolSetHash.insert(HAND, new HandTool(this));
49  mToolSetHash.insert(MOVE, new MoveTool(this));
50  mToolSetHash.insert(POLYLINE, new PolylineTool(this));
51  mToolSetHash.insert(SELECT, new SelectTool(this));
52  mToolSetHash.insert(SMUDGE, new SmudgeTool(this));
53 
54  foreach(BaseTool* pTool, mToolSetHash.values())
55  {
56  pTool->initialize(editor());
57  }
58 
59  setDefaultTool();
60 
61  return true;
62 }
63 
64 Status ToolManager::load(Object*)
65 {
66  return Status::OK;
67 }
68 
69 Status ToolManager::save(Object*)
70 {
71  return Status::OK;
72 }
73 
74 BaseTool* ToolManager::getTool(ToolType eToolType)
75 {
76  return mToolSetHash[eToolType];
77 }
78 
79 void ToolManager::setDefaultTool()
80 {
81  // Set default tool
82  // (called by the main window init)
83  ToolType defaultToolType = PENCIL;
84 
85  setCurrentTool(defaultToolType);
86  meTabletBackupTool = defaultToolType;
87 }
88 
89 void ToolManager::setCurrentTool(ToolType eToolType)
90 {
91  if (mCurrentTool != nullptr)
92  {
93  leavingThisTool();
94  }
95 
96  mCurrentTool = getTool(eToolType);
97  emit toolChanged(eToolType);
98 }
99 
100 bool ToolManager::leavingThisTool()
101 {
102  return mCurrentTool->leavingThisTool();
103 }
104 
105 void ToolManager::cleanupAllToolsData()
106 {
107  foreach(BaseTool* tool, mToolSetHash)
108  {
109  tool->clearToolData();
110  }
111 }
112 
113 void ToolManager::resetAllTools()
114 {
115  // Reset can be useful to solve some pencil settings problems.
116  // Beta-testers should be recommended to reset before sending tool related issues.
117  // This can prevent from users to stop working on their project.
118 
119  foreach(BaseTool* tool, mToolSetHash)
120  {
121  tool->resetToDefault();
122  }
123  qDebug("tools restored to default settings");
124 }
125 
126 void ToolManager::setWidth(float newWidth)
127 {
128  if (std::isnan(newWidth) || newWidth < 0)
129  {
130  newWidth = 1.f;
131  }
132 
133  currentTool()->setWidth(static_cast<qreal>(newWidth));
134  emit penWidthValueChanged(newWidth);
135  emit toolPropertyChanged(currentTool()->type(), WIDTH);
136 }
137 
138 void ToolManager::setFeather(float newFeather)
139 {
140  if (std::isnan(newFeather) || newFeather < 0)
141  {
142  newFeather = 0.f;
143  }
144 
145  currentTool()->setFeather(static_cast<qreal>(newFeather));
146  emit penFeatherValueChanged(newFeather);
147  emit toolPropertyChanged(currentTool()->type(), FEATHER);
148 }
149 
150 void ToolManager::setUseFeather(bool usingFeather)
151 {
152  int usingAA = currentTool()->properties.useAA;
153  int value = propertySwitch(usingFeather, usingAA);
154 
155  currentTool()->setAA(value);
156  currentTool()->setUseFeather(usingFeather);
157  emit toolPropertyChanged(currentTool()->type(), USEFEATHER);
158  emit toolPropertyChanged(currentTool()->type(), ANTI_ALIASING);
159 }
160 
161 void ToolManager::setInvisibility(bool isInvisible)
162 {
163  currentTool()->setInvisibility(isInvisible);
164  emit toolPropertyChanged(currentTool()->type(), INVISIBILITY);
165 }
166 
167 void ToolManager::setPreserveAlpha(bool isPreserveAlpha)
168 {
169  currentTool()->setPreserveAlpha(isPreserveAlpha);
170  emit toolPropertyChanged(currentTool()->type(), PRESERVEALPHA);
171 }
172 
173 void ToolManager::setVectorMergeEnabled(bool isVectorMergeEnabled)
174 {
175  currentTool()->setVectorMergeEnabled(isVectorMergeEnabled);
176  emit toolPropertyChanged(currentTool()->type(), VECTORMERGE);
177 }
178 
179 void ToolManager::setBezier(bool isBezierOn)
180 {
181  currentTool()->setBezier(isBezierOn);
182  emit toolPropertyChanged(currentTool()->type(), BEZIER);
183 }
184 
185 void ToolManager::setPressure(bool isPressureOn)
186 {
187  currentTool()->setPressure(isPressureOn);
188  emit toolPropertyChanged(currentTool()->type(), PRESSURE);
189 }
190 
191 void ToolManager::setAA(int usingAA)
192 {
193  currentTool()->setAA(usingAA);
194  emit toolPropertyChanged(currentTool()->type(), ANTI_ALIASING);
195 }
196 
197 void ToolManager::setStabilizerLevel(int level)
198 {
199  currentTool()->setStabilizerLevel(level);
200  emit toolPropertyChanged(currentTool()->type(), STABILIZATION);
201 }
202 
203 void ToolManager::setTolerance(int newTolerance)
204 {
205  newTolerance = qMax(0, newTolerance);
206 
207  currentTool()->setTolerance(newTolerance);
208  emit toleranceValueChanged(newTolerance);
209  emit toolPropertyChanged(currentTool()->type(), TOLERANCE);
210 }
211 
212 void ToolManager::setUseFillContour(bool useFillContour)
213 {
214  currentTool()->setUseFillContour(useFillContour);
215  emit toolPropertyChanged(currentTool()->type(), FILLCONTOUR);
216 }
217 
218 
219 // Switches on/off two actions
220 // eg. if x = true, then y = false
221 int ToolManager::propertySwitch(bool condition, int tool)
222 {
223  int value = 0;
224  int newValue = 0;
225 
226  if (condition == true) {
227  value = -1;
228  newValue = mOldValue;
229  mOldValue = tool;
230  }
231 
232  if (condition == false) {
233  if (newValue == 1) {
234  value = 1;
235  }
236  else {
237  value = mOldValue;
238  }
239  }
240  return value;
241 }
242 
243 void ToolManager::tabletSwitchToEraser()
244 {
245  if (!mIsSwitchedToEraser)
246  {
247  mIsSwitchedToEraser = true;
248 
249  meTabletBackupTool = mCurrentTool->type();
250  setCurrentTool(ERASER);
251  }
252 }
253 
254 void ToolManager::tabletRestorePrevTool()
255 {
256  if (mIsSwitchedToEraser)
257  {
258  mIsSwitchedToEraser = false;
259  if (meTabletBackupTool == INVALID_TOOL)
260  {
261  meTabletBackupTool = PENCIL;
262  }
263  setCurrentTool(meTabletBackupTool);
264  }
265 }
QHash::iterator insert(const Key &key, const T &value)
QList< T > values() const const
Definition: object.h:54
Definition: editor.h:51