Take the modal dialog out of RotateTextsCommand's constructor

RotateTextsCommand called QDialog::exec() from inside its constructor, so
the command could not be built without a human answering a dialog. That
made it untestable headlessly, undrivable from any script or test harness,
and it is why bugtracker #312 (PR #707) shipped with its save/reload
round-trip unverified -- the symptom could not be reproduced without a GUI.

The command now takes the angle as a parameter and does no asking. Two
statics carry the interactive half:

  hasSelectedTexts(diagram)  -- is there anything to rotate
  askRotation(rotation)      -- open the dialog, false if cancelled

The single call site in QETDiagramEditor asks first, then builds the
command, so the user-visible behaviour is unchanged: same dialog, same
title, same no-dialog-on-empty-selection. Keeping askRotation() in this
class also keeps the QObject tr() context, so existing translations of
"Orienter les textes sélectionnés" are not invalidated.

Also guards undo()/redo() against a null m_anim_group. When nothing is
selected the constructor calls setObsolete(true) without ever creating the
animation group, and QUndoStack::push() calls redo() before discarding an
obsolete command -- a latent null dereference on that path.

Verified headlessly, which was the point: driving the command through a
scratch --test-ops op on examples/741.qet (67 conductors), rotation
attributes written on save go 0 -> 67 with the #707 fix present and stay
at 0 with it reverted, while the reverted build instead writes userx on
all 67. That is bugtracker #312 reproduced and fixed under test for the
first time.
This commit is contained in:
ispyisail
2026-08-16 10:08:16 +12:00
parent ddfa234943
commit 8ab4e24b61
3 changed files with 85 additions and 17 deletions
+50 -12
View File
@@ -25,15 +25,34 @@
#include "../qetgraphicsitem/elementtextitemgroup.h"
#include "../qtextorientationspinboxwidget.h"
/**
@brief RotateTextsCommand::hasSelectedTexts
@param diagram
@return true if @p diagram has at least one selected text or text group.
Lets a caller decide whether to ask the user for an angle at all, keeping
the previous behaviour where no dialog appeared for an empty selection.
*/
bool RotateTextsCommand::hasSelectedTexts(Diagram *diagram)
{
if(!diagram)
return false;
DiagramContent dc(diagram);
return (!dc.selectedTexts().isEmpty() || !dc.selectedTextsGroup().isEmpty());
}
/**
@brief RotateTextsCommand::RotateTextsCommand
@param diagram : Apply the rotation to the selected texts and group of texts
of diagram at construction time.
of diagram at construction time.
@param rotation : the angle to apply, in degrees. Obtain it from
askRotation() for interactive use; pass it directly from a test or script.
@param parent : undo parent
*/
RotateTextsCommand::RotateTextsCommand(Diagram *diagram, QUndoCommand *parent) :
RotateTextsCommand::RotateTextsCommand(Diagram *diagram, qreal rotation, QUndoCommand *parent) :
QUndoCommand(parent),
m_diagram(diagram)
m_diagram(diagram),
m_rotation(rotation)
{
DiagramContent dc(m_diagram);
QList <DiagramTextItem *> texts_list;
@@ -53,8 +72,6 @@ m_diagram(diagram)
if(texts_list.count() || groups_list.count())
{
openDialog();
QStringList parts;
if (texts_list.count())
parts << QObject::tr("%n texte(s)", "", texts_list.count());
@@ -76,9 +93,15 @@ void RotateTextsCommand::undo()
if(m_diagram)
m_diagram.data()->showMe();
//Nothing was selected at construction time: there is no animation to
//run. QUndoStack::push() calls redo() before it discards an obsolete
//command, so this has to be survivable rather than assumed away.
if(!m_anim_group)
return;
m_anim_group->setDirection(QAnimationGroup::Backward);
m_anim_group->start();
for(ConductorTextItem *cti : m_cond_texts.keys())
cti->forceRotateByUser(m_cond_texts.value(cti));
}
@@ -88,14 +111,28 @@ void RotateTextsCommand::redo()
if(m_diagram)
m_diagram.data()->showMe();
if(!m_anim_group)
return;
m_anim_group->setDirection(QAnimationGroup::Forward);
m_anim_group->start();
for(ConductorTextItem *cti : m_cond_texts.keys())
cti->forceRotateByUser(true);
}
void RotateTextsCommand::openDialog()
/**
@brief RotateTextsCommand::askRotation
Ask the user for an orientation.
@param rotation : set to the chosen angle when the dialog is accepted,
left untouched otherwise.
@return true if the user accepted, false if they cancelled.
Deliberately static and separate from the command: a QUndoCommand that
blocks on a modal in its constructor cannot be built by a test, a script,
or any headless caller.
*/
bool RotateTextsCommand::askRotation(qreal &rotation)
{
//Open the dialog
QDialog ori_text_dialog;
@@ -120,10 +157,11 @@ void RotateTextsCommand::openDialog()
layout_v.addStretch();
layout_v.addWidget(&buttons);
if (ori_text_dialog.exec() == QDialog::Accepted)
m_rotation = ori_widget->orientation();
else
setObsolete(true);
if (ori_text_dialog.exec() != QDialog::Accepted)
return false;
rotation = ori_widget->orientation();
return true;
}
void RotateTextsCommand::setupAnimation(QObject *target, const QByteArray &propertyName, const QVariant& start, const QVariant& end)