try to use TerminalData in Terminal and PartTerminal, sou the code must not be written double

This commit is contained in:
Martin Marmsoler
2020-05-25 22:17:17 +02:00
committed by Laurent Trinques
parent 8c6b4120f5
commit ec52032532
7 changed files with 284 additions and 109 deletions

View File

@@ -0,0 +1,82 @@
#include "terminaldata.h"
#include <QGraphicsObject>
TerminalData::TerminalData():
PropertiesInterface()
{
}
TerminalData::TerminalData(QGraphicsObject *parent):
PropertiesInterface(),
q(parent)
{
}
TerminalData::~TerminalData()
{
}
void TerminalData::setParent(QGraphicsObject* parent)
{
q = parent;
}
void TerminalData::toSettings(QSettings &settings, const QString) const
{
}
void TerminalData::fromSettings(const QSettings &settings, const QString)
{
}
QDomElement TerminalData::toXml(QDomDocument &xml_document) const
{
QDomElement xml_element = xml_document.createElement("terminal");
// ecrit la position de la borne
xml_element.setAttribute("x", QString("%1").arg(q->scenePos().x()));
xml_element.setAttribute("y", QString("%1").arg(q->scenePos().y()));
xml_element.setAttribute("uuid", m_uuid.toString());
xml_element.setAttribute("name", m_name);
// ecrit l'orientation de la borne
xml_element.setAttribute("orientation", Qet::orientationToString(m_orientation));
// Write name and number to XML
return(xml_element);
}
bool TerminalData::fromXml (const QDomElement &xml_element)
{
// lit la position de la borne
qreal term_x = 0.0, term_y = 0.0;
if (!QET::attributeIsAReal(xml_element, "x", &term_x))
return false;
if (!QET::attributeIsAReal(xml_element, "y", &term_y))
return false;
m_pos = QPointF(term_x, term_y);
//emit posFromXML(QPointF(term_x, term_y));
QString uuid = xml_element.attribute("uuid");
// update part and add uuid, which is used in the new version to connect terminals together
// if the attribute not exists, in the constructor of PartTerminal already one is created
if (!uuid.isEmpty())
m_uuid = QUuid(uuid);
m_name = xml_element.attribute("name");
// lit l'orientation de la borne
m_orientation = Qet::orientationFromString(xml_element.attribute("orientation"));
return true;
}

View File

@@ -0,0 +1,67 @@
#ifndef TERMINALDATA_H
#define TERMINALDATA_H
#include "propertiesinterface.h"
#include "qet.h"
#include <QUuid>
#include <QPointF>
class QGraphicsObject;
/*!
* \brief The TerminalData class
* Data of the terminal. Stored in extra class so it can be used by PartTerminal and Terminal without
* defining everything again.
*/
class TerminalData : public PropertiesInterface
{
public:
TerminalData();
TerminalData(QGraphicsObject* parent);
~TerminalData();
void setParent(QGraphicsObject* parent);
// Save/load properties to setting file. QString is use for prefix a word befor the name of each paramètre
void toSettings(QSettings &settings, const QString = QString()) const override;
void fromSettings(const QSettings &settings, const QString = QString()) override;
// Save/load properties to xml element
// This method is only called from the PartTerminal and should never called from the Terminal class
QDomElement toXml(QDomDocument &xml_element) const override;
bool fromXml(const QDomElement &xml_element) override;
// must be public, because this class is a private member of PartTerminal/Terminal and they must
// access this data
public:
Qet::Orientation m_orientation;
QPointF second_point;
/*!
* \brief m_uuid
* Uuid of the terminal.
*
* In elementscene.cpp an element gets a new uuid when saving the element. In the current state
* each connection is made by using the local position of the terminal and a dynamic id. In the new
* case, each terminal should have it's own uuid to identify it uniquely. When changing each time this
* uuid, the conductor after updating the part is anymore valid. So if in the loaded document a uuid exists,
* use this one and don't create a new one.
*/
QUuid m_uuid;
/*!
* \brief m_name
* Name of the element. It can be used to create wiring harness tables
*/
QString m_name;
/*!
* \brief m_pos
* Position of the terminal. The second point is calculated from this position and the orientation
* Important: this variable is only updated during read from xml and not during mouse move!
* It is used to store the initial position so that PartTerminal and Terminal have access to it.
*/
QPointF m_pos;
private:
QGraphicsObject* q{nullptr};
};
#endif // TERMINALDATA_H