Add undo/redo support for folio add, delete, and reorder (#575)

Three new QUndoCommand subclasses (AddDiagramCommand, RemoveDiagramCommand,
MoveDiagramCommand) pushed onto the project's existing (already
project-scoped) undo stack, so folio structure edits are undoable
alongside every item-level edit already on that stack.

- QETProject::addDiagram()/detachDiagram() are the shared attach/detach
  primitives: they mutate the diagram list, connect/disconnect the two
  per-diagram signals set up at add time, and emit diagramAdded/
  diagramRemoved. AddDiagramCommand and RemoveDiagramCommand call these
  (via friend access) for both redo and undo, so a removed diagram is
  parked rather than destroyed -- it's only actually deleted if the
  command itself falls out of undo history while still detached.
- ProjectView reacts to diagramRemoved the same way it already reacted to
  diagramAdded (tearing down/rebuilding the tab), so both directions of
  both commands go through the same reactive path every other diagram
  listener (project database, cross-references, generic panel) already
  relies on.
- MoveDiagramCommand wraps a new ProjectView::setDiagramPosition(), which
  performs the tab move and the project's diagramOrderChanged() list
  reorder synchronously in one step, instead of relying on the queued
  tabMoved connection (needed for interactive drag-and-drop) to catch up
  later -- avoiding a second, redundant reorder from that queued call.
- Multi-folio delete and multi-folio move (QETDiagramEditor::removeDiagrams()
  and the moveDiagram*(QList<Diagram*>) batch slots) wrap their per-diagram
  loop in QUndoStack::beginMacro()/endMacro(), so a multi-select action is
  one undo step, matching current UX.
- Softened the delete confirmation's "this change is irreversible" wording
  now that it no longer is.

Verified headlessly (Xvfb + xdotool + scrot): add/undo/redo, delete/undo/
redo (single and multi-select, single undo step for the batch), and
move/undo/redo all behave correctly against a 7-folio project.
This commit is contained in:
ispyisail
2026-08-01 17:03:39 +12:00
parent c772e1d3ea
commit 55886e5e8a
12 changed files with 499 additions and 25 deletions
+68
View File
@@ -0,0 +1,68 @@
/*
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 "adddiagramcommand.h"
#include "../qetproject.h"
#include "../diagram.h"
/**
@brief AddDiagramCommand::AddDiagramCommand
@param project : project the diagram is added to
@param diagram : the already constructed diagram to add (not yet part of any project's diagram list)
@param pos : position to insert the diagram at, -1 to append at the end
@param parent
*/
AddDiagramCommand::AddDiagramCommand(QETProject *project, Diagram *diagram, int pos, QUndoCommand *parent) :
QUndoCommand(parent),
m_project(project),
m_diagram(diagram),
m_position(pos)
{
setText(QObject::tr("Ajouter un folio", "undo command text"));
}
/**
@brief AddDiagramCommand::~AddDiagramCommand
Destroy the diagram only if it is currently detached from the project
(i.e. this add was undone) -- otherwise the project owns it and will
destroy it itself.
*/
AddDiagramCommand::~AddDiagramCommand()
{
if (m_owns_diagram && m_diagram) {
m_diagram->deleteLater();
}
}
void AddDiagramCommand::redo()
{
if (!m_project || !m_diagram) {
return;
}
m_project->addDiagram(m_diagram, m_position);
m_owns_diagram = false;
}
void AddDiagramCommand::undo()
{
if (!m_project || !m_diagram) {
return;
}
m_project->detachDiagram(m_diagram);
m_owns_diagram = true;
}
+52
View File
@@ -0,0 +1,52 @@
/*
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/>.
*/
#ifndef ADDDIAGRAMCOMMAND_H
#define ADDDIAGRAMCOMMAND_H
#include <QUndoCommand>
#include <QPointer>
class QETProject;
class Diagram;
/**
@brief The AddDiagramCommand class
Undo/redo support for adding a new folio (diagram) to a project.
Takes ownership of the (already constructed, not yet inserted) diagram:
redo() inserts it into the project, undo() detaches it again without
destroying it. The diagram is only actually destroyed when this command
itself is destroyed while the diagram is detached (i.e. while undone).
*/
class AddDiagramCommand : public QUndoCommand
{
public:
AddDiagramCommand(QETProject *project, Diagram *diagram, int pos = -1, QUndoCommand *parent = nullptr);
~AddDiagramCommand() override;
void undo() override;
void redo() override;
private:
QPointer<QETProject> m_project;
Diagram *m_diagram;
int m_position;
/// true while the diagram is detached from the project (not currently in its diagram list)
bool m_owns_diagram = true;
};
#endif // ADDDIAGRAMCOMMAND_H
@@ -0,0 +1,56 @@
/*
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 "movediagramcommand.h"
#include "../projectview.h"
#include "../qetproject.h"
/**
@brief MoveDiagramCommand::MoveDiagramCommand
@param project_view : project view the diagram's tab belongs to
@param diagram : the diagram to move
@param new_position : target position (may be out of range, e.g. for a
x10/x100 move near either end -- ProjectView::setDiagramPosition() clamps
it the same way the underlying tab bar always has)
@param parent
*/
MoveDiagramCommand::MoveDiagramCommand(ProjectView *project_view, Diagram *diagram, int new_position, QUndoCommand *parent) :
QUndoCommand(parent),
m_project_view(project_view),
m_diagram(diagram),
m_old_position(project_view ? project_view->project()->folioIndex(diagram) : -1),
m_new_position(new_position)
{
setText(QObject::tr("Déplacer un folio", "undo command text"));
}
void MoveDiagramCommand::redo()
{
if (!m_project_view) {
return;
}
m_project_view->setDiagramPosition(m_diagram, m_new_position);
}
void MoveDiagramCommand::undo()
{
if (!m_project_view) {
return;
}
m_project_view->setDiagramPosition(m_diagram, m_old_position);
}
+49
View File
@@ -0,0 +1,49 @@
/*
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/>.
*/
#ifndef MOVEDIAGRAMCOMMAND_H
#define MOVEDIAGRAMCOMMAND_H
#include <QUndoCommand>
#include <QPointer>
class ProjectView;
class Diagram;
/**
@brief The MoveDiagramCommand class
Undo/redo support for reordering a folio (diagram) within a project's
tabs. Wraps ProjectView::setDiagramPosition(), which performs the tab
move and the matching (synchronous) project diagram-list reorder in one
step.
*/
class MoveDiagramCommand : public QUndoCommand
{
public:
MoveDiagramCommand(ProjectView *project_view, Diagram *diagram, int new_position, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
private:
QPointer<ProjectView> m_project_view;
Diagram *m_diagram;
int m_old_position;
int m_new_position;
};
#endif // MOVEDIAGRAMCOMMAND_H
@@ -0,0 +1,67 @@
/*
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 "removediagramcommand.h"
#include "../qetproject.h"
#include "../diagram.h"
/**
@brief RemoveDiagramCommand::RemoveDiagramCommand
@param project : project the diagram is removed from
@param diagram : the diagram to remove, currently part of \p project
@param parent
*/
RemoveDiagramCommand::RemoveDiagramCommand(QETProject *project, Diagram *diagram, QUndoCommand *parent) :
QUndoCommand(parent),
m_project(project),
m_diagram(diagram),
m_position(project ? project->folioIndex(diagram) : -1)
{
setText(QObject::tr("Supprimer un folio", "undo command text"));
}
/**
@brief RemoveDiagramCommand::~RemoveDiagramCommand
Destroy the diagram only if it is currently detached from the project
(i.e. the removal is currently in effect) -- otherwise the project owns
it and will destroy it itself.
*/
RemoveDiagramCommand::~RemoveDiagramCommand()
{
if (m_owns_diagram && m_diagram) {
m_diagram->deleteLater();
}
}
void RemoveDiagramCommand::redo()
{
if (!m_project || !m_diagram) {
return;
}
m_project->detachDiagram(m_diagram);
m_owns_diagram = true;
}
void RemoveDiagramCommand::undo()
{
if (!m_project || !m_diagram) {
return;
}
m_project->addDiagram(m_diagram, m_position);
m_owns_diagram = false;
}
@@ -0,0 +1,53 @@
/*
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/>.
*/
#ifndef REMOVEDIAGRAMCOMMAND_H
#define REMOVEDIAGRAMCOMMAND_H
#include <QUndoCommand>
#include <QPointer>
class QETProject;
class Diagram;
/**
@brief The RemoveDiagramCommand class
Undo/redo support for removing a folio (diagram) from a project.
redo() detaches the diagram from the project (and, reactively, its
ProjectView tab) without destroying it; undo() reinserts it at its
original position. The diagram is only actually destroyed when this
command itself is destroyed while the diagram is detached (i.e. while
the removal is still in effect).
*/
class RemoveDiagramCommand : public QUndoCommand
{
public:
RemoveDiagramCommand(QETProject *project, Diagram *diagram, QUndoCommand *parent = nullptr);
~RemoveDiagramCommand() override;
void undo() override;
void redo() override;
private:
QPointer<QETProject> m_project;
Diagram *m_diagram;
int m_position;
/// true while the diagram is detached from the project (i.e. the removal is in effect)
bool m_owns_diagram = false;
};
#endif // REMOVEDIAGRAMCOMMAND_H