diff --git a/sources/autoNum/assignvariables.cpp b/sources/autoNum/assignvariables.cpp index f1161cc41..60afff257 100644 --- a/sources/autoNum/assignvariables.cpp +++ b/sources/autoNum/assignvariables.cpp @@ -19,6 +19,8 @@ #include "diagram.h" #include "element.h" #include "diagramposition.h" +#include "qetapp.h" + #include #include @@ -236,11 +238,11 @@ namespace autonum } /** - * @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 @@ namespace autonum 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(); + } + } diff --git a/sources/autoNum/assignvariables.h b/sources/autoNum/assignvariables.h index 8bbabfb74..e341b1807 100644 --- a/sources/autoNum/assignvariables.h +++ b/sources/autoNum/assignvariables.h @@ -26,6 +26,7 @@ class Diagram; class Element; +class ElementsLocation; namespace autonum { @@ -64,7 +65,8 @@ namespace autonum void setSequentialToList(QStringList &list, NumerotationContext &nc, QString type); void setFolioSequentialToHash(QStringList &list, QHash &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 diff --git a/sources/autoNum/ui/selectautonumw.cpp b/sources/autoNum/ui/selectautonumw.cpp index c7732a1f9..d50172900 100644 --- a/sources/autoNum/ui/selectautonumw.cpp +++ b/sources/autoNum/ui/selectautonumw.cpp @@ -255,7 +255,7 @@ void SelectAutonumW::contextToFormula() if (m_faw) { m_faw->clearContext(); - m_faw->setContext(autonum::NumerotationContextToFormula(toNumContext())); + m_faw->setContext(autonum::numerotationContextToFormula(toNumContext())); } } diff --git a/sources/qetgraphicsitem/customelement.cpp b/sources/qetgraphicsitem/customelement.cpp index aeb729c0a..e79f19ec2 100644 --- a/sources/qetgraphicsitem/customelement.cpp +++ b/sources/qetgraphicsitem/customelement.cpp @@ -63,6 +63,7 @@ CustomElement::CustomElement(const ElementsLocation &location, QGraphicsItem *qg 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 @@ void CustomElement::paint(QPainter *qp, const QStyleOptionGraphicsItem *options) 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++; - } - //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(); + //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); } } + 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); diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index acb0d5cdd..c9085d0f7 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -434,7 +434,7 @@ QHash QETProject::conductorAutoNumHash() { 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();