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)
{
m_insert_diagram_query.bindValue(":uuid", diagram->uuid().toString());

View File

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

View File

@ -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<Element>(elmt), QPair(old_info, new_info));
setText(QObject::tr("Modifier les informations de l'élément : %1")
.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)
{
if (id() != other->id()) return false;
ChangeElementInformationCommand const *undo =
static_cast<const ChangeElementInformationCommand*>(other);
if (m_element != undo->m_element) return false;
m_new_info = undo->m_new_info;
if (id() != other->id())
return false;
ChangeElementInformationCommand const *other_undo = static_cast<const ChangeElementInformationCommand*>(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<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,
QUndoCommand *parent = nullptr);
ChangeElementInformationCommand(QMap<QPointer<Element>, QPair<DiagramContext, DiagramContext>> 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<QPointer<Element>, QPair<DiagramContext, DiagramContext>> m_map;
};
#endif // CHANGEELEMENTINFORMATIONCOMMAND_H