Conductors num can be exported to csv file.

This commit is contained in:
joshua
2019-10-04 19:37:46 +02:00
parent 8cc34d5f6a
commit 5de220c485
5 changed files with 214 additions and 18 deletions

View File

@@ -0,0 +1,133 @@
/*
Copyright 2006-2019 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 "conductornumexport.h"
#include "diagram.h"
#include "diagramcontent.h"
#include "conductor.h"
#include "terminal.h"
#include "element.h"
#include "conductortextitem.h"
#include <QFileDialog>
/**
* @brief ConductorNumExport::ConductorNumExport
* @param project : the project to export the conductors num
* @param parent : parent widget
*/
ConductorNumExport::ConductorNumExport(QETProject *project, QWidget *parent) :
m_project(project),
m_parent_widget(parent)
{
fillHash();
}
/**
* @brief ConductorNumExport::toCsv
* Export the num of conductors into a csv file.
* @return true if suceesfully exported.
*/
bool ConductorNumExport::toCsv()
{
QString name = QObject::tr("numero_de_fileries_") + m_project->title();
if(!name.endsWith(".csv")) {
name += ".csv";
}
QString filename = QFileDialog::getSaveFileName(m_parent_widget, QObject::tr("Enregister sous... "), name, QObject::tr("Fichiers csv (*.csv)"));
QFile file(filename);
if(!filename.isEmpty())
{
if(QFile::exists(filename))
{
// if file already exist -> delete it
if(!QFile::remove(filename))
{
QMessageBox::critical(m_parent_widget, QObject::tr("Erreur"),
QObject::tr("Impossible de remplacer le fichier!\n\n")+
"Destination : "+filename+"\n");
return false;
}
}
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream stream(&file);
stream << wiresNum() << endl;
}
else {
return false;
}
}
else {
return false;
}
return true;
}
/**
* @brief ConductorNumExport::wiresNum
* @return the wire num formated in csv
*/
QString ConductorNumExport::wiresNum() const
{
QString csv;
QStringList list = m_hash.keys();
list.sort();
for (QString key : list)
{
for (int i=0; i<m_hash.value(key) ; ++i) {
csv.append(key + "\n");
}
}
return csv;
}
void ConductorNumExport::fillHash()
{
//We used this rx to avoid insert num composed only withe white space.
QRegularExpression rx("^ *$");
for (Diagram *d : m_project->diagrams())
{
DiagramContent dc(d, false);
for (Conductor *c : dc.conductors())
{
QString num = c->textItem()->toPlainText();
if (num.isEmpty() || num.contains(rx)) {
continue;
}
//We must to define if the connected terminal is a folio report, if it is the case
//we don't add the num to the hash because the terminal doesn't represent a real terminal.
if(!(c->terminal1->parentElement()->linkType() & Element::AllReport))
{
int value = m_hash.value(num, 0);
++value;
m_hash.insert(num, value);
}
if(!(c->terminal2->parentElement()->linkType() & Element::AllReport))
{
int value = m_hash.value(num, 0);
++value;
m_hash.insert(num, value);
}
}
}
}

View File

@@ -0,0 +1,46 @@
/*
Copyright 2006-2019 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/>.
*/
#ifndef ConductorNumExport_H
#define ConductorNumExport_H
#include <QHash>
class QETProject;
class QWidget;
/**
* @brief The ConductorNumExport class
* A class to export the num of conductors into a csv file.
*/
class ConductorNumExport
{
public:
ConductorNumExport(QETProject *project, QWidget *parent = nullptr);
bool toCsv();
QString wiresNum() const;
private:
void fillHash();
private:
QETProject *m_project = nullptr;
QHash <QString, int> m_hash;
QWidget *m_parent_widget = nullptr;
};
#endif // ConductorNumExport_H

View File

@@ -88,23 +88,26 @@ DiagramContent::DiagramContent(Diagram *diagram, bool selected) :
//For each selected element, we determine if conductors must be moved or updated.
for(Element *elmt : m_elements)
{
for(Terminal *terminal : elmt->terminals())
{
for(Conductor *conductor : terminal->conductors())
{
Terminal *other_terminal;
if (conductor->terminal1 == terminal)
other_terminal = conductor->terminal2;
else
other_terminal = conductor->terminal1;
if (elmt->isSelected())
{
for(Terminal *terminal : elmt->terminals())
{
for(Conductor *conductor : terminal->conductors())
{
Terminal *other_terminal;
if (conductor->terminal1 == terminal)
other_terminal = conductor->terminal2;
else
other_terminal = conductor->terminal1;
//If the two elements of conductor are movable
if (m_elements.contains(other_terminal -> parentElement()) && !m_conductors_to_move.contains(conductor))
m_conductors_to_move << conductor;
else if (!m_conductors_to_update.contains(conductor))
m_conductors_to_update << conductor;
}
}
//If the two elements of conductor are movable
if (m_elements.contains(other_terminal -> parentElement()) && !m_conductors_to_move.contains(conductor))
m_conductors_to_move << conductor;
else if (!m_conductors_to_update.contains(conductor))
m_conductors_to_update << conductor;
}
}
}
}
}

View File

@@ -48,6 +48,7 @@
#include "diagramcommands.h"
#include "dialogwaiting.h"
#include "addelementtextcommand.h"
#include "conductornumexport.h"
#include <QMessageBox>
#include <QStandardPaths>
@@ -402,6 +403,17 @@ void QETDiagramEditor::setUpActions()
//Lauch the plugin of terminal generator
m_project_terminalBloc = new QAction(QET::Icons::TerminalStrip, tr("Lancer le plugin de création de borniers"), this);
connect(m_project_terminalBloc, &QAction::triggered, this, &QETDiagramEditor::generateTerminalBlock);
//Export conductor num to csv
m_project_export_conductor_num = new QAction(QET::Icons::DocumentSpreadsheet, tr("Exporter la liste des noms de conducteurs"), this);
connect(m_project_export_conductor_num, &QAction::triggered, [this]() {
QETProject *project = this->currentProject();
if (project)
{
ConductorNumExport wne(project, this);
wne.toCsv();
}
});
//MDI view style
m_tabbed_view_mode = new QAction(tr("en utilisant des onglets"), this);
@@ -744,6 +756,7 @@ void QETDiagramEditor::setUpMenu() {
menu_project -> addSeparator();
menu_project -> addAction(m_project_folio_list);
menu_project -> addAction(m_project_nomenclature);
menu_project -> addAction(m_project_export_conductor_num);
menu_project -> addAction(m_project_terminalBloc);
main_tool_bar -> toggleViewAction() -> setStatusTip(tr("Affiche ou non la barre d'outils principale"));
@@ -1418,7 +1431,8 @@ void QETDiagramEditor::slot_updateActions()
m_close_file -> setEnabled(opened_project);
m_save_file -> setEnabled(opened_project);
m_save_file_as -> setEnabled(opened_project);
m_project_edit_properties -> setEnabled(opened_project);
m_project_edit_properties->setEnabled(opened_project);
m_project_export_conductor_num->setEnabled(opened_project);
//prj_terminalBloc -> setEnabled(opened_project);
m_rotate_texts -> setEnabled(editable_project);
m_project_add_diagram -> setEnabled(editable_project);
@@ -1429,7 +1443,6 @@ void QETDiagramEditor::slot_updateActions()
m_export_diagram -> setEnabled(opened_diagram);
m_print -> setEnabled(opened_diagram);
m_edit_diagram_properties -> setEnabled(opened_diagram);
m_project_nomenclature -> setEnabled(editable_project);
m_zoom_actions_group. setEnabled(opened_diagram);
m_select_actions_group. setEnabled(opened_diagram);
m_add_item_actions_group. setEnabled(editable_project);

View File

@@ -185,6 +185,7 @@ class QETDiagramEditor : public QETMainWindow
QAction *m_project_folio_list; ///< Sommaire des schemas
QAction *m_project_nomenclature; ///< generate nomenclature
QAction *m_project_terminalBloc; ///< generate terminal block
QAction *m_project_export_conductor_num; ///<Export the wire num to csv
QAction *m_tile_window; ///< Show MDI subwindows as tile
QAction *m_cascade_window; ///< Show MDI subwindows as cascade
QAction *m_previous_window; ///< Switch to the previous document