Move code use to find prefix of element from element class, to autonum namespace

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4793 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2016-11-26 19:30:59 +00:00
parent 01af4c52e5
commit 489f2c2035
5 changed files with 122 additions and 88 deletions

View File

@@ -19,6 +19,8 @@
#include "diagram.h" #include "diagram.h"
#include "element.h" #include "element.h"
#include "diagramposition.h" #include "diagramposition.h"
#include "qetapp.h"
#include <QVariant> #include <QVariant>
#include <QStringList> #include <QStringList>
@@ -236,11 +238,11 @@ namespace autonum
} }
/** /**
* @brief NumerotationContextToFormula * @brief numerotationContextToFormula
* @param nc * @param nc
* @return the numerotation context, converted to formula * @return the numerotation context, converted to formula
*/ */
QString NumerotationContextToFormula(const NumerotationContext &nc) QString numerotationContextToFormula(const NumerotationContext &nc)
{ {
QString type; QString type;
QString value; QString value;
@@ -312,4 +314,85 @@ namespace autonum
return formula; 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();
}
} }

View File

@@ -26,6 +26,7 @@
class Diagram; class Diagram;
class Element; class Element;
class ElementsLocation;
namespace autonum namespace autonum
{ {
@@ -64,7 +65,8 @@ namespace autonum
void setSequentialToList(QStringList &list, NumerotationContext &nc, QString type); void setSequentialToList(QStringList &list, NumerotationContext &nc, QString type);
void setFolioSequentialToHash(QStringList &list, QHash<QString, QStringList> &hash, QString autoNumName); 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::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 #endif // ASSIGNVARIABLES_H

View File

@@ -255,7 +255,7 @@ void SelectAutonumW::contextToFormula()
if (m_faw) if (m_faw)
{ {
m_faw->clearContext(); m_faw->clearContext();
m_faw->setContext(autonum::NumerotationContextToFormula(toNumContext())); m_faw->setContext(autonum::numerotationContextToFormula(toNumContext()));
} }
} }

View File

@@ -63,6 +63,7 @@ CustomElement::CustomElement(const ElementsLocation &location, QGraphicsItem *qg
list_circles_.clear(); list_circles_.clear();
list_polygons_.clear(); list_polygons_.clear();
list_arcs_.clear(); list_arcs_.clear();
setPrefix(autonum::elementPrefixForLocation(location));
int elmt_state; int elmt_state;
buildFromXml(location.xml(), &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 has a specific label, it will be applied. See qet_labels.xml for more
instructions. instructions.
*/ */
void CustomElement::parseLabels() { void CustomElement::parseLabels()
if ((this->taggedText("label")!= NULL) && (location().projectId()!=-1) && (this->taggedText("label")->toPlainText()=="_")) { //element is being added {
QXmlStreamReader rxml; QString formula = location().project()->elementAutoNumCurrentFormula();
QString path[10]; DiagramContext &dc = rElementInformations();
QString prefix;
int i = -1;
ElementsLocation current_location = location();
int dirLevel = -1;
//Add location name to path array //element is being added
while(current_location.parent().fileName() != "import") { if (taggedText("label") && (location().projectId()!=-1) && (taggedText("label")->toPlainText()=="_"))
i++; {
path[i]=current_location.fileName(); if (getPrefix().isEmpty())
current_location = current_location.parent(); {
dirLevel++; if (!formula.isEmpty() && (this->linkType() != Element::Slave))
} {
//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();
dc.addValue("label", formula); dc.addValue("label", formula);
this->setTaggedText("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 //apply formula to specific label - This condition specify elements which have different labels e.g KM
//are being loaded at first time or being pasted //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) && else if ((this->taggedText("label")!= NULL) && (location().projectId()!=-1) &&
(!location().project()->elementAutoNumCurrentFormula().isEmpty()) && (!location().project()->elementAutoNumCurrentFormula().isEmpty()) &&
(this->linkType()!=Element::Slave) && !this->diagram()->item_paste) { (this->linkType()!=Element::Slave) && !this->diagram()->item_paste)
QString formula = location().project()->elementAutoNumCurrentFormula(); {
DiagramContext &dc = this->rElementInformations();
QString prefix = this->taggedText("label")->toPlainText(); QString prefix = this->taggedText("label")->toPlainText();
this->setPrefix(prefix); this->setPrefix(prefix);
dc.addValue("label", formula); dc.addValue("label", formula);

View File

@@ -434,7 +434,7 @@ QHash <QString, QString> QETProject::conductorAutoNumHash() {
QString QETProject::elementAutoNumFormula (QString key) const QString QETProject::elementAutoNumFormula (QString key) const
{ {
if (m_element_autonum.contains(key)) { if (m_element_autonum.contains(key)) {
return autonum::NumerotationContextToFormula(m_element_autonum[key]); return autonum::numerotationContextToFormula(m_element_autonum[key]);
} }
return QString(); return QString();