mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-03-25 08:49:59 +01:00
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:
@@ -727,7 +727,7 @@ QDomElement Element::toXml(QDomDocument &document, QHash<Terminal *, int> &table
|
||||
//Set the alignment to top, because top is not used by groupand so,
|
||||
//each time a text is removed from the group, the alignement is not updated
|
||||
Qt::Alignment al = group->alignment();
|
||||
group->setAlignement(Qt::AlignTop);
|
||||
group->setAlignment(Qt::AlignTop);
|
||||
|
||||
//Remove the texts from group
|
||||
QList<DynamicElementTextItem *> deti_list = group->texts();
|
||||
@@ -743,7 +743,7 @@ QDomElement Element::toXml(QDomDocument &document, QHash<Terminal *, int> &table
|
||||
group->addToGroup(deti);
|
||||
|
||||
//Restor the alignement
|
||||
group->setAlignement(al);
|
||||
group->setAlignment(al);
|
||||
|
||||
//Save the group to xml
|
||||
texts_group.appendChild(group->toXml(document));
|
||||
@@ -759,7 +759,7 @@ QDomElement Element::toXml(QDomDocument &document, QHash<Terminal *, int> &table
|
||||
|
||||
/**
|
||||
* @brief Element::addDynamiqueTextItem
|
||||
* Add @deti as a dynamic text item of this element
|
||||
* Add @deti as a dynamic text item of this element, @deti is reparented to this
|
||||
* If @deti is null, a new DynamicElementTextItem is created and added to this element.
|
||||
* @param deti
|
||||
*/
|
||||
@@ -768,6 +768,7 @@ void Element::addDynamicTextItem(DynamicElementTextItem *deti)
|
||||
if (deti && !m_dynamic_text_list.contains(deti))
|
||||
{
|
||||
m_dynamic_text_list.append(deti);
|
||||
deti->setParentItem(this);
|
||||
emit textAdded(deti);
|
||||
}
|
||||
else
|
||||
@@ -780,9 +781,8 @@ void Element::addDynamicTextItem(DynamicElementTextItem *deti)
|
||||
|
||||
/**
|
||||
* @brief Element::removeDynamicTextItem
|
||||
* Remove @deti, no matter if is a child of this element or
|
||||
* a child of a group of this element.
|
||||
* The parent item of deti stay this item and deti is not deleted.
|
||||
* Remove @deti, no matter if is a child of this element or a child of a group of this element.
|
||||
* Set he parent item of @deti to 0, @deti is not deleted.
|
||||
* @param deti
|
||||
*/
|
||||
void Element::removeDynamicTextItem(DynamicElementTextItem *deti)
|
||||
@@ -790,6 +790,7 @@ void Element::removeDynamicTextItem(DynamicElementTextItem *deti)
|
||||
if (m_dynamic_text_list.contains(deti))
|
||||
{
|
||||
m_dynamic_text_list.removeOne(deti);
|
||||
deti->setParentItem(nullptr);
|
||||
emit textRemoved(deti);
|
||||
return;
|
||||
}
|
||||
@@ -800,6 +801,7 @@ void Element::removeDynamicTextItem(DynamicElementTextItem *deti)
|
||||
{
|
||||
removeTextFromGroup(deti, group);
|
||||
m_dynamic_text_list.removeOne(deti);
|
||||
deti->setParentItem(nullptr);
|
||||
emit textRemoved(deti);
|
||||
return;
|
||||
}
|
||||
@@ -848,10 +850,26 @@ ElementTextItemGroup *Element::addTextGroup(const QString &name)
|
||||
return group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Element::addTextGroup
|
||||
* @param group add group @group to the group of this element.
|
||||
* the group must not be owned by an element.
|
||||
*/
|
||||
void Element::addTextGroup(ElementTextItemGroup *group)
|
||||
{
|
||||
if(group->parentElement())
|
||||
return;
|
||||
|
||||
m_texts_group << group;
|
||||
group->setParentItem(this);
|
||||
emit textsGroupAdded(group);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Element::removeTextGroup
|
||||
* Remove the text group with name @name
|
||||
* All text owned by the group will be reparented to this element
|
||||
* Remove the text group @group from this element, and set the parent of group to 0.
|
||||
* group is not deleted.
|
||||
* All texts owned by the group will be reparented to this element
|
||||
* @param name
|
||||
*/
|
||||
void Element::removeTextGroup(ElementTextItemGroup *group)
|
||||
@@ -870,9 +888,10 @@ void Element::removeTextGroup(ElementTextItemGroup *group)
|
||||
}
|
||||
}
|
||||
|
||||
m_texts_group.removeOne(group);
|
||||
|
||||
emit textsGroupAboutToBeRemoved(group);
|
||||
delete group;
|
||||
m_texts_group.removeOne(group);
|
||||
group->setParentItem(nullptr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -912,10 +931,13 @@ bool Element::addTextToGroup(DynamicElementTextItem *text, ElementTextItemGroup
|
||||
return false;
|
||||
if(!m_texts_group.contains(group))
|
||||
return false;
|
||||
|
||||
m_dynamic_text_list.removeOne(text);
|
||||
emit textRemoved(text);
|
||||
|
||||
removeDynamicTextItem(text);
|
||||
group->addToGroup(text);
|
||||
emit textAddedToGroup(text, group);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -929,7 +951,7 @@ bool Element::removeTextFromGroup(DynamicElementTextItem *text, ElementTextItemG
|
||||
if(!m_texts_group.contains(group))
|
||||
return false;
|
||||
|
||||
if(group->childItems().contains(text))
|
||||
if(group->texts().contains(text))
|
||||
{
|
||||
group->removeFromGroup(text);
|
||||
emit textRemovedFromGroup(text, group);
|
||||
|
||||
@@ -209,6 +209,7 @@ class Element : public QetGraphicsItem
|
||||
void removeDynamicTextItem(DynamicElementTextItem *deti);
|
||||
QList<DynamicElementTextItem *> dynamicTextItems() const;
|
||||
ElementTextItemGroup *addTextGroup(const QString &name);
|
||||
void addTextGroup(ElementTextItemGroup *group);
|
||||
void removeTextGroup(ElementTextItemGroup *group);
|
||||
ElementTextItemGroup *textGroup(const QString &name) const;
|
||||
QList<ElementTextItemGroup *> textGroups() const;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "dynamicelementtextitem.h"
|
||||
#include "element.h"
|
||||
#include "diagram.h"
|
||||
#include "addelementtextcommand.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
@@ -34,8 +35,7 @@ bool sorting(QGraphicsItem *qgia, QGraphicsItem *qgib)
|
||||
*/
|
||||
ElementTextItemGroup::ElementTextItemGroup(const QString &name, Element *parent) :
|
||||
QGraphicsItemGroup(parent),
|
||||
m_name(name),
|
||||
m_element(parent)
|
||||
m_name(name)
|
||||
{
|
||||
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable);
|
||||
}
|
||||
@@ -53,14 +53,14 @@ void ElementTextItemGroup::addToGroup(QGraphicsItem *item)
|
||||
{
|
||||
item->setFlag(QGraphicsItem::ItemIsSelectable, false);
|
||||
QGraphicsItemGroup::addToGroup(item);
|
||||
updateAlignement();
|
||||
updateAlignment();
|
||||
|
||||
DynamicElementTextItem *deti = qgraphicsitem_cast<DynamicElementTextItem *>(item);
|
||||
connect(deti, &DynamicElementTextItem::fontSizeChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
connect(deti, &DynamicElementTextItem::textChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
connect(deti, &DynamicElementTextItem::textFromChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
connect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
connect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
connect(deti, &DynamicElementTextItem::fontSizeChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
connect(deti, &DynamicElementTextItem::textChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
connect(deti, &DynamicElementTextItem::textFromChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
connect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
connect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,15 +72,15 @@ void ElementTextItemGroup::removeFromGroup(QGraphicsItem *item)
|
||||
{
|
||||
QGraphicsItemGroup::removeFromGroup(item);
|
||||
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
|
||||
updateAlignement();
|
||||
updateAlignment();
|
||||
|
||||
if(DynamicElementTextItem *deti = qgraphicsitem_cast<DynamicElementTextItem *>(item))
|
||||
{
|
||||
disconnect(deti, &DynamicElementTextItem::fontSizeChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
disconnect(deti, &DynamicElementTextItem::textChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
disconnect(deti, &DynamicElementTextItem::textFromChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
disconnect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
disconnect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignement);
|
||||
disconnect(deti, &DynamicElementTextItem::fontSizeChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
disconnect(deti, &DynamicElementTextItem::textChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
disconnect(deti, &DynamicElementTextItem::textFromChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
disconnect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
disconnect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,25 +89,27 @@ void ElementTextItemGroup::removeFromGroup(QGraphicsItem *item)
|
||||
* Set the alignement of this group
|
||||
* @param alignement
|
||||
*/
|
||||
void ElementTextItemGroup::setAlignement(Qt::Alignment alignement)
|
||||
void ElementTextItemGroup::setAlignment(Qt::Alignment alignement)
|
||||
{
|
||||
m_alignement = alignement;
|
||||
updateAlignement();
|
||||
m_alignment = alignement;
|
||||
updateAlignment();
|
||||
}
|
||||
|
||||
Qt::Alignment ElementTextItemGroup::alignment() const
|
||||
{
|
||||
return m_alignement;
|
||||
return m_alignment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::setAlignement
|
||||
* @brief ElementTextItemGroup::setAlignment
|
||||
* Update the alignement of the items in this group, according
|
||||
* to the current alignement.
|
||||
* @param alignement
|
||||
*/
|
||||
void ElementTextItemGroup::updateAlignement()
|
||||
void ElementTextItemGroup::updateAlignment()
|
||||
{
|
||||
prepareGeometryChange();
|
||||
|
||||
QList <QGraphicsItem *> texts = childItems();
|
||||
if (texts.size() > 1)
|
||||
{
|
||||
@@ -116,7 +118,7 @@ void ElementTextItemGroup::updateAlignement()
|
||||
|
||||
qreal y_offset =0;
|
||||
|
||||
if(m_alignement == Qt::AlignLeft)
|
||||
if(m_alignment == Qt::AlignLeft)
|
||||
{
|
||||
QPointF ref = texts.first()->pos();
|
||||
|
||||
@@ -127,7 +129,7 @@ void ElementTextItemGroup::updateAlignement()
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if(m_alignement == Qt::AlignVCenter)
|
||||
else if(m_alignment == Qt::AlignVCenter)
|
||||
{
|
||||
QPointF ref(texts.first()->pos().x() + texts.first()->boundingRect().width()/2,
|
||||
texts.first()->pos().y());
|
||||
@@ -141,7 +143,7 @@ void ElementTextItemGroup::updateAlignement()
|
||||
return;
|
||||
|
||||
}
|
||||
else if (m_alignement == Qt::AlignRight)
|
||||
else if (m_alignment == Qt::AlignRight)
|
||||
{
|
||||
QPointF ref(texts.first()->pos().x() + texts.first()->boundingRect().width(),
|
||||
texts.first()->pos().y());
|
||||
@@ -193,6 +195,18 @@ Diagram *ElementTextItemGroup::diagram() const
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::parentElement
|
||||
* @return The parent element of this group or nullptr
|
||||
*/
|
||||
Element *ElementTextItemGroup::parentElement() const
|
||||
{
|
||||
if(parentItem() && parentItem()->type() == Element::Type)
|
||||
return static_cast<Element *>(parentItem());
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::toXml
|
||||
* Export data of this group to xml
|
||||
@@ -205,7 +219,7 @@ QDomElement ElementTextItemGroup::toXml(QDomDocument &dom_document) const
|
||||
dom_element.setAttribute("name", m_name);
|
||||
|
||||
QMetaEnum me = QMetaEnum::fromType<Qt::Alignment>();
|
||||
dom_element.setAttribute("alignment", me.valueToKey(m_alignement));
|
||||
dom_element.setAttribute("alignment", me.valueToKey(m_alignment));
|
||||
|
||||
QDomElement dom_texts = dom_document.createElement("texts");
|
||||
for(DynamicElementTextItem *deti : texts())
|
||||
@@ -233,19 +247,22 @@ void ElementTextItemGroup::fromXml(QDomElement &dom_element)
|
||||
|
||||
m_name = dom_element.attribute("name", "no name");
|
||||
QMetaEnum me = QMetaEnum::fromType<Qt::Alignment>();
|
||||
m_alignement = Qt::Alignment(me.keyToValue(dom_element.attribute("alignment").toStdString().data()));
|
||||
m_alignment = Qt::Alignment(me.keyToValue(dom_element.attribute("alignment").toStdString().data()));
|
||||
|
||||
for(QDomElement text : QET::findInDomElement(dom_element, "texts", "text"))
|
||||
if(parentElement())
|
||||
{
|
||||
DynamicElementTextItem *deti = nullptr;
|
||||
QUuid uuid(text.attribute("uuid"));
|
||||
|
||||
for(DynamicElementTextItem *txt : m_element->dynamicTextItems())
|
||||
if(txt->uuid() == uuid)
|
||||
deti = txt;
|
||||
|
||||
if (deti)
|
||||
m_element->addTextToGroup(deti, this);
|
||||
for(QDomElement text : QET::findInDomElement(dom_element, "texts", "text"))
|
||||
{
|
||||
DynamicElementTextItem *deti = nullptr;
|
||||
QUuid uuid(text.attribute("uuid"));
|
||||
|
||||
for(DynamicElementTextItem *txt : parentElement()->dynamicTextItems())
|
||||
if(txt->uuid() == uuid)
|
||||
deti = txt;
|
||||
|
||||
if (deti)
|
||||
parentElement()->addTextToGroup(deti, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,7 +338,11 @@ void ElementTextItemGroup::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
|
||||
QPointF old_pos = pos();
|
||||
if(m_first_move)
|
||||
{
|
||||
m_mouse_to_origin_movement = old_pos - event->buttonDownScenePos(Qt::LeftButton);
|
||||
if(parentElement())
|
||||
parentElement()->setHighlighted(true);
|
||||
}
|
||||
|
||||
QPointF expected_pos = event->scenePos() + m_mouse_to_origin_movement;
|
||||
setPos(Diagram::snapToGrid(expected_pos));
|
||||
@@ -344,7 +365,11 @@ void ElementTextItemGroup::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
void ElementTextItemGroup::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if(diagram())
|
||||
{
|
||||
diagram()->endMoveElementTexts();
|
||||
if(parentElement())
|
||||
parentElement()->setHighlighted(false);
|
||||
}
|
||||
|
||||
if(!(event->modifiers() & Qt::ControlModifier))
|
||||
QGraphicsItemGroup::mouseReleaseEvent(event);
|
||||
@@ -355,13 +380,27 @@ void ElementTextItemGroup::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
* @param event
|
||||
*/
|
||||
void ElementTextItemGroup::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
prepareGeometryChange();
|
||||
if(event->key() == Qt::Key_A)
|
||||
setAlignement(Qt::AlignLeft);
|
||||
else if (event->key() == Qt::Key_Z)
|
||||
setAlignement(Qt::AlignVCenter);
|
||||
else if (event->key() == Qt::Key_E)
|
||||
setAlignement(Qt::AlignRight);
|
||||
{
|
||||
if(event->key() == Qt::Key_A && m_alignment != Qt::AlignLeft)
|
||||
{
|
||||
if(diagram())
|
||||
diagram()->undoStack().push(new AlignmentTextsGroupCommand(this, Qt::AlignLeft));
|
||||
else
|
||||
setAlignment(Qt::AlignLeft);
|
||||
}
|
||||
else if (event->key() == Qt::Key_Z && m_alignment != Qt::AlignVCenter)
|
||||
{
|
||||
if(diagram())
|
||||
diagram()->undoStack().push(new AlignmentTextsGroupCommand(this, Qt::AlignVCenter));
|
||||
else
|
||||
setAlignment(Qt::AlignVCenter);
|
||||
}
|
||||
else if (event->key() == Qt::Key_E && m_alignment != Qt::AlignRight)
|
||||
{
|
||||
if(diagram())
|
||||
diagram()->undoStack().push(new AlignmentTextsGroupCommand(this, Qt::AlignRight));
|
||||
else
|
||||
setAlignment(Qt::AlignRight);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,19 +35,22 @@ class ElementTextItemGroup : public QObject, public QGraphicsItemGroup
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
|
||||
|
||||
public:
|
||||
ElementTextItemGroup(const QString &name, Element *parent);
|
||||
~ElementTextItemGroup() override;
|
||||
void addToGroup(QGraphicsItem *item);
|
||||
void removeFromGroup(QGraphicsItem *item);
|
||||
|
||||
void setAlignement(Qt::Alignment alignement);
|
||||
void setAlignment(Qt::Alignment alignement);
|
||||
Qt::Alignment alignment() const;
|
||||
void updateAlignement();
|
||||
void updateAlignment();
|
||||
void setName(QString name);
|
||||
QString name() const {return m_name;}
|
||||
QList<DynamicElementTextItem *> texts() const;
|
||||
Diagram *diagram() const;
|
||||
Element *parentElement() const;
|
||||
|
||||
QDomElement toXml(QDomDocument &dom_document) const;
|
||||
void fromXml(QDomElement &dom_element);
|
||||
@@ -63,11 +66,10 @@ class ElementTextItemGroup : public QObject, public QGraphicsItemGroup
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
|
||||
private:
|
||||
Qt::Alignment m_alignement = Qt::AlignJustify;
|
||||
Qt::Alignment m_alignment = Qt::AlignJustify;
|
||||
QString m_name;
|
||||
bool m_first_move = true;
|
||||
QPointF m_mouse_to_origin_movement;
|
||||
Element *m_element = nullptr;
|
||||
};
|
||||
|
||||
#endif // ELEMENTTEXTITEMGROUP_H
|
||||
|
||||
Reference in New Issue
Block a user