[qet] qet/qet: [4793] Move code use to find prefix of element from element class , to autonum namespace

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


Revision: 4793
Author:   blacksun
Date:     2016-11-26 20:30:59 +0100 (Sat, 26 Nov 2016)
Log Message:
-----------
Move code use to find prefix of element from element class, to autonum namespace

Modified Paths:
--------------
    trunk/sources/autoNum/assignvariables.cpp
    trunk/sources/autoNum/assignvariables.h
    trunk/sources/autoNum/ui/selectautonumw.cpp
    trunk/sources/qetgraphicsitem/customelement.cpp
    trunk/sources/qetproject.cpp

Modified: trunk/sources/autoNum/assignvariables.cpp
===================================================================
--- trunk/sources/autoNum/assignvariables.cpp	2016-11-26 17:12:31 UTC (rev 4792)
+++ trunk/sources/autoNum/assignvariables.cpp	2016-11-26 19:30:59 UTC (rev 4793)
@@ -19,6 +19,8 @@
 #include "diagram.h"
 #include "element.h"
 #include "diagramposition.h"
+#include "qetapp.h"
+
 #include <QVariant>
 #include <QStringList>
 
@@ -236,11 +238,11 @@
 	}
 
 	/**
-	 * @brief NumerotationContextToFormula
+	 * @brief numerotationContextToFormula
 	 * @param nc
 	 * @return the numerotation context, converted to formula
 	 */
-	QString NumerotationContextToFormula(const NumerotationContext &nc)
+	QString numerotationContextToFormula(const NumerotationContext &nc)
 	{
 		QString type;
 		QString value;
@@ -312,4 +314,85 @@
 		return formula;
 	}
 
+	/**
+	 * @brief elementPrefixForLocation
+	 * @param location
+	 * @return the prefix for an element represented by location, prefix can be null.
+	 * Search for a prefix only if @location represent an element embedded in a project
+	 */
+	QString elementPrefixForLocation(const ElementsLocation &location)
+	{
+		if (!location.isProject())
+			return QString();
+
+		QXmlStreamReader rxml;
+		QString path[10];
+		int i = -1;
+		ElementsLocation current_location = location;
+		int dirLevel = -1;
+
+			//Add location name to path array
+		while(current_location.parent().fileName() != "import")
+		{
+			i++;
+			path[i]=current_location.fileName();
+			current_location = current_location.parent();
+			dirLevel++;
+		}
+			//User Element without folder treatment
+		if (i == -1)
+		{
+			i = 0;
+			path[i]=current_location.fileName();
+			current_location = current_location.parent();
+			dirLevel = 0;
+		}
+
+		// Only Electric labels created so far
+		//if (current_location.fileName() != "10_electric")
+		QString qet_labels = "10_electric/qet_labels.xml";
+		QString filepath = QETApp::commonElementsDir().append(qet_labels);
+		QFile file(filepath);
+		file.isReadable();
+
+		if (!file.open(QFile::ReadOnly | QFile::Text))
+			return QString();
+
+		rxml.setDevice(&file);
+		rxml.readNext();
+
+		while(!rxml.atEnd())
+		{
+			if (rxml.attributes().value("name").toString() == path[i])
+			{
+				rxml.readNext();
+				i=i-1;
+					//reached element directory
+				if (i==0)
+				{
+					for (int j=i; j<= dirLevel; j = j +1)
+					{
+							//if there is a prefix available apply prefix
+						if(rxml.name()=="prefix")
+						{
+							return rxml.readElementText();
+						}
+							//if there isn't a prefix available, find parent prefix in parent folder
+						else
+						{
+							while (rxml.readNextStartElement() && rxml.name()!="prefix")
+							{
+								rxml.skipCurrentElement();
+								rxml.readNext();
+							}
+						}
+					}
+				}
+			}
+			rxml.readNext();
+		}
+
+		return QString();
+	}
+
 }

Modified: trunk/sources/autoNum/assignvariables.h
===================================================================
--- trunk/sources/autoNum/assignvariables.h	2016-11-26 17:12:31 UTC (rev 4792)
+++ trunk/sources/autoNum/assignvariables.h	2016-11-26 19:30:59 UTC (rev 4793)
@@ -26,6 +26,7 @@
 
 class Diagram;
 class Element;
+class ElementsLocation;
 
 namespace autonum
 {
@@ -64,7 +65,8 @@
 	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);
-	QString NumerotationContextToFormula(const NumerotationContext &nc);
+	QString numerotationContextToFormula(const NumerotationContext &nc);
+	QString elementPrefixForLocation(const ElementsLocation &location);
 }
 
 #endif // ASSIGNVARIABLES_H

Modified: trunk/sources/autoNum/ui/selectautonumw.cpp
===================================================================
--- trunk/sources/autoNum/ui/selectautonumw.cpp	2016-11-26 17:12:31 UTC (rev 4792)
+++ trunk/sources/autoNum/ui/selectautonumw.cpp	2016-11-26 19:30:59 UTC (rev 4793)
@@ -255,7 +255,7 @@
 	if (m_faw)
 	{
 		m_faw->clearContext();
-		m_faw->setContext(autonum::NumerotationContextToFormula(toNumContext()));
+		m_faw->setContext(autonum::numerotationContextToFormula(toNumContext()));
 	}
 }
 

Modified: trunk/sources/qetgraphicsitem/customelement.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/customelement.cpp	2016-11-26 17:12:31 UTC (rev 4792)
+++ trunk/sources/qetgraphicsitem/customelement.cpp	2016-11-26 19:30:59 UTC (rev 4793)
@@ -63,6 +63,7 @@
 	list_circles_.clear();
 	list_polygons_.clear();
 	list_arcs_.clear();
+	setPrefix(autonum::elementPrefixForLocation(location));
 
 	int elmt_state;
 	buildFromXml(location.xml(), &elmt_state);
@@ -270,97 +271,45 @@
 	has a specific label, it will be applied. See qet_labels.xml for more
 	instructions.
 */
-void CustomElement::parseLabels() {
-	if ((this->taggedText("label")!= NULL) && (location().projectId()!=-1) && (this->taggedText("label")->toPlainText()=="_")) { //element is being added
-		QXmlStreamReader rxml;
-		QString path[10];
-		QString prefix;
-		int i = -1;
-		ElementsLocation current_location = location();
-		int dirLevel = -1;
+void CustomElement::parseLabels()
+{
+	QString formula = location().project()->elementAutoNumCurrentFormula();
+	DiagramContext &dc = rElementInformations();
 
-		//Add location name to path array
-		while(current_location.parent().fileName() != "import") {
-			i++;
-			path[i]=current_location.fileName();
-			current_location = current_location.parent();
-			dirLevel++;
+		//element is being added
+	if (taggedText("label") && (location().projectId()!=-1) && (taggedText("label")->toPlainText()=="_"))
+	{
+		if (getPrefix().isEmpty())
+		{
+			if (!formula.isEmpty() && (this->linkType() != Element::Slave))
+			{
+				dc.addValue("label", formula);
+				this->setTaggedText("label",formula);
+			}
 		}
-		//User Element without folder treatment
-		if (i == -1) {
-			i = 0;
-			path[i]=current_location.fileName();
-			current_location = current_location.parent();
-			dirLevel = 0;
-		}
-
-		// Only Electric labels created so far
-		//if (current_location.fileName() != "10_electric")
-		QString qet_labels = "10_electric/qet_labels.xml";
-		QString filepath = QETApp::commonElementsDir().append(qet_labels);
-		QFile file(filepath);
-		file.isReadable();
-		if (!file.open(QFile::ReadOnly | QFile::Text)) return;
-		rxml.setDevice(&file);
-		rxml.readNext();
-
-		while(!rxml.atEnd()) {
-				if (rxml.attributes().value("name").toString() == path[i]) {
-					rxml.readNext();
-					i=i-1;
-					//reached element directory
-					if (i==0) {
-						for (int j=i; j<= dirLevel; j = j +1){
-							//if there is a prefix available apply prefix
-							if(rxml.name()=="prefix") {
-								prefix = rxml.readElementText();
-								DiagramContext &dc = this->rElementInformations();
-								//if there is a formula to assign, assign it
-								if (!(location().project()->elementAutoNumCurrentFormula().isEmpty()) && (location().project()->elementAutoNumCurrentFormula() != "") &&
-										(this->linkType()!=Element::Slave)) {
-									QString formula = location().project()->elementAutoNumCurrentFormula();
-									this->setPrefix(prefix);
-									dc.addValue("label", formula);
-									this->setTaggedText("label",formula);
-								} else { //assign only prefix
-									this->setPrefix(prefix);
-									dc.addValue("label", "%prefix");
-									this->setTaggedText("label", prefix);
-								}
-								this->setElementInformations(dc);
-								return;
-							}
-							//if there isn't a prefix available, find parent prefix in parent folder
-							else {
-								while (rxml.readNextStartElement() && rxml.name()!="prefix") {
-									rxml.skipCurrentElement();
-									rxml.readNext();
-								}
-							}
-						}
-					}
-				}
-				rxml.readNext();
-		}
-		if (prefix == "") {
-			if (!(location().project()->elementAutoNumCurrentFormula().isEmpty()) && (location().project()->elementAutoNumCurrentFormula() != "") &&
-					(this->linkType()!=Element::Slave)) {
-				QString formula = location().project()->elementAutoNumCurrentFormula();
-				this->setPrefix(prefix);
-				DiagramContext &dc = this->rElementInformations();
+		else
+		{
+				//if there is a formula to assign, assign it
+			if (!formula.isEmpty() && (this->linkType() != Element::Slave))
+			{
 				dc.addValue("label", formula);
 				this->setTaggedText("label",formula);
 			}
+			else
+			{ //assign only prefix
+				dc.addValue("label", "%prefix");
+				this->setTaggedText("label", getPrefix());
+			}
 		}
 	}
-	//apply formula to specific label - This condition specify elements which have different labels e.g KM
-	//that are already specified in the element label (inside .elmt file). This method is not called if elements
-	//are being loaded at first time or being pasted
+
+		//apply formula to specific label - This condition specify elements which have different labels e.g KM
+		//that are already specified in the element label (inside .elmt file). This method is not called if elements
+		//are being loaded at first time or being pasted
 	else if ((this->taggedText("label")!= NULL) && (location().projectId()!=-1) &&
 			 (!location().project()->elementAutoNumCurrentFormula().isEmpty()) &&
-			 (this->linkType()!=Element::Slave) && !this->diagram()->item_paste) {
-		QString formula = location().project()->elementAutoNumCurrentFormula();
-		DiagramContext &dc = this->rElementInformations();
+			 (this->linkType()!=Element::Slave) && !this->diagram()->item_paste)
+	{
 		QString prefix = this->taggedText("label")->toPlainText();
 		this->setPrefix(prefix);
 		dc.addValue("label", formula);

Modified: trunk/sources/qetproject.cpp
===================================================================
--- trunk/sources/qetproject.cpp	2016-11-26 17:12:31 UTC (rev 4792)
+++ trunk/sources/qetproject.cpp	2016-11-26 19:30:59 UTC (rev 4793)
@@ -434,7 +434,7 @@
 QString QETProject::elementAutoNumFormula (QString key) const
 {
 	if (m_element_autonum.contains(key)) {
-		return autonum::NumerotationContextToFormula(m_element_autonum[key]);
+		return autonum::numerotationContextToFormula(m_element_autonum[key]);
 	}
 
 	return QString();


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