Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • app
  • src
toolbox.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU 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 "layermanager.h"
33#include "pencilsettings.h"
34
35// ----------------------------------------------------------------------------------
36QString GetToolTips(QString strCommandName)
37{
38 strCommandName = QString("shortcuts/") + strCommandName;
39 QKeySequence keySequence(pencilSettings().value(strCommandName).toString());
40 return QString("<b>%1</b>").arg(keySequence.toString()); // don't tr() this string.
41}
42
43ToolBoxWidget::ToolBoxWidget(QWidget* parent) :
44 BaseDockWidget(parent),
45 ui(new Ui::ToolBoxWidget)
46{
47 ui->setupUi(this);
48}
49
50ToolBoxWidget::~ToolBoxWidget()
51{
52 QSettings settings(PENCIL2D, PENCIL2D);
53 settings.setValue("ToolBoxGeom", this->saveGeometry());
54 delete ui;
55}
56
57void ToolBoxWidget::initUI()
58{
59#ifdef __APPLE__
60 // Only Mac needs this. ToolButton is naturally borderless on Win/Linux.
61 QString sStyle =
62 "QToolButton { border: 0px; }"
63 "QToolButton:pressed { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }"
64 "QToolButton:checked { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }";
65 ui->pencilButton->setStyleSheet(sStyle);
66 ui->selectButton->setStyleSheet(sStyle);
67 ui->moveButton->setStyleSheet(sStyle);
68 ui->handButton->setStyleSheet(sStyle);
69 ui->penButton->setStyleSheet(sStyle);
70 ui->eraserButton->setStyleSheet(sStyle);
71 ui->polylineButton->setStyleSheet(sStyle);
72 ui->bucketButton->setStyleSheet(sStyle);
73 ui->brushButton->setStyleSheet(sStyle);
74 ui->eyedropperButton->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->smudgeButton->setToolTip( tr( "Smudge Tool (%1):<br>Edit polyline/curves<br>"
100 "Liquify bitmap pixels<br> (%1)+[Alt]: Smooth" )
101 .arg( GetToolTips( CMD_TOOL_SMUDGE ) ) );
102
103 ui->pencilButton->setWhatsThis( tr( "Pencil Tool (%1)" )
104 .arg( GetToolTips( CMD_TOOL_PENCIL ) ) );
105 ui->selectButton->setWhatsThis( tr( "Select Tool (%1)" )
106 .arg( GetToolTips( CMD_TOOL_SELECT ) ) );
107 ui->moveButton->setWhatsThis( tr( "Move Tool (%1)" )
108 .arg( GetToolTips( CMD_TOOL_MOVE ) ) );
109 ui->handButton->setWhatsThis( tr( "Hand Tool (%1)" )
110 .arg( GetToolTips( CMD_TOOL_HAND ) ) );
111 ui->penButton->setWhatsThis( tr( "Pen Tool (%1)" )
112 .arg( GetToolTips( CMD_TOOL_PEN ) ) );
113 ui->eraserButton->setWhatsThis( tr( "Eraser Tool (%1)" )
114 .arg( GetToolTips( CMD_TOOL_ERASER ) ) );
115 ui->polylineButton->setWhatsThis( tr( "Polyline Tool (%1)" )
116 .arg( GetToolTips( CMD_TOOL_POLYLINE ) ) );
117 ui->bucketButton->setWhatsThis( tr( "Paint Bucket Tool (%1)" )
118 .arg( GetToolTips( CMD_TOOL_BUCKET ) ) );
119 ui->brushButton->setWhatsThis( tr( "Brush Tool (%1)" )
120 .arg( GetToolTips( CMD_TOOL_BRUSH ) ) );
121 ui->eyedropperButton->setWhatsThis( tr( "Eyedropper Tool (%1)" )
122 .arg( GetToolTips( CMD_TOOL_EYEDROPPER ) ) );
123 ui->smudgeButton->setWhatsThis( tr( "Smudge Tool (%1)" )
124 .arg( GetToolTips( CMD_TOOL_SMUDGE ) ) );
125
126 connect(ui->pencilButton, &QToolButton::clicked, this, &ToolBoxWidget::pencilOn);
127 connect(ui->eraserButton, &QToolButton::clicked, this, &ToolBoxWidget::eraserOn);
128 connect(ui->selectButton, &QToolButton::clicked, this, &ToolBoxWidget::selectOn);
129 connect(ui->moveButton, &QToolButton::clicked, this, &ToolBoxWidget::moveOn);
130 connect(ui->penButton, &QToolButton::clicked, this, &ToolBoxWidget::penOn);
131 connect(ui->handButton, &QToolButton::clicked, this, &ToolBoxWidget::handOn);
132 connect(ui->polylineButton, &QToolButton::clicked, this, &ToolBoxWidget::polylineOn);
133 connect(ui->bucketButton, &QToolButton::clicked, this, &ToolBoxWidget::bucketOn);
134 connect(ui->eyedropperButton, &QToolButton::clicked, this, &ToolBoxWidget::eyedropperOn);
135 connect(ui->brushButton, &QToolButton::clicked, this, &ToolBoxWidget::brushOn);
136 connect(ui->smudgeButton, &QToolButton::clicked, this, &ToolBoxWidget::smudgeOn);
137
138 connect(editor()->layers(), &LayerManager::currentLayerChanged, this, &ToolBoxWidget::onLayerDidChange);
139
140 connect(this, &QDockWidget::dockLocationChanged, this, [=](Qt::DockWidgetArea area) {
141 if (area == Qt::DockWidgetArea::TopDockWidgetArea || area == Qt::BottomDockWidgetArea) {
142 const int minimumHeight = ui->scrollAreaWidgetContents_2->layout()->heightForWidth(width());
143 ui->scrollArea->setMinimumHeight(minimumHeight);
144 setMinimumHeight(minimumHeight);
145 } else {
146 ui->scrollArea->setMinimumHeight(0); // Default value
147 // Don't set own minimum height and let Qt come up with a sensible value
148 }
149 });
150
151 FlowLayout* flowlayout = new FlowLayout(3,3,3);
152
153 flowlayout->addWidget(ui->pencilButton);
154 flowlayout->addWidget(ui->eraserButton);
155 flowlayout->addWidget(ui->selectButton);
156 flowlayout->addWidget(ui->moveButton);
157 flowlayout->addWidget(ui->penButton);
158 flowlayout->addWidget(ui->handButton);
159 flowlayout->addWidget(ui->polylineButton);
160 flowlayout->addWidget(ui->bucketButton);
161 flowlayout->addWidget(ui->eyedropperButton);
162 flowlayout->addWidget(ui->brushButton);
163 flowlayout->addWidget(ui->smudgeButton);
164
165 delete ui->scrollAreaWidgetContents_2->layout();
166 ui->scrollAreaWidgetContents_2->setLayout(flowlayout);
167 ui->scrollAreaWidgetContents_2->setContentsMargins(0,0,0,0);
168
169 QSettings settings(PENCIL2D, PENCIL2D);
170 restoreGeometry(settings.value("ToolBoxGeom").toByteArray());
171}
172
173void ToolBoxWidget::updateUI()
174{
175}
176
177void ToolBoxWidget::resizeEvent(QResizeEvent* event)
178{
179 QDockWidget::resizeEvent(event);
180
181 const int minimumHeight = ui->scrollArea->minimumHeight();
182 if (minimumHeight <= 0) { return; }
183 setMinimumHeight(minimumHeight);
184}
185
186void ToolBoxWidget::onToolSetActive(ToolType toolType)
187{
188 deselectAllTools();
189 switch (toolType) {
190 case ToolType::BRUSH:
191 ui->brushButton->setChecked(true);
192 break;
193 case ToolType::PEN:
194 ui->penButton->setChecked(true);
195 break;
196 case ToolType::PENCIL:
197 ui->pencilButton->setChecked(true);
198 break;
199 case ToolType::SELECT:
200 ui->selectButton->setChecked(true);
201 break;
202 case ToolType::HAND:
203 ui->handButton->setChecked(true);
204 break;
205 case ToolType::MOVE:
206 case ToolType::CAMERA:
207 ui->moveButton->setChecked(true);
208 break;
209 case ToolType::ERASER:
210 ui->eraserButton->setChecked(true);
211 break;
212 case ToolType::POLYLINE:
213 ui->polylineButton->setChecked(true);
214 break;
215 case ToolType::SMUDGE:
216 ui->smudgeButton->setChecked(true);
217 break;
218 case ToolType::BUCKET:
219 ui->bucketButton->setChecked(true);
220 break;
221 case ToolType::EYEDROPPER:
222 ui->eyedropperButton->setChecked(true);
223 break;
224 default:
225 break;
226 }
227}
228
229void ToolBoxWidget::pencilOn()
230{
231 toolOn(PENCIL, ui->pencilButton);
232}
233
234void ToolBoxWidget::eraserOn()
235{
236 toolOn(ERASER, ui->eraserButton);
237}
238
239void ToolBoxWidget::selectOn()
240{
241 toolOn(SELECT, ui->selectButton);
242}
243
244void ToolBoxWidget::moveOn()
245{
246 if (editor()->layers()->currentLayer()->type() == Layer::CAMERA) {
247 toolOn(CAMERA, ui->moveButton);
248 } else {
249 toolOn(MOVE, ui->moveButton);
250 }
251}
252
253void ToolBoxWidget::penOn()
254{
255 toolOn(PEN, ui->penButton);
256}
257
258void ToolBoxWidget::handOn()
259{
260 toolOn(HAND, ui->handButton);
261}
262
263void ToolBoxWidget::polylineOn()
264{
265 toolOn(POLYLINE, ui->polylineButton);
266}
267
268void ToolBoxWidget::bucketOn()
269{
270 toolOn(BUCKET, ui->bucketButton);
271}
272
273void ToolBoxWidget::eyedropperOn()
274{
275 toolOn(EYEDROPPER, ui->eyedropperButton);
276}
277
278void ToolBoxWidget::brushOn()
279{
280 toolOn(BRUSH, ui->brushButton);
281}
282
283void ToolBoxWidget::smudgeOn()
284{
285 toolOn(SMUDGE, ui->smudgeButton);
286}
287
288void ToolBoxWidget::deselectAllTools()
289{
290 ui->pencilButton->setChecked(false);
291 ui->eraserButton->setChecked(false);
292 ui->selectButton->setChecked(false);
293 ui->moveButton->setChecked(false);
294 ui->handButton->setChecked(false);
295 ui->penButton->setChecked(false);
296 ui->polylineButton->setChecked(false);
297 ui->bucketButton->setChecked(false);
298 ui->eyedropperButton->setChecked(false);
299 ui->brushButton->setChecked(false);
300 ui->smudgeButton->setChecked(false);
301}
302
303void ToolBoxWidget::toolOn(ToolType toolType, QToolButton* toolButton)
304{
305 if (editor()->tools()->currentTool()->type() == toolType) {
306 // Prevent un-checking the current tool and do nothing
307 toolButton->setChecked(true);
308 return;
309 }
310 if (!editor()->tools()->leavingThisTool())
311 {
312 toolButton->setChecked(false);
313 return;
314 }
315 editor()->tools()->setCurrentTool(toolType);
316}
317
318void ToolBoxWidget::onLayerDidChange(int)
319{
320 BaseTool* currentTool = editor()->tools()->currentTool();
321 if (currentTool->type() == MOVE || currentTool->type() == CAMERA)
322 {
323 moveOn();
324 }
325}
BaseDockWidget
Definition: basedockwidget.h:27
BaseTool
Definition: basetool.h:70
FlowLayout
Definition: flowlayout.h:59
ToolBoxWidget
Definition: toolbox.h:36
QAbstractButton::setChecked
void setChecked(bool)
QAbstractButton::clicked
void clicked(bool checked)
QDockWidget::dockLocationChanged
void dockLocationChanged(Qt::DockWidgetArea area)
QDockWidget::event
virtual bool event(QEvent *event) override
QKeySequence
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
QResizeEvent
QSettings
QString
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Qt::DockWidgetArea
DockWidgetArea
QTest::keySequence
void keySequence(QWindow *window, const QKeySequence &keySequence)
QTest::toString
char * toString(const T &value)
QToolButton
QWidget
QWidget::heightForWidth
virtual int heightForWidth(int w) const const
QWidget::minimumHeight
minimumHeight
QWidget::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
QWidget::restoreGeometry
bool restoreGeometry(const QByteArray &geometry)
QWidget::saveGeometry
QByteArray saveGeometry() const const
QWidget::setupUi
void setupUi(QWidget *widget)
QWidget::width
width
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39