mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 20:50:34 +01:00
Improve update of the project database
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2006-2020 QElectroTech Team
|
Copyright 2006-2020 QElectroTech Team
|
||||||
This file is part of QElectroTech.
|
This file is part of QElectroTech.
|
||||||
|
|
||||||
@@ -42,6 +42,28 @@ projectDataBase::projectDataBase(QETProject *project, QObject *parent) :
|
|||||||
m_project(project)
|
m_project(project)
|
||||||
{
|
{
|
||||||
createDataBase();
|
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)
|
void projectDataBase::addDiagram(Diagram *diagram)
|
||||||
{
|
{
|
||||||
m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString());
|
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()) {
|
if(!m_insert_diagram_query.exec()) {
|
||||||
qDebug() << "projectDataBase::addDiagram insert error : " << m_insert_diagram_query.lastError();
|
qDebug() << "projectDataBase::addDiagram insert error : " << m_insert_diagram_query.lastError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bindDiagramInfoValues(m_insert_diagram_info_query, diagram);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_insert_diagram_info_query.exec()) {
|
if (!m_insert_diagram_info_query.exec()) {
|
||||||
qDebug() << "projectDataBase::addDiagram insert info error : " << m_insert_diagram_info_query.lastError();
|
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
|
@brief projectDataBase::createDataBase
|
||||||
Create the data base
|
Create the data base
|
||||||
@@ -342,7 +364,7 @@ void projectDataBase::populateDiagramTable()
|
|||||||
for (auto diagram : m_project->diagrams())
|
for (auto diagram : m_project->diagrams())
|
||||||
{
|
{
|
||||||
m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString());
|
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()) {
|
if(!m_insert_diagram_query.exec()) {
|
||||||
qDebug() << "projectDataBase::populateDiagramTable insert error : " << m_insert_diagram_query.lastError();
|
qDebug() << "projectDataBase::populateDiagramTable insert error : " << m_insert_diagram_query.lastError();
|
||||||
}
|
}
|
||||||
@@ -417,21 +439,7 @@ void projectDataBase::populateDiagramInfoTable()
|
|||||||
|
|
||||||
for (auto *diagram : m_project->diagrams())
|
for (auto *diagram : m_project->diagrams())
|
||||||
{
|
{
|
||||||
m_insert_diagram_info_query.bindValue(":uuid", diagram->uuid());
|
bindDiagramInfoValues(m_insert_diagram_info_query, diagram);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_insert_diagram_info_query.exec()) {
|
if (!m_insert_diagram_info_query.exec()) {
|
||||||
qDebug() << "projectDataBase::populateDiagramInfoTable insert error : " << m_insert_diagram_info_query.lastError();
|
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);
|
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
|
//INSERT ELEMENT
|
||||||
QString insert_element_query("INSERT INTO element (uuid, diagram_uuid, pos, type, sub_type) VALUES (:uuid, :diagram_uuid, :pos, :type, :sub_type)");
|
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);
|
m_insert_elements_query = QSqlQuery(m_data_base);
|
||||||
@@ -518,6 +542,25 @@ QHash<QString, QString> projectDataBase::elementInfoToString(Element *elmt)
|
|||||||
return hash;
|
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
|
#ifdef QET_EXPORT_PROJECT_DB
|
||||||
/**
|
/**
|
||||||
@brief projectDataBase::sqliteHandle
|
@brief projectDataBase::sqliteHandle
|
||||||
|
|||||||
@@ -47,11 +47,15 @@ class projectDataBase : public QObject
|
|||||||
void updateDB();
|
void updateDB();
|
||||||
QETProject *project() const;
|
QETProject *project() const;
|
||||||
QSqlQuery newQuery(const QString &query = QString());
|
QSqlQuery newQuery(const QString &query = QString());
|
||||||
void addElement(Element *element);
|
|
||||||
void removeElement(Element *element);
|
void addElement (Element *element);
|
||||||
void elementInfoChanged(Element *element);
|
void removeElement (Element *element);
|
||||||
void addDiagram(Diagram *diagram);
|
void elementInfoChanged (Element *element);
|
||||||
void removeDiagram(Diagram *diagram);
|
|
||||||
|
void addDiagram (Diagram *diagram);
|
||||||
|
void removeDiagram (Diagram *diagram);
|
||||||
|
void diagramInfoChanged (Diagram *diagram);
|
||||||
|
void diagramOrderChanged();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void dataBaseUpdated();
|
void dataBaseUpdated();
|
||||||
@@ -67,6 +71,7 @@ class projectDataBase : public QObject
|
|||||||
void prepareQuery();
|
void prepareQuery();
|
||||||
static QHash<QString, QString> elementInfoToString(
|
static QHash<QString, QString> elementInfoToString(
|
||||||
Element *elmt);
|
Element *elmt);
|
||||||
|
void bindDiagramInfoValues(QSqlQuery &query, Diagram *diagram);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QPointer<QETProject> m_project;
|
QPointer<QETProject> m_project;
|
||||||
@@ -77,7 +82,10 @@ class projectDataBase : public QObject
|
|||||||
m_update_element_query,
|
m_update_element_query,
|
||||||
m_insert_diagram_query,
|
m_insert_diagram_query,
|
||||||
m_remove_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
|
#ifdef QET_EXPORT_PROJECT_DB
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -1650,6 +1650,8 @@ void Diagram::setTitleBlockTemplate(const QString &template_name)
|
|||||||
|
|
||||||
if (template_name != current_name)
|
if (template_name != current_name)
|
||||||
emit(usedTitleBlockTemplateChanged(template_name));
|
emit(usedTitleBlockTemplateChanged(template_name));
|
||||||
|
|
||||||
|
project()->dataBase()->diagramInfoChanged(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1221,7 +1221,6 @@ void QETProject::removeDiagram(Diagram *diagram)
|
|||||||
|
|
||||||
if (m_diagrams_list.removeAll(diagram))
|
if (m_diagrams_list.removeAll(diagram))
|
||||||
{
|
{
|
||||||
m_data_base.removeDiagram(diagram);
|
|
||||||
emit(diagramRemoved(this, diagram));
|
emit(diagramRemoved(this, diagram));
|
||||||
diagram->deleteLater();
|
diagram->deleteLater();
|
||||||
}
|
}
|
||||||
@@ -1652,7 +1651,7 @@ void QETProject::addDiagram(Diagram *diagram, int pos)
|
|||||||
} else {
|
} else {
|
||||||
m_diagrams_list.insert(pos, diagram);
|
m_diagrams_list.insert(pos, diagram);
|
||||||
}
|
}
|
||||||
m_data_base.addDiagram(diagram);
|
|
||||||
updateDiagramsFolioData();
|
updateDiagramsFolioData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user