diff --git a/sources/scripting/qetscriptapi.cpp b/sources/scripting/qetscriptapi.cpp index 7475d4669..1fdc9acf9 100644 --- a/sources/scripting/qetscriptapi.cpp +++ b/sources/scripting/qetscriptapi.cpp @@ -33,7 +33,12 @@ #include "../qetgraphicsitem/conductor.h" #include "../qetgraphicsitem/independenttextitem.h" #include "../qetgraphicsitem/qetshapeitem.h" +#include "../TerminalStrip/UndoCommand/addterminalstripcommand.h" +#include "../TerminalStrip/UndoCommand/addterminaltostripcommand.h" +#include "../TerminalStrip/realterminal.h" +#include "../TerminalStrip/terminalstrip.h" #include "../qetgraphicsitem/terminal.h" +#include "../qetgraphicsitem/terminalelement.h" #include "../qetinformation.h" #include "../titleblockproperties.h" #include "../undocommand/addgraphicsobjectcommand.h" @@ -1368,6 +1373,86 @@ bool QetScriptApi::setFolioProperty(int folioIndex, const QString &property, con return true; } +QStringList QetScriptApi::terminalStrips() const +{ + QStringList list; + if (!m_project) return list; + const QVector strips = m_project->terminalStrip(); + for (int i = 0 ; i < strips.count() ; ++i) + { + TerminalStrip *t = strips.at(i); + list << QStringLiteral("%1: installation='%2' location='%3' name='%4' (%5 terminal(s))") + .arg(i).arg(t->installation(), t->location(), t->name()) + .arg(t->realTerminals().count()); + } + return list; +} + +/** + @brief QetScriptApi::addTerminalStrip + Create an empty terminal strip through AddTerminalStripCommand, as the + editor's creation dialog does after it is accepted. + @return its index in terminalStrips(), or -1 +*/ +int QetScriptApi::addTerminalStrip(const QString &installation, const QString &location, + const QString &name) +{ + if (!m_project) return -1; + if (m_project->isReadOnly()) { + log(QStringLiteral("qet.addTerminalStrip: project is read-only")); + return -1; + } + auto *strip = new TerminalStrip(installation, location, name, m_project); + m_project->undoStack()->push(new AddTerminalStripCommand(strip, m_project)); + return m_project->terminalStrip().indexOf(strip); +} + +bool QetScriptApi::removeTerminalStrip(int stripIndex) +{ + if (!m_project) return false; + if (m_project->isReadOnly()) { + log(QStringLiteral("qet.removeTerminalStrip: project is read-only")); + return false; + } + const QVector strips = m_project->terminalStrip(); + if (stripIndex < 0 || stripIndex >= strips.count()) { + log(QStringLiteral("qet.removeTerminalStrip: no strip at index %1").arg(stripIndex)); + return false; + } + m_project->undoStack()->push(new RemoveTerminalStripCommand(strips.at(stripIndex), m_project)); + return m_project->terminalStrip().count() == strips.count() - 1; +} + +bool QetScriptApi::addTerminalToStrip(int stripIndex, int folioIndex, const QString &elementUuid) +{ + if (!m_project) return false; + const QString caller = QStringLiteral("addTerminalToStrip"); + if (m_project->isReadOnly()) { + log(QStringLiteral("qet.%1: project is read-only").arg(caller)); + return false; + } + const QVector strips = m_project->terminalStrip(); + if (stripIndex < 0 || stripIndex >= strips.count()) { + log(QStringLiteral("qet.%1: no strip at index %2").arg(caller).arg(stripIndex)); + return false; + } + Element *element = findElement(folioIndex, elementUuid); + if (!element) return false; + auto *terminal_element = qobject_cast(element); + if (!terminal_element) { + log(QStringLiteral("qet.%1: %2 is not a terminal-type element").arg(caller, elementUuid)); + return false; + } + QSharedPointer real = terminal_element->realTerminal(); + if (!real) return false; + if (real->parentStrip()) { + log(QStringLiteral("qet.%1: %2 already belongs to a strip").arg(caller, elementUuid)); + return false; + } + m_project->undoStack()->push(new AddTerminalToStripCommand(real, strips.at(stripIndex))); + return real->parentStrip() == strips.at(stripIndex); +} + int QetScriptApi::addFolio() { if (!m_project) return -1; diff --git a/sources/scripting/qetscriptapi.h b/sources/scripting/qetscriptapi.h index d61c7b945..72914d7c5 100644 --- a/sources/scripting/qetscriptapi.h +++ b/sources/scripting/qetscriptapi.h @@ -150,6 +150,12 @@ class QetShapeItem; depend on. The underlying tables are how the cache happens to be arranged today, and a column may move. tables() lists both so a script can see what it is querying rather than guess. + - @b Terminal @b strips: create a strip, put terminal-type elements on + it, remove it. Strips are addressed by index into terminalStrips(), + which is the project's own order (unlike texts and shapes it is not + re-sorted) and does shift when one is removed. Only elements whose + link type is "terminal" can be added, the same restriction the + editor enforces by construction. - @b Navigating and @b messaging: select an element, zoom the active view, and show the user a message. Deliberately narrow: selection and messaging work with no view at all (headless `--run`); zoom is a no-op @@ -269,6 +275,14 @@ class QetScriptApi : public QObject Q_INVOKABLE bool setFolioProperty(int folioIndex, const QString &property, const QString &value); Q_INVOKABLE QString folioProperty(int folioIndex, const QString &property) const; + // -- terminal strips (borniers) -- + Q_INVOKABLE QStringList terminalStrips() const; + Q_INVOKABLE int addTerminalStrip(const QString &installation, const QString &location, + const QString &name); + Q_INVOKABLE bool removeTerminalStrip(int stripIndex); + Q_INVOKABLE bool addTerminalToStrip(int stripIndex, int folioIndex, + const QString &elementUuid); + // -- folios -- Q_INVOKABLE int addFolio(); Q_INVOKABLE bool setFolioTitle(int folioIndex, const QString &title);