mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-03 17:34:12 +02:00
Script buttons: a manager to write, try and delete stored scripts
Projet > Scripts > Gérer les scripts… lists the stored scripts with their icons, and the files that get no button with the reason. For each one it edits the name, icon (a file copied next to the script, a theme icon, or the initials), tooltip, shortcut, when it is enabled, and the script itself; "Tester" saves it and runs it on the current project, one Ctrl+Z to undo; "Supprimer" deletes it with its icon if no other script uses it. It only reads and writes the files in the scripts folder, so a script written here, by hand or by an assistant over the qet MCP server is the same thing, and the folder's watcher turns each into a button. ScriptHeader gains compose(), bodyOf() and idFor(), header-only and tested: what the manager writes reads back as what was typed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -113,6 +113,63 @@ struct ScriptHeader
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief compose
|
||||
A whole script file: the header parse() reads, then @a body. Empty
|
||||
fields are left out, so a file saved by the script manager and a
|
||||
hand-written one look alike.
|
||||
*/
|
||||
static QString compose(const QString &name, const QString &icon,
|
||||
const QString &tooltip, const QString &shortcut,
|
||||
const QString &context, const QString &body)
|
||||
{
|
||||
QString text = QStringLiteral("// ==QETScript==\n");
|
||||
auto line = [&text](const QString &key, const QString &value) {
|
||||
const QString v = value.simplified();
|
||||
if (!v.isEmpty())
|
||||
text += QStringLiteral("// @%1 %2\n").arg(key.leftJustified(8), v);
|
||||
};
|
||||
line(QStringLiteral("name"), name);
|
||||
line(QStringLiteral("icon"), icon);
|
||||
line(QStringLiteral("tooltip"), tooltip);
|
||||
line(QStringLiteral("shortcut"), shortcut);
|
||||
if (context != QLatin1String("canvas")) line(QStringLiteral("context"), context);
|
||||
line(QStringLiteral("api"), QStringLiteral("1"));
|
||||
text += QStringLiteral("// ==/QETScript==\n") + body;
|
||||
if (!text.endsWith(QLatin1Char('\n'))) text += QLatin1Char('\n');
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief bodyOf
|
||||
Everything after the header, or the whole text if there is none.
|
||||
*/
|
||||
static QString bodyOf(const QString &text)
|
||||
{
|
||||
const int end = text.indexOf(QStringLiteral("// ==/QETScript=="));
|
||||
if (end < 0) return text;
|
||||
const int next = text.indexOf(QLatin1Char('\n'), end);
|
||||
return next < 0 ? QString() : text.mid(next + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief idFor
|
||||
A file name for a new script called @a name: lower-case letters,
|
||||
digits and dashes, accents dropped, not one of @a taken.
|
||||
*/
|
||||
static QString idFor(const QString &name, const QStringList &taken)
|
||||
{
|
||||
QString base = name.normalized(QString::NormalizationForm_KD).toLower();
|
||||
static const QRegularExpression not_kept(QStringLiteral("[^a-z0-9\\s_-]"));
|
||||
base.remove(not_kept);
|
||||
base = base.simplified().replace(QLatin1Char(' '), QLatin1Char('-')).left(48);
|
||||
if (base.isEmpty()) base = QStringLiteral("script");
|
||||
QString id = base;
|
||||
for (int i = 2; taken.contains(id); ++i)
|
||||
id = base + QLatin1Char('-') + QString::number(i);
|
||||
return id;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // SCRIPTHEADER_H
|
||||
|
||||
Reference in New Issue
Block a user