Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
toolbox.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2013-2017 Matt 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 <cmath>
19 
20 #include <QFrame>
21 #include <QToolButton>
22 #include <QGridLayout>
23 #include <QKeySequence>
24 
25 #include "spinslider.h"
26 #include "toolbox.h"
27 #include "editor.h"
28 #include "toolmanager.h"
29 #include "pencilsettings.h"
30 
31 // ----------------------------------------------------------------------------------
32 QString GetToolTips( QString strCommandName )
33 {
34  strCommandName = QString( "shortcuts/" ) + strCommandName;
35  QKeySequence keySequence( pencilSettings().value( strCommandName ).toString() );
36  return QString("<b>%1</b>").arg( keySequence.toString() ); // don't tr() this string.
37 }
38 
39 
40 ToolBoxWidget::ToolBoxWidget( QWidget* parent ) : BaseDockWidget( parent )
41 {
42  setWindowTitle( tr( "Tools", "Window title of tool box" ) );
43 }
44 
45 
46 void ToolBoxWidget::initUI()
47 {
48 
49  QGridLayout* layout = new QGridLayout();
50 
51  pencilButton = newToolButton( QIcon( ":icons/pencil2.png" ),
52  tr( "Pencil Tool (%1): Sketch with pencil" )
53  .arg( GetToolTips( CMD_TOOL_PENCIL ) ) );
54  selectButton = newToolButton( QIcon( ":icons/select.png" ),
55  tr( "Select Tool (%1): Select an object" )
56  .arg( GetToolTips( CMD_TOOL_SELECT ) ) );
57  moveButton = newToolButton( QIcon( ":icons/arrow.png" ),
58  tr( "Move Tool (%1): Move an object" )
59  .arg( GetToolTips( CMD_TOOL_MOVE ) ) );
60  handButton = newToolButton( QIcon( ":icons/hand.png" ),
61  tr( "Hand Tool (%1): Move the canvas" )
62  .arg( GetToolTips( CMD_TOOL_HAND ) ) );
63  penButton = newToolButton( QIcon( ":icons/pen.png" ),
64  tr( "Pen Tool (%1): Sketch with pen" )
65  .arg( GetToolTips( CMD_TOOL_PEN ) ) );
66  eraserButton = newToolButton( QIcon( ":icons/eraser.png" ),
67  tr( "Eraser Tool (%1): Erase" )
68  .arg( GetToolTips( CMD_TOOL_ERASER ) ) );
69  polylineButton = newToolButton( QIcon( ":icons/polyline.png" ),
70  tr( "Polyline Tool (%1): Create line/curves" )
71  .arg( GetToolTips( CMD_TOOL_POLYLINE ) ) );
72  bucketButton = newToolButton( QIcon( ":icons/bucket.png" ),
73  tr( "Paint Bucket Tool (%1): Fill selected area with a color" )
74  .arg( GetToolTips( CMD_TOOL_BUCKET ) ) );
75  colouringButton = newToolButton( QIcon( ":icons/brush.png" ),
76  tr( "Brush Tool (%1): Paint smooth stroke with a brush" )
77  .arg( GetToolTips( CMD_TOOL_BRUSH ) ) );
78  eyedropperButton = newToolButton( QIcon( ":icons/eyedropper.png" ),
79  tr( "Eyedropper Tool (%1): "
80  "Set color from the stage<br>[ALT] for instant access" )
81  .arg( GetToolTips( CMD_TOOL_EYEDROPPER ) ) );
82  clearButton = newToolButton( QIcon( ":icons/clear.png" ),
83  tr( "Clear Frame (%1): Erases content of selected frame" )
84  .arg( GetToolTips( CMD_CLEAR_FRAME ) ) );
85  smudgeButton = newToolButton( QIcon( ":icons/smudge.png" ),
86  tr( "Smudge Tool (%1):<br>Edit polyline/curves<br>"
87  "Liquify bitmap pixels<br> (%1)+[Alt]: Smooth" )
88  .arg( GetToolTips( CMD_TOOL_SMUDGE ) ) );
89 
90  pencilButton->setWhatsThis( tr( "Pencil Tool (%1)" )
91  .arg( GetToolTips( CMD_TOOL_PENCIL ) ) );
92  selectButton->setWhatsThis( tr( "Select Tool (%1)" )
93  .arg( GetToolTips( CMD_TOOL_SELECT ) ) );
94  moveButton->setWhatsThis( tr( "Move Tool (%1)" )
95  .arg( GetToolTips( CMD_TOOL_MOVE ) ) );
96  handButton->setWhatsThis( tr( "Hand Tool (%1)" )
97  .arg( GetToolTips( CMD_TOOL_HAND ) ) );
98  penButton->setWhatsThis( tr( "Pen Tool (%1)" )
99  .arg( GetToolTips( CMD_TOOL_PEN ) ) );
100  eraserButton->setWhatsThis( tr( "Eraser Tool (%1)" )
101  .arg( GetToolTips( CMD_TOOL_ERASER ) ) );
102  polylineButton->setWhatsThis( tr( "Polyline Tool (%1)" )
103  .arg( GetToolTips( CMD_TOOL_POLYLINE ) ) );
104  bucketButton->setWhatsThis( tr( "Paint Bucket Tool (%1)" )
105  .arg( GetToolTips( CMD_TOOL_BUCKET ) ) );
106  colouringButton->setWhatsThis( tr( "Brush Tool (%1)" )
107  .arg( GetToolTips( CMD_TOOL_BRUSH ) ) );
108  eyedropperButton->setWhatsThis( tr( "Eyedropper Tool (%1)" )
109  .arg( GetToolTips( CMD_TOOL_EYEDROPPER ) ) );
110  clearButton->setWhatsThis( tr( "Clear Tool (%1)" )
111  .arg( GetToolTips( CMD_CLEAR_FRAME ) ) );
112  smudgeButton->setWhatsThis( tr( "Smudge Tool (%1)" )
113  .arg( GetToolTips( CMD_TOOL_SMUDGE ) ) );
114 
115  pencilButton->setCheckable( true );
116  penButton->setCheckable( true );
117  polylineButton->setCheckable( true );
118  bucketButton->setCheckable( true );
119  colouringButton->setCheckable( true );
120  smudgeButton->setCheckable( true );
121  eyedropperButton->setCheckable( true );
122  selectButton->setCheckable( true );
123  moveButton->setCheckable( true );
124  handButton->setCheckable( true );
125  eraserButton->setCheckable( true );
126  pencilButton->setChecked( true );
127 
128  layout->setMargin( 2 );
129  layout->setSpacing( 0 );
130 
131  layout->addWidget( moveButton, 0, 0 );
132  layout->setAlignment( moveButton, Qt::AlignRight );
133  layout->addWidget( clearButton, 0, 1 );
134  layout->setAlignment( clearButton, Qt::AlignLeft );
135 
136  layout->addWidget( selectButton, 1, 0 );
137  layout->setAlignment( selectButton, Qt::AlignRight );
138  layout->addWidget( colouringButton, 1, 1 );
139  layout->setAlignment( colouringButton, Qt::AlignLeft );
140 
141  layout->addWidget( polylineButton, 2, 0 );
142  layout->setAlignment( polylineButton, Qt::AlignRight );
143  layout->addWidget( smudgeButton, 2, 1 );
144  layout->setAlignment( smudgeButton, Qt::AlignLeft );
145 
146  layout->addWidget( penButton, 3, 0 );
147  layout->setAlignment( penButton, Qt::AlignRight );
148  layout->addWidget( handButton, 3, 1 );
149  layout->setAlignment( handButton, Qt::AlignLeft );
150 
151  layout->addWidget( pencilButton, 4, 0 );
152  layout->setAlignment( pencilButton, Qt::AlignRight );
153  layout->addWidget( bucketButton, 4, 1 );
154  layout->setAlignment( bucketButton, Qt::AlignLeft );
155 
156  layout->addWidget( eyedropperButton, 5, 0 );
157  layout->setAlignment( eyedropperButton, Qt::AlignRight );
158  layout->addWidget( eraserButton, 5, 1 );
159  layout->setAlignment( eraserButton, Qt::AlignLeft );
160 
161  QWidget* toolGroup = new QWidget();
162  setWidget( toolGroup );
163  toolGroup->setLayout( layout );
164  toolGroup->setMaximumHeight( 6 * 32 + 1 );
165 
166  setMaximumHeight( 200 );
167 
168  connect( pencilButton, &QToolButton::clicked, this, &ToolBoxWidget::pencilOn );
169  connect( eraserButton, &QToolButton::clicked, this, &ToolBoxWidget::eraserOn );
170  connect( selectButton, &QToolButton::clicked, this, &ToolBoxWidget::selectOn );
171  connect( moveButton, &QToolButton::clicked, this, &ToolBoxWidget::moveOn );
172  connect( penButton, &QToolButton::clicked, this, &ToolBoxWidget::penOn );
173  connect( handButton, &QToolButton::clicked, this, &ToolBoxWidget::handOn );
174  connect( polylineButton, &QToolButton::clicked, this, &ToolBoxWidget::polylineOn );
175  connect( bucketButton, &QToolButton::clicked, this, &ToolBoxWidget::bucketOn );
176  connect( eyedropperButton, &QToolButton::clicked, this, &ToolBoxWidget::eyedropperOn );
177  connect( colouringButton, &QToolButton::clicked, this, &ToolBoxWidget::brushOn );
178  connect( smudgeButton, &QToolButton::clicked, this, &ToolBoxWidget::smudgeOn );
179 
180  // pass to editor
181  connect( clearButton, &QToolButton::clicked, this, &ToolBoxWidget::clearButtonClicked );
182 
183 }
184 
185 void ToolBoxWidget::updateUI()
186 {
187 
188 }
189 
190 QToolButton* ToolBoxWidget::newToolButton(const QIcon& icon, QString strToolTip)
191 {
192  QToolButton* toolButton = new QToolButton(this);
193  toolButton->setAutoRaise(true);
194  toolButton->setIconSize( QSize(24,24) );
195  toolButton->setFixedSize(32, 32);
196  toolButton->setIcon(icon);
197  toolButton->setToolTip(strToolTip);
198 
199  return toolButton;
200 }
201 
202 void ToolBoxWidget::pencilOn()
203 {
204  editor()->tools()->setCurrentTool( PENCIL );
205 
206  deselectAllTools();
207  pencilButton->setChecked(true);
208 }
209 
210 void ToolBoxWidget::eraserOn()
211 {
212  editor()->tools()->setCurrentTool( ERASER );
213 
214  deselectAllTools();
215  eraserButton->setChecked(true);
216 }
217 
218 void ToolBoxWidget::selectOn()
219 {
220  editor()->tools()->setCurrentTool( SELECT );
221 
222  deselectAllTools();
223  selectButton->setChecked(true);
224 }
225 
226 void ToolBoxWidget::moveOn()
227 {
228  editor()->tools()->setCurrentTool( MOVE );
229 
230  deselectAllTools();
231  moveButton->setChecked(true);
232 }
233 
234 void ToolBoxWidget::penOn()
235 {
236  editor()->tools()->setCurrentTool( PEN );
237 
238  deselectAllTools();
239  penButton->setChecked(true);
240 }
241 
242 void ToolBoxWidget::handOn()
243 {
244  editor()->tools()->setCurrentTool( HAND );
245 
246  deselectAllTools();
247  handButton->setChecked(true);
248 }
249 
250 void ToolBoxWidget::polylineOn()
251 {
252  editor()->tools()->setCurrentTool( POLYLINE );
253 
254  deselectAllTools();
255  polylineButton->setChecked(true);
256 }
257 
258 void ToolBoxWidget::bucketOn()
259 {
260  editor()->tools()->setCurrentTool( BUCKET );
261 
262  deselectAllTools();
263  bucketButton->setChecked(true);
264 }
265 
266 void ToolBoxWidget::eyedropperOn()
267 {
268  editor()->tools()->setCurrentTool( EYEDROPPER );
269 
270  deselectAllTools();
271  eyedropperButton->setChecked(true);
272 }
273 
274 void ToolBoxWidget::brushOn()
275 {
276  editor()->tools()->setCurrentTool( BRUSH );
277 
278  deselectAllTools();
279  colouringButton->setChecked(true);
280 }
281 
282 void ToolBoxWidget::smudgeOn()
283 {
284  editor()->tools()->setCurrentTool( SMUDGE );
285 
286  deselectAllTools();
287  smudgeButton->setChecked(true);
288 }
289 
290 void ToolBoxWidget::deselectAllTools()
291 {
292  pencilButton->setChecked(false);
293  eraserButton->setChecked(false);
294  selectButton->setChecked(false);
295  moveButton->setChecked(false);
296  handButton->setChecked(false);
297  penButton->setChecked(false);
298  polylineButton->setChecked(false);
299  bucketButton->setChecked(false);
300  eyedropperButton->setChecked(false);
301  colouringButton->setChecked(false);
302  smudgeButton->setChecked(false);
303 }