From b514c39883d66a2fc7e3d97c877b99d78c022b15 Mon Sep 17 00:00:00 2001 From: Claveau Joshua Date: Sun, 1 Nov 2020 21:27:40 +0100 Subject: [PATCH] Improve update of the project database --- sources/dataBase/projectdatabase.cpp | 111 +++++++++++++++++++-------- sources/dataBase/projectdatabase.h | 20 +++-- sources/diagram.cpp | 2 + sources/qetproject.cpp | 3 +- 4 files changed, 94 insertions(+), 42 deletions(-) diff --git a/sources/dataBase/projectdatabase.cpp b/sources/dataBase/projectdatabase.cpp index 0cd57524e..d4dbd8e10 100644 --- a/sources/dataBase/projectdatabase.cpp +++ b/sources/dataBase/projectdatabase.cpp @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2020 QElectroTech Team This file is part of QElectroTech. @@ -42,6 +42,28 @@ projectDataBase::projectDataBase(QETProject *project, QObject *parent) : m_project(project) { createDataBase(); + connect(m_project, &QETProject::diagramAdded, [this](QETProject *, Diagram *diagram) { + this->addDiagram(diagram); + }); + connect(m_project, &QETProject::diagramRemoved, [this](QETProject *, Diagram *diagram) { + this->removeDiagram(diagram); + }); + connect(m_project, &QETProject::projectDiagramsOrderChanged, [this]() + { + for (auto diagram : m_project->diagrams()) + { + m_diagram_order_changed.bindValue(":pos", m_project->folioIndex(diagram)+1); + m_diagram_order_changed.bindValue(":uuid", diagram->uuid()); + m_diagram_order_changed.exec(); + + + m_diagram_info_order_changed.bindValue(":folio", diagram->border_and_titleblock.titleblockInformation().value("folio")); + m_diagram_info_order_changed.bindValue(":uuid", diagram->uuid()); + m_diagram_info_order_changed.exec(); + + } + emit dataBaseUpdated(); + }); } /** @@ -151,27 +173,12 @@ 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)); + m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram)+1); 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", - QLocale::system().toDate(infos.value("date").toString(), - QLocale::ShortFormat)); - } else { - auto value = infos.value(key); - auto bind = key.prepend(":"); - m_insert_diagram_info_query.bindValue(bind, value); - } - } + bindDiagramInfoValues(m_insert_diagram_info_query, diagram); if (!m_insert_diagram_info_query.exec()) { qDebug() << "projectDataBase::addDiagram insert info error : " << m_insert_diagram_info_query.lastError(); @@ -190,6 +197,21 @@ void projectDataBase::removeDiagram(Diagram *diagram) } } +void projectDataBase::diagramInfoChanged(Diagram *diagram) +{ + bindDiagramInfoValues(m_update_diagram_info_query, diagram); + + if (!m_update_diagram_info_query.exec()) { + qDebug() << "projectDataBase::diagramInfoChanged update error : " << m_update_diagram_info_query.lastError(); + } else { + emit dataBaseUpdated(); + } +} + +void projectDataBase::diagramOrderChanged() +{ +} + /** @brief projectDataBase::createDataBase Create the data base @@ -342,7 +364,7 @@ void projectDataBase::populateDiagramTable() for (auto diagram : m_project->diagrams()) { m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString()); - m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram)); + m_insert_diagram_query.bindValue(":pos", m_project->folioIndex(diagram)+1); if(!m_insert_diagram_query.exec()) { qDebug() << "projectDataBase::populateDiagramTable insert error : " << m_insert_diagram_query.lastError(); } @@ -417,21 +439,7 @@ void projectDataBase::populateDiagramInfoTable() for (auto *diagram : m_project->diagrams()) { - 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", - QLocale::system().toDate(infos.value("date").toString(), - QLocale::ShortFormat)); - } else { - auto value = infos.value(key); - auto bind = key.prepend(":"); - m_insert_diagram_info_query.bindValue(bind, value); - } - } + bindDiagramInfoValues(m_insert_diagram_info_query, diagram); if (!m_insert_diagram_info_query.exec()) { qDebug() << "projectDataBase::populateDiagramInfoTable insert error : " << m_insert_diagram_info_query.lastError(); @@ -462,6 +470,22 @@ void projectDataBase::prepareQuery() ")"); m_insert_diagram_info_query.prepare(insert_diag_info); + //UPDATE DIAGRAM INFO + QString update_diagram_str("UPDATE diagram_info SET "); + for (auto str : QETApp::diagramInfoKeys()) { + update_diagram_str.append(str + " = :" + str + ", "); + } + update_diagram_str.remove(update_diagram_str.length()-2, 2); //Remove the last ", " + update_diagram_str.append(" WHERE diagram_uuid = :uuid"); + m_update_diagram_info_query = QSqlQuery(m_data_base); + m_update_diagram_info_query.prepare(update_diagram_str); + + //UPDATE DIAGRAM ORDER + m_diagram_order_changed = QSqlQuery(m_data_base); + m_diagram_order_changed.prepare("UPDATE diagram SET pos = :pos WHERE uuid = :uuid"); + m_diagram_info_order_changed = QSqlQuery(m_data_base); + m_diagram_info_order_changed.prepare("UPDATE diagram_info SET folio = :folio WHERE diagram_uuid = :uuid"); + //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); @@ -518,6 +542,25 @@ QHash projectDataBase::elementInfoToString(Element *elmt) return hash; } +void projectDataBase::bindDiagramInfoValues(QSqlQuery &query, Diagram *diagram) +{ + query.bindValue(":uuid", diagram->uuid()); + + auto infos = diagram->border_and_titleblock.titleblockInformation(); + for (auto key : QETApp::diagramInfoKeys()) + { + if (key == "date") { + query.bindValue( ":date", + QLocale::system().toDate(infos.value("date").toString(), + QLocale::ShortFormat)); + } else { + auto value = infos.value(key); + auto bind = key.prepend(":"); + query.bindValue(bind, value); + } + } +} + #ifdef QET_EXPORT_PROJECT_DB /** @brief projectDataBase::sqliteHandle diff --git a/sources/dataBase/projectdatabase.h b/sources/dataBase/projectdatabase.h index 2fe470f87..9b7603979 100644 --- a/sources/dataBase/projectdatabase.h +++ b/sources/dataBase/projectdatabase.h @@ -47,11 +47,15 @@ class projectDataBase : public QObject void updateDB(); QETProject *project() const; QSqlQuery newQuery(const QString &query = QString()); - void addElement(Element *element); - void removeElement(Element *element); - void elementInfoChanged(Element *element); - void addDiagram(Diagram *diagram); - void removeDiagram(Diagram *diagram); + + void addElement (Element *element); + void removeElement (Element *element); + void elementInfoChanged (Element *element); + + void addDiagram (Diagram *diagram); + void removeDiagram (Diagram *diagram); + void diagramInfoChanged (Diagram *diagram); + void diagramOrderChanged(); signals: void dataBaseUpdated(); @@ -67,6 +71,7 @@ class projectDataBase : public QObject void prepareQuery(); static QHash elementInfoToString( Element *elmt); + void bindDiagramInfoValues(QSqlQuery &query, Diagram *diagram); private: QPointer m_project; @@ -77,7 +82,10 @@ class projectDataBase : public QObject m_update_element_query, m_insert_diagram_query, m_remove_diagram_query, - m_insert_diagram_info_query; + m_insert_diagram_info_query, + m_update_diagram_info_query, + m_diagram_order_changed, + m_diagram_info_order_changed; #ifdef QET_EXPORT_PROJECT_DB public: diff --git a/sources/diagram.cpp b/sources/diagram.cpp index 5414c35bb..f93e0bcfe 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -1650,6 +1650,8 @@ void Diagram::setTitleBlockTemplate(const QString &template_name) if (template_name != current_name) emit(usedTitleBlockTemplateChanged(template_name)); + + project()->dataBase()->diagramInfoChanged(this); } /** diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 4e8238d3e..5d059ec2b 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -1221,7 +1221,6 @@ void QETProject::removeDiagram(Diagram *diagram) if (m_diagrams_list.removeAll(diagram)) { - m_data_base.removeDiagram(diagram); emit(diagramRemoved(this, diagram)); diagram->deleteLater(); } @@ -1652,7 +1651,7 @@ void QETProject::addDiagram(Diagram *diagram, int pos) } else { m_diagrams_list.insert(pos, diagram); } - m_data_base.addDiagram(diagram); + updateDiagramsFolioData(); }