mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-06-12 15:03:15 +02:00
CLI export: add wiring list and wire-number list (CSV)
Extends the headless command-line export with two CSV outputs: qelectrotech --export-cables <project.qet> <output.csv> wiring list qelectrotech --export-wires <project.qet> <output.csv> wire numbers - --export-cables reuses WiringListExport (one row per conductor). - --export-wires reuses ConductorNumExport::wiresNum() (distinct wire numbers). WiringListExport::toCsv() mixed CSV generation with the file dialog and writing. Extracted the generation into a new const method toCsvString() that returns the CSV; toCsv() now calls it and writes the result. This makes the wiring list usable headlessly with no behavioural change to the GUI export. Addresses part of the CLI export requests (#162, #309): @pkess specifically asked to "export all connections as a list".
This commit is contained in:
@@ -18,10 +18,13 @@
|
||||
#include "cli_export.h"
|
||||
|
||||
#include "bordertitleblock.h"
|
||||
#include "conductornumexport.h"
|
||||
#include "diagram.h"
|
||||
#include "qetproject.h"
|
||||
#include "wiringlistexport.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QPainter>
|
||||
#include <QPdfWriter>
|
||||
@@ -40,6 +43,8 @@ const QHash<QString, QString> &exportFlags()
|
||||
{"--export-pdf", "pdf"},
|
||||
{"--export-png", "png"},
|
||||
{"--export-svg", "svg"},
|
||||
{"--export-cables", "cables"},
|
||||
{"--export-wires", "wires"},
|
||||
};
|
||||
return flags;
|
||||
}
|
||||
@@ -159,6 +164,33 @@ int exportImages(QETProject &project, const QString &format,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int exportCsv(QETProject &project, const QString &format, const QString &output)
|
||||
{
|
||||
QString csv;
|
||||
if (format == "cables") {
|
||||
WiringListExport wle(&project, nullptr);
|
||||
csv = wle.toCsvString();
|
||||
} else { // wires
|
||||
ConductorNumExport cne(&project, nullptr);
|
||||
csv = cne.wiresNum();
|
||||
}
|
||||
if (csv.isEmpty()) {
|
||||
err << "Nothing to export (empty list).\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
QFile file(output);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
err << "Cannot open '" << output << "' for writing.\n";
|
||||
return 1;
|
||||
}
|
||||
QTextStream fout(&file);
|
||||
fout << csv;
|
||||
file.close();
|
||||
out << "Exported " << format << " list -> " << output << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
namespace CLIExport {
|
||||
@@ -205,6 +237,8 @@ int run(const QStringList &args)
|
||||
const QString format = exportFlags().value(flag);
|
||||
if (format == "pdf")
|
||||
return exportPdf(project, output);
|
||||
if (format == "cables" || format == "wires")
|
||||
return exportCsv(project, format, output);
|
||||
return exportImages(project, format, output);
|
||||
}
|
||||
|
||||
|
||||
@@ -42,12 +42,16 @@ namespace CLIExport {
|
||||
@return process exit code (0 on success).
|
||||
|
||||
Usage:
|
||||
qelectrotech --export-pdf <project.qet> <output.pdf>
|
||||
qelectrotech --export-png <project.qet> <output_dir>
|
||||
qelectrotech --export-svg <project.qet> <output_dir>
|
||||
qelectrotech --export-pdf <project.qet> <output.pdf>
|
||||
qelectrotech --export-png <project.qet> <output_dir>
|
||||
qelectrotech --export-svg <project.qet> <output_dir>
|
||||
qelectrotech --export-cables <project.qet> <output.csv>
|
||||
qelectrotech --export-wires <project.qet> <output.csv>
|
||||
|
||||
PDF: one multi-page document (one diagram per page).
|
||||
PNG/SVG: one file per diagram, named <output_dir>/<NN>_<title>.<ext>.
|
||||
cables: wiring list (one row per conductor) as CSV.
|
||||
wires: list of distinct wire numbers as CSV.
|
||||
*/
|
||||
int run(const QStringList &args);
|
||||
|
||||
|
||||
@@ -151,13 +151,39 @@ void WiringListExport::toCsv()
|
||||
{
|
||||
if (!m_project) return;
|
||||
|
||||
QDomDocument doc = m_project->toXml();
|
||||
|
||||
if (doc.isNull()) {
|
||||
const QString csv = toCsvString();
|
||||
if (csv.isEmpty()) {
|
||||
QMessageBox::warning(m_parent, tr("Erreur"), tr("Impossible de lire la structure en mémoire du projet."));
|
||||
return;
|
||||
}
|
||||
|
||||
QFileDialog dialog(m_parent);
|
||||
dialog.setAcceptMode(QFileDialog::AcceptSave);
|
||||
dialog.setWindowTitle(tr("Exporter le plan de câblage"));
|
||||
dialog.setDefaultSuffix("csv");
|
||||
dialog.setNameFilter(tr("Fichiers CSV (*.csv)"));
|
||||
|
||||
if (dialog.exec() != QDialog::Accepted) return;
|
||||
QString fileName = dialog.selectedFiles().first();
|
||||
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
QMessageBox::warning(m_parent, tr("Erreur"), tr("Impossible d'ouvrir le fichier pour l'écriture."));
|
||||
return;
|
||||
}
|
||||
QTextStream out(&file);
|
||||
out << csv;
|
||||
file.close();
|
||||
QMessageBox::information(m_parent, tr("Export réussi"), tr("Le plan de câblage a été exporté avec succès !"));
|
||||
}
|
||||
|
||||
QString WiringListExport::toCsvString() const
|
||||
{
|
||||
if (!m_project) return QString();
|
||||
|
||||
QDomDocument doc = m_project->toXml();
|
||||
if (doc.isNull()) return QString();
|
||||
|
||||
QSet<QString> conductorDefinitionTypes;
|
||||
QDomElement rootElem = doc.documentElement();
|
||||
QDomElement collection = rootElem.firstChildElement("collection");
|
||||
@@ -197,21 +223,6 @@ void WiringListExport::toCsv()
|
||||
}
|
||||
}
|
||||
|
||||
QFileDialog dialog(m_parent);
|
||||
dialog.setAcceptMode(QFileDialog::AcceptSave);
|
||||
dialog.setWindowTitle(tr("Exporter le plan de câblage"));
|
||||
dialog.setDefaultSuffix("csv");
|
||||
dialog.setNameFilter(tr("Fichiers CSV (*.csv)"));
|
||||
|
||||
if (dialog.exec() != QDialog::Accepted) return;
|
||||
QString fileName = dialog.selectedFiles().first();
|
||||
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
QMessageBox::warning(m_parent, tr("Erreur"), tr("Impossible d'ouvrir le fichier pour l'écriture."));
|
||||
return;
|
||||
}
|
||||
|
||||
QMap<QString, ElementInfo> elementsInfo = collectElementsInfo(doc.documentElement());
|
||||
QList<ConductorData> conductors = collectConductors(doc.documentElement());
|
||||
|
||||
@@ -353,7 +364,8 @@ void WiringListExport::toCsv()
|
||||
return a.terminalname2 < b.terminalname2;
|
||||
});
|
||||
|
||||
QTextStream out(&file);
|
||||
QString csv;
|
||||
QTextStream out(&csv);
|
||||
out << tr("Page", "Wiring list CSV header") << ";"
|
||||
<< tr("Composant 1", "Wiring list CSV header") << ";"
|
||||
<< tr("Borne 1", "Wiring list CSV header") << ";"
|
||||
@@ -376,6 +388,5 @@ void WiringListExport::toCsv()
|
||||
<< c.function << "\n";
|
||||
}
|
||||
|
||||
file.close();
|
||||
QMessageBox::information(m_parent, tr("Export réussi"), tr("Le plan de câblage a été exporté avec succès !"));
|
||||
return csv;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,11 @@ class WiringListExport : public QObject
|
||||
public:
|
||||
explicit WiringListExport(QETProject *project, QWidget *parent = nullptr);
|
||||
void toCsv();
|
||||
/**
|
||||
Build the wiring-list CSV and return it as a string (no GUI).
|
||||
Used by toCsv() and by the headless command-line export.
|
||||
*/
|
||||
QString toCsvString() const;
|
||||
|
||||
private:
|
||||
QETProject *m_project;
|
||||
|
||||
Reference in New Issue
Block a user