From a303a38d0d1db9afb721e8abdb1b4414b8d9d356 Mon Sep 17 00:00:00 2001 From: Claveau Joshua Date: Sun, 22 Nov 2020 11:31:43 +0100 Subject: [PATCH] ProjectDatabase improvement Use transaction / commit when several elements was changed instead of update one by one each element. --- sources/dataBase/projectdatabase.cpp | 10 +++ sources/dataBase/projectdatabase.h | 1 + .../changeelementinformationcommand.cpp | 64 ++++++++++++++----- .../changeelementinformationcommand.h | 6 +- 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/sources/dataBase/projectdatabase.cpp b/sources/dataBase/projectdatabase.cpp index 1cb6795d6..a433276a1 100644 --- a/sources/dataBase/projectdatabase.cpp +++ b/sources/dataBase/projectdatabase.cpp @@ -171,6 +171,16 @@ void projectDataBase::elementInfoChanged(Element *element) } } +void projectDataBase::elementInfoChanged(QList elements) +{ + m_data_base.transaction(); + for (auto elmt : elements) { + elementInfoChanged(elmt); + } + m_data_base.commit(); + emit dataBaseUpdated(); +} + void projectDataBase::addDiagram(Diagram *diagram) { m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString()); diff --git a/sources/dataBase/projectdatabase.h b/sources/dataBase/projectdatabase.h index 9b7603979..9feae728c 100644 --- a/sources/dataBase/projectdatabase.h +++ b/sources/dataBase/projectdatabase.h @@ -51,6 +51,7 @@ class projectDataBase : public QObject void addElement (Element *element); void removeElement (Element *element); void elementInfoChanged (Element *element); + void elementInfoChanged (QList elements); void addDiagram (Diagram *diagram); void removeDiagram (Diagram *diagram); diff --git a/sources/undocommand/changeelementinformationcommand.cpp b/sources/undocommand/changeelementinformationcommand.cpp index 1bfd32cd3..908c35809 100644 --- a/sources/undocommand/changeelementinformationcommand.cpp +++ b/sources/undocommand/changeelementinformationcommand.cpp @@ -33,23 +33,44 @@ ChangeElementInformationCommand::ChangeElementInformationCommand( DiagramContext &old_info, DiagramContext &new_info, QUndoCommand *parent) : - QUndoCommand (parent), - m_element (elmt), - m_old_info (old_info), - m_new_info (new_info) + QUndoCommand (parent) { + m_map.insert(QPointer(elmt), QPair(old_info, new_info)); setText(QObject::tr("Modifier les informations de l'élément : %1") - .arg(elmt -> name())); + .arg(elmt -> name())); +} + +ChangeElementInformationCommand::ChangeElementInformationCommand(QMap, QPair > map, + QUndoCommand *parent) : + QUndoCommand(parent), + m_map(map) +{ + setText(QObject::tr("Modifier les informations de plusieurs éléments")); } bool ChangeElementInformationCommand::mergeWith(const QUndoCommand *other) { - if (id() != other->id()) return false; - ChangeElementInformationCommand const *undo = - static_cast(other); - if (m_element != undo->m_element) return false; - m_new_info = undo->m_new_info; - return true; + if (id() != other->id()) + return false; + + ChangeElementInformationCommand const *other_undo = static_cast(other); + + //In case of other undo_undo have the same elements as keys + //We move the QMap m_map of other_undo to this m_map. + if (m_map.size() == other_undo->m_map.size()) + { + for (auto key : other_undo->m_map.keys()) { + if (!m_map.keys().contains(key)) { + return false; + } + } + + m_map = other_undo->m_map; + return true; + } + else { + return false; + } } /** @@ -57,7 +78,9 @@ bool ChangeElementInformationCommand::mergeWith(const QUndoCommand *other) */ void ChangeElementInformationCommand::undo() { - m_element -> setElementInformations(m_old_info); + for (auto element : m_map.keys()) { + element->setElementInformations(m_map.value(element).first); + } updateProjectDB(); } @@ -66,14 +89,23 @@ void ChangeElementInformationCommand::undo() */ void ChangeElementInformationCommand::redo() { - m_element -> setElementInformations(m_new_info); + for (auto element : m_map.keys()) { + element->setElementInformations(m_map.value(element).second); + } updateProjectDB(); } void ChangeElementInformationCommand::updateProjectDB() { - if(m_element->diagram()) { - m_element->diagram()->project()->dataBase()->elementInfoChanged( - m_element); + auto elmt = m_map.keys().first().data(); + if(elmt && elmt->diagram()) + { + //need to have a list of element instead of QPointer + //for the function elementInfoChange of the database + QList list_; + for (auto p_elmt : m_map.keys()) + list_ << p_elmt.data(); + + elmt->diagram()->project()->dataBase()->elementInfoChanged(list_); } } diff --git a/sources/undocommand/changeelementinformationcommand.h b/sources/undocommand/changeelementinformationcommand.h index 968a626b5..9fc8db087 100644 --- a/sources/undocommand/changeelementinformationcommand.h +++ b/sources/undocommand/changeelementinformationcommand.h @@ -36,6 +36,9 @@ class ChangeElementInformationCommand : public QUndoCommand DiagramContext &new_info, QUndoCommand *parent = nullptr); + ChangeElementInformationCommand(QMap, QPair> map, + QUndoCommand *parent = nullptr); + int id() const override {return 1;} bool mergeWith(const QUndoCommand *other) override; void undo() override; @@ -45,8 +48,7 @@ class ChangeElementInformationCommand : public QUndoCommand void updateProjectDB(); private: - Element *m_element; - DiagramContext m_old_info, m_new_info; + QMap, QPair> m_map; }; #endif // CHANGEELEMENTINFORMATIONCOMMAND_H