Scripting: qet.currentFolio(), and one undo step per script run in the editor

A script started from the editor had no way to know which folio is on
screen, and each qet.* call was its own undo step, so a script that adds
twenty items needed twenty Ctrl+Z to take back.

qet.currentFolio() returns the folio shown in the editor; through --run,
which has no view, the first folio, or -1 when there is none.

A run from the editor is now one undo macro, named after the script. A
run that changed nothing leaves no empty entry behind. qet.undo() and
qet.redo() inside a grouped run say why they cannot work (QUndoStack
ignores them inside a macro) instead of failing silently. --run is
unchanged: one step per call, so scripts that call qet.undo() keep
working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ispyisail
2026-10-02 08:47:23 +13:00
parent b6495b51a6
commit 1c1f9db3eb
6 changed files with 192 additions and 2 deletions
+37
View File
@@ -115,6 +115,23 @@ int QetScriptApi::folioCount() const
return m_project ? m_project->diagrams().count() : 0;
}
/**
@brief QetScriptApi::currentFolio
The index of the folio on screen, so a script started from the editor
acts where the user is looking. With no view (--run) there is no such
folio, and the first one stands in for it so a script written for the
editor can still be tried headless; -1 if the project has none.
*/
int QetScriptApi::currentFolio() const
{
if (!m_project) return -1;
const QList<Diagram *> diagrams = m_project->diagrams();
if (m_view && m_view->diagram()) {
return diagrams.indexOf(m_view->diagram());
}
return diagrams.isEmpty() ? -1 : 0;
}
QString QetScriptApi::folioTitle(int index) const
{
if (!m_project) return QString();
@@ -3854,8 +3871,24 @@ bool QetScriptApi::setFolioTitle(int folioIndex, const QString &title)
return true;
}
/**
@brief QetScriptApi::setUndoGrouped
Set by QetScripting::runOnProject() while the whole run is one undo
macro. QUndoStack cannot undo or redo inside a macro: it prints a
warning and does nothing, so undo() and redo() say so instead.
*/
void QetScriptApi::setUndoGrouped(bool grouped)
{
m_undo_grouped = grouped;
}
bool QetScriptApi::undo()
{
if (m_undo_grouped) {
log(QStringLiteral("qet.undo: not available here -- this run is one "
"undo step; press Ctrl+Z after it to undo it"));
return false;
}
if (!m_project || !m_project->undoStack()->canUndo()) return false;
m_project->undoStack()->undo();
return true;
@@ -3863,6 +3896,10 @@ bool QetScriptApi::undo()
bool QetScriptApi::redo()
{
if (m_undo_grouped) {
log(QStringLiteral("qet.redo: not available while this run is one undo step"));
return false;
}
if (!m_project || !m_project->undoStack()->canRedo()) return false;
m_project->undoStack()->redo();
return true;
+4
View File
@@ -354,6 +354,7 @@ class QetScriptApi : public QObject
Q_INVOKABLE QString projectTitle() const;
Q_INVOKABLE QString filePath() const;
Q_INVOKABLE int folioCount() const;
Q_INVOKABLE int currentFolio() const;
Q_INVOKABLE QString folioTitle(int index) const;
Q_INVOKABLE QString folioUuid(int index) const;
Q_INVOKABLE int folioIndex(const QString &uuid) const;
@@ -578,6 +579,8 @@ class QetScriptApi : public QObject
// -- logging: a script has no console of its own --
Q_INVOKABLE void log(const QString &message);
void setUndoGrouped(bool grouped);
private:
bool runFlag(const QString &flag, const QStringList &args);
Element *findElement(int folioIndex, const QString &elementUuid) const;
@@ -597,6 +600,7 @@ class QetScriptApi : public QObject
QETProject *m_project;
DiagramView *m_view;
bool m_undo_grouped = false;
QString m_query_error;
};
+27
View File
@@ -26,6 +26,7 @@
#include <QFileInfo>
#include <QObject>
#include <QTextStream>
#include <QUndoStack>
#ifdef QET_HAS_SCRIPTING
#include <QJSEngine>
@@ -162,6 +163,20 @@ bool runOnProject(const QString &scriptPath, QETProject *project, DiagramView *v
}
});
// From the editor, a run is one undo step: a script that places
// twenty items is undone with one Ctrl+Z, not twenty. Headless --run
// keeps one step per call, which is what a script calling qet.undo()
// itself relies on. QUndoStack keeps an empty macro as a blank entry
// (and marks the project modified), so a script that changed nothing
// has its macro taken off again: an obsolete command is deleted by
// QUndoStack::undo() instead of being undone onto the redo side.
QUndoStack *stack = (view && project) ? project->undoStack() : nullptr;
if (stack) {
stack->beginMacro(QObject::tr("Script : %1")
.arg(QFileInfo(scriptPath).completeBaseName()));
api->setUndoGrouped(true);
}
QJSValue result = engine.evaluate(source, scriptPath);
{
std::lock_guard<std::mutex> lock(mtx);
@@ -170,6 +185,18 @@ bool runOnProject(const QString &scriptPath, QETProject *project, DiagramView *v
cv.notify_one();
watchdog.join();
if (stack) {
api->setUndoGrouped(false);
stack->endMacro();
// Always the top of the undo side once the macro is closed, even
// when beginMacro() dropped a redo tail and count() went down.
const QUndoCommand *macro = stack->command(stack->index() - 1);
if (macro && macro->childCount() == 0) {
const_cast<QUndoCommand *>(macro)->setObsolete(true);
stack->undo();
}
}
if (result.isError()) {
const QString message = QStringLiteral("Script error: %1:%2: %3")
.arg(scriptPath)
+3 -2
View File
@@ -51,8 +51,9 @@ namespace QetScripting {
/**
@brief Run @p scriptPath against an already-open @p project (the
"Run Script..." GUI macro path). Errors go to stderr; there is no
modal reporting in this first version.
"Run Script..." GUI macro path). Errors go to stderr, and with a
@p view also to a message box. With a @p view the whole run is one
undo step; without one, each call is its own step, as on --run.
@param view the active DiagramView, so the script's zoom methods
have something to act on; nullptr from the headless entry point,
where they become no-ops (see QetScriptApi).