/* Copyright 2006-2020 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 . */ #include "bomexportdialog.h" #include "ui_bomexportdialog.h" #include "elementquerywidget.h" #include "qetproject.h" #include "qetapp.h" #include #include #include /** @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); } /** @brief BOMExportDialog::~BOMExportDialog */ BOMExportDialog::~BOMExportDialog() { delete ui; } /** @brief BOMExportDialog::exec @return */ int BOMExportDialog::exec() { auto r = QDialog::exec(); if (r == QDialog::Accepted) { //save in csv file QString file_name = tr("nomenclature_") + QString(m_project ->title() + ".csv"); 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 stream << getBom() << endl; #else #pragma message("@TODO remove code for QT 5.15 or later") stream << getBom() << &Qt::endl(stream); #endif } } } 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(); } else { //HEADERS if (ui->m_include_headers) { auto record_ = query_.record(); QStringList header_name; for (auto i=0 ; isetGroupBy("designation", checked); m_query_widget->setCount("COUNT(*) AS designation_qty", checked); }