Export to csv improvement

Export to csv doesn't use is own datase, but the project database
instead.
This commit is contained in:
Claveau Joshua
2020-08-17 16:42:07 +02:00
parent bc0fecea6c
commit 319188e010
5 changed files with 187 additions and 1168 deletions

View File

@@ -365,10 +365,49 @@ QString ElementQueryWidget::queryStr() const
filter_.prepend( " WHERE"); filter_.prepend( " WHERE");
} }
QString q(select + column + from + where + filter_ + order_by); QString q(select + column + m_count + from + where + filter_ + m_group_by + order_by);
return q; return q;
} }
/**
* @brief ElementQueryWidget::setGroupBy
* Add the query instruction GROUP BY.
* @param text : the text of the GROUP BY instruction:
* ex : if @text = designation, the query will contain "GROUP BY designation"
* @param set :
* true by default -> GROUP BY will be used.
* false -> GROUP BY will be not used
*/
void ElementQueryWidget::setGroupBy(QString text, bool set)
{
if (set) {
m_group_by = QString(" GROUP BY ") + text;
} else {
m_group_by.clear();
}
updateQueryLine();
}
/**
* @brief ElementQueryWidget::setCount
* Add the query instruction COUNT.
* Unlike setGroupBy, you have to write the entire sentance.
* ex : text = "COUNT(*) AS designation_qty". the query will contain what you write.
* @param text : the count instruction
* @param set :
* true by default -> count will be used.
* false -> count will be not used.
*/
void ElementQueryWidget::setCount(QString text, bool set)
{
if (set) {
m_count = QString(", " + text + " ");
} else {
m_count.clear();
}
updateQueryLine();
}
/** /**
@brief ElementQueryWidget::updateQueryLine @brief ElementQueryWidget::updateQueryLine
*/ */

View File

@@ -42,6 +42,8 @@ class ElementQueryWidget : public QWidget
void setQuery(const QString &query); void setQuery(const QString &query);
QString queryStr() const; QString queryStr() const;
void setGroupBy(QString text, bool set = true);
void setCount(QString text, bool set = true);
static QString modelIdentifier() {return "nomenclature";} static QString modelIdentifier() {return "nomenclature";}
@@ -74,7 +76,9 @@ class ElementQueryWidget : public QWidget
QHash <QString, QString> m_export_info; QHash <QString, QString> m_export_info;
QButtonGroup m_button_group; QButtonGroup m_button_group;
QList <QListWidgetItem *> m_items_list; QList <QListWidgetItem *> m_items_list;
QString m_custom_query; QString m_custom_query,
m_group_by,
m_count;
QHash <QString, QPair<int, QString>> m_filter; QHash <QString, QPair<int, QString>> m_filter;
}; };

View File

@@ -1,144 +1,66 @@
/* /*
Copyright 2006-2020 The QElectroTech Team Copyright 2006-2020 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "bomexportdialog.h" #include "bomexportdialog.h"
#include "ui_bomexportdialog.h" #include "ui_bomexportdialog.h"
#include "qetapp.h" #include "elementquerywidget.h"
#include "qetproject.h" #include "qetproject.h"
#include "elementprovider.h" #include "qetapp.h"
#include "element.h"
#include "diagram.h"
#include "diagramposition.h"
#include <QButtonGroup>
#include <QListWidgetItem>
#include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
#include <QSqlQuery>
#include <QSqlError> #include <QSqlError>
#include <QJsonDocument>
#include <QSqlRecord> #include <QSqlRecord>
/** /**
@brief BOMExportDialog::BOMExportDialog * @brief BOMExportDialog::BOMExportDialog
@param project the project for create the bill of material * @param parent
@param parent widget */
*/
BOMExportDialog::BOMExportDialog(QETProject *project, QWidget *parent) : BOMExportDialog::BOMExportDialog(QETProject *project, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::BOMExportDialog), ui(new Ui::BOMExportDialog),
m_project(project) m_project(project)
{ {
ui->setupUi(this); ui->setupUi(this);
m_export_info.insert("pos", tr("Position")); m_query_widget = new ElementQueryWidget(this);
m_export_info.insert("folio_title", tr("Titre du folio")); ui->m_main_layout->insertWidget(0, m_query_widget);
m_export_info.insert("folio_pos", tr("Position de folio")); //By default format as bom is clicked
m_export_info.insert("folio_num", tr("Numéro de folio")); on_m_format_as_bom_clicked(true);
m_export_info.insert("designation_qty", tr("Quantité (Numéro d'article)"));
m_button_group.setExclusive(false);
m_button_group.addButton(ui->m_all_cb, 0);
m_button_group.addButton(ui->m_terminal_cb, 1);
m_button_group.addButton(ui->m_simple_cb, 2);
m_button_group.addButton(ui->m_button_cb, 3);
m_button_group.addButton(ui->m_coil_cb, 4);
m_button_group.addButton(ui->m_protection_cb, 5);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) // ### Qt 6: remove
connect(&m_button_group,
static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
[this](int id)
#else
#pragma message("@TODO remove code for QT 5.15 or later")
connect(&m_button_group,
static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::idClicked),
[this](int id)
#endif
{
auto check_box = static_cast<QCheckBox *>(m_button_group.button(0));
if (id == 0)
{
switch (check_box->checkState()) {
case Qt::Checked :
for (auto button : m_button_group.buttons()) {
button->setChecked(true);
}
break;
case Qt::Unchecked :
for (auto button : m_button_group.buttons()) {
button->setChecked(false);
}
break;
default: break;
}
}
else
{
int checked = 0;
for (int i=1 ; i<5 ; ++i) {
if (m_button_group.button(i)->isChecked()) {++checked;}
}
switch (checked)
{
case 0 :
check_box->setCheckState(Qt::Unchecked);
break;
case 5:
check_box->setCheckState(Qt::Checked);
break;
default:
check_box->setCheckState(Qt::PartiallyChecked);
break;
}
}
updateQueryLine();
});
setUpItems();
createDataBase();
fillSavedQuery();
} }
/** /**
@brief BOMExportDialog::~BOMExportDialog * @brief BOMExportDialog::~BOMExportDialog
*/ */
BOMExportDialog::~BOMExportDialog() BOMExportDialog::~BOMExportDialog()
{ {
delete ui; delete ui;
m_data_base.close();
} }
/** /**
@brief BOMExportDialog::exec * @brief BOMExportDialog::exec
Reimplemented from QDialog * @return
@return */
*/
int BOMExportDialog::exec() int BOMExportDialog::exec()
{ {
int r = QDialog::exec(); auto r = QDialog::exec();
if (r == QDialog::Accepted) if (r == QDialog::Accepted)
{ {
//save in csv file //save in csv file
QString file_name = tr("nomenclature_") + QString(m_project ->title() + ".csv"); QString file_name = tr("nomenclature_") + QString(m_project ->title() + ".csv");
// if (!file_name.endsWith(".csv")) {
// file_name += ".csv";
// }
QString file_path = QFileDialog::getSaveFileName(this, tr("Enregister sous... "), file_name, tr("Fichiers csv (*.csv)")); QString file_path = QFileDialog::getSaveFileName(this, tr("Enregister sous... "), file_name, tr("Fichiers csv (*.csv)"));
QFile file(file_path); QFile file(file_path);
if (!file_path.isEmpty()) if (!file_path.isEmpty())
@@ -168,595 +90,73 @@ int BOMExportDialog::exec()
return r; return r;
} }
/**
@brief BOMExportDialog::selectedKeys
@return the current keys of selected infos to be exported
*/
QStringList BOMExportDialog::selectedKeys() const
{
//Made a string list with the colomns (keys) choosen by the user
QStringList keys;
int row = 0;
while (auto *item = ui->m_choosen_list->item(row))
{
keys.append(item->data(Qt::UserRole).toString());
++row;
}
return keys;
}
/**
@brief BOMExportDialog::translatedKeys
@param key
@return
*/
QString BOMExportDialog::translatedKeys(const QString &key) const
{
if (QETApp::elementInfoKeys().contains(key)) {
return QETApp::elementTranslatedInfoKey(key);
}
else if (m_export_info.keys().contains(key)) {
return m_export_info.value(key);
} else {
auto str(key);
str.replace("_", "-");
return QETApp::elementTranslatedInfoKey(str);
}
}
/**
@brief BOMExportDialog::setUpItems
Setup all items available for create the column of the bill of material.
*/
void BOMExportDialog::setUpItems()
{
for(QString key : QETApp::elementInfoKeys())
{
auto item = new QListWidgetItem(QETApp::elementTranslatedInfoKey(key), ui->m_var_list);
item->setData(Qt::UserRole+1, key); //We store the real key before replace "-" by "_" to easily retrieve it in the element information
item->setData(Qt::UserRole, key.replace("-", "_")); //We must to replace "-" by "_" because "-" is a sql keyword.
m_items_list << item;
}
for (auto key : m_export_info.keys())
{
auto item = new QListWidgetItem(m_export_info.value(key), ui->m_var_list);
item->setData(Qt::UserRole, key);
item->setData(Qt::UserRole+1, key);
m_items_list << item;
}
}
/**
@brief BOMExportDialog::on_m_add_pb_clicked
*/
void BOMExportDialog::on_m_add_pb_clicked()
{
if (auto *item = ui->m_var_list->takeItem(ui->m_var_list->currentRow())) {
ui->m_choosen_list->addItem(item);
}
updateQueryLine();
}
/**
@brief BOMExportDialog::on_m_remove_pb_clicked
*/
void BOMExportDialog::on_m_remove_pb_clicked()
{
if (auto *item = ui->m_choosen_list->takeItem(ui->m_choosen_list->currentRow())) {
ui->m_var_list->addItem(item);
}
updateQueryLine();
}
/**
@brief BOMExportDialog::on_m_up_pb_clicked
*/
void BOMExportDialog::on_m_up_pb_clicked()
{
auto row = ui->m_choosen_list->currentRow();
if(row <= 0) {
return;
}
auto *item = ui->m_choosen_list->takeItem(row);
ui->m_choosen_list->insertItem(row-1, item);
ui->m_choosen_list->setCurrentItem(item);
updateQueryLine();
}
/**
@brief BOMExportDialog::on_m_down_pb_clicked
*/
void BOMExportDialog::on_m_down_pb_clicked()
{
auto row = ui->m_choosen_list->currentRow();
if (row == -1) {
return;
}
auto *item = ui->m_choosen_list->takeItem(row);
ui->m_choosen_list->insertItem(row+1, item);
ui->m_choosen_list->setCurrentItem(item);
updateQueryLine();
}
void BOMExportDialog::on_m_save_name_le_textChanged(const QString &arg1) {
ui->m_save_current_conf_pb->setDisabled(arg1.isEmpty());
}
/**
@brief BOMExportDialog::getBom
@return the bill of material as string already formated
for export to csv.
*/
QString BOMExportDialog::getBom() QString BOMExportDialog::getBom()
{ {
QString data; //The string to be returned m_project->dataBase()->updateDB();
if (ui->m_include_header_cb->isChecked()) { auto query_ = m_project->dataBase()->newQuery(m_query_widget->queryStr());
data = headers(); QString return_string;
data += "\n";
}
QSqlQuery query (queryStr() , m_data_base);
if (!query.exec()) {
qDebug() << "Query error : " << query.lastError();
}
QStringList record;
while (query.next())
{
if (ui->m_edit_sql_query_cb->isChecked()) //In case of custom query, we only append each value to @record
{
auto i=0;
while (query.value(i).isValid())
{
record << query.value(i).toString();
++i;
}
}
else //In case of query made with the gui, we ensure that returned values are in the same order as list created by user
{
QSqlRecord sql_record = query.record();
for (auto key : selectedKeys()) {
record << sql_record.value(key).toString();
}
}
data += record.join(";") + "\n";
record.clear();
}
m_data_base.close();
return data;
}
/** if (!query_.exec()) {
@brief BOMExportDialog::headers qDebug() << "BOMExportDialog::getBom : query errir : " << query_.lastError();
@return the header to be use for the csv file
*/
QString BOMExportDialog::headers() const
{
QString header_string;
if (!ui->m_edit_sql_query_cb->isChecked())
{
for (auto key : selectedKeys())
{
if (!header_string.isEmpty()) {
header_string += ";";
}
header_string += translatedKeys(key);
}
header_string += "\n";
}
else if (!queryStr().isEmpty())//Try to retreive the header according to the sql query
{
if (queryStr().startsWith("SELECT ") && queryStr().contains("FROM"))
{
auto header = queryStr();
header.remove(0, 7); //Remove SELECT from the string;
header.truncate(header.indexOf("FROM")); //Now we only have the string between SELECT and FROM
header.replace(" ", ""); //remove white space
QStringList list = header.split(",");
if (!list.isEmpty())
{
for (int i=0 ; i<list.size() ; i++)
{
if(!header_string.isEmpty()) {
header_string += ";";
}
header_string += QETApp::elementTranslatedInfoKey(list.at(i));
}
header_string += "\n";
}
}
}
return header_string;
}
/**
@brief BOMExportDialog::createDataBase
@return true if database is successfully created
*/
bool BOMExportDialog::createDataBase()
{
//Create a sqlite data base to sort the bom
m_data_base = QSqlDatabase::addDatabase("QSQLITE", "bill_of_material");
if (!m_data_base.open())
{
m_data_base.close();
return false;
}
//Create the table:
QStringList keys;
auto row = 0;
while (ui->m_var_list->item(row))
{
keys << ui->m_var_list->item(row)->data(Qt::UserRole).toString();
++row;
}
keys << "element_type" << "element_subtype";
keys.removeAll("designation_qty");
QString table("CREATE TABLE bom(");
bool first = true;
for (auto string : keys)
{
if (first) {
first = false;
} else {
table += ",";
}
table += string += " VARCHAR(512)";
}
table += ");";
m_data_base.exec(table);
QStringList bind_values;
for (auto key : keys) {
bind_values << key.prepend(":");
}
//Prepare the query used for insert new record
QString insert("INSERT INTO bom (" +
keys.join(", ") +
") VALUES (" +
bind_values.join(", ") +
")");
m_insert_query = QSqlQuery(m_data_base);
m_insert_query.prepare(insert);
populateDataBase();
return true;
}
/**
@brief BOMExportDialog::populateDataBase
Populate the database
*/
void BOMExportDialog::populateDataBase()
{
for (auto *diagram : m_project->diagrams())
{
ElementProvider ep(diagram);
QList<Element *> elements_list = ep.find(Element::Simple | Element::Terminale | Element::Master);
//Insert all value into the database
for (auto elmt : elements_list)
{
auto hash = elementInfoToString(elmt);
for (auto key : hash.keys())
{
QString value = hash.value(key);
QString bind = key.prepend(":");
m_insert_query.bindValue(bind, value);
}
m_insert_query.bindValue(":element_type", elmt->linkTypeToString());
m_insert_query.bindValue(":element_subtype", elmt->kindInformations()["type"].toString());
if (!m_insert_query.exec()) {
qDebug() << "BOMExportDialog::populateDataBase insert error : " << m_insert_query.lastError();
}
}
}
}
/**
@brief BOMExportDialog::elementInfoToString
@param elmt
@return a Hash with as key the name of bdd columns and value the value of @elmt for each columns.
*/
QHash<QString, QString> BOMExportDialog::elementInfoToString(Element *elmt) const
{
QHash<QString, QString> keys_hash; //Use to get the element info according to the database columns name
int row = 0;
while (auto *item = ui->m_var_list->item(row))
{
keys_hash.insert(item->data(Qt::UserRole).toString(),
item->data(Qt::UserRole+1).toString());
++row;
}
QHash<QString, QString> hash; //Store the value for each columns
for (auto key : keys_hash.keys())
{
if (key == "pos") {
hash.insert(key, elmt->diagram()->convertPosition(elmt->scenePos()).toString());
}
else if (key == "folio_title") {
hash.insert(key, elmt->diagram()->title());
}
else if (key == "folio_pos") {
hash.insert(key, QString::number(elmt->diagram()->folioIndex() + 1));
}
else if (key == "folio_num") {
hash.insert(key, elmt->diagram()->border_and_titleblock.finalfolio());
}
else if (key == "label") {
hash.insert(key, elmt->actualLabel());
}
else {
hash.insert(key, elmt->elementInformations()[keys_hash.value(key)].toString());
}
}
return hash;
}
/**
@brief BOMExportDialog::queryStr
@return the query string
*/
QString BOMExportDialog::queryStr() const
{
//User define is own query
if (ui->m_edit_sql_query_cb->isChecked()) {
return ui->m_sql_query->text();
}
//Made a string list with the colomns (keys) choosen by the user
QStringList keys = selectedKeys();
keys.removeAll("designation_qty");
QString select ="SELECT ";
QString order_by = " ORDER BY ";
QString column;
bool first = true;
for (auto key: keys) {
if (first) {
first = false;
} else {
column += ", ";
order_by += ", ";
}
column += key;
order_by += key;
}
QString from = " FROM bom";
QString count = ui->m_format_as_bom_rb->isChecked() ? QString(", COUNT(*) AS designation_qty ") : QString();
QString where;
if (ui->m_all_cb->checkState() == Qt::PartiallyChecked)
{
if (ui->m_terminal_cb->isChecked()) {
where = " WHERE element_type = 'Terminale'";
}
if (ui->m_simple_cb->isChecked()) {
auto str = where.isEmpty() ? " WHERE element_type = 'Simple'" : " AND element_type = 'Simple'";
where += str;
}
if (ui->m_button_cb->isChecked()) {
auto str = where.isEmpty() ? " WHERE element_subtype = 'commutator'" : " AND element_subtype = 'commutator'";
where += str;
}
if (ui->m_coil_cb->isChecked()) {
auto str = where.isEmpty() ? " WHERE element_subtype = 'coil'" : " AND element_subtype = 'coil'";
where += str;
}
if (ui->m_protection_cb->isChecked()) {
auto str = where.isEmpty() ? " WHERE element_subtype = 'protection'" : " AND element_subtype = 'protection'";
where += str;
}
}
QString where_bom;
if(ui->m_format_as_bom_rb->isChecked())
{
if (where.isEmpty()) {
where = " WHERE designation IS NOT NULL";
} else {
where.append(" AND designation IS NOT NULL");
}
}
QString group_by = ui->m_format_as_bom_rb->isChecked() ? " GROUP BY designation" : "";
QString q(select + column + count + from + where + where_bom + group_by + order_by);
return q;
}
void BOMExportDialog::updateQueryLine() {
ui->m_sql_query->setText(queryStr());
}
/**
@brief BOMExportDialog::fillSavedQuery
Fill the combo box with the name of the saved query
*/
void BOMExportDialog::fillSavedQuery()
{
QFile file(QETApp::configDir() + "/bill_of_materials.json");
if (file.open(QFile::ReadOnly))
{
QJsonDocument jsd(QJsonDocument::fromJson(file.readAll()));
QJsonObject jso = jsd.object();
for (auto it = jso.begin() ; it != jso.end() ; ++it) {
ui->m_conf_cb->addItem(it.key());
}
}
}
void BOMExportDialog::on_m_format_as_nomenclature_rb_toggled(bool checked) {
Q_UNUSED(checked)
updateQueryLine();
}
/**
@brief BOMExportDialog::on_m_edit_sql_query_cb_clicked
Update widgets
*/
void BOMExportDialog::on_m_edit_sql_query_cb_clicked()
{
ui->m_sql_query->setEnabled(ui->m_edit_sql_query_cb->isChecked());
ui->m_info_widget->setDisabled(ui->m_edit_sql_query_cb->isChecked());
ui->m_parametre_widget->setDisabled(ui->m_edit_sql_query_cb->isChecked());
ui->m_format_as_gb->setDisabled(ui->m_edit_sql_query_cb->isChecked());
if (ui->m_edit_sql_query_cb->isChecked() && !m_custom_query.isEmpty())
{
ui->m_sql_query->setText(m_custom_query);
}
else if (!ui->m_edit_sql_query_cb->isChecked())
{
m_custom_query = ui->m_sql_query->text();
updateQueryLine();
}
}
/**
@brief BOMExportDialog::on_m_save_current_conf_pb_clicked
Save the current query to file
*/
void BOMExportDialog::on_m_save_current_conf_pb_clicked()
{
QFile file(QETApp::configDir() + "/bill_of_materials.json");
if (file.open(QFile::ReadWrite))
{
QJsonDocument jsd(QJsonDocument::fromJson(file.readAll()));
QJsonObject root_object;
if (!jsd.isEmpty())
{
root_object = jsd.object();
if (root_object.contains(ui->m_save_name_le->text())) {
root_object.remove(ui->m_save_name_le->text());
}
}
QVariantMap vm;
vm.insert("user query", ui->m_edit_sql_query_cb->isChecked());
if (ui->m_edit_sql_query_cb->isChecked()) {
vm.insert("query", ui->m_sql_query->text());
}
else
{
vm.insert("header", ui->m_include_header_cb->isChecked());
vm.insert("format as bill of material", ui->m_format_as_bom_rb->isChecked());
QJsonArray keys_array;
for (auto key : selectedKeys()) {
keys_array.append(QJsonValue(key));
}
vm.insert("selected infos", keys_array);
QJsonArray selected_elements_array;
for (auto button : m_button_group.buttons())
{
QJsonObject element_type;
element_type.insert("checked", button->isChecked());
element_type.insert("ID", m_button_group.id(button));
selected_elements_array.append(element_type);
}
vm.insert("selected elements", selected_elements_array);
}
root_object[ui->m_save_name_le->text()] = QJsonObject::fromVariantMap(vm);
jsd.setObject(root_object);
file.resize(0);
file.write(jsd.toJson());
}
}
/**
@brief BOMExportDialog::on_m_load_pb_clicked
Load the current selected query from file
*/
void BOMExportDialog::on_m_load_pb_clicked()
{
auto name = ui->m_conf_cb->currentText();
if (name.isEmpty()) {
return;
}
QFile file(QETApp::configDir() + "/bill_of_materials.json");
if (!file.open(QFile::ReadOnly)) {
return;
}
QJsonDocument jsd(QJsonDocument::fromJson(file.readAll()));
QJsonObject jso = jsd.object();
auto value = jso.value(name);
if (!value.isObject()) {
return;
}
auto value_object = value.toObject();
if (value_object["user query"].toBool())
{
ui->m_edit_sql_query_cb->setChecked(true);
ui->m_sql_query->setText(value_object["query"].toString());
} }
else else
{ {
ui->m_edit_sql_query_cb->setChecked(false); //HEADERS
ui->m_include_header_cb->setChecked(value_object["header"].toBool()); if (ui->m_include_headers)
ui->m_format_as_bom_rb->setChecked(value_object["format as bill of material"].toBool());
//Ugly hack to force to remove all selected infos
while (auto item = ui->m_choosen_list->takeItem(0)) {
ui->m_var_list->addItem(item);
}
QVariantList vl = value_object["selected infos"].toArray().toVariantList();
for (auto variant : vl)
{ {
for (auto item : m_items_list) auto record_ = query_.record();
QStringList header_name;
for (auto i=0 ; i<record_.count() ; ++i)
{ {
if (item->data(Qt::UserRole).toString() == variant.toString()) auto field_name = record_.fieldName(i);
{
ui->m_var_list->takeItem(ui->m_var_list->row(item)); qDebug() << "field name = " << field_name;
ui->m_choosen_list->addItem(item); 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 << QETApp::elementTranslatedInfoKey(field_name);
if (header_name.isEmpty()) {
header_name << QETApp::diagramTranslatedInfoKey(field_name);
}
if (header_name.isEmpty()) {
header_name << field_name;
}
} }
} }
return_string = header_name.join(";") + "\n";
} }
QJsonArray selected_elements_array = value_object["selected elements"].toArray(); //ROWS
for (int id=0 ; id<selected_elements_array.size() ; ++id) while (query_.next())
{ {
QJsonObject obj = selected_elements_array[id].toObject(); auto i=0;
m_button_group.button(obj["ID"].toInt())->setChecked(obj["checked"].toBool()); QStringList values;
while (query_.value(i).isValid())
{
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;
}
return_string += values.join(";") + "\n";
values.clear();
} }
updateQueryLine();
} }
on_m_edit_sql_query_cb_clicked(); //Force to update dialog qDebug() << return_string;
return return_string;
}
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);
} }

View File

@@ -1,81 +1,53 @@
/* /*
Copyright 2006-2020 The QElectroTech Team Copyright 2006-2020 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef BOMExportDialog_H #ifndef BOMEXPORTDIALOG_H
#define BOMExportDialog_H #define BOMEXPORTDIALOG_H
#include <QDialog> #include <QDialog>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QButtonGroup>
class QListWidgetItem;
class QETProject; class QETProject;
class Element; class ElementQueryWidget;
namespace Ui { namespace Ui {
class BOMExportDialog; class BOMExportDialog;
} }
/**
* @brief The BOMExportDialog class
*/
class BOMExportDialog : public QDialog class BOMExportDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit BOMExportDialog(QETProject *project, explicit BOMExportDialog(QETProject *project, QWidget *parent = nullptr);
QWidget *parent = nullptr);
~BOMExportDialog() override; ~BOMExportDialog() override;
virtual int exec() override; virtual int exec() override;
QStringList selectedKeys() const; QString getBom();
QString translatedKeys(const QString &key) const;
private slots: private slots:
void on_m_add_pb_clicked(); void on_m_format_as_bom_clicked(bool checked);
void on_m_remove_pb_clicked();
void on_m_up_pb_clicked();
void on_m_down_pb_clicked();
void on_m_save_name_le_textChanged(const QString &arg1);
void on_m_format_as_nomenclature_rb_toggled(bool checked);
void on_m_edit_sql_query_cb_clicked();
void on_m_save_current_conf_pb_clicked();
void on_m_load_pb_clicked();
private: private:
void setUpItems();
QString getBom();
QString headers() const;
bool createDataBase();
void populateDataBase();
QHash<QString, QString> elementInfoToString(
Element *elmt) const;
QString queryStr() const;
void updateQueryLine();
void fillSavedQuery();
private:
Ui::BOMExportDialog *ui; Ui::BOMExportDialog *ui;
ElementQueryWidget *m_query_widget = nullptr;
QETProject *m_project = nullptr; QETProject *m_project = nullptr;
QSqlDatabase m_data_base;
QSqlQuery m_insert_query;
QString m_custom_query;
QHash <QString, QString> m_export_info;
QButtonGroup m_button_group;
QList <QListWidgetItem *> m_items_list;
}; };
#endif // BOMExportDialog_H #endif // BOMEXPORTDIALOG_H

View File

@@ -6,420 +6,58 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>436</width> <width>610</width>
<height>657</height> <height>232</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Export CSV</string> <string>Dialog</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="m_main_layout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<widget class="QWidget" name="m_info_widget" native="true">
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Informations disponibles</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label">
<property name="text">
<string>Informations à exporter</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="margin">
<number>0</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QListWidget" name="m_var_list"/>
</item>
<item row="1" column="2">
<widget class="QListWidget" name="m_choosen_list"/>
</item>
<item row="1" column="1">
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,0,0,0,0,0">
<property name="spacing">
<number>6</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="m_up_pb">
<property name="toolTip">
<string>Monter la sélection</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../qelectrotech.qrc">
<normaloff>:/ico/16x16/go-up.png</normaloff>:/ico/16x16/go-up.png</iconset>
</property>
<property name="checkable">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_add_pb">
<property name="toolTip">
<string>Ajouter la sélection</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../qelectrotech.qrc">
<normaloff>:/ico/16x16/list-add.png</normaloff>:/ico/16x16/list-add.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_remove_pb">
<property name="toolTip">
<string>Supprimer la sélection</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../qelectrotech.qrc">
<normaloff>:/ico/16x16/list-remove.png</normaloff>:/ico/16x16/list-remove.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_down_pb">
<property name="toolTip">
<string>Descendre la sélection</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../qelectrotech.qrc">
<normaloff>:/ico/16x16/go-down.png</normaloff>:/ico/16x16/go-down.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="m_parametre_widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Type d'éléments</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="4" column="0">
<widget class="QCheckBox" name="m_simple_cb">
<property name="text">
<string>Simples</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="m_all_cb">
<property name="text">
<string>Tous</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QCheckBox" name="m_coil_cb">
<property name="text">
<string>Contacteurs et relais</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="m_button_cb">
<property name="text">
<string>Boutons et commutateurs</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="m_terminal_cb">
<property name="text">
<string>Borniers</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="m_protection_cb">
<property name="text">
<string>Organes de protection</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="m_format_as_gb">
<property name="title">
<string>Mise en page</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="flat">
<bool>false</bool>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="1">
<widget class="QCheckBox" name="m_include_header_cb">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Inclure les en-têtes</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QRadioButton" name="m_format_as_nomenclature_rb">
<property name="toolTip">
<string>Chaque élément portant la même référence sera listé</string>
</property>
<property name="text">
<string>Formater en tant que nomenclature</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QRadioButton" name="m_format_as_bom_rb">
<property name="toolTip">
<string>Une même référence utilisé par plusieurs éléments ne sera listé qu'une fois</string>
</property>
<property name="text">
<string>Formater en tant que liste de matériel</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="Line" name="line_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item> <item>
<widget class="QGroupBox" name="groupBox"> <widget class="QGroupBox" name="groupBox">
<property name="title"> <property name="title">
<string>Configuration</string> <string>Mise en page</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item row="0" column="1"> <item>
<widget class="QPushButton" name="m_load_pb"> <widget class="QCheckBox" name="m_include_headers">
<property name="enabled"> <property name="text">
<string>inclure les en-têtes</string>
</property>
<property name="checked">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="toolTip"> </widget>
<string>Ouvrir la configuration sélectionné</string> </item>
</property> <item>
<widget class="QCheckBox" name="m_format_as_bom">
<property name="text"> <property name="text">
<string/> <string>Formater en tant que liste de materiel</string>
</property> </property>
<property name="icon"> <property name="checked">
<iconset resource="../../qelectrotech.qrc"> <bool>true</bool>
<normaloff>:/ico/16x16/folder-open.png</normaloff>:/ico/16x16/folder-open.png</iconset>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="0"> <item>
<widget class="QComboBox" name="m_conf_cb"> <spacer name="verticalSpacer">
<property name="editable"> <property name="orientation">
<bool>false</bool> <enum>Qt::Vertical</enum>
</property> </property>
</widget> <property name="sizeHint" stdset="0">
</item> <size>
<item row="1" column="0"> <width>20</width>
<widget class="QLineEdit" name="m_save_name_le"/> <height>40</height>
</item> </size>
<item row="1" column="1">
<widget class="QPushButton" name="m_save_current_conf_pb">
<property name="enabled">
<bool>false</bool>
</property> </property>
<property name="toolTip"> </spacer>
<string>Sauvegarder la configuration actuelle</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../qelectrotech.qrc">
<normaloff>:/ico/16x16/document-save.png</normaloff>:/ico/16x16/document-save.png</iconset>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item> </item>
</layout> </layout>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="Line" name="line_3"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="m_edit_sql_query_cb">
<property name="toolTip">
<string>Requête SQL personnalisée</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Requête SQL :</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="m_sql_query">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="m_button_box">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
@@ -430,12 +68,10 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<resources> <resources/>
<include location="../../qelectrotech.qrc"/>
</resources>
<connections> <connections>
<connection> <connection>
<sender>m_button_box</sender> <sender>buttonBox</sender>
<signal>accepted()</signal> <signal>accepted()</signal>
<receiver>BOMExportDialog</receiver> <receiver>BOMExportDialog</receiver>
<slot>accept()</slot> <slot>accept()</slot>
@@ -451,7 +87,7 @@
</hints> </hints>
</connection> </connection>
<connection> <connection>
<sender>m_button_box</sender> <sender>buttonBox</sender>
<signal>rejected()</signal> <signal>rejected()</signal>
<receiver>BOMExportDialog</receiver> <receiver>BOMExportDialog</receiver>
<slot>reject()</slot> <slot>reject()</slot>
@@ -466,37 +102,5 @@
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>m_var_list</sender>
<signal>itemDoubleClicked(QListWidgetItem*)</signal>
<receiver>m_add_pb</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>165</x>
<y>173</y>
</hint>
<hint type="destinationlabel">
<x>342</x>
<y>158</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_choosen_list</sender>
<signal>itemDoubleClicked(QListWidgetItem*)</signal>
<receiver>m_remove_pb</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>519</x>
<y>173</y>
</hint>
<hint type="destinationlabel">
<x>342</x>
<y>188</y>
</hint>
</hints>
</connection>
</connections> </connections>
</ui> </ui>