ProjectDatabase improvement

Use transaction / commit when several elements was changed instead of
update one by one each element.
This commit is contained in:
Claveau Joshua 2020-11-22 11:31:43 +01:00
parent 51bae7cdf1
commit a303a38d0d
4 changed files with 63 additions and 18 deletions

View File

@ -171,6 +171,16 @@ void projectDataBase::elementInfoChanged(Element *element)
} }
} }
void projectDataBase::elementInfoChanged(QList<Element *> elements)
{
m_data_base.transaction();
for (auto elmt : elements) {
elementInfoChanged(elmt);
}
m_data_base.commit();
emit dataBaseUpdated();
}
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());

View File

@ -51,6 +51,7 @@ class projectDataBase : public QObject
void addElement (Element *element); void addElement (Element *element);
void removeElement (Element *element); void removeElement (Element *element);
void elementInfoChanged (Element *element); void elementInfoChanged (Element *element);
void elementInfoChanged (QList<Element *> elements);
void addDiagram (Diagram *diagram); void addDiagram (Diagram *diagram);
void removeDiagram (Diagram *diagram); void removeDiagram (Diagram *diagram);

View File

@ -33,31 +33,54 @@ ChangeElementInformationCommand::ChangeElementInformationCommand(
DiagramContext &old_info, DiagramContext &old_info,
DiagramContext &new_info, DiagramContext &new_info,
QUndoCommand *parent) : QUndoCommand *parent) :
QUndoCommand (parent), QUndoCommand (parent)
m_element (elmt),
m_old_info (old_info),
m_new_info (new_info)
{ {
m_map.insert(QPointer<Element>(elmt), QPair(old_info, new_info));
setText(QObject::tr("Modifier les informations de l'élément : %1") setText(QObject::tr("Modifier les informations de l'élément : %1")
.arg(elmt -> name())); .arg(elmt -> name()));
} }
ChangeElementInformationCommand::ChangeElementInformationCommand(QMap<QPointer<Element>, QPair<DiagramContext, DiagramContext> > map,
QUndoCommand *parent) :
QUndoCommand(parent),
m_map(map)
{
setText(QObject::tr("Modifier les informations de plusieurs éléments"));
}
bool ChangeElementInformationCommand::mergeWith(const QUndoCommand *other) bool ChangeElementInformationCommand::mergeWith(const QUndoCommand *other)
{ {
if (id() != other->id()) return false; if (id() != other->id())
ChangeElementInformationCommand const *undo = return false;
static_cast<const ChangeElementInformationCommand*>(other);
if (m_element != undo->m_element) return false; ChangeElementInformationCommand const *other_undo = static_cast<const ChangeElementInformationCommand*>(other);
m_new_info = undo->m_new_info;
//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; return true;
} }
else {
return false;
}
}
/** /**
@brief ChangeElementInformationCommand::undo @brief ChangeElementInformationCommand::undo
*/ */
void ChangeElementInformationCommand::undo() void ChangeElementInformationCommand::undo()
{ {
m_element -> setElementInformations(m_old_info); for (auto element : m_map.keys()) {
element->setElementInformations(m_map.value(element).first);
}
updateProjectDB(); updateProjectDB();
} }
@ -66,14 +89,23 @@ void ChangeElementInformationCommand::undo()
*/ */
void ChangeElementInformationCommand::redo() void ChangeElementInformationCommand::redo()
{ {
m_element -> setElementInformations(m_new_info); for (auto element : m_map.keys()) {
element->setElementInformations(m_map.value(element).second);
}
updateProjectDB(); updateProjectDB();
} }
void ChangeElementInformationCommand::updateProjectDB() void ChangeElementInformationCommand::updateProjectDB()
{ {
if(m_element->diagram()) { auto elmt = m_map.keys().first().data();
m_element->diagram()->project()->dataBase()->elementInfoChanged( if(elmt && elmt->diagram())
m_element); {
//need to have a list of element instead of QPointer<Element>
//for the function elementInfoChange of the database
QList<Element *> list_;
for (auto p_elmt : m_map.keys())
list_ << p_elmt.data();
elmt->diagram()->project()->dataBase()->elementInfoChanged(list_);
} }
} }

View File

@ -36,6 +36,9 @@ class ChangeElementInformationCommand : public QUndoCommand
DiagramContext &new_info, DiagramContext &new_info,
QUndoCommand *parent = nullptr); QUndoCommand *parent = nullptr);
ChangeElementInformationCommand(QMap<QPointer<Element>, QPair<DiagramContext, DiagramContext>> map,
QUndoCommand *parent = nullptr);
int id() const override {return 1;} int id() const override {return 1;}
bool mergeWith(const QUndoCommand *other) override; bool mergeWith(const QUndoCommand *other) override;
void undo() override; void undo() override;
@ -45,8 +48,7 @@ class ChangeElementInformationCommand : public QUndoCommand
void updateProjectDB(); void updateProjectDB();
private: private:
Element *m_element; QMap<QPointer<Element>, QPair<DiagramContext, DiagramContext>> m_map;
DiagramContext m_old_info, m_new_info;
}; };
#endif // CHANGEELEMENTINFORMATIONCOMMAND_H #endif // CHANGEELEMENTINFORMATIONCOMMAND_H