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::onToolSetActive(ToolType toolType)
170 {
171  deselectAllTools();
172  switch (toolType) {
173  case ToolType::BRUSH:
174  ui->brushButton->setChecked(true);
175  break;
176  case ToolType::PEN:
177  ui->penButton->setChecked(true);
178  break;
179  case ToolType::PENCIL:
180  ui->pencilButton->setChecked(true);
181  break;
182  case ToolType::SELECT:
183  ui->selectButton->setChecked(true);
184  break;
185  case ToolType::HAND:
186  ui->handButton->setChecked(true);
187  break;
188  case ToolType::MOVE:
189  ui->moveButton->setChecked(true);
190  break;
191  case ToolType::ERASER:
192  ui->eraserButton->setChecked(true);
193  break;
194  case ToolType::POLYLINE:
195  ui->polylineButton->setChecked(true);
196  break;
197  case ToolType::SMUDGE:
198  ui->smudgeButton->setChecked(true);
199  break;
200  case ToolType::BUCKET:
201  ui->bucketButton->setChecked(true);
202  break;
203  case ToolType::EYEDROPPER:
204  ui->eyedropperButton->setChecked(true);
205  break;
206  default:
207  break;
208  }
209 }
210 
211 void ToolBoxWidget::pencilOn()
212 {
213  toolOn(PENCIL, ui->pencilButton);
214 }
215 
216 void ToolBoxWidget::eraserOn()
217 {
218  toolOn(ERASER, ui->eraserButton);
219 }
220 
221 void ToolBoxWidget::selectOn()
222 {
223  toolOn(SELECT, ui->selectButton);
224 }
225 
226 void ToolBoxWidget::moveOn()
227 {
228  toolOn(MOVE, ui->moveButton);
229 }
230 
231 void ToolBoxWidget::penOn()
232 {
233  toolOn(PEN, ui->penButton);
234 }
235 
236 void ToolBoxWidget::handOn()
237 {
238  toolOn(HAND, ui->handButton);
239 }
240 
241 void ToolBoxWidget::polylineOn()
242 {
243  toolOn(POLYLINE, ui->polylineButton);
244 }
245 
246 void ToolBoxWidget::bucketOn()
247 {
248  toolOn(BUCKET, ui->bucketButton);
249 }
250 
251 void ToolBoxWidget::eyedropperOn()
252 {
253  toolOn(EYEDROPPER, ui->eyedropperButton);
254 }
255 
256 void ToolBoxWidget::brushOn()
257 {
258  toolOn(BRUSH, ui->brushButton);
259 }
260 
261 void ToolBoxWidget::smudgeOn()
262 {
263  toolOn(SMUDGE, ui->smudgeButton);
264 }
265 
266 int ToolBoxWidget::getMinHeightForWidth(int width)
267 {
268  return ui->toolGroup->layout()->heightForWidth(width);
269 }
270 
271 void ToolBoxWidget::deselectAllTools()
272 {
273  ui->pencilButton->setChecked(false);
274  ui->eraserButton->setChecked(false);
275  ui->selectButton->setChecked(false);
276  ui->moveButton->setChecked(false);
277  ui->handButton->setChecked(false);
278  ui->penButton->setChecked(false);
279  ui->polylineButton->setChecked(false);
280  ui->bucketButton->setChecked(false);
281  ui->eyedropperButton->setChecked(false);
282  ui->brushButton->setChecked(false);
283  ui->smudgeButton->setChecked(false);
284 }
285 
286 bool ToolBoxWidget::toolOn(ToolType toolType, QToolButton* toolButton)
287 {
288  if (!editor()->tools()->leavingThisTool())
289  {
290  toolButton->setChecked(false);
291  return false;
292  }
293  editor()->tools()->setCurrentTool(toolType);
294  return true;
295 }
QString tr(const char *sourceText, const char *disambiguation, int n)
bool restoreGeometry(const QByteArray &geometry)
void clicked(bool checked)
void addWidget(QWidget *w)
void setChecked(bool)
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)