QElectroTech  0.70
masterelement.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "masterelement.h"
19 #include "crossrefitem.h"
20 #include "diagram.h"
21 #include "dynamicelementtextitem.h"
22 #include <QRegularExpression>
23 
32 MasterElement::MasterElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state) :
33  Element(location, qgi, state, Element::Master)
34 {}
35 
42 }
43 
51 {
52  // check if element is slave and if isn't already linked
53  if (elmt->linkType() == Slave && !connected_elements.contains(elmt))
54  {
55  connected_elements << elmt;
56  elmt->linkToElement(this);
57 
58  XRefProperties xrp = diagram()->project()->defaultXRefProperties(kindInformations()["type"].toString());
59  if (!m_Xref_item && xrp.snapTo() == XRefProperties::Bottom)
60  m_Xref_item = new CrossRefItem(this); //create cross ref item if not yet
61  else
63 
64  emit linkedElementChanged();
65  }
66 }
67 
73 {
74  // if this element is free no need to do something
75  if (!isFree())
76  {
77  foreach(Element *elmt, connected_elements)
78  unlinkElement(elmt);
79  emit linkedElementChanged();
80  }
81 }
82 
89 {
90  //Ensure elmt is linked to this element
91  if (connected_elements.contains(elmt))
92  {
93  connected_elements.removeOne(elmt);
94  elmt -> unlinkElement (this);
95  elmt -> setHighlighted (false);
96 
98  emit linkedElementChanged();
99  }
100 }
101 
109  //Create the link with other element if needed
110  Element::initLink(project);
111 }
112 
119 {
120  if(m_Xref_item)
121  return m_Xref_item->boundingRect();
122  else
123  return QRectF();
124 }
125 
126 QVariant MasterElement::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
127 {
128  if(change == QGraphicsItem::ItemSceneHasChanged && m_first_scene_change)
129  {
130  m_first_scene_change = false;
132  }
133  return Element::itemChange(change, value);
134 }
135 
137 {
138  if(!diagram())
139  return;
140 
141  XRefProperties xrp = diagram()->project()->defaultXRefProperties(kindInformations()["type"].toString());
142  if(xrp.snapTo() == XRefProperties::Bottom)
143  {
144  //We create a Xref, and just after we call aboutDeleteXref,
145  //because the Xref may be useless.
146  if(!m_Xref_item)
147  m_Xref_item = new CrossRefItem(this);
148  }
149  aboutDeleteXref();
150 }
151 
163 {
164  if(!m_Xref_item)
165  return;
166 
167  XRefProperties xrp = diagram()->project()->defaultXRefProperties(kindInformations()["type"].toString());
168  if (xrp.snapTo() != XRefProperties::Bottom && m_Xref_item)
169  {
170  delete m_Xref_item;
171  m_Xref_item = nullptr;
172  return;
173  }
174 
175  if (m_Xref_item->boundingRect().isNull())
176  {
177  delete m_Xref_item;
178  m_Xref_item = nullptr;
179  return;
180  }
181 }
The CrossRefItem class This clas provide an item, for show the cross reference, like the contacts lin...
Definition: crossrefitem.h:40
CrossRefItem * m_Xref_item
Definition: masterelement.h:53
virtual void linkToElement(Element *)
Definition: element.h:133
virtual kind linkType() const
Definition: element.h:138
QList< Element * > connected_elements
Definition: element.h:171
bool isFree() const
Definition: element.h:201
bool m_first_scene_change
Definition: masterelement.h:54
void unlinkElement(Element *elmt) override
MasterElement::unlinkElement Unlink the given elmt in parametre.
The XRefProperties class this class store properties used by XrefItem.
MasterElement(const ElementsLocation &, QGraphicsItem *=nullptr, int *=nullptr)
MasterElement::MasterElement Default constructor.
Diagram * diagram() const
QetGraphicsItem::diagram return the diagram of this item.
virtual void setHighlighted(bool)
Definition: element.cpp:162
void linkedElementChanged()
virtual void initLink(QETProject *)
Element::initLink Initialise the link between this element and other elements. This method can be cal...
Definition: element.cpp:1381
void linkToElement(Element *elmt) override
MasterElement::linkToElement Link this master to another element For this class element must be a sla...
void xrefPropertiesChanged()
QRectF boundingRect() const override
CrossRefItem::boundingRect.
~MasterElement() override
MasterElement::~MasterElement default destructor.
void initLink(QETProject *project) override
MasterElement::initLink.
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
SnapTo snapTo() const
XRefProperties defaultXRefProperties(const QString &type) const
Definition: qetproject.h:102
DiagramContext kindInformations() const
Definition: element.h:91
QRectF XrefBoundingRect() const
MasterElement::XrefBoundingRect.
void XRefPropertiesChanged()
QETProject * project() const
Definition: diagram.cpp:1716
void unlinkAllElements() override
MasterElement::unlinkAllElements Unlink all of the element in the QList connected_elements.
void aboutDeleteXref()
MasterElement::aboutDeleteXref Check if Xref item must be displayed, if not, delete it...