[qet] qet/qet: [4803] Revamp struct sequenceStruct to class sequentialNumbers.

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 4803
Author:   blacksun
Date:     2016-12-08 16:06:46 +0100 (Thu, 08 Dec 2016)
Log Message:
-----------
Revamp struct sequenceStruct to class sequentialNumbers.
Element now use methods (toXml and fromXml) of sequentialNumbers to store and load sequential.

Modified Paths:
--------------
    trunk/sources/autoNum/assignvariables.cpp
    trunk/sources/autoNum/assignvariables.h
    trunk/sources/conductorautonumerotation.cpp
    trunk/sources/qetgraphicsitem/conductor.cpp
    trunk/sources/qetgraphicsitem/conductor.h
    trunk/sources/qetgraphicsitem/crossrefitem.cpp
    trunk/sources/qetgraphicsitem/element.cpp
    trunk/sources/qetgraphicsitem/element.h
    trunk/sources/qetgraphicsitem/terminal.cpp
    trunk/sources/qetxml.cpp
    trunk/sources/qetxml.h

Modified: trunk/sources/autoNum/assignvariables.cpp
===================================================================
--- trunk/sources/autoNum/assignvariables.cpp	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/autoNum/assignvariables.cpp	2016-12-08 15:06:46 UTC (rev 4803)
@@ -20,6 +20,7 @@
 #include "element.h"
 #include "diagramposition.h"
 #include "qetapp.h"
+#include "qetxml.h"
 
 #include <QVariant>
 #include <QStringList>
@@ -26,7 +27,108 @@
 
 namespace autonum
 {
+
 	/**
+	 * @brief sequentialNumbers::sequentialNumbers
+	 */
+	sequentialNumbers::sequentialNumbers()
+	{}
+
+	sequentialNumbers &sequentialNumbers::operator=(const sequentialNumbers &other)
+	{
+		if (&other == this || other == *this)
+			return (*this);
+
+		unit          = other.unit;
+		unit_folio    = other.unit_folio;
+		ten           = other.ten;
+		ten_folio     = other.ten_folio;
+		hundred       = other.hundred;
+		hundred_folio = other.hundred_folio;
+
+		return (*this);
+	}
+
+	bool sequentialNumbers::operator==(const sequentialNumbers &other) const
+	{
+		if (unit          == other.unit && \
+			unit_folio    == other.unit_folio && \
+			ten           == other.ten && \
+			ten_folio     == other.ten_folio && \
+			hundred       == other.hundred && \
+			hundred_folio == other.hundred_folio)
+			return true;
+		else
+			return false;
+	}
+
+	bool sequentialNumbers::operator!=(const sequentialNumbers &other) const
+	{
+		if (*this == other)
+			return false;
+		else
+			return true;
+	}
+
+	/**
+	 * @brief sequentialNumbers::toXml
+	 * export this sequential numbers into a QDomElement.
+	 * @param document : QDomDocument used to create the QDomElement
+	 * @param tag_name : the tag name used for the QDomElement.
+	 * @return A QDomElement, if this sequential have no value, the returned QDomELement is empty
+	 */
+	QDomElement sequentialNumbers::toXml(QDomDocument &document, QString tag_name) const
+	{
+		QDomElement element = document.createElement(tag_name);
+
+		if (!unit.isEmpty())
+			element.appendChild(QETXML::textToDomElement(document, "unit", unit.join(";")));
+		if (!unit_folio.isEmpty())
+			element.appendChild(QETXML::textToDomElement(document, "unitFolio", unit_folio.join(";")));
+		if(!ten.isEmpty())
+			element.appendChild(QETXML::textToDomElement(document, "ten", ten.join(";")));
+		if(!ten_folio.isEmpty())
+			element.appendChild(QETXML::textToDomElement(document, "tenFolio", ten_folio.join(";")));
+		if(!hundred.isEmpty())
+			element.appendChild(QETXML::textToDomElement(document, "hundred", hundred.join(";")));
+		if(!hundred_folio.isEmpty())
+			element.appendChild(QETXML::textToDomElement(document, "hundredFolio", hundred_folio.join(";")));
+
+		return element;
+	}
+
+	/**
+	 * @brief sequentialNumbers::fromXml
+	 * Import sequential values from a QDomElement
+	 * @param element
+	 */
+	void sequentialNumbers::fromXml(const QDomElement &element)
+	{
+		if (!element.hasChildNodes())
+			return;
+
+		QDomElement from;
+
+		from = element.firstChildElement("unit");
+		unit = from.text().split(";");
+
+		from = element.firstChildElement("unitFolio");
+		unit_folio = from.text().split(";");
+
+		from = element.firstChildElement("ten");
+		ten = from.text().split(";");
+
+		from = element.firstChildElement("tenFolio");
+		ten_folio = from.text().split(";");
+
+		from = element.firstChildElement("hundred");
+		hundred = from.text().split(";");
+
+		from = element.firstChildElement("hundredFolio");
+		hundred_folio = from.text().split(";");
+	}
+
+	/**
 	 * @brief AssignVariables::formulaToLabel
 	 * Return the @formula with variable assigned (ready to be displayed)
 	 * @param formula - the formula to work
@@ -35,7 +137,7 @@
 	 * @param elmt - parent element (if any) of the formula
 	 * @return the string with variable assigned.
 	 */
-	QString AssignVariables::formulaToLabel(QString formula, sequenceStruct &seqStruct, Diagram *diagram, const Element *elmt)
+	QString AssignVariables::formulaToLabel(QString formula, sequentialNumbers &seqStruct, Diagram *diagram, const Element *elmt)
 	{
 		AssignVariables av(formula, seqStruct, diagram, elmt);
 		seqStruct = av.m_seq_struct;
@@ -42,7 +144,7 @@
 		return av.m_assigned_label;
 	}
 
-	AssignVariables::AssignVariables(QString formula, sequenceStruct seqStruct , Diagram *diagram, const Element *elmt):
+	AssignVariables::AssignVariables(QString formula, sequentialNumbers seqStruct , Diagram *diagram, const Element *elmt):
 	m_diagram(diagram),
 	m_arg_formula(formula),
 	m_assigned_label(formula),
@@ -203,7 +305,7 @@
 	 * to keep up to date the current sequential of folio.
 	 * @param hashKey : the hash key used to store the sequential for folio type.
 	 */
-	void setSequential(QString label, sequenceStruct &seqStruct, NumerotationContext &context, Diagram *diagram, QString hashKey)
+	void setSequential(QString label, sequentialNumbers &seqStruct, NumerotationContext &context, Diagram *diagram, QString hashKey)
 	{
 		if (!context.isEmpty())
 		{
@@ -394,5 +496,4 @@
 
 		return QString();
 	}
-
 }

Modified: trunk/sources/autoNum/assignvariables.h
===================================================================
--- trunk/sources/autoNum/assignvariables.h	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/autoNum/assignvariables.h	2016-12-08 15:06:46 UTC (rev 4803)
@@ -30,7 +30,18 @@
 
 namespace autonum
 {
-	struct sequenceStruct {
+	class sequentialNumbers
+	{
+		public:
+			sequentialNumbers();
+
+			sequentialNumbers &operator= (const sequentialNumbers &other);
+			bool operator== (const sequentialNumbers &other) const;
+			bool operator!= (const sequentialNumbers &other) const;
+
+			QDomElement toXml(QDomDocument &document, QString tag_name = QString("sequentialNumbers")) const;
+			void fromXml(const QDomElement &element);
+
 			QStringList unit;
 			QStringList unit_folio;
 			QStringList ten;
@@ -47,10 +58,10 @@
 	class AssignVariables
 	{
 		public:
-			static QString formulaToLabel (QString formula, sequenceStruct &seqStruct, Diagram *diagram, const Element *elmt = nullptr);
+			static QString formulaToLabel (QString formula, sequentialNumbers &seqStruct, Diagram *diagram, const Element *elmt = nullptr);
 
 		private:
-			AssignVariables(QString formula, sequenceStruct seqStruct , Diagram *diagram, const Element *elmt = nullptr);
+			AssignVariables(QString formula, sequentialNumbers seqStruct , Diagram *diagram, const Element *elmt = nullptr);
 			void assignTitleBlockVar();
 			void assignProjectVar();
 			void assignSequence();
@@ -58,13 +69,13 @@
 			Diagram *m_diagram  = nullptr;
 			QString m_arg_formula;
 			QString m_assigned_label;
-			sequenceStruct m_seq_struct;
+			sequentialNumbers m_seq_struct;
 			const Element *m_element = nullptr;
 	};
 
 	void setSequentialToList(QStringList &list, NumerotationContext &nc, QString type);
 	void setFolioSequentialToHash(QStringList &list, QHash<QString, QStringList> &hash, QString autoNumName);
-	void setSequential(QString label, autonum::sequenceStruct &seqStruct, NumerotationContext &context, Diagram *diagram, QString hashKey);
+	void setSequential(QString label, autonum::sequentialNumbers &seqStruct, NumerotationContext &context, Diagram *diagram, QString hashKey);
 	QString numerotationContextToFormula(const NumerotationContext &nc);
 	QString elementPrefixForLocation(const ElementsLocation &location);
 }

Modified: trunk/sources/conductorautonumerotation.cpp
===================================================================
--- trunk/sources/conductorautonumerotation.cpp	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/conductorautonumerotation.cpp	2016-12-08 15:06:46 UTC (rev 4803)
@@ -122,7 +122,7 @@
 		cp.text = text_list.first();
 		cp.m_formula = formula_list.first();
 		m_conductor->setProperties(cp);
-		m_conductor->setOthersSequential(conductor_list.first());
+		m_conductor->rSequenceNum() = conductor_list.first()->sequenceNum();
 		m_conductor->setText(text_list.first());
 	}
 		//the texts isn't identicals
@@ -153,10 +153,10 @@
 	cp.m_formula = formula;
 	m_conductor->setProperties(cp);
 
-	autonum::setSequential(formula, m_conductor->rSequenceStruct(), context, m_diagram, autoNum_name);
+	autonum::setSequential(formula, m_conductor->rSequenceNum(), context, m_diagram, autoNum_name);
 
 	NumerotationContextCommands ncc (context, m_diagram);
 	m_diagram->project()->addConductorAutoNum(autoNum_name, ncc.next());
 
-	applyText(autonum::AssignVariables::formulaToLabel(formula, m_conductor->rSequenceStruct(), m_diagram));
+	applyText(autonum::AssignVariables::formulaToLabel(formula, m_conductor->rSequenceNum(), m_diagram));
 }

Modified: trunk/sources/qetgraphicsitem/conductor.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/conductor.cpp	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/qetgraphicsitem/conductor.cpp	2016-12-08 15:06:46 UTC (rev 4803)
@@ -1269,22 +1269,6 @@
 }
 
 /**
- * @brief Conductor::setOthersSequential
- * Copy sequentials from conductor in argument to this conductor
- * @param conductor to copy sequentials from
- */
-void Conductor::setOthersSequential(Conductor *other) {
-	QString conductor_currentAutoNum = other->diagram()->project()->conductorCurrentAutoNum();
-	NumerotationContext nc = other->diagram()->project()->conductorAutoNum(conductor_currentAutoNum);
-	m_autoNum_seq.unit = other->m_autoNum_seq.unit;
-	m_autoNum_seq.unit_folio = other->m_autoNum_seq.unit_folio;
-	m_autoNum_seq.ten = other->m_autoNum_seq.ten;
-	m_autoNum_seq.ten_folio = other->m_autoNum_seq.ten_folio;
-	m_autoNum_seq.hundred = other->m_autoNum_seq.hundred;
-	m_autoNum_seq.hundred_folio = other->m_autoNum_seq.hundred_folio;
-}
-
-/**
  * @brief Conductor::setText
  * The text of this conductor
  * @param t
@@ -1292,24 +1276,6 @@
 void Conductor::setText(const QString &t)
 {
 	text_item->setPlainText(t);
-//	text_item->setPlainText(t);
-//	if (setSeq && diagram())
-//	{
-//		QString conductor_currentAutoNum = diagram()->project()->conductorCurrentAutoNum();
-//		NumerotationContext nc = diagram()->project()->conductorAutoNum(conductor_currentAutoNum);
-
-//		autonum::setSequential(text(), m_autoNum_seq, nc, diagram(), conductor_currentAutoNum);
-
-//		NumerotationContextCommands ncc (nc);
-//		diagram()->project()->addConductorAutoNum(conductor_currentAutoNum, ncc.next());
-
-//		setSeq = false;
-//	}
-//	if (diagram())
-//	{
-//		QString label = autonum::AssignVariables::formulaToLabel(t, m_autoNum_seq, diagram());
-//		text_item -> setPlainText(label);
-//	}
 }
 
 /**

Modified: trunk/sources/qetgraphicsitem/conductor.h
===================================================================
--- trunk/sources/qetgraphicsitem/conductor.h	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/qetgraphicsitem/conductor.h	2016-12-08 15:06:46 UTC (rev 4803)
@@ -92,7 +92,6 @@
 		QString text() const;
 		void setText(const QString &);
 		void refreshText();
-		void setOthersSequential (Conductor *);
 
 	public:
 		static bool valideXml (QDomElement &);
@@ -117,10 +116,10 @@
 		QETDiagramEditor* diagramEditor() const;
 		void editProperty ();
 
-		autonum::sequenceStruct sequenceStruct () const {return m_autoNum_seq;}
-		autonum::sequenceStruct& rSequenceStruct()      {return m_autoNum_seq;}
+		autonum::sequentialNumbers sequenceNum () const {return m_autoNum_seq;}
+		autonum::sequentialNumbers& rSequenceNum()      {return m_autoNum_seq;}
 	private:
-		autonum::sequenceStruct m_autoNum_seq;
+		autonum::sequentialNumbers m_autoNum_seq;
 
 	public:
 		void setFreezeLabel(bool freeze);

Modified: trunk/sources/qetgraphicsitem/crossrefitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/crossrefitem.cpp	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/qetgraphicsitem/crossrefitem.cpp	2016-12-08 15:06:46 UTC (rev 4803)
@@ -94,7 +94,7 @@
 {
 	XRefProperties xrp = m_element->diagram()->defaultXRefProperties(m_element->kindInformations()["type"].toString());
 	QString formula = xrp.masterLabel();
-	autonum::sequenceStruct seq;
+	autonum::sequentialNumbers seq;
 	QString txt = autonum::AssignVariables::formulaToLabel(formula, seq, elmt->diagram(), elmt);
 
 	if (add_prefix)

Modified: trunk/sources/qetgraphicsitem/element.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/element.cpp	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/qetgraphicsitem/element.cpp	2016-12-08 15:06:46 UTC (rev 4803)
@@ -29,6 +29,35 @@
 #include "numerotationcontextcommands.h"
 #include "diagramcontext.h"
 
+class ElementXmlRetroCompatibility
+{
+	friend class Element;
+
+	static void loadSequential(const QDomElement &dom_element, QString seq, QStringList* list)
+	{
+		int i = 0;
+		while (!dom_element.attribute(seq + QString::number(i+1)).isEmpty())
+		{
+				list->append(dom_element.attribute(seq + QString::number(i+1)));
+				i++;
+		}
+	}
+
+	static void loadSequential(const QDomElement &dom_element, Element *element)
+	{
+		autonum::sequentialNumbers sn;
+
+		loadSequential(dom_element,"sequ_",&sn.unit);
+		loadSequential(dom_element,"sequf_",&sn.unit_folio);
+		loadSequential(dom_element,"seqt_",&sn.ten);
+		loadSequential(dom_element,"seqtf_",&sn.ten_folio);
+		loadSequential(dom_element,"seqh_",&sn.hundred);
+		loadSequential(dom_element,"seqhf_",&sn.hundred_folio);
+
+		element->rSequenceStruct() = sn;
+	}
+};
+
 /**
 	Constructeur pour un element sans scene ni parent
 */
@@ -426,16 +455,14 @@
 	//uuid of this element
 	uuid_= QUuid(e.attribute("uuid", QUuid::createUuid().toString()));
 
-	//load prefix
+		//load prefix
 	m_prefix = e.attribute("prefix");
 
-	//Load Sequential Values
-	loadSequential(&e,"sequ_",&m_autoNum_seq.unit);
-	loadSequential(&e,"sequf_",&m_autoNum_seq.unit_folio);
-	loadSequential(&e,"seqt_",&m_autoNum_seq.ten);
-	loadSequential(&e,"seqtf_",&m_autoNum_seq.ten_folio);
-	loadSequential(&e,"seqh_",&m_autoNum_seq.hundred);
-	loadSequential(&e,"seqhf_",&m_autoNum_seq.hundred_folio);
+		//Load Sequential Values
+	if (e.hasAttribute("sequ_1") || e.hasAttribute("sequf_1") || e.hasAttribute("seqt_1") || e.hasAttribute("seqtf_1") || e.hasAttribute("seqh_1") || e.hasAttribute("sequf_1"))
+		ElementXmlRetroCompatibility::loadSequential(e, this);
+	else
+		m_autoNum_seq.fromXml(e.firstChildElement("sequentialNumbers"));
 
 		//load informations
 	m_element_informations.fromXml(e.firstChildElement("elementInformations"), "elementInformation");
@@ -468,21 +495,6 @@
 }
 
 /**
-	Load Sequentials to display on element label
-	@param element QDomElement to set Attributes
-	@param Qstring seq to be retrieved
-	@param QStringList list to be inserted values
-*/
-void Element::loadSequential(QDomElement* e, QString seq, QStringList* list) {
-	//Load Sequential Values
-	int i = 0;
-	while (!e->attribute(seq + QString::number(i+1)).isEmpty()) {
-			list->append(e->attribute(seq + QString::number(i+1)));
-			i++;
-	}
-}
-
-/**
 	Permet d'exporter l'element en XML
 	@param document Document XML a utiliser
 	@param table_adr_id Table de correspondance entre les adresses des bornes
@@ -490,46 +502,23 @@
 	methode
 	@return L'element XML representant cet element electrique
 */
-QDomElement Element::toXml(QDomDocument &document, QHash<Terminal *, int> &table_adr_id) const {
+QDomElement Element::toXml(QDomDocument &document, QHash<Terminal *, int> &table_adr_id) const
+{
 	QDomElement element = document.createElement("element");
 	
-	// type
+		// type
 	element.setAttribute("type", typeId());
-	// uuid
+
+		// uuid
 	element.setAttribute("uuid", uuid().toString());
-	// prefix
+
+		// prefix
 	element.setAttribute("prefix", m_prefix);
 
-	// Save Element sequential values to Xml
-	// Save Unit Sequential Values
-	for (int i = 0; i < m_autoNum_seq.unit.size(); i++) {
-			element.setAttribute("sequ_" + QString::number(i+1),m_autoNum_seq.unit.at(i));
-	}
-
-	// Save UnitFolio Sequential Values
-	for (int i = 0; i < m_autoNum_seq.unit_folio.size(); i++) {
-			element.setAttribute("sequf_" + QString::number(i+1),m_autoNum_seq.unit_folio.at(i));
-	}
-
-	// Save Ten Sequential Values
-	for (int i = 0; i < m_autoNum_seq.ten.size(); i++) {
-			element.setAttribute("seqt_" + QString::number(i+1),m_autoNum_seq.ten.at(i));
-	}
-
-	// Save TenFolio Sequential Values
-	for (int i = 0; i < m_autoNum_seq.ten_folio.size(); i++) {
-			element.setAttribute("seqtf_" + QString::number(i+1),m_autoNum_seq.ten_folio.at(i));
-	}
-
-	// Save Hundred Sequential Values
-	for (int i = 0; i < m_autoNum_seq.hundred.size(); i++) {
-			element.setAttribute("seqh_" + QString::number(i+1),m_autoNum_seq.hundred.at(i));
-	}
-
-	// Save Hundred Sequential Values
-	for (int i = 0; i < m_autoNum_seq.hundred_folio.size(); i++) {
-			element.setAttribute("seqhf_" + QString::number(i+1),m_autoNum_seq.hundred_folio.at(i));
-	}
+		// sequential num
+	QDomElement seq = m_autoNum_seq.toXml(document);
+	if (seq.hasChildNodes())
+		element.appendChild(seq);
 	
 	// position, selection et orientation
 	element.setAttribute("x", QString("%1").arg(pos().x()));

Modified: trunk/sources/qetgraphicsitem/element.h
===================================================================
--- trunk/sources/qetgraphicsitem/element.h	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/qetgraphicsitem/element.h	2016-12-08 15:06:46 UTC (rev 4803)
@@ -137,8 +137,8 @@
 																									//about the herited class like contactelement for know
 																									// kind of contact (simple tempo) or number of contact show by the element.
 
-		autonum::sequenceStruct sequenceStruct () const {return m_autoNum_seq;}
-		autonum::sequenceStruct& rSequenceStruct()      {return m_autoNum_seq;}
+		autonum::sequentialNumbers sequenceStruct () const {return m_autoNum_seq;}
+		autonum::sequentialNumbers& rSequenceStruct()      {return m_autoNum_seq;}
 
 		void setUpFormula(bool code_letter = true);
 		void setPrefix(QString);
@@ -153,7 +153,7 @@
 		//ATTRIBUTES
 	protected:
 		DiagramContext m_element_informations, kind_informations_;
-		autonum::sequenceStruct m_autoNum_seq;
+		autonum::sequentialNumbers m_autoNum_seq;
 
 	/**
 		Draw this element
@@ -203,7 +203,6 @@
 		void drawHighlight(QPainter *, const QStyleOptionGraphicsItem *);
 		void updatePixmap();
 		void etiToElementLabels(ElementTextItem*);
-		void loadSequential(QDomElement* e, QString seq, QStringList* list);
 
 	protected:
 		virtual void mouseMoveEvent    ( QGraphicsSceneMouseEvent *event );

Modified: trunk/sources/qetgraphicsitem/terminal.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/terminal.cpp	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/qetgraphicsitem/terminal.cpp	2016-12-08 15:06:46 UTC (rev 4803)
@@ -609,7 +609,7 @@
 	if (use_properties)
 	{
 		Conductor *other = conductors_list.toList().first();
-		new_conductor->setOthersSequential(other);
+		new_conductor->rSequenceNum() = other->sequenceNum();
 		new_conductor->setProperties(others_properties);
 	}
 	else

Modified: trunk/sources/qetxml.cpp
===================================================================
--- trunk/sources/qetxml.cpp	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/qetxml.cpp	2016-12-08 15:06:46 UTC (rev 4803)
@@ -254,3 +254,20 @@
 
 	return(true);
 }
+
+/**
+ * @brief QETXML::textToDomElement
+ * Return a QDomElement, created from @document, with tag name @tag_name and text @value.
+ * @param document
+ * @param tag_name
+ * @param value
+ * @return
+ */
+QDomElement QETXML::textToDomElement(QDomDocument &document, QString tag_name, QString value)
+{
+	QDomElement element = document.createElement(tag_name);
+	QDomText text = document.createTextNode(value);
+
+	element.appendChild(text);
+	return element;
+}

Modified: trunk/sources/qetxml.h
===================================================================
--- trunk/sources/qetxml.h	2016-12-07 18:56:31 UTC (rev 4802)
+++ trunk/sources/qetxml.h	2016-12-08 15:06:46 UTC (rev 4803)
@@ -41,6 +41,7 @@
 
 	bool writeXmlFile(const QDomDocument &xml_document, const QString &file_path, QString *error_message = nullptr);
 
+	QDomElement textToDomElement (QDomDocument &document, QString tag_name, QString value);
 }
 
 #endif // QETXML_H


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/