Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
toolmanager.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 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 
18 #include "pentool.h"
19 #include "penciltool.h"
20 #include "brushtool.h"
21 #include "buckettool.h"
22 #include "erasertool.h"
23 #include "eyedroppertool.h"
24 #include "handtool.h"
25 #include "movetool.h"
26 #include "polylinetool.h"
27 #include "selecttool.h"
28 #include "smudgetool.h"
29 #include "toolmanager.h"
30 #include "editor.h"
31 #include "pencilsettings.h"
32 
33 
34 ToolManager::ToolManager(QObject* parent ) : BaseManager( parent )
35 {
36 }
37 
38 bool ToolManager::init()
39 {
40  mIsSwitchedToEraser = false;
41 
42  mToolSetHash.insert( PEN, new PenTool( parent() ) );
43  mToolSetHash.insert( PENCIL, new PencilTool( parent() ) );
44  mToolSetHash.insert( BRUSH, new BrushTool( parent() ) );
45  mToolSetHash.insert( ERASER, new EraserTool( parent() ) );
46  mToolSetHash.insert( BUCKET, new BucketTool( parent() ) );
47  mToolSetHash.insert( EYEDROPPER, new EyedropperTool( parent() ) );
48  mToolSetHash.insert( HAND, new HandTool( parent() ) );
49  mToolSetHash.insert( MOVE, new MoveTool( parent() ) );
50  mToolSetHash.insert( POLYLINE, new PolylineTool( parent() ) );
51  mToolSetHash.insert( SELECT, new SelectTool( parent() ) );
52  mToolSetHash.insert( SMUDGE, new SmudgeTool( parent() ) );
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 != NULL)
92  {
93  mCurrentTool->leavingThisTool();
94  }
95  mCurrentTool = getTool( eToolType );
96  Q_EMIT toolChanged( eToolType );
97 }
98 
99 void ToolManager::cleanupAllToolsData()
100 {
101  foreach ( BaseTool* pTool, mToolSetHash.values() )
102  {
103  pTool->clear();
104  }
105 }
106 
107 void ToolManager::resetAllTools()
108 {
109  // Reset can be useful to solve some pencil settings problems.
110  // Betatesters should be recommended to reset before sending tool related issues.
111  // This can prevent from users to stop working on their project.
112  getTool( PEN )->properties.width = 1.5; // not supposed to use feather
113  getTool( PEN )->properties.inpolLevel = -1;
114  getTool( POLYLINE )->properties.width = 1.5; // PEN dependent
115  getTool( PENCIL )->properties.width = 1.0;
116  getTool( PENCIL )->properties.feather = -1.0; // locks feather usage (can be changed)
117  getTool( PENCIL )->properties.inpolLevel = -1;
118  getTool( ERASER )->properties.width = 25.0;
119  getTool( ERASER )->properties.feather = 50.0;
120  getTool( BRUSH )->properties.width = 15.0;
121  getTool( BRUSH )->properties.feather = 200.0;
122  getTool( BRUSH )->properties.inpolLevel = -1;
123  getTool( BRUSH )->properties.useFeather = false;
124  getTool( SMUDGE )->properties.width = 25.0;
125  getTool( SMUDGE )->properties.feather = 200.0;
126  getTool( BUCKET )->properties.tolerance = 10.0;
127 
128  // todo: add all the default settings
129 
130  qDebug( "tools restored to default settings" );
131 }
132 
133 void ToolManager::setWidth( float newWidth )
134 {
135  if ( std::isnan( newWidth ) || newWidth < 0 )
136  {
137  newWidth = 1.f;
138  }
139 
140  currentTool()->setWidth(newWidth);
141  Q_EMIT penWidthValueChanged( newWidth );
142  Q_EMIT toolPropertyChanged( currentTool()->type(), WIDTH );
143 }
144 
145 void ToolManager::setFeather( float newFeather )
146 {
147  if ( std::isnan( newFeather ) || newFeather < 0 )
148  {
149  newFeather = 0.f;
150  }
151 
152  currentTool()->setFeather(newFeather);
153  Q_EMIT penFeatherValueChanged( newFeather );
154  Q_EMIT toolPropertyChanged( currentTool()->type(), FEATHER );
155 }
156 
157 void ToolManager::setUseFeather( bool usingFeather )
158 {
159  int usingAA = currentTool()->properties.useAA;
160  int value = propertySwitch(usingFeather, usingAA);
161 
162  currentTool()->setAA(value);
163  currentTool()->setUseFeather( usingFeather );
164  Q_EMIT toolPropertyChanged( currentTool()->type(), USEFEATHER );
165  Q_EMIT toolPropertyChanged( currentTool()->type(), ANTI_ALIASING );
166 }
167 
168 void ToolManager::setInvisibility( bool isInvisible )
169 {
170  currentTool()->setInvisibility(isInvisible);
171  Q_EMIT toolPropertyChanged( currentTool()->type(), INVISIBILITY );
172 }
173 
174 void ToolManager::setPreserveAlpha( bool isPreserveAlpha )
175 {
176  currentTool()->setPreserveAlpha(isPreserveAlpha);
177  Q_EMIT toolPropertyChanged( currentTool()->type(), PRESERVEALPHA );
178 }
179 
180 void ToolManager::setVectorMergeEnabled(bool isVectorMergeEnabled)
181 {
182  currentTool()->setVectorMergeEnabled(isVectorMergeEnabled);
183  Q_EMIT toolPropertyChanged( currentTool()->type(), VECTORMERGE );
184 }
185 
186 void ToolManager::setBezier( bool isBezierOn )
187 {
188  currentTool()->setBezier( isBezierOn );
189  Q_EMIT toolPropertyChanged( currentTool()->type(), BEZIER );
190 }
191 
192 void ToolManager::setPressure( bool isPressureOn )
193 {
194  currentTool()->setPressure( isPressureOn );
195  Q_EMIT toolPropertyChanged( currentTool()->type(), PRESSURE );
196 }
197 
198 void ToolManager::setAA( int usingAA )
199 {
200  currentTool()->setAA( usingAA );
201  Q_EMIT toolPropertyChanged( currentTool()->type(), ANTI_ALIASING );
202 }
203 
204 void ToolManager::setInpolLevel(int level)
205 {
206  currentTool()->setInpolLevel( level );
207  Q_EMIT toolPropertyChanged(currentTool()->type(), INTERPOLATION );
208 }
209 
210 
211 void ToolManager::setTolerance( int newTolerance )
212 {
213  if ( newTolerance < 0 )
214  {
215  newTolerance = 1;
216  }
217 
218  currentTool()->setTolerance( newTolerance );
219  Q_EMIT toleranceValueChanged( newTolerance );
220  Q_EMIT toolPropertyChanged( currentTool()->type(), TOLERANCE );
221 }
222 
223 void ToolManager::setUseFillContour(bool useFillContour)
224 {
225  currentTool()->setUseFillContour( useFillContour );
226  Q_EMIT toolPropertyChanged( currentTool()->type(), FILLCONTOUR);
227 }
228 
229 
230 // Switches on/off two actions
231 // eg. if x = true, then y = false
232 int ToolManager::propertySwitch(bool condition, int tool)
233 {
234  int value = 0;
235  int newValue = 0;
236 
237  if (condition == true){
238  value = -1;
239  newValue = oldValue;
240  oldValue = tool;
241  }
242 
243  if (condition == false) {
244  if (newValue == 1) {
245  value = 1;
246  } else {
247  value = oldValue;
248  }
249  }
250  return value;
251 }
252 
253 void ToolManager::tabletSwitchToEraser()
254 {
255  if (!mIsSwitchedToEraser)
256  {
257  mIsSwitchedToEraser = true;
258 
259  meTabletBackupTool = mCurrentTool->type();
260  setCurrentTool( ERASER );
261  }
262 }
263 
264 void ToolManager::tabletRestorePrevTool()
265 {
266  if ( mIsSwitchedToEraser )
267  {
268  mIsSwitchedToEraser = false;
269  if ( meTabletBackupTool == INVALID_TOOL )
270  {
271  meTabletBackupTool = PENCIL;
272  }
273  setCurrentTool( meTabletBackupTool );
274  }
275 }
Definition: object.h:71