Element texts group is fully managed by the undo stack

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5124 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2017-11-29 14:49:12 +00:00
parent 12b30e7f30
commit 4e440456fc
11 changed files with 611 additions and 132 deletions

View File

@@ -18,8 +18,15 @@
#include "addelementtextcommand.h"
#include "element.h"
#include "dynamicelementtextitem.h"
#include "elementtextitemgroup.h"
#include <QGraphicsScene>
/************************
* AddElementTextCommand*
* **********************/
AddElementTextCommand::AddElementTextCommand(Element *element, DynamicElementTextItem *deti, QUndoCommand *parent):
QUndoCommand(parent),
m_element(element),
@@ -40,7 +47,6 @@ AddElementTextCommand::~AddElementTextCommand()
void AddElementTextCommand::undo()
{
m_element->removeDynamicTextItem(m_text);
m_text->setParentItem(nullptr);
if(m_text->scene())
m_text->scene()->removeItem(m_text);
}
@@ -51,36 +57,253 @@ void AddElementTextCommand::redo()
m_element->addDynamicTextItem(m_text);
}
//RemoveElementTextCommand::RemoveElementTextCommand(DynamicElementTextItem *deti, QUndoCommand *parent) :
// QUndoCommand(parent),
// m_text(deti)
//{
// setText(QObject::tr("Supprimer un texte d'élément"));
// m_element = deti->ParentElement();
//}
//RemoveElementTextCommand::~RemoveElementTextCommand()
//{
// if(m_element && !m_element->dynamicTextItems().contains(m_text))
// delete m_text;
//}
/***********************
* AddTextsGroupCommand*
* *********************/
/**
* @brief AddTextsGroupCommand::AddTextsGroupCommand
* @param element : the element to add a new group
* @param groupe_name : the name of the group
* @param parent : parent undo
*/
AddTextsGroupCommand::AddTextsGroupCommand(Element *element, QString groupe_name, QUndoCommand *parent) :
QUndoCommand(parent),
m_element(element),
m_name(groupe_name)
{
setText(QObject::tr("Ajouter un groupe de textes d'élément"));
}
//void RemoveElementTextCommand::undo()
//{
// if(m_element)
// {
// m_text->setParentItem(m_element);
// m_element->addDynamicTextItem(m_text);
// }
//}
/**
* @brief AddTextsGroupCommand::~AddTextsGroupCommand
* Destructor
*/
AddTextsGroupCommand::~AddTextsGroupCommand()
{}
//void RemoveElementTextCommand::redo()
//{
// if(m_element && m_text->scene())
// {
// m_element->removeDynamicTextItem(m_text);
// m_text->setParentItem(nullptr);
// m_text->scene()->removeItem(m_text);
// }
//}
void AddTextsGroupCommand::undo()
{
if(m_element && m_group)
m_element.data()->removeTextGroup(m_group);
}
void AddTextsGroupCommand::redo()
{
if(m_element)
{
if(m_first_undo)
{
m_group = m_element.data()->addTextGroup(m_name);
m_first_undo = false;
}
else if(m_group)
m_element.data()->addTextGroup(m_group.data());
}
}
/**************************
* RemoveTextsGroupCommand*
* ************************/
/**
* @brief RemoveTextsGroupCommand::RemoveTextsGroupCommand
* @param element : The element where we remove a group
* @param group : the group to remove
* @param parent : the parent undo command
*/
RemoveTextsGroupCommand::RemoveTextsGroupCommand(Element *element, ElementTextItemGroup *group, QUndoCommand *parent) :
QUndoCommand(parent),
m_element(element),
m_group(group)
{
setText(QObject::tr("Supprimer un groupe de textes d'élément"));
}
RemoveTextsGroupCommand::~RemoveTextsGroupCommand()
{}
void RemoveTextsGroupCommand::undo()
{
if(m_element && m_group)
m_element.data()->addTextGroup(m_group.data());
}
void RemoveTextsGroupCommand::redo()
{
if(m_element && m_group)
m_element.data()->removeTextGroup(m_group.data());
}
/************************
* AddTextToGroupCommand*
* **********************/
/**
* @brief AddTextToGroupCommand::AddTextToGroupCommand
* @param text
* @param group
* @param parent
*/
AddTextToGroupCommand::AddTextToGroupCommand(DynamicElementTextItem *text, ElementTextItemGroup *group, QUndoCommand *parent) :
QUndoCommand(parent),
m_text(text),
m_group(group),
m_element(group->parentElement())
{
setText(QObject::tr("Insérer un texte d'élément dans un groupe de textes"));
}
/**
* @brief AddTextToGroupCommand::~AddTextToGroupCommand
* Destructor
*/
AddTextToGroupCommand::~AddTextToGroupCommand()
{
if(m_group && m_text && m_element)
{
if(!m_group.data()->texts().contains(m_text.data()) &&
!m_element.data()->dynamicTextItems().contains(m_text.data()))
delete m_text.data();
}
}
void AddTextToGroupCommand::undo()
{
if(m_element && m_group && m_text)
m_element.data()->removeTextFromGroup(m_text, m_group);
}
void AddTextToGroupCommand::redo()
{
if(m_element && m_group && m_text)
m_element.data()->addTextToGroup(m_text, m_group);
}
/*****************************
* RemoveTextFromGroupCommand*
* ***************************/
/**
* @brief RemoveTextFromGroupCommand::RemoveTextFromGroupCommand
* @param text : text to add to @group
* @param group
* @param parent : parent undo command
*/
RemoveTextFromGroupCommand::RemoveTextFromGroupCommand(DynamicElementTextItem *text, ElementTextItemGroup *group, QUndoCommand *parent):
QUndoCommand(parent),
m_text(text),
m_group(group),
m_element(group->parentElement())
{
setText(QObject::tr("Enlever un texte d'élément d'un groupe de textes"));
}
/**
* @brief RemoveTextFromGroupCommand::~RemoveTextFromGroupCommand
* Destructor
*/
RemoveTextFromGroupCommand::~RemoveTextFromGroupCommand()
{
if(m_group && m_text && m_element)
{
if(!m_group.data()->texts().contains(m_text.data()) &&
!m_element.data()->dynamicTextItems().contains(m_text.data()))
delete m_text.data();
}
}
void RemoveTextFromGroupCommand::undo()
{
if(m_element && m_group && m_text)
m_element.data()->addTextToGroup(m_text, m_group);
}
void RemoveTextFromGroupCommand::redo()
{
if(m_element && m_group && m_text)
m_element.data()->removeTextFromGroup(m_text, m_group);
}
/*****************************
* AlignmentTextsGroupCommand*
* ***************************/
/**
* @brief AlignmentTextsGroupCommand::AlignmentTextsGroupCommand
* @param group : Group to change the alignment
* @param new_alignment : the new alignment of the group
* @param parent : the parent QUndoCommand of this undo
*/
AlignmentTextsGroupCommand::AlignmentTextsGroupCommand(ElementTextItemGroup *group, Qt::Alignment new_alignment, QUndoCommand *parent) :
QUndoCommand(parent),
m_group(group),
m_previous_alignment(group->alignment()),
m_new_alignment(new_alignment)
{
setText(QObject::tr("Modifier l'alignement d'un groupe de textes"));
//Text haven't got alignment
if(m_previous_alignment != Qt::AlignLeft ||
m_previous_alignment != Qt::AlignVCenter ||
m_previous_alignment != Qt::AlignRight)
{
for(DynamicElementTextItem *deti : group->texts())
m_texts_pos.insert(deti, deti->pos());
}
}
/**
* @brief AlignmentTextsGroupCommand::~AlignmentTextsGroupCommand
* Destructor
*/
AlignmentTextsGroupCommand::~AlignmentTextsGroupCommand()
{}
/**
* @brief AlignmentTextsGroupCommand::mergeWith
* Try to merge this command with other command
* @param other
* @return true if was merged, else false
*/
bool AlignmentTextsGroupCommand::mergeWith(const QUndoCommand *other)
{
if (id() != other->id() || other->childCount())
return false;
AlignmentTextsGroupCommand const *undo = static_cast<const AlignmentTextsGroupCommand *>(other);
if (m_group != undo->m_group)
return false;
m_new_alignment= undo->m_new_alignment;
return true;
}
/**
* @brief AlignmentTextsGroupCommand::undo
*/
void AlignmentTextsGroupCommand::undo()
{
if(m_group)
{
m_group.data()->setAlignment(m_previous_alignment);
//The alignment befor this command was free, then we must
//to restor the pos of each texts
if(!m_texts_pos.isEmpty())
{
for(DynamicElementTextItem *deti : m_group.data()->texts())
{
if(m_texts_pos.keys().contains(deti))
deti->setPos(m_texts_pos.value(deti));
}
}
}
}
/**
* @brief AlignmentTextsGroupCommand::redo
*/
void AlignmentTextsGroupCommand::redo()
{
if(m_group)
m_group.data()->setAlignment(m_new_alignment);
}

View File

@@ -19,10 +19,16 @@
#define ADDELEMENTTEXTCOMMAND_H
#include <QUndoCommand>
#include <QPointer>
class Element;
class DynamicElementTextItem;
class ElementTextItemGroup;
/**
* @brief The AddElementTextCommand class
* Manage the adding of element text
*/
class AddElementTextCommand : public QUndoCommand
{
public:
@@ -37,18 +43,90 @@ class AddElementTextCommand : public QUndoCommand
DynamicElementTextItem *m_text = nullptr;
};
//class RemoveElementTextCommand : public QUndoCommand
//{
// public:
// RemoveElementTextCommand(DynamicElementTextItem *deti, QUndoCommand *parent = nullptr);
// virtual ~RemoveElementTextCommand();
/**
* @brief The AddTextsGroupCommand class
* Manage the adding of a texts group
*/
class AddTextsGroupCommand : public QUndoCommand
{
public:
AddTextsGroupCommand(Element *element, QString groupe_name, QUndoCommand *parent = nullptr);
~AddTextsGroupCommand() override;
// virtual void undo();
// virtual void redo();
// private:
// Element *m_element = nullptr;
// DynamicElementTextItem *m_text = nullptr;
//};
void undo() override;
void redo() override;
private:
QPointer<Element> m_element;
QPointer<ElementTextItemGroup> m_group;
QString m_name;
bool m_first_undo = true;
};
/**
* @brief The RemoveTextsGroupCommand class
* Manage the removinf of a texts group
*/
class RemoveTextsGroupCommand : public QUndoCommand
{
public:
RemoveTextsGroupCommand(Element *element, ElementTextItemGroup *group, QUndoCommand *parent = nullptr);
~RemoveTextsGroupCommand() override;
void undo() override;
void redo() override;
private:
QPointer<Element> m_element;
QPointer<ElementTextItemGroup> m_group;
};
class AddTextToGroupCommand : public QUndoCommand
{
public:
AddTextToGroupCommand(DynamicElementTextItem *text, ElementTextItemGroup *group, QUndoCommand *parent = nullptr);
~AddTextToGroupCommand() override;
void undo() override;
void redo() override;
private:
QPointer<DynamicElementTextItem> m_text;
QPointer<ElementTextItemGroup> m_group;
QPointer<Element> m_element;
};
class RemoveTextFromGroupCommand : public QUndoCommand
{
public:
RemoveTextFromGroupCommand(DynamicElementTextItem *text, ElementTextItemGroup *group, QUndoCommand *parent = nullptr);
~RemoveTextFromGroupCommand() override;
void undo() override;
void redo() override;
private:
QPointer<DynamicElementTextItem> m_text;
QPointer<ElementTextItemGroup> m_group;
QPointer<Element> m_element;
};
class AlignmentTextsGroupCommand : public QUndoCommand
{
public:
AlignmentTextsGroupCommand(ElementTextItemGroup *group, Qt::Alignment new_alignment, QUndoCommand *parent = nullptr);
~AlignmentTextsGroupCommand() override;
int id() const override{return 6;}
bool mergeWith(const QUndoCommand *other) override;
void undo() override;
void redo() override;
private:
QPointer<ElementTextItemGroup> m_group;
Qt::Alignment m_previous_alignment,
m_new_alignment;
QHash<DynamicElementTextItem *, QPointF> m_texts_pos;
};
#endif // ADDELEMENTTEXTCOMMAND_H

View File

@@ -21,6 +21,7 @@
#include "element.h"
#include "conductor.h"
#include "conductortextitem.h"
#include "elementtextitemgroup.h"
/**
* @brief DeleteQGraphicsItemCommand::DeleteQGraphicsItemCommand
@@ -44,7 +45,12 @@ DeleteQGraphicsItemCommand::DeleteQGraphicsItemCommand(Diagram *diagram, const D
}
for(DynamicElementTextItem *deti : m_removed_contents.m_element_texts)
m_elmt_text_hash.insert(deti, deti->parentElement());
{
if(deti->parentGroup())
m_grp_texts_hash.insert(deti, deti->parentGroup());
else
m_elmt_text_hash.insert(deti, deti->parentElement());
}
setText(QString(QObject::tr("supprimer %1", "undo caption - %1 is a sentence listing the removed content")).arg(m_removed_contents.sentence(DiagramContent::All)));
m_diagram->qgiManager().manage(m_removed_contents.items(DiagramContent::All));
@@ -72,8 +78,14 @@ void DeleteQGraphicsItemCommand::undo()
for(DynamicElementTextItem *deti : m_removed_contents.m_element_texts)
{
deti->setParentItem(m_elmt_text_hash.value(deti));
m_elmt_text_hash.value(deti)->addDynamicTextItem(deti);
if(m_elmt_text_hash.keys().contains(deti))
m_elmt_text_hash.value(deti)->addDynamicTextItem(deti);
else if (m_grp_texts_hash.keys().contains(deti))
{
Element *elmt = m_grp_texts_hash.value(deti)->parentElement();
elmt->addDynamicTextItem(deti);
elmt->addTextToGroup(deti, m_grp_texts_hash.value(deti));
}
}
}
@@ -109,6 +121,9 @@ void DeleteQGraphicsItemCommand::redo()
for(DynamicElementTextItem *deti : m_removed_contents.m_element_texts)
{
if(deti->parentGroup() && deti->parentGroup()->parentElement())
deti->parentGroup()->parentElement()->removeTextFromGroup(deti, deti->parentGroup());
deti->parentElement()->removeDynamicTextItem(deti);
deti->setParentItem(nullptr);
}

View File

@@ -22,6 +22,7 @@
#include "diagramcontent.h"
class Diagram;
class ElementTextItemGroup;
class DeleteQGraphicsItemCommand : public QUndoCommand
{
@@ -41,6 +42,7 @@ class DeleteQGraphicsItemCommand : public QUndoCommand
Diagram *m_diagram;
QHash <Element *, QList<Element *> > m_link_hash; /// keep linked element for each removed element linked to other element.
QHash <DynamicElementTextItem *, Element *> m_elmt_text_hash; /// Keep the parent element of each deleted dynamic element text item
QHash <DynamicElementTextItem *, ElementTextItemGroup *> m_grp_texts_hash; ///Keep the parent group of each deleted element text item
};
#endif // DELETEQGRAPHICSITEMCOMMAND_H