Merge pull request #1219 from ispyisail/feature/script-current-folio-undo

Make a script run one undo step, and let it find the folio on screen
This commit is contained in:
ispyisail
2026-10-02 15:11:03 +13:00
committed by GitHub
6 changed files with 251 additions and 2 deletions
+71
View File
@@ -116,6 +116,57 @@ 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.
*/
/**
@brief QetScriptApi::apiSignatures
Every call a script can make, as "returnType name(type param, ...)",
read from the meta-object rather than written out by hand, so the list
is the one this build has and cannot drift from it: for a person
writing a script, and for an assistant that has to write one without
the source at hand. A call with default arguments is listed once, with
all of them.
*/
QStringList QetScriptApi::apiSignatures() const
{
QStringList list;
const QMetaObject *meta = metaObject();
for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) {
const QMetaMethod method = meta->method(i);
if (method.methodType() != QMetaMethod::Method
|| method.access() != QMetaMethod::Public
|| (method.attributes() & QMetaMethod::Cloned)) {
continue;
}
const QList<QByteArray> types = method.parameterTypes();
const QList<QByteArray> names = method.parameterNames();
QStringList params;
for (int p = 0; p < types.size(); ++p) {
params << QString::fromLatin1(types.at(p) + ' ' + names.value(p));
}
list << QStringLiteral("%1 %2(%3)")
.arg(QString::fromLatin1(method.typeName()),
QString::fromLatin1(method.name()),
params.join(QStringLiteral(", ")));
}
return list;
}
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();
@@ -3938,8 +3989,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;
@@ -3947,6 +4014,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;