Skip to content

Commit 3dd851e

Browse files
committed
Add ESC key handling to cancel shape drawing
1 parent 0993d9a commit 3dd851e

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/tiled/shapefilltool.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828

2929
#include <QActionGroup>
3030
#include <QApplication>
31+
#include <QKeyEvent>
3132
#include <QToolBar>
3233
#include <QUndoStack>
3334

34-
#include <memory>
35-
3635
using namespace Tiled;
3736

3837
ShapeFillTool::ShapeFillTool(QObject *parent)
@@ -86,9 +85,7 @@ void ShapeFillTool::mousePressed(QGraphicsSceneMouseEvent *event)
8685
{
8786
// Right-click cancels drawing a shape
8887
if (mToolBehavior == MakingShape && event->button() == Qt::RightButton) {
89-
mToolBehavior = Free;
90-
clearOverlay();
91-
updateStatusInfo();
88+
cancelMakingShape();
9289
return;
9390
}
9491

@@ -139,6 +136,18 @@ void ShapeFillTool::modifiersChanged(Qt::KeyboardModifiers modifiers)
139136
updateFillOverlay();
140137
}
141138

139+
void ShapeFillTool::keyPressed(QKeyEvent *event)
140+
{
141+
if (event->key() == Qt::Key_Escape) {
142+
if (mToolBehavior == MakingShape) {
143+
cancelMakingShape();
144+
return;
145+
}
146+
}
147+
148+
AbstractTileFillTool::keyPressed(event);
149+
}
150+
142151
void ShapeFillTool::languageChanged()
143152
{
144153
setName(tr("Shape Fill Tool"));
@@ -153,7 +162,7 @@ void ShapeFillTool::populateToolBar(QToolBar *toolBar)
153162
{
154163
AbstractTileFillTool::populateToolBar(toolBar);
155164

156-
QActionGroup *actionGroup = new QActionGroup(toolBar);
165+
auto *actionGroup = new QActionGroup(toolBar);
157166
actionGroup->addAction(mRectFill);
158167
actionGroup->addAction(mCircleFill);
159168

@@ -194,6 +203,13 @@ void ShapeFillTool::setCurrentShape(Shape shape)
194203
mCurrentShape = shape;
195204
}
196205

206+
void ShapeFillTool::cancelMakingShape()
207+
{
208+
mToolBehavior = Free;
209+
clearOverlay();
210+
updateStatusInfo();
211+
}
212+
197213
void ShapeFillTool::updateFillOverlay()
198214
{
199215
int dx = tilePosition().x() - mStartCorner.x();

src/tiled/shapefilltool.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class ShapeFillTool : public AbstractTileFillTool
4141

4242
void modifiersChanged(Qt::KeyboardModifiers) override;
4343

44+
void keyPressed(QKeyEvent *event) override;
45+
4446
void languageChanged() override;
4547

4648
void populateToolBar(QToolBar *toolBar) override;
@@ -73,6 +75,7 @@ class ShapeFillTool : public AbstractTileFillTool
7375
void setActionsEnabled(bool enabled);
7476

7577
void setCurrentShape(Shape shape);
78+
void cancelMakingShape();
7679
void updateFillOverlay();
7780
};
7881

0 commit comments

Comments
 (0)