mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-06 19:54:13 +02:00
Live mode: create a new project (new_project, qet_live_new_project)
An assistant in live mode could only work on the project already open. new_project makes one as File > New does -- this installation's new-folio defaults, optional title and number of folios -- and makes it the current project, so the next request works on it. An optional absolute path saves it at once, never over an existing file: QElectroTech refuses, and the MCP server refuses before asking (a new never-overwrite class in its path policy, so an "overwrite" flag cannot get round it). Pages and links across pages already worked through scripts (qet.addFolio, setFolioTitle, linkElements across folios); the tool's description now says so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -48,6 +48,9 @@
|
||||
#include <QPlainTextEdit>
|
||||
#include <QVBoxLayout>
|
||||
#include <QDialog>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QMdiSubWindow>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QLabel>
|
||||
#include "../projectview.h"
|
||||
@@ -251,6 +254,8 @@ void LiveServer::handle(const QJsonObject &request)
|
||||
answer = runStored(request.value(QStringLiteral("script")).toString());
|
||||
} else if (cmd == QLatin1String("command")) {
|
||||
answer = command(request.value(QStringLiteral("action")).toString());
|
||||
} else if (cmd == QLatin1String("new_project")) {
|
||||
answer = newProject(request);
|
||||
} else if (cmd == QLatin1String("show_folio")) {
|
||||
answer = showFolio(request.value(QStringLiteral("folio")).toInt(-1));
|
||||
} else if (cmd == QLatin1String("undo_last")) {
|
||||
@@ -441,6 +446,62 @@ QJsonObject LiveServer::showFolio(int folio)
|
||||
return {{QStringLiteral("ok"), true}, {QStringLiteral("folio"), folio}};
|
||||
}
|
||||
|
||||
/**
|
||||
@brief LiveServer::newProject
|
||||
A new project, as File > New makes it: the new-folio defaults of this
|
||||
QElectroTech, opened and made the current project, so the next script
|
||||
works on it. Optional: "title", "folios" (how many empty folios, 1 to
|
||||
100, default 1), and "path" to save it to at once -- never over an
|
||||
existing file, so an assistant cannot replace the user's work.
|
||||
*/
|
||||
QJsonObject LiveServer::newProject(const QJsonObject &request)
|
||||
{
|
||||
QETDiagramEditor *e = editor();
|
||||
if (!e) return failure(QStringLiteral("no QElectroTech editor window is open"));
|
||||
|
||||
const int folios = request.value(QStringLiteral("folios")).toInt(1);
|
||||
if (folios < 1 || folios > 100)
|
||||
return failure(QStringLiteral("folios must be between 1 and 100, not %1").arg(folios));
|
||||
const QString path = request.value(QStringLiteral("path")).toString().trimmed();
|
||||
if (!path.isEmpty()) {
|
||||
const QFileInfo info(path);
|
||||
if (info.isRelative())
|
||||
return failure(QStringLiteral("path must be absolute: %1").arg(path));
|
||||
if (info.exists())
|
||||
return failure(QStringLiteral("%1 already exists; a new project is never "
|
||||
"saved over a file").arg(path));
|
||||
if (!info.dir().exists())
|
||||
return failure(QStringLiteral("the folder %1 does not exist").arg(info.absolutePath()));
|
||||
}
|
||||
|
||||
auto project = new QETProject(e);
|
||||
for (int i = 0; i < folios; ++i) project->addNewDiagram();
|
||||
const QString title = request.value(QStringLiteral("title")).toString().trimmed();
|
||||
if (!title.isEmpty()) project->setTitle(title);
|
||||
if (!e->addProject(project)) return failure(QStringLiteral("QElectroTech refused the new project"));
|
||||
|
||||
//Current at once, whether or not QElectroTech is the active
|
||||
//application: the next request runs on it (see editor())
|
||||
for (ProjectView *pv : e->openedProjects()) {
|
||||
if (pv->project() == project) {
|
||||
for (QMdiSubWindow *w : e->m_workspace.subWindowList())
|
||||
if (w->widget() == pv) e->m_workspace.setActiveSubWindow(w);
|
||||
}
|
||||
}
|
||||
|
||||
if (!path.isEmpty()) {
|
||||
project->setFilePath(path);
|
||||
const QETResult result = project->write();
|
||||
if (!result.isOk())
|
||||
return failure(QStringLiteral("the project was created but not saved to %1: %2")
|
||||
.arg(path, result.errorMessage()));
|
||||
}
|
||||
|
||||
QJsonObject answer = status();
|
||||
answer.insert(QStringLiteral("created"), true);
|
||||
return answer;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief LiveServer::undoLast
|
||||
Undo the newest step, only if the assistant made it: what the user did
|
||||
|
||||
Reference in New Issue
Block a user