All Classes Namespaces Functions Variables Enumerations Properties Pages
toolbox.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 
18 #include "toolbox.h"
19 #include "ui_toolboxwidget.h"
20 
21 #include <cmath>
22 
23 #include <QToolButton>
24 #include <QGridLayout>
25 #include <QKeySequence>
26 #include <QResizeEvent>
27 
28 #include "flowlayout.h"
29 #include "spinslider.h"
30 #include "editor.h"
31 #include "toolmanager.h"
32 #include "pencilsettings.h"
33 
34 // ----------------------------------------------------------------------------------
35 QString GetToolTips(QString strCommandName)
36 {
37  strCommandName = QString("shortcuts/") + strCommandName;
38  QKeySequence keySequence(pencilSettings().value(strCommandName).toString());
39  return QString("<b>%1</b>").arg(keySequence.toString()); // don't tr() this string.
40 }
41 
42 ToolBoxWidget::ToolBoxWidget(QWidget* parent) :
43  BaseDockWidget(parent),
44  ui(new Ui::ToolBoxWidget)
45 {
46  ui->setupUi(this);
47 }
48 
49 ToolBoxWidget::~ToolBoxWidget()
50 {
51  QSettings settings(PENCIL2D, PENCIL2D);
52  settings.setValue("ToolBoxGeom", this->saveGeometry());
53  delete ui;
54 }
55 
56 void ToolBoxWidget::initUI()
57 {
58 #ifdef __APPLE__
59  // Only Mac needs this. ToolButton is naturally borderless on Win/Linux.
60  QString sStyle =
61  "QToolButton { border: 0px; }"
62  "QToolButton:pressed { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }"
63  "QToolButton:checked { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }";
64  ui->pencilButton->setStyleSheet(sStyle);
65  ui->selectButton->setStyleSheet(sStyle);
66  ui->moveButton->setStyleSheet(sStyle);
67  ui->handButton->setStyleSheet(sStyle);
68  ui->penButton->setStyleSheet(sStyle);
69  ui->eraserButton->setStyleSheet(sStyle);
70  ui->polylineButton->setStyleSheet(sStyle);
71  ui->bucketButton->setStyleSheet(sStyle);
72  ui->brushButton->setStyleSheet(sStyle);
73  ui->eyedropperButton->setStyleSheet(sStyle);
74  ui->clearButton->setStyleSheet(sStyle);
75  ui->smudgeButton->setStyleSheet(sStyle);
76 #endif
77 
78  ui->pencilButton->setToolTip( tr( "Pencil Tool (%1): Sketch with pencil" )
79  .arg( GetToolTips( CMD_TOOL_PENCIL ) ) );
80  ui->selectButton->setToolTip( tr( "Select Tool (%1): Select an object" )
81  .arg( GetToolTips( CMD_TOOL_SELECT ) ) );
82  ui->moveButton->setToolTip( tr( "Move Tool (%1): Move an object" )
83  .arg( GetToolTips( CMD_TOOL_MOVE ) ) );
84  ui->handButton->setToolTip( tr( "Hand Tool (%1): Move the canvas" )
85  .arg( GetToolTips( CMD_TOOL_HAND ) ) );
86  ui->penButton->setToolTip( tr( "Pen Tool (%1): Sketch with pen" )
87  .arg( GetToolTips( CMD_TOOL_PEN ) ) );
88  ui->eraserButton->setToolTip( tr( "Eraser Tool (%1): Erase" )
89  .arg( GetToolTips( CMD_TOOL_ERASER ) ) );
90  ui->polylineButton->setToolTip( tr( "Polyline Tool (%1): Create line/curves" )
91  .arg( GetToolTips( CMD_TOOL_POLYLINE ) ) );
92  ui->bucketButton->setToolTip( tr( "Paint Bucket Tool (%1): Fill selected area with a color" )
93  .arg( GetToolTips( CMD_TOOL_BUCKET ) ) );
94  ui->brushButton->setToolTip( tr( "Brush Tool (%1): Paint smooth stroke with a brush" )
95  .arg( GetToolTips( CMD_TOOL_BRUSH ) ) );
96  ui->eyedropperButton->setToolTip( tr( "Eyedropper Tool (%1): "
97  "Set color from the stage<br>[ALT] for instant access" )
98  .arg( GetToolTips( CMD_TOOL_EYEDROPPER ) ) );
99  ui->clearButton->setToolTip( tr( "Clear Frame (%1): Erases content of selected frame" )
100  .arg( GetToolTips( CMD_CLEAR_FRAME ) ) );
101  ui->smudgeButton->setToolTip( tr( "Smudge Tool (%1):<br>Edit polyline/curves<br>"
102  "Liquify bitmap pixels<br> (%1)+[Alt]: Smooth" )
103  .arg( GetToolTips( CMD_TOOL_SMUDGE ) ) );
104 
105  ui->pencilButton->setWhatsThis( tr( "Pencil Tool (%1)" )
106  .arg( GetToolTips( CMD_TOOL_PENCIL ) ) );
107  ui->selectButton->setWhatsThis( tr( "Select Tool (%1)" )
108  .arg( GetToolTips( CMD_TOOL_SELECT ) ) );
109  ui->moveButton->setWhatsThis( tr( "Move Tool (%1)" )
110  .arg( GetToolTips( CMD_TOOL_MOVE ) ) );
111  ui->handButton->setWhatsThis( tr( "Hand Tool (%1)" )
112  .arg( GetToolTips( CMD_TOOL_HAND ) ) );
113  ui->penButton->setWhatsThis( tr( "Pen Tool (%1)" )
114  .arg( GetToolTips( CMD_TOOL_PEN ) ) );
115  ui->eraserButton->setWhatsThis( tr( "Eraser Tool (%1)" )
116  .arg( GetToolTips( CMD_TOOL_ERASER ) ) );
117  ui->polylineButton->setWhatsThis( tr( "Polyline Tool (%1)" )
118  .arg( GetToolTips( CMD_TOOL_POLYLINE ) ) );
119  ui->bucketButton->setWhatsThis( tr( "Paint Bucket Tool (%1)" )
120  .arg( GetToolTips( CMD_TOOL_BUCKET ) ) );
121  ui->brushButton->setWhatsThis( tr( "Brush Tool (%1)" )
122  .arg( GetToolTips( CMD_TOOL_BRUSH ) ) );
123  ui->eyedropperButton->setWhatsThis( tr( "Eyedropper Tool (%1)" )
124  .arg( GetToolTips( CMD_TOOL_EYEDROPPER ) ) );
125  ui->clearButton->setWhatsThis( tr( "Clear Tool (%1)" )
126  .arg( GetToolTips( CMD_CLEAR_FRAME ) ) );
127  ui->smudgeButton->setWhatsThis( tr( "Smudge Tool (%1)" )
128  .arg( GetToolTips( CMD_TOOL_SMUDGE ) ) );
129 
130  connect(ui->clearButton, &QToolButton::clicked, this, &ToolBoxWidget::clearButtonClicked);
131  connect(ui->pencilButton, &QToolButton::clicked, this, &ToolBoxWidget::pencilOn);
132  connect(ui->eraserButton, &QToolButton::clicked, this, &ToolBoxWidget::eraserOn);
133  connect(ui->selectButton, &QToolButton::clicked, this, &ToolBoxWidget::selectOn);
134  connect(ui->moveButton, &QToolButton::clicked, this, &ToolBoxWidget::moveOn);
135  connect(ui->penButton, &QToolButton::clicked, this, &ToolBoxWidget::penOn);
136  connect(ui->handButton, &QToolButton::clicked, this, &ToolBoxWidget::handOn);
137  connect(ui->polylineButton, &QToolButton::clicked, this, &ToolBoxWidget::polylineOn);
138  connect(ui->bucketButton, &QToolButton::clicked, this, &ToolBoxWidget::bucketOn);
139  connect(ui->eyedropperButton, &QToolButton::clicked, this, &ToolBoxWidget::eyedropperOn);
140  connect(ui->brushButton, &QToolButton::clicked, this, &ToolBoxWidget::brushOn);
141  connect(ui->smudgeButton, &QToolButton::clicked, this, &ToolBoxWidget::smudgeOn);
142 
143  FlowLayout* flowlayout = new FlowLayout;
144 
145  flowlayout->addWidget(ui->clearButton);
146  flowlayout->addWidget(ui->pencilButton);
147  flowlayout->addWidget(ui->eraserButton);
148  flowlayout->addWidget(ui->selectButton);
149  flowlayout->addWidget(ui->moveButton);
150  flowlayout->addWidget(ui->penButton);
151  flowlayout->addWidget(ui->handButton);
152  flowlayout->addWidget(ui->polylineButton);
153  flowlayout->addWidget(ui->bucketButton);
154  flowlayout->addWidget(ui->eyedropperButton);
155  flowlayout->addWidget(ui->brushButton);
156  flowlayout->addWidget(ui->smudgeButton);
157 
158  delete ui->scrollAreaWidgetContents_2->layout();
159  ui->scrollAreaWidgetContents_2->setLayout(flowlayout);
160 
161  QSettings settings(PENCIL2D, PENCIL2D);
162  restoreGeometry(settings.value("ToolBoxGeom").toByteArray());
163 }
164 
165 void ToolBoxWidget::updateUI()
166 {
167 }
168 
169 void ToolBoxWidget::pencilOn()
170 {
171  if (!leavingTool(ui->pencilButton)) { return; }
172 
173  editor()->tools()->setCurrentTool(PENCIL);
174 
175  deselectAllTools();
176  ui->pencilButton->setChecked(true);
177 }
178 
179 void ToolBoxWidget::eraserOn()
180 {
181  if (!leavingTool(ui->eraserButton)) { return; }
182 
183  editor()->tools()->setCurrentTool(ERASER);
184 
185  deselectAllTools();
186  ui->eraserButton->setChecked(true);
187 }
188 
189 void ToolBoxWidget::selectOn()
190 {
191  if (!leavingTool(ui->selectButton)) { return; }
192 
193  editor()->tools()->setCurrentTool(SELECT);
194 
195  deselectAllTools();
196  ui->selectButton->setChecked(true);
197 }
198 
199 void ToolBoxWidget::moveOn()
200 {
201  if (!leavingTool(ui->moveButton)) { return; }
202 
203  editor()->tools()->setCurrentTool(MOVE);
204 
205  deselectAllTools();
206  ui->moveButton->setChecked(true);
207 }
208 
209 void ToolBoxWidget::penOn()
210 {
211  if (!leavingTool(ui->penButton)) { return; }
212 
213  editor()->tools()->setCurrentTool(PEN);
214 
215  deselectAllTools();
216  ui->penButton->setChecked(true);
217 }
218 
219 void ToolBoxWidget::handOn()
220 {
221  if (!leavingTool(ui->handButton)) { return; }
222 
223  editor()->tools()->setCurrentTool( HAND );
224 
225  deselectAllTools();
226  ui->handButton->setChecked(true);
227 }
228 
229 void ToolBoxWidget::polylineOn()
230 {
231  if (!leavingTool(ui->polylineButton)) { return; }
232 
233  editor()->tools()->setCurrentTool(POLYLINE);
234 
235  deselectAllTools();
236  ui->polylineButton->setChecked(true);
237 }
238 
239 void ToolBoxWidget::bucketOn()
240 {
241  if (!leavingTool(ui->bucketButton)) { return; }
242 
243  editor()->tools()->setCurrentTool(BUCKET);
244 
245  deselectAllTools();
246  ui->bucketButton->setChecked(true);
247 }
248 
249 void ToolBoxWidget::eyedropperOn()
250 {
251  if (!leavingTool(ui->eyedropperButton)) { return; }
252  editor()->tools()->setCurrentTool(EYEDROPPER);
253 
254  deselectAllTools();
255  ui->eyedropperButton->setChecked(true);
256 }
257 
258 void ToolBoxWidget::brushOn()
259 {
260  if (!leavingTool(ui->brushButton)) { return; }
261 
262  editor()->tools()->setCurrentTool( BRUSH );
263 
264  deselectAllTools();
265  ui->brushButton->setChecked(true);
266 }
267 
268 void ToolBoxWidget::smudgeOn()
269 {
270  if (!leavingTool(ui->smudgeButton)) { return; }
271  editor()->tools()->setCurrentTool(SMUDGE);
272 
273  deselectAllTools();
274  ui->smudgeButton->setChecked(true);
275 }
276 
277 int ToolBoxWidget::getMinHeightForWidth(int width)
278 {
279  return ui->toolGroup->layout()->heightForWidth(width);
280 }
281 
282 void ToolBoxWidget::deselectAllTools()
283 {
284  ui->pencilButton->setChecked(false);
285  ui->eraserButton->setChecked(false);
286  ui->selectButton->setChecked(false);
287  ui->moveButton->setChecked(false);
288  ui->handButton->setChecked(false);
289  ui->penButton->setChecked(false);
290  ui->polylineButton->setChecked(false);
291  ui->bucketButton->setChecked(false);
292  ui->eyedropperButton->setChecked(false);
293  ui->brushButton->setChecked(false);
294  ui->smudgeButton->setChecked(false);
295 }
296 
297 bool ToolBoxWidget::leavingTool(QToolButton* toolButton)
298 {
299  if (!editor()->tools()->leavingThisTool())
300  {
301  if (toolButton->isChecked()) {
302  toolButton->setChecked(false);
303  }
304  return false;
305  }
306  return true;
307 }
QString tr(const char *sourceText, const char *disambiguation, int n)
bool restoreGeometry(const QByteArray &geometry)
void clicked(bool checked)
void addWidget(QWidget *w)
bool isChecked() const const
char * toString(const T &value)
QByteArray saveGeometry() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void keySequence(QWindow *window, const QKeySequence &keySequence)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)