Files
qelectrotech-source-mirror/sources/materiallist/materialentrydialog.cpp
T
Kellermorph d5ae15f568 Add a material list lookup for the element article fields
The element description dialog only offered typing, so every article of
a catalogue had to be copied in by hand into the Artikelbeschreibung
row of each block, over and over.

Settings > General now keeps the path of a CSV material list (Créer...
writes a template with the header lines), and the Artikelbeschreibung
row of every block of the Bauteil dialog shows a "..." button opening a
searchable listing of that file. "Appliquer" fills the fields of that
block only -- the main block gets everything but label and formula, an
auxiliary block gets description, number, manufacturer, references,
supplier, quantity, unit and additional info -- and a cell that is
empty in the file never erases a value already in the element. The
symbol editor is left alone.

- sources/materiallist/: CSV reading and writing (UTF-8 BOM, ';', every
  field quoted, atomic write through QSaveFile), two header lines
  (translated labels then the canonical English keys), the selection
  window and the new entry form.
- The selection window reads the file again every time it opens, keeps
  its size, column widths, column order and sort between openings,
  shows every column of the file, scrolls all four directions with the
  wheel (Shift + wheel moves the columns) and puts back the order of
  the file when the corner above the row numbers or a row number is
  clicked.
- German translations for the new strings in lang/qet_de.ts.
2026-09-27 21:18:54 +02:00

145 lines
4.1 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 "materialentrydialog.h"
#include "../qetmessagebox.h"
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QScrollArea>
#include <QVBoxLayout>
/**
@brief MaterialEntryDialog::MaterialEntryDialog
Build one field per column of the material file.
@param columns the columns of the file, in file order
@param parent parent widget
*/
MaterialEntryDialog::MaterialEntryDialog(const QStringList &columns, QWidget *parent) :
QDialog(parent),
m_columns(columns)
{
setWindowTitle(tr("Nouvelle entrée"));
//Two fields per row : the form shows the whole article at once
//instead of stacking twenty lines in a narrow column.
setMinimumSize(780, 560);
resize(1120, 800);
auto *main_layout = new QVBoxLayout(this);
auto *intro = new QLabel(
tr("Renseignez l'article à ajouter. Les champs laissés vides "
"restent vides dans le fichier."), this);
intro->setWordWrap(true);
main_layout->addWidget(intro);
auto *scroll_area = new QScrollArea(this);
scroll_area->setWidgetResizable(true);
scroll_area->setFrameShape(QFrame::NoFrame);
auto *container = new QWidget(scroll_area);
auto *grid = new QGridLayout(container);
for (int i = 0; i < m_columns.size(); ++i)
{
const QString column = m_columns.at(i);
auto *label = new QLabel(MaterialList::translatedColumn(column) + QLatin1Char(' '), container);
label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
auto *edit = new QLineEdit(container);
edit->setClearButtonEnabled(true);
label->setBuddy(edit);
const int row = i / 2;
const int pair = i % 2;
grid->addWidget(label, row, pair * 2);
grid->addWidget(edit, row, pair * 2 + 1);
m_edits.append(edit);
}
grid->setColumnStretch(1, 1);
grid->setColumnStretch(3, 1);
scroll_area->setWidget(container);
main_layout->addWidget(scroll_area, 1);
auto *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
buttons->button(QDialogButtonBox::Ok)->setText(tr("Enregistrer"));
buttons->button(QDialogButtonBox::Cancel)->setText(tr("Annuler"));
connect(buttons, &QDialogButtonBox::accepted, this, &MaterialEntryDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, this, &MaterialEntryDialog::reject);
main_layout->addWidget(buttons);
if (!m_edits.isEmpty()) {
m_edits.first()->setFocus();
}
}
/**
@brief MaterialEntryDialog::~MaterialEntryDialog
Destructor
*/
MaterialEntryDialog::~MaterialEntryDialog() = default;
/**
@brief MaterialEntryDialog::record
@return the article described by the form. Empty fields are left out :
an empty column never means anything for an article which is being
created.
*/
MaterialRecord MaterialEntryDialog::record() const
{
MaterialRecord record;
for (int i = 0; i < m_columns.size() && i < m_edits.size(); ++i)
{
QString value = m_edits.at(i)->text();
value.remove(QLatin1Char('\r'));
value.remove(QLatin1Char('\n'));
value = value.trimmed();
if (!value.isEmpty()) {
record.setValue(m_columns.at(i), value);
}
}
return record;
}
/**
@brief MaterialEntryDialog::accept
Refuse an empty form: an article without any information is not worth
a line in the file.
*/
void MaterialEntryDialog::accept()
{
if (record().values.isEmpty())
{
QET::QetMessageBox::warning(this,
tr("Aucun renseignement"),
tr("Saisissez au moins un renseignement "
"pour créer une entrée."));
return;
}
QDialog::accept();
}