mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-01-10 16:19:59 +01:00
Add initial support of texts group
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5117 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
#include "diagramcontext.h"
|
||||
#include "changeelementinformationcommand.h"
|
||||
#include "dynamicelementtextitem.h"
|
||||
#include "elementtextitemgroup.h"
|
||||
|
||||
class ElementXmlRetroCompatibility
|
||||
{
|
||||
@@ -583,6 +584,12 @@ bool Element::fromXml(QDomElement &e, QHash<int, Terminal *> &table_id_adr, bool
|
||||
}
|
||||
}
|
||||
|
||||
for (QDomElement qde : QET::findInDomElement(e, "texts_group", ElementTextItemGroup::xmlTaggName()))
|
||||
{
|
||||
ElementTextItemGroup *group = addTextGroup("loaded_from_xml_group");
|
||||
group->fromXml(qde);
|
||||
}
|
||||
|
||||
//load informations
|
||||
m_element_informations.fromXml(e.firstChildElement("elementInformations"), "elementInformation");
|
||||
/**
|
||||
@@ -710,7 +717,42 @@ QDomElement Element::toXml(QDomDocument &document, QHash<Terminal *, int> &table
|
||||
QDomElement dyn_text = document.createElement("dynamic_texts");
|
||||
for (DynamicElementTextItem *deti : m_dynamic_text_list)
|
||||
dyn_text.appendChild(deti->toXml(document));
|
||||
element.appendChild(dyn_text);
|
||||
|
||||
QDomElement texts_group = document.createElement("texts_group");
|
||||
|
||||
//Dynamic texts owned by groups
|
||||
for(ElementTextItemGroup *group : m_texts_group)
|
||||
{
|
||||
//temporarily remove the texts from group to get the pos relative to element and not group.
|
||||
//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);
|
||||
|
||||
//Remove the texts from group
|
||||
QList<DynamicElementTextItem *> deti_list = group->texts();
|
||||
for(DynamicElementTextItem *deti : deti_list)
|
||||
group->removeFromGroup(deti);
|
||||
|
||||
//Save the texts to xml
|
||||
for (DynamicElementTextItem *deti : deti_list)
|
||||
dyn_text.appendChild(deti->toXml(document));
|
||||
|
||||
//Re add texts to group
|
||||
for(DynamicElementTextItem *deti : deti_list)
|
||||
group->addToGroup(deti);
|
||||
|
||||
//Restor the alignement
|
||||
group->setAlignement(al);
|
||||
|
||||
//Save the group to xml
|
||||
texts_group.appendChild(group->toXml(document));
|
||||
}
|
||||
|
||||
//Append the dynamic texts to element
|
||||
element.appendChild(dyn_text);
|
||||
//Append the texts group to element
|
||||
element.appendChild(texts_group);
|
||||
|
||||
return(element);
|
||||
}
|
||||
@@ -726,24 +768,42 @@ void Element::addDynamicTextItem(DynamicElementTextItem *deti)
|
||||
if (deti && !m_dynamic_text_list.contains(deti))
|
||||
{
|
||||
m_dynamic_text_list.append(deti);
|
||||
emit textAdded(deti);
|
||||
}
|
||||
else
|
||||
{
|
||||
DynamicElementTextItem *text = new DynamicElementTextItem(this);
|
||||
m_dynamic_text_list.append(text);
|
||||
emit textAdded(text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Element::removeDynamicTextItem
|
||||
* Remove @deti as dynamic text item of this element.
|
||||
* The parent item of deti stay this item.
|
||||
* 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.
|
||||
* @param deti
|
||||
*/
|
||||
void Element::removeDynamicTextItem(DynamicElementTextItem *deti)
|
||||
{
|
||||
if (m_dynamic_text_list.contains(deti))
|
||||
{
|
||||
m_dynamic_text_list.removeOne(deti);
|
||||
emit textRemoved(deti);
|
||||
return;
|
||||
}
|
||||
|
||||
for(ElementTextItemGroup *group : m_texts_group)
|
||||
{
|
||||
if(group->texts().contains(deti))
|
||||
{
|
||||
removeTextFromGroup(deti, group);
|
||||
m_dynamic_text_list.removeOne(deti);
|
||||
emit textRemoved(deti);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -751,7 +811,133 @@ void Element::removeDynamicTextItem(DynamicElementTextItem *deti)
|
||||
* @return all dynamic text items of this element
|
||||
*/
|
||||
QList<DynamicElementTextItem *> Element::dynamicTextItems() const {
|
||||
return m_dynamic_text_list;
|
||||
return m_dynamic_text_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Element::addTextGroup
|
||||
* Create and add an element text item group to this element.
|
||||
* If this element already have a group with the same name,
|
||||
* then @name will renamed to name1 or name2 etc....
|
||||
* @param name : the name of the group
|
||||
* @return the created group.
|
||||
*/
|
||||
ElementTextItemGroup *Element::addTextGroup(const QString &name)
|
||||
{
|
||||
if(m_texts_group.isEmpty())
|
||||
{
|
||||
ElementTextItemGroup *group = new ElementTextItemGroup(name, this);
|
||||
m_texts_group << group;
|
||||
emit textsGroupAdded(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
//Set a new name if name already exist
|
||||
QString rename = name;
|
||||
int i=1;
|
||||
while (textGroup(rename))
|
||||
{
|
||||
rename = name+QString::number(i);
|
||||
i++;
|
||||
}
|
||||
|
||||
//Create the group
|
||||
ElementTextItemGroup *group = new ElementTextItemGroup(rename, this);
|
||||
m_texts_group << group;
|
||||
emit textsGroupAdded(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Element::removeTextGroup
|
||||
* Remove the text group with name @name
|
||||
* All text owned by the group will be reparented to this element
|
||||
* @param name
|
||||
*/
|
||||
void Element::removeTextGroup(ElementTextItemGroup *group)
|
||||
{
|
||||
if(!m_texts_group.contains(group))
|
||||
return;
|
||||
|
||||
const QList <QGraphicsItem *> items_list = group->childItems();
|
||||
|
||||
for(QGraphicsItem *qgi : items_list)
|
||||
{
|
||||
if(qgi->type() == DynamicElementTextItem::Type)
|
||||
{
|
||||
DynamicElementTextItem *deti = static_cast<DynamicElementTextItem *>(qgi);
|
||||
removeTextFromGroup(deti, group);
|
||||
}
|
||||
}
|
||||
|
||||
m_texts_group.removeOne(group);
|
||||
emit textsGroupAboutToBeRemoved(group);
|
||||
delete group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Element::textGroup
|
||||
* @param name
|
||||
* @return the text group named @name or nullptr if this element
|
||||
* haven't got a group with this name
|
||||
*/
|
||||
ElementTextItemGroup *Element::textGroup(const QString &name) const
|
||||
{
|
||||
for (ElementTextItemGroup *group : m_texts_group)
|
||||
if(group->name() == name)
|
||||
return group;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Element::textGroups
|
||||
* @return All texts groups of this element
|
||||
*/
|
||||
QList<ElementTextItemGroup *> Element::textGroups() const
|
||||
{
|
||||
return m_texts_group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Element::addTextToGroup
|
||||
* Add the text @text to the group @group;
|
||||
* If @group isn't owned by this element return false.
|
||||
* The text must be a text of this element.
|
||||
* @return : true if the text was succesfully added to the group.
|
||||
*/
|
||||
bool Element::addTextToGroup(DynamicElementTextItem *text, ElementTextItemGroup *group)
|
||||
{
|
||||
if(!m_dynamic_text_list.contains(text))
|
||||
return false;
|
||||
if(!m_texts_group.contains(group))
|
||||
return false;
|
||||
|
||||
removeDynamicTextItem(text);
|
||||
group->addToGroup(text);
|
||||
emit textAddedToGroup(text, group);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Element::removeTextFromGroup
|
||||
* Remove the text @text from the group @group, en reparent @text to this element
|
||||
* @return true if text was succesfully removed
|
||||
*/
|
||||
bool Element::removeTextFromGroup(DynamicElementTextItem *text, ElementTextItemGroup *group)
|
||||
{
|
||||
if(!m_texts_group.contains(group))
|
||||
return false;
|
||||
|
||||
if(group->childItems().contains(text))
|
||||
{
|
||||
group->removeFromGroup(text);
|
||||
emit textRemovedFromGroup(text, group);
|
||||
addDynamicTextItem(text);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,6 +31,7 @@ class Conductor;
|
||||
class NumerotationContext;
|
||||
class DiagramTextItem;
|
||||
class DynamicElementTextItem;
|
||||
class ElementTextItemGroup;
|
||||
|
||||
/**
|
||||
This is the base class for electrical elements.
|
||||
@@ -131,6 +132,12 @@ class Element : public QetGraphicsItem
|
||||
void linkedElementChanged(); //This signal is emited when the linked elements with this element change
|
||||
void elementInfoChange(DiagramContext old_info, DiagramContext new_info);
|
||||
void updateLabel(); //This signal is emited to update element's label
|
||||
void textAdded(DynamicElementTextItem *deti);
|
||||
void textRemoved(DynamicElementTextItem *deti);
|
||||
void textsGroupAdded(ElementTextItemGroup *group);
|
||||
void textsGroupAboutToBeRemoved(ElementTextItemGroup *group);
|
||||
void textAddedToGroup(DynamicElementTextItem *text, ElementTextItemGroup *group);
|
||||
void textRemovedFromGroup(DynamicElementTextItem *text, ElementTextItemGroup *group);
|
||||
|
||||
//METHODS related to information
|
||||
public:
|
||||
@@ -201,6 +208,12 @@ class Element : public QetGraphicsItem
|
||||
void addDynamicTextItem(DynamicElementTextItem *deti = nullptr);
|
||||
void removeDynamicTextItem(DynamicElementTextItem *deti);
|
||||
QList<DynamicElementTextItem *> dynamicTextItems() const;
|
||||
ElementTextItemGroup *addTextGroup(const QString &name);
|
||||
void removeTextGroup(ElementTextItemGroup *group);
|
||||
ElementTextItemGroup *textGroup(const QString &name) const;
|
||||
QList<ElementTextItemGroup *> textGroups() const;
|
||||
bool addTextToGroup(DynamicElementTextItem *text, ElementTextItemGroup *group);
|
||||
bool removeTextFromGroup(DynamicElementTextItem *text, ElementTextItemGroup *group);
|
||||
|
||||
protected:
|
||||
void drawAxes(QPainter *, const QStyleOptionGraphicsItem *);
|
||||
@@ -227,6 +240,7 @@ class Element : public QetGraphicsItem
|
||||
bool m_mouse_over;
|
||||
QString m_prefix;
|
||||
QList <DynamicElementTextItem *> m_dynamic_text_list;
|
||||
QList <ElementTextItemGroup *> m_texts_group;
|
||||
|
||||
};
|
||||
|
||||
|
||||
367
sources/qetgraphicsitem/elementtextitemgroup.cpp
Normal file
367
sources/qetgraphicsitem/elementtextitemgroup.cpp
Normal file
@@ -0,0 +1,367 @@
|
||||
/*
|
||||
Copyright 2006-2017 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "elementtextitemgroup.h"
|
||||
#include "dynamicelementtextitem.h"
|
||||
#include "element.h"
|
||||
#include "diagram.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
|
||||
bool sorting(QGraphicsItem *qgia, QGraphicsItem *qgib)
|
||||
{
|
||||
return qgia->pos().y() < qgib->pos().y();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::ElementTextItemGroup
|
||||
* @param parent
|
||||
*/
|
||||
ElementTextItemGroup::ElementTextItemGroup(const QString &name, Element *parent) :
|
||||
QGraphicsItemGroup(parent),
|
||||
m_name(name),
|
||||
m_element(parent)
|
||||
{
|
||||
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable);
|
||||
}
|
||||
|
||||
ElementTextItemGroup::~ElementTextItemGroup()
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::addToGroup
|
||||
* @param item
|
||||
*/
|
||||
void ElementTextItemGroup::addToGroup(QGraphicsItem *item)
|
||||
{
|
||||
if(item->type() == DynamicElementTextItem::Type)
|
||||
{
|
||||
item->setFlag(QGraphicsItem::ItemIsSelectable, false);
|
||||
QGraphicsItemGroup::addToGroup(item);
|
||||
updateAlignement();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::removeFromGroup
|
||||
* @param item
|
||||
*/
|
||||
void ElementTextItemGroup::removeFromGroup(QGraphicsItem *item)
|
||||
{
|
||||
QGraphicsItemGroup::removeFromGroup(item);
|
||||
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
|
||||
updateAlignement();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::setAlignement
|
||||
* Set the alignement of this group
|
||||
* @param alignement
|
||||
*/
|
||||
void ElementTextItemGroup::setAlignement(Qt::Alignment alignement)
|
||||
{
|
||||
m_alignement = alignement;
|
||||
updateAlignement();
|
||||
}
|
||||
|
||||
Qt::Alignment ElementTextItemGroup::alignment() const
|
||||
{
|
||||
return m_alignement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::setAlignement
|
||||
* Update the alignement of the items in this group, according
|
||||
* to the current alignement.
|
||||
* @param alignement
|
||||
*/
|
||||
void ElementTextItemGroup::updateAlignement()
|
||||
{
|
||||
QList <QGraphicsItem *> texts = childItems();
|
||||
if (texts.size() > 1)
|
||||
{
|
||||
prepareGeometryChange();
|
||||
std::sort(texts.begin(), texts.end(), sorting);
|
||||
|
||||
qreal y_offset =0;
|
||||
|
||||
if(m_alignement == Qt::AlignLeft)
|
||||
{
|
||||
QPointF ref = texts.first()->pos();
|
||||
|
||||
for(QGraphicsItem *item : texts)
|
||||
{
|
||||
item->setPos(ref.x(), ref.y()+y_offset);
|
||||
y_offset+=item->boundingRect().height();
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if(m_alignement == Qt::AlignVCenter)
|
||||
{
|
||||
QPointF ref(texts.first()->pos().x() + texts.first()->boundingRect().width()/2,
|
||||
texts.first()->pos().y());
|
||||
|
||||
for(QGraphicsItem *item : texts)
|
||||
{
|
||||
item->setPos(ref.x() - item->boundingRect().width()/2,
|
||||
ref.y() + y_offset);
|
||||
y_offset+=item->boundingRect().height();
|
||||
}
|
||||
return;
|
||||
|
||||
}
|
||||
else if (m_alignement == Qt::AlignRight)
|
||||
{
|
||||
QPointF ref(texts.first()->pos().x() + texts.first()->boundingRect().width(),
|
||||
texts.first()->pos().y());
|
||||
|
||||
for(QGraphicsItem *item : texts)
|
||||
{
|
||||
item->setPos(ref.x() - item->boundingRect().width(),
|
||||
ref.y() + y_offset);
|
||||
y_offset+=item->boundingRect().height();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::setName
|
||||
* @param name Set the name of this group
|
||||
*/
|
||||
void ElementTextItemGroup::setName(QString name)
|
||||
{
|
||||
m_name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::texts
|
||||
* @return Every texts in this group
|
||||
*/
|
||||
QList<DynamicElementTextItem *> ElementTextItemGroup::texts() const
|
||||
{
|
||||
QList<DynamicElementTextItem *> list;
|
||||
for(QGraphicsItem *qgi : childItems())
|
||||
{
|
||||
if(qgi->type() == DynamicElementTextItem::Type)
|
||||
list << static_cast<DynamicElementTextItem *>(qgi);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::diagram
|
||||
* @return The diagram of this group, or nullptr if this group is not in a diagram
|
||||
*/
|
||||
Diagram *ElementTextItemGroup::diagram() const
|
||||
{
|
||||
if(scene())
|
||||
return static_cast<Diagram *>(scene());
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::toXml
|
||||
* Export data of this group to xml
|
||||
* @param dom_document
|
||||
* @return
|
||||
*/
|
||||
QDomElement ElementTextItemGroup::toXml(QDomDocument &dom_document) const
|
||||
{
|
||||
QDomElement dom_element = dom_document.createElement(this->xmlTaggName());
|
||||
dom_element.setAttribute("name", m_name);
|
||||
|
||||
QMetaEnum me = QMetaEnum::fromType<Qt::Alignment>();
|
||||
dom_element.setAttribute("alignment", me.valueToKey(m_alignement));
|
||||
|
||||
QDomElement dom_texts = dom_document.createElement("texts");
|
||||
for(DynamicElementTextItem *deti : texts())
|
||||
{
|
||||
QDomElement text = dom_document.createElement("text");
|
||||
text.setAttribute("uuid", deti->uuid().toString());
|
||||
dom_texts.appendChild(text);
|
||||
}
|
||||
|
||||
dom_element.appendChild(dom_texts);
|
||||
return dom_element;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::fromXml
|
||||
* Import data of this group from xml
|
||||
* @param dom_element
|
||||
*/
|
||||
void ElementTextItemGroup::fromXml(QDomElement &dom_element)
|
||||
{
|
||||
if (dom_element.tagName() != xmlTaggName()) {
|
||||
qDebug() << "ElementTextItemGroup::fromXml : Wrong tagg name";
|
||||
return;
|
||||
}
|
||||
|
||||
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()));
|
||||
|
||||
for(QDomElement text : QET::findInDomElement(dom_element, "texts", "text"))
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::paint
|
||||
* @param painter
|
||||
* @param option
|
||||
* @param widget
|
||||
*/
|
||||
void ElementTextItemGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||
{
|
||||
Q_UNUSED(option);
|
||||
Q_UNUSED(widget);
|
||||
if(isSelected())
|
||||
{
|
||||
painter->save();
|
||||
QPen t;
|
||||
t.setColor(Qt::gray);
|
||||
t.setStyle(Qt::DashDotLine);
|
||||
t.setCosmetic(true);
|
||||
painter->setPen(t);
|
||||
painter->drawRoundRect(boundingRect().adjusted(1, 1, -1, -1), 10, 10);
|
||||
painter->restore();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::boundingRect
|
||||
* @return
|
||||
*/
|
||||
QRectF ElementTextItemGroup::boundingRect() const
|
||||
{
|
||||
//If we refer to the Qt doc, the bounding rect of a QGraphicsItemGroup,
|
||||
//is the bounding of all childrens in the group
|
||||
//When add an item in the group, the bounding rect is good, but
|
||||
//if we move an item already in the group, the bounding rect of the group stay unchanged.
|
||||
//We reimplement this function to avoid this behavior.
|
||||
QRectF rect;
|
||||
for(QGraphicsItem *qgi : childItems())
|
||||
{
|
||||
QRectF r(qgi->pos(), QSize(qgi->boundingRect().width(), qgi->boundingRect().height()));
|
||||
rect = rect.united(r);
|
||||
}
|
||||
return rect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::mousePressEvent
|
||||
* @param event
|
||||
*/
|
||||
void ElementTextItemGroup::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if(event->button() == Qt::LeftButton)
|
||||
{
|
||||
m_first_move = true;
|
||||
if(event->modifiers() & Qt::ControlModifier)
|
||||
setSelected(!isSelected());
|
||||
}
|
||||
|
||||
QGraphicsItemGroup::mousePressEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::mouseMoveEvent
|
||||
* @param event
|
||||
*/
|
||||
void ElementTextItemGroup::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if(isSelected() && event->buttons() & Qt::LeftButton)
|
||||
{
|
||||
if(diagram() && m_first_move)
|
||||
diagram()->beginMoveElementTexts(this);
|
||||
|
||||
QPointF old_pos = pos();
|
||||
if(m_first_move)
|
||||
m_mouse_to_origin_movement = old_pos - event->buttonDownScenePos(Qt::LeftButton);
|
||||
|
||||
QPointF expected_pos = event->scenePos() + m_mouse_to_origin_movement;
|
||||
setPos(Diagram::snapToGrid(expected_pos));
|
||||
|
||||
QPointF effective_movement = pos() - old_pos;
|
||||
if(diagram())
|
||||
diagram()->continueMoveElementTexts(effective_movement);
|
||||
}
|
||||
else
|
||||
event->ignore();
|
||||
|
||||
if(m_first_move)
|
||||
m_first_move = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::mouseReleaseEvent
|
||||
* @param event
|
||||
*/
|
||||
void ElementTextItemGroup::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if(diagram())
|
||||
diagram()->endMoveElementTexts();
|
||||
|
||||
if(!(event->modifiers() & Qt::ControlModifier))
|
||||
QGraphicsItemGroup::mouseReleaseEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementTextItemGroup::keyPressEvent
|
||||
* @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);
|
||||
}
|
||||
|
||||
73
sources/qetgraphicsitem/elementtextitemgroup.h
Normal file
73
sources/qetgraphicsitem/elementtextitemgroup.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
Copyright 2006-2017 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef ELEMENTTEXTITEMGROUP_H
|
||||
#define ELEMENTTEXTITEMGROUP_H
|
||||
|
||||
#include <QGraphicsItemGroup>
|
||||
#include <QObject>
|
||||
#include <QDomElement>
|
||||
|
||||
class Element;
|
||||
class DynamicElementTextItem;
|
||||
class Diagram;
|
||||
|
||||
/**
|
||||
* @brief The ElementTextItemGroup class
|
||||
* This class represent a group of element text
|
||||
* Texts in the group can be aligned left / center /right
|
||||
*/
|
||||
class ElementTextItemGroup : public QObject, public QGraphicsItemGroup
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ElementTextItemGroup(const QString &name, Element *parent);
|
||||
~ElementTextItemGroup() override;
|
||||
void addToGroup(QGraphicsItem *item);
|
||||
void removeFromGroup(QGraphicsItem *item);
|
||||
|
||||
void setAlignement(Qt::Alignment alignement);
|
||||
Qt::Alignment alignment() const;
|
||||
void updateAlignement();
|
||||
void setName(QString name);
|
||||
QString name() const {return m_name;}
|
||||
QList<DynamicElementTextItem *> texts() const;
|
||||
Diagram *diagram() const;
|
||||
|
||||
QDomElement toXml(QDomDocument &dom_document) const;
|
||||
void fromXml(QDomElement &dom_element);
|
||||
static QString xmlTaggName() {return QString("text_group");}
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
||||
QRectF boundingRect() const override;
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
|
||||
private:
|
||||
Qt::Alignment m_alignement = Qt::AlignJustify;
|
||||
QString m_name;
|
||||
bool m_first_move = true;
|
||||
QPointF m_mouse_to_origin_movement;
|
||||
Element *m_element = nullptr;
|
||||
};
|
||||
|
||||
#endif // ELEMENTTEXTITEMGROUP_H
|
||||
@@ -49,10 +49,9 @@ Diagram* QetGraphicsItem::diagram() const{
|
||||
*/
|
||||
void QetGraphicsItem::setPos(const QPointF &p) {
|
||||
QPointF pp = Diagram::snapToGrid(p);
|
||||
if (pp == pos() || !is_movable_) return;
|
||||
if (scene() && snap_to_grid_) {
|
||||
QGraphicsItem::setPos(pp);
|
||||
} else QGraphicsItem::setPos(pp);
|
||||
if (pp == pos() || !is_movable_)
|
||||
return;
|
||||
QGraphicsItem::setPos(pp);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user