diff --git a/sources/dataBase/projectdatabase.cpp b/sources/dataBase/projectdatabase.cpp index f24e1a230..93e3cea9c 100644 --- a/sources/dataBase/projectdatabase.cpp +++ b/sources/dataBase/projectdatabase.cpp @@ -56,10 +56,7 @@ projectDataBase::~projectDataBase() { /** * @brief projectDataBase::updateDB * Up to date the content of the data base. - * Except at the creation of this class, - * call this method each time you want to query the data base - * to be sure that the content reflect the current state of the project. - * Emit the singal dataBaseUpdated + * Emit the signal dataBaseUpdated */ void projectDataBase::updateDB() { @@ -149,6 +146,45 @@ void projectDataBase::elementInfoChanged(Element *element) } } +void projectDataBase::addDiagram(Diagram *diagram) +{ + m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString()); + m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram)); + if(!m_insert_diagram_query.exec()) { + qDebug() << "projectDataBase::addDiagram insert error : " << m_insert_diagram_query.lastError(); + } + + + m_insert_diagram_info_query.bindValue(":uuid", diagram->uuid()); + auto infos = diagram->border_and_titleblock.titleblockInformation(); + for (auto key : QETApp::diagramInfoKeys()) + { + if (key == "date") { + m_insert_diagram_info_query.bindValue(":date", QDate::fromString(infos.value("date").toString(), Qt::SystemLocaleShortDate)); + } else { + auto value = infos.value(key); + auto bind = key.prepend(":"); + m_insert_diagram_info_query.bindValue(bind, value); + } + } + + if (!m_insert_diagram_info_query.exec()) { + qDebug() << "projectDataBase::addDiagram insert info error : " << m_insert_diagram_info_query.lastError(); + } else { + emit dataBaseUpdated(); + } +} + +void projectDataBase::removeDiagram(Diagram *diagram) +{ + m_remove_diagram_query.bindValue(":uuid", diagram->uuid().toString()); + if (!m_remove_diagram_query.exec()) { + qDebug() << "projectDataBase::removeDiagram delete error : " << m_remove_diagram_query.lastError(); + } else { + emit dataBaseUpdated(); + } +} + /** * @brief projectDataBase::createDataBase * Create the data base @@ -313,14 +349,12 @@ void projectDataBase::populateDiagramTable() QSqlQuery query_(m_data_base); query_.exec("DELETE FROM diagram"); - QString insert_("INSERT INTO diagram (uuid, pos) VALUES (:uuid, :pos)"); - query_.prepare(insert_); for (auto diagram : m_project->diagrams()) { - query_.bindValue(":uuid", diagram->uuid().toString()); - query_.bindValue(":pos", m_project->folioIndex(diagram)); - if(!query_.exec()) { - qDebug() << "projectDataBase::populateDiagramTable insert error : " << query_.lastError(); + m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString()); + m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram)); + if(!m_insert_diagram_query.exec()) { + qDebug() << "projectDataBase::populateDiagramTable insert error : " << m_insert_diagram_query.lastError(); } } } @@ -391,43 +425,51 @@ void projectDataBase::populateDiagramInfoTable() QSqlQuery query(m_data_base); query.exec("DELETE FROM diagram_info"); - //Prepare the query used for insert new record - QStringList bind_values; - for (auto key : QETApp::diagramInfoKeys()) { - bind_values << key.prepend(":"); - } - QString insert("INSERT INTO diagram_info (diagram_uuid, " + - QETApp::diagramInfoKeys().join(", ") + - ") VALUES (:uuid, " + - bind_values.join(", ") + - ")"); - - query.prepare(insert); - for (auto *diagram : m_project->diagrams()) { - query.bindValue(":uuid", diagram->uuid()); + m_insert_diagram_info_query.bindValue(":uuid", diagram->uuid()); auto infos = diagram->border_and_titleblock.titleblockInformation(); for (auto key : QETApp::diagramInfoKeys()) { if (key == "date") { - query.bindValue(":date", QDate::fromString(infos.value("date").toString(), Qt::SystemLocaleShortDate)); + m_insert_diagram_info_query.bindValue(":date", QDate::fromString(infos.value("date").toString(), Qt::SystemLocaleShortDate)); } else { auto value = infos.value(key); auto bind = key.prepend(":"); - query.bindValue(bind, value); + m_insert_diagram_info_query.bindValue(bind, value); } } - if (!query.exec()) { - qDebug() << "projectDataBase::populateDiagramInfoTable insert error : " << query.lastError(); + if (!m_insert_diagram_info_query.exec()) { + qDebug() << "projectDataBase::populateDiagramInfoTable insert error : " << m_insert_diagram_info_query.lastError(); } } } void projectDataBase::prepareQuery() { + //INSERT DIAGRAM + m_insert_diagram_query = QSqlQuery(m_data_base); + m_insert_diagram_query.prepare("INSERT INTO diagram (uuid, pos) VALUES (:uuid, :pos)"); + + //REMOVE DIAGRAM + m_remove_diagram_query = QSqlQuery(m_data_base); + m_remove_diagram_query.prepare("DELETE FROM diagram WHERE uuid=:uuid"); + + //INSERT DIAGRAM INFO + m_insert_diagram_info_query = QSqlQuery(m_data_base); + QStringList bind_diag_info_values; + for (auto key : QETApp::diagramInfoKeys()) { + bind_diag_info_values << key.prepend(":"); + } + QString insert_diag_info("INSERT INTO diagram_info (diagram_uuid, " + + QETApp::diagramInfoKeys().join(", ") + + ") VALUES (:uuid, " + + bind_diag_info_values.join(", ") + + ")"); + m_insert_diagram_info_query.prepare(insert_diag_info); + //INSERT ELEMENT QString insert_element_query("INSERT INTO element (uuid, diagram_uuid, pos, type, sub_type) VALUES (:uuid, :diagram_uuid, :pos, :type, :sub_type)"); m_insert_elements_query = QSqlQuery(m_data_base); diff --git a/sources/dataBase/projectdatabase.h b/sources/dataBase/projectdatabase.h index 237dbf059..bcfca5659 100644 --- a/sources/dataBase/projectdatabase.h +++ b/sources/dataBase/projectdatabase.h @@ -26,6 +26,7 @@ class Element; class QETProject; +class Diagram; /** * @brief The projectDataBase class @@ -51,6 +52,8 @@ class projectDataBase : public QObject void addElement(Element *element); void removeElement(Element *element); void elementInfoChanged(Element *element); + void addDiagram(Diagram *diagram); + void removeDiagram(Diagram *diagram); signals: void dataBaseUpdated(); @@ -72,7 +75,10 @@ class projectDataBase : public QObject QSqlQuery m_insert_elements_query, m_insert_element_info_query, m_remove_element_query, - m_update_element_query; + m_update_element_query, + m_insert_diagram_query, + m_remove_diagram_query, + m_insert_diagram_info_query; public: static void exportDb(projectDataBase *db, QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString()); diff --git a/sources/diagram.cpp b/sources/diagram.cpp index 6fb9bdd14..6e09f01ca 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -60,7 +60,7 @@ QColor Diagram::background_color = Qt::white; */ Diagram::Diagram(QETProject *project) : QGraphicsScene (project), - m_project (nullptr), + m_project (project), diagram_qet_version_ (-1), draw_grid_ (true), use_border_ (true), @@ -76,8 +76,7 @@ Diagram::Diagram(QETProject *project) : //https://stackoverflow.com/questions/38458830/crash-after-qgraphicssceneremoveitem-with-custom-item-class //http://www.qtcentre.org/archive/index.php/t-33730.html //http://tech-artists.org/t/qt-properly-removing-qgraphicitems/3063 - - setProject(project); + qgi_manager_ = new QGIManager(this); setBackgroundBrush(Qt::white); conductor_setter_ = new QGraphicsLineItem(nullptr); @@ -1836,20 +1835,6 @@ QETProject *Diagram::project() const { return(m_project); } -/** - * @brief Diagram::setProject - * Set parent project of this diagram, project also become the parent QObject of this diagram - * @param project new project - */ -void Diagram::setProject(QETProject *project) -{ - if (m_project == project) - return; - - m_project = project; - setParent (project); -} - /** @return the folio number of this diagram within its parent project, or -1 if it is has no parent project diff --git a/sources/diagram.h b/sources/diagram.h index 8428a16dc..d9b020d5b 100644 --- a/sources/diagram.h +++ b/sources/diagram.h @@ -48,15 +48,21 @@ class DiagramEventInterface; This class represents an electric diagram. It manages its various child elements, conductors and texts and handles their graphic rendering. */ + +class DiagramFolioList; +class QETProject; + class Diagram : public QGraphicsScene { + friend DiagramFolioList; + friend QETProject; + Q_OBJECT // constructors, destructor - public: + private: Diagram(QETProject *project); ~Diagram() override; - private: Diagram(const Diagram &diagram); // ATTRIBUTES @@ -148,7 +154,6 @@ class Diagram : public QGraphicsScene // methods related to parent project QETProject *project() const; - void setProject(QETProject *); int folioIndex() const; qreal declaredQElectroTechVersion(bool = true) const; void showMe() {emit showDiagram(this);} diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index ad211116f..71f1ac82a 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -112,8 +112,10 @@ QETProject::QETProject(KAutoSaveFile *backup, QObject *parent) : * @brief QETProject::~QETProject * Destructor */ -QETProject::~QETProject() { - qDeleteAll(m_diagrams_list); +QETProject::~QETProject() +{ + for (auto diagram : m_diagrams_list) + diagram->deleteLater(); } /** @@ -871,7 +873,7 @@ QDomDocument QETProject::toXml() { qDebug() << qPrintable(QString("QETProject::toXml() : exporting diagram \"%1\"").arg(diagram -> title())) << "[" << diagram << "]"; QDomElement xml_diagram = diagram->toXml().documentElement(); QDomNode xml_node = xml_doc.importNode(xml_diagram, true); - + QDomNode appended_diagram = project_root.appendChild(xml_node); appended_diagram.toElement().setAttribute("order", order_num ++); } @@ -1209,18 +1211,24 @@ QList QETProject::addNewDiagramFolioList() return(diagram_list); } -/** - Enleve un schema du projet et emet le signal diagramRemoved - @param diagram le schema a enlever -*/ -void QETProject::removeDiagram(Diagram *diagram) { - // ne fait rien si le projet est en lecture seule - if (isReadOnly()) return; - if (!diagram || !m_diagrams_list.contains(diagram)) return; - if (m_diagrams_list.removeAll(diagram)) { +/** + * @brief QETProject::removeDiagram + * Remove @diagram from project + * @param diagram + */ +void QETProject::removeDiagram(Diagram *diagram) +{ + if (isReadOnly() || + !diagram || !m_diagrams_list.contains(diagram)) { + return; + } + + if (m_diagrams_list.removeAll(diagram)) + { + m_data_base.removeDiagram(diagram); emit(diagramRemoved(this, diagram)); - delete diagram; + diagram->deleteLater(); } updateDiagramsFolioData(); @@ -1333,8 +1341,6 @@ void QETProject::readProjectXml(QDomDocument &xml_project) */ void QETProject::readDiagramsXml(QDomDocument &xml_project) { - QMultiMap loaded_diagrams; - //@TODO try to solve a weird bug (dialog is black) since port to Qt5 with the DialogWaiting //show DialogWaiting DialogWaiting *dlgWaiting = nullptr; @@ -1364,27 +1370,17 @@ void QETProject::readDiagramsXml(QDomDocument &xml_project) { QDomElement diagram_xml_element = diagram_nodes.at(i).toElement(); Diagram *diagram = new Diagram(this); - bool diagram_loading = diagram -> initFromXml(diagram_xml_element); - if (diagram_loading) - { - if(dlgWaiting) - dlgWaiting->setDetail( diagram->title() ); - - //Get the attribute "order" of the diagram - int diagram_order = -1; - if (!QET::attributeIsAnInteger(diagram_xml_element, "order", &diagram_order)) diagram_order = 500000; - loaded_diagrams.insert(diagram_order, diagram); - } - else - { - delete diagram; - } + + int diagram_order = -1; + if (!QET::attributeIsAnInteger(diagram_xml_element, "order", &diagram_order)) diagram_order = 500000; + + addDiagram(diagram, diagram_order-1); + + diagram->initFromXml(diagram_xml_element); + if(dlgWaiting) + dlgWaiting->setDetail(diagram->title()); } } - - //Add the diagrams according to there "order" attribute - foreach(Diagram *diagram, loaded_diagrams.values()) - addDiagram(diagram); //Initialise links between elements in this project //and refresh the text of conductor @@ -1638,9 +1634,6 @@ void QETProject::addDiagram(Diagram *diagram, int pos) if (!diagram) { return; } - - // Ensure diagram know is parent project - diagram->setProject(this); connect(&diagram->border_and_titleblock, &BorderTitleBlock::needFolioData, this, &QETProject::updateDiagramsFolioData); connect(diagram, &Diagram::usedTitleBlockTemplateChanged, this, &QETProject::usedTitleBlockTemplateChanged); @@ -1650,7 +1643,7 @@ void QETProject::addDiagram(Diagram *diagram, int pos) } else { m_diagrams_list.insert(pos, diagram); } - + m_data_base.addDiagram(diagram); updateDiagramsFolioData(); }