Files
qelectrotech-source-mirror/sources/diagramevent/diagrameventaddtext.cpp
T
ispyisail 3c1fb2fe72 Let Escape cancel the text tool, like every other placement (#903)
Issue #903 reported that Escape stopped cancelling an in-progress
placement. PR #899 fixed that by letting Escape through to the active
tool whenever Diagram::eventInterfaceIsRunning(), and 863ac5f0a tightened
it to isRunning() so a second Escape cannot retrigger an abort already in
flight. That covers seven of the eight tools. It cannot cover the eighth.

eventInterfaceIsRunning() is m_event_interface && isRunning(), and
isRunning() returns m_running. DiagramEventAddText is the only class
under sources/diagramevent/ that never sets m_running, so the guard reads
false for the whole time the tool is armed and DiagramView::keyPressEvent
keeps swallowing Escape for its own selection/focus handling.

It is also the only one of the eight with no RightButton branch --
right-clicking the folio with the text tool armed opens the folio context
menu. So this tool currently has no way to cancel at all: the only way
out is to pick a different tool, and any stray click drops a text field
the user did not want.

The tool is armed from the moment it is attached, so m_running is set in
the constructor and cleared where the text is placed, before finish().

Measured on a virtual display against a fixture holding one free text
field, counting diagram-level text fields in the saved project:

  arm the text tool, click            2 fields   places, as it should
  arm it, press Escape, then click    2 fields   before  -- not cancelled
                                      1 field    after   -- cancelled

The same run on master's rectangle tool cancels correctly, which is what
made the text tool look fixed when it was not.

ctest 12/12, Qt 6.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-19 08:32:58 +12:00

68 lines
2.1 KiB
C++

/*
Copyright 2006-2026 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "diagrameventaddtext.h"
#include "../diagram.h"
#include "../undocommand/addgraphicsobjectcommand.h"
#include "../qetgraphicsitem/independenttextitem.h"
/**
@brief DiagramEventAddText::DiagramEventAddText
Default constructor
@param diagram : the diagram where this event must operate
*/
DiagramEventAddText::DiagramEventAddText(Diagram *diagram) :
DiagramEventInterface(diagram)
{
//The tool is armed from the moment it is attached: the next left
//click places a text. DiagramView::keyPressEvent() asks
//Diagram::eventInterfaceIsRunning() -- which is isRunning(), i.e.
//m_running -- before letting Escape through to the tool, so without
//this the view swallows Escape and the tool cannot be cancelled.
m_running = true;
}
/**
@brief DiagramEventAddText::~DiagramEventAddText
*/
DiagramEventAddText::~DiagramEventAddText()
{}
/**
@brief DiagramEventAddText::mousePressEvent
@param event : event of mouse press event.
*/
void DiagramEventAddText::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
IndependentTextItem *text = new IndependentTextItem();
m_diagram->undoStack().push(new AddGraphicsObjectCommand(
text,
m_diagram,
event->scenePos()));
text->setTextInteractionFlags(Qt::TextEditorInteraction);
text->setFocus(Qt::MouseFocusReason);
//Placed: the tool is done before it announces it.
m_running = false;
emit finish();
event->setAccepted(true);
}
}