qelectrotech-source-mirror/sources/ui/bomexportdialog.cpp

171 lines
4.4 KiB
C++
Raw Normal View History

2020-08-16 09:40:14 +02:00
/*
2025-01-04 13:37:40 +01:00
Copyright 2006-2025 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 "bomexportdialog.h"
2020-12-08 19:57:35 +01:00
#include "../dataBase/ui/elementquerywidget.h"
#include "../qetapp.h"
#include "../qetinformation.h"
#include "../qetproject.h"
#include "ui_bomexportdialog.h"
#include <QMessageBox>
#include <QSqlError>
#include <QSqlRecord>
/**
2020-08-18 21:28:52 +02:00
@brief BOMExportDialog::BOMExportDialog
@param project
@param parent
*/
BOMExportDialog::BOMExportDialog(QETProject *project, QWidget *parent) :
QDialog(parent),
ui(new Ui::BOMExportDialog),
m_project(project)
{
ui->setupUi(this);
m_query_widget = new ElementQueryWidget(this);
ui->m_main_layout->insertWidget(0, m_query_widget);
//By default format as bom is clicked
on_m_format_as_bom_clicked(true);
}
/**
2020-08-18 21:28:52 +02:00
@brief BOMExportDialog::~BOMExportDialog
*/
BOMExportDialog::~BOMExportDialog()
{
2020-07-15 20:20:07 +02:00
delete ui;
}
/**
2020-08-18 21:28:52 +02:00
@brief BOMExportDialog::exec
@return
*/
int BOMExportDialog::exec()
{
auto r = QDialog::exec();
2020-07-15 20:20:07 +02:00
if (r == QDialog::Accepted)
{
//save in csv file in same directory as project by default
QString dir = m_project->currentDir();
if (dir.isEmpty()) dir = QETApp::documentDir();
QString file_name = dir % "/" % tr("nomenclature_") % QString(m_project ->title() % ".csv");
2020-07-15 20:20:07 +02:00
QString file_path = QFileDialog::getSaveFileName(this, tr("Enregister sous... "), file_name, tr("Fichiers csv (*.csv)"));
QFile file(file_path);
if (!file_path.isEmpty())
{
if (QFile::exists(file_path ))
{
// if file already exist -> delete it
if (!QFile::remove(file_path) )
{
QMessageBox::critical(this, tr("Erreur"),
tr("Impossible de remplacer le fichier!\n\n")+
"Destination : "+file_path+"\n");
}
}
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream stream(&file);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) // ### Qt 6: remove
2020-07-15 20:20:07 +02:00
stream << getBom() << endl;
#else
#if TODO_LIST
2020-07-19 22:06:42 +02:00
#pragma message("@TODO remove code for QT 5.15 or later")
#endif
2020-07-15 20:20:07 +02:00
stream << getBom() << &Qt::endl(stream);
#endif
2020-07-15 20:20:07 +02:00
}
}
}
return r;
}
QString BOMExportDialog::getBom()
{
m_project->dataBase()->updateDB();
auto query_ = m_project->dataBase()->newQuery(m_query_widget->queryStr());
QString return_string;
if (!query_.exec()) {
qDebug() << "BOMExportDialog::getBom : query errir : " << query_.lastError();
2020-07-15 20:20:07 +02:00
}
else
2020-07-15 20:20:07 +02:00
{
//HEADERS
if (ui->m_include_headers)
2020-07-15 20:20:07 +02:00
{
auto record_ = query_.record();
QStringList header_name;
for (auto i=0 ; i<record_.count() ; ++i)
2020-07-15 20:20:07 +02:00
{
auto field_name = record_.fieldName(i);
qDebug() << "field name = " << field_name;
if (field_name == "position") {
header_name << tr("Position");
} else if (field_name == "diagram_position") {
header_name << tr("Position du folio");
} else if (field_name == "designation_qty") {
header_name << tr("Quantité numéro d'article", "Special field with name : designation quantity");
} else {
header_name << QETInformation::translatedInfoKey(field_name);
if (header_name.isEmpty()) {
header_name << field_name;
2020-07-15 20:20:07 +02:00
}
}
}
return_string = header_name.join(";") % "\n";
}
//ROWS
while (query_.next())
2020-07-15 20:20:07 +02:00
{
auto i=0;
QStringList values;
while (query_.value(i).isValid())
2020-07-15 20:20:07 +02:00
{
auto date = query_.value(i).toDate();
if (!date.isNull()) {
values << QLocale::system().toString(query_.value(i).toDate(), QLocale::ShortFormat);
} else {
values << query_.value(i).toString();
}
++i;
2020-07-15 20:20:07 +02:00
}
return_string += values.join(";") % "\n";
values.clear();
2019-11-09 14:28:13 +01:00
}
}
qDebug() << return_string;
return return_string;
}
2020-08-18 21:28:52 +02:00
/**
@brief BOMExportDialog::on_m_format_as_bom_clicked
@param checked
*/
void BOMExportDialog::on_m_format_as_bom_clicked(bool checked) {
m_query_widget->setGroupBy("designation", checked);
m_query_widget->setCount("COUNT(*) AS designation_qty", checked);
}