Files
qelectrotech-source-mirror/sources/bomexport.cpp
T
ispyisail 8434b88eb8 Add options to leave junctions and contact blocks out of the parts list (#1178)
Since #849 the parts list has a row for every contact block (slave) and
terminal-type element. Users who draw the dots and bends where wires
branch as terminal-type symbols (114_connections) get one empty row per
junction: 109 of 151 rows on one reported cabinet.

Two options, both off by default so existing exports are unchanged:
- leave out the contact blocks: --no-slaves, "no_slaves" in qet_export,
  or uncheck the new "Contacts esclaves" element type in the dialog;
- leave out the junctions: terminal-type elements with no label,
  designation, manufacturer or manufacturer reference. --no-junctions,
  "no_junctions", or "Laisser de côté les jonctions" in the dialog.
  A terminal block with a label or a part number stays.

The element type filter had no box for slaves, so every query it built
left them out: the export dialog never listed contact blocks, although
the command line has since #849. The dialog now checks the new box by
default and gives the same rows as --export-bom. The box is unchecked by
default elsewhere, so nomenclature tables keep their rows.

docs/smart-device-bom.md still said slaves and terminals were excluded;
corrected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 22:08:44 +13:00

164 lines
4.9 KiB
C++

/*
Copyright 2006-2026 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bomexport.h"
#include <QSaveFile>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QVariant>
namespace {
QString csvField(QString value)
{
value.replace(QLatin1Char('"'), QStringLiteral("\"\""));
return QLatin1Char('"') + value + QLatin1Char('"');
}
QByteArray csvRecord(const QStringList &values)
{
QStringList escaped;
for (const auto &value : values) {
escaped.append(csvField(value));
}
return (escaped.join(QLatin1Char(';')) + QLatin1Char('\n')).toUtf8();
}
}
QStringList BomExport::defaultColumns()
{
return {
QStringLiteral("label"),
QStringLiteral("designation"),
QStringLiteral("manufacturer"),
QStringLiteral("manufacturer_reference"),
QStringLiteral("model"),
QStringLiteral("description"),
QStringLiteral("category"),
QStringLiteral("quantity"),
QStringLiteral("voltage_rating"),
QStringLiteral("current_rating"),
QStringLiteral("folio"),
QStringLiteral("notes")
};
}
/**
@brief BomExport::junctionFilter
@return a condition true for every row that is not a junction: a
terminal-type element with no label and no part data. Some users draw
the dots and bends where wires branch as their own terminal-type
symbols, and each one became an empty row of the parts list (#1178).
A terminal block with a label or a part number is kept.
*/
QString BomExport::junctionFilter()
{
return QStringLiteral("NOT (element_type = 'terminal' "
"AND IFNULL(label, '') = '' "
"AND IFNULL(designation, '') = '' "
"AND IFNULL(manufacturer, '') = '' "
"AND IFNULL(manufacturer_reference, '') = '')");
}
/**
@brief BomExport::defaultQuery
@param include_slaves : false leaves out the contact blocks (slave
elements), which otherwise appear as rows of their own
@param include_junctions : false leaves out the junctions, see
junctionFilter()
@return the query of the parts list
*/
QString BomExport::defaultQuery(bool include_slaves, bool include_junctions)
{
//Slaves and terminals are included because both are routinely
//separately orderable hardware. A circuit breaker can carry ten or
//twenty auxiliary blocks, each with its own order code, and a
//terminal block is a purchased part in its own right. Neither shares
//a line with its master: the query is ungrouped, one row per element,
//so each appears as the distinct item it is.
//
//Anything that should not be ordered is kept out by setting
//exclude_from_bom on the element, which the view already honours --
//a relay's own auxiliary contact, say.
//
//Thumbnails are deliberately left out for now even though ten of
//them in the shipped examples carry manufacturer and reference data
//(the assembly-plan mounting-plate symbols), because that has not
//been asked for and is a separate question. The folio report arrows
//and the conductor definition stay out because they are not hardware.
//
//See discussion #847.
const QString types = include_slaves
? QStringLiteral("'simple', 'master', 'slave', 'terminal'")
: QStringLiteral("'simple', 'master', 'terminal'");
const QString junctions = include_junctions
? QString()
: QStringLiteral(" AND ") + junctionFilter();
return QStringLiteral("SELECT %1 FROM element_nomenclature_view "
"WHERE element_type IN (%2)%3 "
"ORDER BY diagram_position, position, label")
.arg(defaultColumns().join(QStringLiteral(", ")), types, junctions);
}
QByteArray BomExport::toCsv(QSqlQuery &query, const QStringList &headers,
bool include_headers, int *row_count)
{
QByteArray csv("\xEF\xBB\xBF");
if (include_headers) {
csv += csvRecord(headers);
}
int rows = 0;
while (query.next())
{
QStringList values;
for (int i = 0; i < query.record().count(); ++i) {
values.append(query.value(i).toString());
}
csv += csvRecord(values);
++rows;
}
if (row_count) {
*row_count = rows;
}
return csv;
}
bool BomExport::writeCsv(const QString &file_path, const QByteArray &csv,
QString *error)
{
if (error) {
error->clear();
}
QSaveFile file(file_path);
if (!file.open(QIODevice::WriteOnly) || file.write(csv) != csv.size())
{
if (error) {
*error = file.errorString();
}
file.cancelWriting();
return false;
}
if (!file.commit())
{
if (error) {
*error = file.errorString();
}
return false;
}
return true;
}