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
toolboxwidget.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6Copyright (C) 2024-2099 Oliver S. Larsen
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; version 2 of the License.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17*/
18#include "toolboxwidget.h"
19#include "ui_toolboxwidget.h"
20
21#include <QScrollBar>
22#include <QResizeEvent>
23#include <QDebug>
24#include <QButtonGroup>
25
26#include "layermanager.h"
27#include "toolmanager.h"
28#include "editor.h"
29#include "pencilsettings.h"
30
31// ----------------------------------------------------------------------------------
32QString 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
39ToolBoxWidget::ToolBoxWidget(QWidget* parent)
40 : QWidget(parent), ui(new Ui::ToolBoxWidget)
41{
42 ui->setupUi(this);
43}
44
45ToolBoxWidget::~ToolBoxWidget()
46{
47 delete ui;
48}
49
50void ToolBoxWidget::initUI()
51{
52
53#ifdef __APPLE__
54 // Only Mac needs this. ToolButton is naturally borderless on Win/Linux.
55 QString sStyle =
56 "QToolButton { border: 0px; }"
57 "QToolButton:pressed { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }"
58 "QToolButton:checked { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }";
59 ui->pencilButton->setStyleSheet(sStyle);
60 ui->selectButton->setStyleSheet(sStyle);
61 ui->moveButton->setStyleSheet(sStyle);
62 ui->handButton->setStyleSheet(sStyle);
63 ui->penButton->setStyleSheet(sStyle);
64 ui->eraserButton->setStyleSheet(sStyle);
65 ui->polylineButton->setStyleSheet(sStyle);
66 ui->bucketButton->setStyleSheet(sStyle);
67 ui->brushButton->setStyleSheet(sStyle);
68 ui->eyedropperButton->setStyleSheet(sStyle);
69 ui->smudgeButton->setStyleSheet(sStyle);
70#endif
71
72 ui->pencilButton->setToolTip( tr( "Pencil Tool (%1): Sketch with pencil" )
73 .arg( GetToolTips( CMD_TOOL_PENCIL ) ) );
74 ui->selectButton->setToolTip( tr( "Select Tool (%1): Select an object" )
75 .arg( GetToolTips( CMD_TOOL_SELECT ) ) );
76 ui->moveButton->setToolTip( tr( "Move Tool (%1): Move an object" )
77 .arg( GetToolTips( CMD_TOOL_MOVE ) ) );
78 ui->handButton->setToolTip( tr( "Hand Tool (%1): Move the canvas" )
79 .arg( GetToolTips( CMD_TOOL_HAND ) ) );
80 ui->penButton->setToolTip( tr( "Pen Tool (%1): Sketch with pen" )
81 .arg( GetToolTips( CMD_TOOL_PEN ) ) );
82 ui->eraserButton->setToolTip( tr( "Eraser Tool (%1): Erase" )
83 .arg( GetToolTips( CMD_TOOL_ERASER ) ) );
84 ui->polylineButton->setToolTip( tr( "Polyline Tool (%1): Create line/curves" )
85 .arg( GetToolTips( CMD_TOOL_POLYLINE ) ) );
86 ui->bucketButton->setToolTip( tr( "Paint Bucket Tool (%1): Fill selected area with a color" )
87 .arg( GetToolTips( CMD_TOOL_BUCKET ) ) );
88 ui->brushButton->setToolTip( tr( "Brush Tool (%1): Paint smooth stroke with a brush" )
89 .arg( GetToolTips( CMD_TOOL_BRUSH ) ) );
90 ui->eyedropperButton->setToolTip( tr( "Eyedropper Tool (%1): "
91 "Set color from the stage<br>[ALT] for instant access" )
92 .arg( GetToolTips( CMD_TOOL_EYEDROPPER ) ) );
93 ui->smudgeButton->setToolTip( tr( "Smudge Tool (%1):<br>Edit polyline/curves<br>"
94 "Liquify bitmap pixels<br> (%1)+[Alt]: Smooth" )
95 .arg( GetToolTips( CMD_TOOL_SMUDGE ) ) );
96
97 ui->pencilButton->setWhatsThis( tr( "Pencil Tool (%1)" )
98 .arg( GetToolTips( CMD_TOOL_PENCIL ) ) );
99 ui->selectButton->setWhatsThis( tr( "Select Tool (%1)" )
100 .arg( GetToolTips( CMD_TOOL_SELECT ) ) );
101 ui->moveButton->setWhatsThis( tr( "Move Tool (%1)" )
102 .arg( GetToolTips( CMD_TOOL_MOVE ) ) );
103 ui->handButton->setWhatsThis( tr( "Hand Tool (%1)" )
104 .arg( GetToolTips( CMD_TOOL_HAND ) ) );
105 ui->penButton->setWhatsThis( tr( "Pen Tool (%1)" )
106 .arg( GetToolTips( CMD_TOOL_PEN ) ) );
107 ui->eraserButton->setWhatsThis( tr( "Eraser Tool (%1)" )
108 .arg( GetToolTips( CMD_TOOL_ERASER ) ) );
109 ui->polylineButton->setWhatsThis( tr( "Polyline Tool (%1)" )
110 .arg( GetToolTips( CMD_TOOL_POLYLINE ) ) );
111 ui->bucketButton->setWhatsThis( tr( "Paint Bucket Tool (%1)" )
112 .arg( GetToolTips( CMD_TOOL_BUCKET ) ) );
113 ui->brushButton->setWhatsThis( tr( "Brush Tool (%1)" )
114 .arg( GetToolTips( CMD_TOOL_BRUSH ) ) );
115 ui->eyedropperButton->setWhatsThis( tr( "Eyedropper Tool (%1)" )
116 .arg( GetToolTips( CMD_TOOL_EYEDROPPER ) ) );
117 ui->smudgeButton->setWhatsThis( tr( "Smudge Tool (%1)" )
118 .arg( GetToolTips( CMD_TOOL_SMUDGE ) ) );
119
120 connect(ui->pencilButton, &QToolButton::clicked, this, &ToolBoxWidget::pencilOn);
121 connect(ui->eraserButton, &QToolButton::clicked, this, &ToolBoxWidget::eraserOn);
122 connect(ui->selectButton, &QToolButton::clicked, this, &ToolBoxWidget::selectOn);
123 connect(ui->moveButton, &QToolButton::clicked, this, &ToolBoxWidget::moveOn);
124 connect(ui->penButton, &QToolButton::clicked, this, &ToolBoxWidget::penOn);
125 connect(ui->handButton, &QToolButton::clicked, this, &ToolBoxWidget::handOn);
126 connect(ui->polylineButton, &QToolButton::clicked, this, &ToolBoxWidget::polylineOn);
127 connect(ui->bucketButton, &QToolButton::clicked, this, &ToolBoxWidget::bucketOn);
128 connect(ui->eyedropperButton, &QToolButton::clicked, this, &ToolBoxWidget::eyedropperOn);
129 connect(ui->brushButton, &QToolButton::clicked, this, &ToolBoxWidget::brushOn);
130 connect(ui->smudgeButton, &QToolButton::clicked, this, &ToolBoxWidget::smudgeOn);
131
132 mFlowlayout = new ToolBoxLayout(nullptr, 3,3,3);
133
134 mFlowlayout->addWidget(ui->pencilButton);
135 mFlowlayout->addWidget(ui->eraserButton);
136 mFlowlayout->addWidget(ui->selectButton);
137 mFlowlayout->addWidget(ui->moveButton);
138 mFlowlayout->addWidget(ui->penButton);
139 mFlowlayout->addWidget(ui->handButton);
140 mFlowlayout->addWidget(ui->polylineButton);
141 mFlowlayout->addWidget(ui->bucketButton);
142 mFlowlayout->addWidget(ui->eyedropperButton);
143 mFlowlayout->addWidget(ui->brushButton);
144 mFlowlayout->addWidget(ui->smudgeButton);
145
146 delete ui->scrollAreaWidgetContents_2->layout();
147 ui->scrollAreaWidgetContents_2->setLayout(mFlowlayout);
148
149 // Important to set the proper minimumSize;
150 ui->scrollArea->setMinimumSize(QSize(1,1));
151 setMinimumSize(mFlowlayout->minimumSize());
152
153 QButtonGroup* buttonGroup = new QButtonGroup(this);
154 buttonGroup->addButton(ui->pencilButton);
155 buttonGroup->addButton(ui->eraserButton);
156 buttonGroup->addButton(ui->selectButton);
157 buttonGroup->addButton(ui->moveButton);
158 buttonGroup->addButton(ui->penButton);
159 buttonGroup->addButton(ui->handButton);
160 buttonGroup->addButton(ui->polylineButton);
161 buttonGroup->addButton(ui->bucketButton);
162 buttonGroup->addButton(ui->eyedropperButton);
163 buttonGroup->addButton(ui->brushButton);
164 buttonGroup->addButton(ui->smudgeButton);
165}
166
167int ToolBoxWidget::getMinHeightForWidth(int width) const
168{
169 return mFlowlayout->heightForWidth(width);
170}
171
172QSize ToolBoxWidget::sizeHint() const
173{
174 return minimumSizeHint();
175}
176
177QSize ToolBoxWidget::minimumSizeHint() const
178{
179 int minWidth = mFlowlayout->minimumSize().width();
180 return QSize(minWidth, getMinHeightForWidth(width()));
181}
182
183void ToolBoxWidget::resizeEvent(QResizeEvent *event)
184{
185 QWidget::resizeEvent(event);
186
187 updateLayoutAlignment();
188}
189
190void ToolBoxWidget::updateLayoutAlignment()
191{
192 mFlowlayout->invalidate();
193 if (mFlowlayout->rows() > 1) {
194 mFlowlayout->setAlignment(Qt::AlignJustify);
195 } else {
196 mFlowlayout->setAlignment(Qt::AlignHCenter);
197 }
198
199 mFlowlayout->activate();
200}
201
202void ToolBoxWidget::updateUI()
203{
204}
205
206void ToolBoxWidget::setActiveTool(ToolType toolType)
207{
208 switch (toolType) {
209 case ToolType::BRUSH:
210 brushOn();
211 break;
212 case ToolType::PEN:
213 penOn();
214 break;
215 case ToolType::PENCIL:
216 pencilOn();
217 break;
218 case ToolType::SELECT:
219 selectOn();
220 break;
221 case ToolType::HAND:
222 handOn();
223 break;
224 case ToolType::MOVE:
225 case ToolType::CAMERA:
226 moveOn();
227 break;
228 case ToolType::ERASER:
229 eraserOn();
230 break;
231 case ToolType::POLYLINE:
232 polylineOn();
233 break;
234 case ToolType::SMUDGE:
235 smudgeOn();
236 break;
237 case ToolType::BUCKET:
238 bucketOn();
239 break;
240 case ToolType::EYEDROPPER:
241 eyedropperOn();
242 break;
243 default:
244 break;
245 }
246}
247
248void ToolBoxWidget::pencilOn()
249{
250 toolOn(PENCIL, ui->pencilButton);
251}
252
253void ToolBoxWidget::eraserOn()
254{
255 toolOn(ERASER, ui->eraserButton);
256}
257
258void ToolBoxWidget::selectOn()
259{
260 toolOn(SELECT, ui->selectButton);
261}
262
263void ToolBoxWidget::moveOn()
264{
265 if (mEditor->layers()->currentLayer()->type() == Layer::CAMERA) {
266 toolOn(CAMERA, ui->moveButton);
267 } else {
268 toolOn(MOVE, ui->moveButton);
269 }
270}
271
272void ToolBoxWidget::penOn()
273{
274 toolOn(PEN, ui->penButton);
275}
276
277void ToolBoxWidget::handOn()
278{
279 toolOn(HAND, ui->handButton);
280}
281
282void ToolBoxWidget::polylineOn()
283{
284 toolOn(POLYLINE, ui->polylineButton);
285}
286
287void ToolBoxWidget::bucketOn()
288{
289 toolOn(BUCKET, ui->bucketButton);
290}
291
292void ToolBoxWidget::eyedropperOn()
293{
294 toolOn(EYEDROPPER, ui->eyedropperButton);
295}
296
297void ToolBoxWidget::brushOn()
298{
299 toolOn(BRUSH, ui->brushButton);
300}
301
302void ToolBoxWidget::smudgeOn()
303{
304 toolOn(SMUDGE, ui->smudgeButton);
305}
306
307void ToolBoxWidget::deselectAllTools()
308{
309 ui->pencilButton->setChecked(false);
310 ui->eraserButton->setChecked(false);
311 ui->selectButton->setChecked(false);
312 ui->moveButton->setChecked(false);
313 ui->handButton->setChecked(false);
314 ui->penButton->setChecked(false);
315 ui->polylineButton->setChecked(false);
316 ui->bucketButton->setChecked(false);
317 ui->eyedropperButton->setChecked(false);
318 ui->brushButton->setChecked(false);
319 ui->smudgeButton->setChecked(false);
320}
321
322void ToolBoxWidget::toolOn(ToolType toolType, QToolButton* toolButton)
323{
324 if (mEditor->tools()->currentTool()->type() == toolType) {
325 // Prevent un-checking the current tool and do nothing
326 toolButton->setChecked(true);
327 return;
328 }
329 if (!mEditor->tools()->leavingThisTool())
330 {
331 toolButton->setChecked(false);
332 return;
333 }
334 mEditor->tools()->setCurrentTool(toolType);
335}
ToolBoxLayout
Definition: toolboxlayout.h:24
ToolBoxWidget
Definition: toolboxwidget.h:36
QAbstractButton::setChecked
void setChecked(bool)
QAbstractButton::clicked
void clicked(bool checked)
QButtonGroup
QButtonGroup::addButton
void addButton(QAbstractButton *button, int id)
QKeySequence
QLayout::activate
bool activate()
QLayout::addWidget
void addWidget(QWidget *w)
QLayout::invalidate
virtual void invalidate() override
QLayout::setAlignment
bool setAlignment(QWidget *w, Qt::Alignment alignment)
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
QSize
QSize::width
int width() const const
QString
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Qt::AlignJustify
AlignJustify
QTest::keySequence
void keySequence(QWindow *window, const QKeySequence &keySequence)
QTest::toString
char * toString(const T &value)
QToolButton
QWidget
QWidget::event
virtual bool event(QEvent *event) override
QWidget::setMinimumSize
void setMinimumSize(const QSize &)
QWidget::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
QWidget::setupUi
void setupUi(QWidget *widget)
QWidget::width
width
Generated on Thu Jun 5 2025 14:06:43 for Pencil2D by doxygen 1.9.6 based on revision 4c63407997b2c03e5048716586dec6fbbb755173