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;