QElectroTech  0.70
assignvariables.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "assignvariables.h"
19 #include "diagram.h"
20 #include "element.h"
21 #include "diagramposition.h"
22 #include "qetapp.h"
23 #include "qetxml.h"
24 
25 #include <QVariant>
26 #include <QStringList>
27 #include <utility>
28 
29 namespace autonum
30 {
35  {}
36 
38  {
39  unit = other.unit;
40  unit_folio = other.unit_folio;
41  ten = other.ten;
42  ten_folio = other.ten_folio;
43  hundred = other.hundred;
45  }
46 
48 
50  {
51  if (&other == this || other == *this)
52  return (*this);
53 
54  unit = other.unit;
55  unit_folio = other.unit_folio;
56  ten = other.ten;
57  ten_folio = other.ten_folio;
58  hundred = other.hundred;
60 
61  return (*this);
62  }
63 
65  {
66  if (unit == other.unit && \
67  unit_folio == other.unit_folio && \
68  ten == other.ten && \
69  ten_folio == other.ten_folio && \
70  hundred == other.hundred && \
72  return true;
73  else
74  return false;
75  }
76 
78  {
79  if (*this == other)
80  return false;
81  else
82  return true;
83  }
84 
92  QDomElement sequentialNumbers::toXml(QDomDocument &document, const QString& tag_name) const
93  {
94  QDomElement element = document.createElement(tag_name);
95 
96  if (!unit.isEmpty())
97  element.appendChild(QETXML::textToDomElement(document, "unit", unit.join(";")));
98  if (!unit_folio.isEmpty())
99  element.appendChild(QETXML::textToDomElement(document, "unitFolio", unit_folio.join(";")));
100  if(!ten.isEmpty())
101  element.appendChild(QETXML::textToDomElement(document, "ten", ten.join(";")));
102  if(!ten_folio.isEmpty())
103  element.appendChild(QETXML::textToDomElement(document, "tenFolio", ten_folio.join(";")));
104  if(!hundred.isEmpty())
105  element.appendChild(QETXML::textToDomElement(document, "hundred", hundred.join(";")));
106  if(!hundred_folio.isEmpty())
107  element.appendChild(QETXML::textToDomElement(document, "hundredFolio", hundred_folio.join(";")));
108 
109  return element;
110  }
111 
117  void sequentialNumbers::fromXml(const QDomElement &element)
118  {
119  if (!element.hasChildNodes())
120  return;
121 
122  QDomElement from;
123 
124  from = element.firstChildElement("unit");
125  unit = from.text().split(";");
126 
127  from = element.firstChildElement("unitFolio");
128  unit_folio = from.text().split(";");
129 
130  from = element.firstChildElement("ten");
131  ten = from.text().split(";");
132 
133  from = element.firstChildElement("tenFolio");
134  ten_folio = from.text().split(";");
135 
136  from = element.firstChildElement("hundred");
137  hundred = from.text().split(";");
138 
139  from = element.firstChildElement("hundredFolio");
140  hundred_folio = from.text().split(";");
141  }
142 
143  //Clear this sequence
145  {
146  unit.clear();
147  unit_folio.clear();
148  ten.clear();
149  ten_folio.clear();
150  hundred.clear();
151  hundred_folio.clear();
152  }
153 
163  QString AssignVariables::formulaToLabel(QString formula, sequentialNumbers &seqStruct, Diagram *diagram, const Element *elmt)
164  {
165  AssignVariables av(std::move(formula), seqStruct, diagram, elmt);
166  seqStruct = av.m_seq_struct;
167  return av.m_assigned_label;
168  }
169 
177  QString AssignVariables::replaceVariable(const QString &formula, const DiagramContext &dc)
178  {
179  QString str = formula;
180  str.replace("%{label}", dc.value("label").toString());
181  str.replace("%{plant}", dc.value("plant").toString());
182  str.replace("%{comment}", dc.value("comment").toString());
183  str.replace("%{description}", dc.value("description").toString());
184  str.replace("%{designation}", dc.value("designation").toString());
185  str.replace("%{manufacturer}", dc.value("manufacturer").toString());
186  str.replace("%{manufacturer-reference}", dc.value("manufacturer-reference").toString());
187  str.replace("%{supplier}", dc.value("supplier").toString());
188  str.replace("%{quantity}", dc.value("quantity").toString());
189  str.replace("%{unity}", dc.value("unity").toString());
190  str.replace("%{auxiliary1}", dc.value("auxiliary1").toString());
191  str.replace("%{auxiliary2}", dc.value("auxiliary2").toString());
192  str.replace("%{machine-manufacturer-reference}", dc.value("machine-manufacturer-reference").toString());
193  str.replace("%{location}", dc.value("location").toString());
194  str.replace("%{function}", dc.value("function").toString());
195  str.replace("%{void}", QString());
196 
197  return str;
198  }
199 
200 
201  AssignVariables::AssignVariables(const QString& formula, const sequentialNumbers& seqStruct , Diagram *diagram, const Element *elmt):
202  m_diagram(diagram),
203  m_arg_formula(formula),
204  m_assigned_label(formula),
205  m_seq_struct(seqStruct),
206  m_element(elmt)
207 
208  {
209  if (m_diagram)
210  {
211  m_assigned_label.replace("%F", m_diagram -> border_and_titleblock.folio());
212  m_assigned_label.replace("%f", QString::number(m_diagram->folioIndex()+1));
213  m_assigned_label.replace("%id", QString::number(m_diagram->folioIndex()+1));
214  m_assigned_label.replace("%total", QString::number(m_diagram->border_and_titleblock.folioTotal()));
215  m_assigned_label.replace("%M", m_diagram -> border_and_titleblock.plant());
216  m_assigned_label.replace("%LM", m_diagram -> border_and_titleblock.locmach());
217 
218  QSettings settings;
219  if (m_element)
220  {
221  if (settings.value("border-columns_0", true).toBool()){
222  m_assigned_label.replace("%c", QString::number(m_diagram->convertPosition(m_element->scenePos()).number() - 1));
223  }else{
224  m_assigned_label.replace("%c", QString::number(m_diagram->convertPosition(m_element->scenePos()).number()));
225  }
226  m_assigned_label.replace("%l", m_diagram->convertPosition(m_element->scenePos()).letter());
227  m_assigned_label.replace("%prefix", m_element->getPrefix());
228  }
229 
232  assignSequence();
233  }
234  }
235 
237  {
238  for (int i = 0; i < m_diagram->border_and_titleblock.additionalFields().count(); i++)
239  {
240  QString folio_variable = m_diagram->border_and_titleblock.additionalFields().keys().at(i);
241  QVariant folio_value = m_diagram->border_and_titleblock.additionalFields().operator [](folio_variable);
242 
243  if (m_assigned_label.contains(folio_variable)) {
244  m_assigned_label.replace("%{" + folio_variable + "}", folio_value.toString());
245  m_assigned_label.replace("%" + folio_variable , folio_value.toString());
246  }
247  }
248  }
249 
251  {
252  for (int i = 0; i < m_diagram->project()->projectProperties().count(); i++)
253  {
254  QString folio_variable = m_diagram->project()->projectProperties().keys().at(i);
255  QVariant folio_value = m_diagram->project()->projectProperties().operator [](folio_variable);
256 
257  if (m_assigned_label.contains(folio_variable)) {
258  m_assigned_label.replace("%{" + folio_variable + "}", folio_value.toString());
259  m_assigned_label.replace("%" + folio_variable , folio_value.toString());
260  }
261  }
262  }
263 
265  {
266  int max = qMax(
267  qMax(
268  qMax(m_seq_struct.unit_folio.size(),
269  m_seq_struct.ten_folio.size()),
270  qMax(m_seq_struct.hundred_folio.size(),
271  m_seq_struct.unit.size())),
272  qMax(m_seq_struct.hundred.size(),
273  m_seq_struct.ten.size())
274  );
275 
276  for (int i=1; i<=max ; i++)
277  {
278  if (m_assigned_label.contains("%sequ_" + QString::number(i)) && m_seq_struct.unit.size() >= i) {
279  m_assigned_label.replace("%sequ_" + QString::number(i),m_seq_struct.unit.at(i-1));
280  }
281  if (m_assigned_label.contains("%seqt_" + QString::number(i)) && m_seq_struct.ten.size() >= i) {
282  m_assigned_label.replace("%seqt_" + QString::number(i),m_seq_struct.ten.at(i-1));
283  }
284  if (m_assigned_label.contains("%seqh_" + QString::number(i)) && m_seq_struct.hundred.size() >= i) {
285  m_assigned_label.replace("%seqh_" + QString::number(i),m_seq_struct.hundred.at(i-1));
286  }
287  if (m_assigned_label.contains("%sequf_" + QString::number(i)) && m_seq_struct.unit_folio.size() >= i) {
288  m_assigned_label.replace("%sequf_" + QString::number(i),m_seq_struct.unit_folio.at(i-1));
289  }
290  if (m_assigned_label.contains("%seqtf_" + QString::number(i)) && m_seq_struct.ten_folio.size() >= i) {
291  m_assigned_label.replace("%seqtf_" + QString::number(i),m_seq_struct.ten_folio.at(i-1));
292  }
293  if (m_assigned_label.contains("%seqhf_" + QString::number(i)) && m_seq_struct.hundred_folio.size() >= i) {
294  m_assigned_label.replace("%seqhf_" + QString::number(i),m_seq_struct.hundred_folio.at(i-1));
295  }
296  }
297  }
298 
306  void setSequentialToList(QStringList &list, NumerotationContext &context, const QString& type)
307  {
308  for (int i = 0; i < context.size(); i++)
309  {
310  if (context.itemAt(i).at(0) == type)
311  {
312  QString number;
313  if (type == "ten" || type == "tenfolio")
314  number = QString("%1").arg(context.itemAt(i).at(1).toInt(), 2, 10, QChar('0'));
315  else if (type == "hundred" || type == "hundredfolio")
316  number = QString("%1").arg(context.itemAt(i).at(1).toInt(), 3, 10, QChar('0'));
317  else number = QString::number(context.itemAt(i).at(1).toInt());
318  list.append(number);
319  }
320  }
321  }
322 
330  void setFolioSequentialToHash(QStringList &list, QHash<QString, QStringList> &hash, const QString& autoNumName)
331  {
332  if (hash.isEmpty() || !hash.contains(autoNumName))
333  {
334  QStringList max;
335  for (int i = 0; i < list.size(); i++)
336  {
337  max.append(list.at(i));
338  }
339  hash.insert(autoNumName,max);
340  }
341  else if (hash.contains(autoNumName))
342  {
343  //Load the String List and update it
344  QStringList max = hash.value(autoNumName);
345  for (int i = 0; i < list.size(); i++)
346  {
347  if ((list.at(i).toInt()) > max.at(i).toInt())
348  {
349  max.replace(i,list.at(i));
350  hash.remove(autoNumName);
351  hash.insert(autoNumName,max);
352  }
353  }
354  }
355  }
356 
367  void setSequential(const QString& label, sequentialNumbers &seqStruct, NumerotationContext &context, Diagram *diagram, const QString& hashKey)
368  {
369  if (!context.isEmpty())
370  {
371  if (label.contains("%sequ_"))
372  {
373  autonum::setSequentialToList(seqStruct.unit, context,"unit");
374  }
375  if (label.contains("%sequf_"))
376  {
377  autonum::setSequentialToList(seqStruct.unit_folio, context,"unitfolio");
379  }
380  if (label.contains("%seqt_"))
381  {
382  autonum::setSequentialToList(seqStruct.ten, context,"ten");
383  }
384  if (label.contains("%seqtf_"))
385  {
386  autonum::setSequentialToList(seqStruct.ten_folio, context,"tenfolio");
388  }
389  if (label.contains("%seqh_"))
390  {
391  autonum::setSequentialToList(seqStruct.hundred, context,"hundred");
392  }
393  if (label.contains("%seqhf_"))
394  {
395  autonum::setSequentialToList(seqStruct.hundred_folio, context,"hundredfolio");
397  }
398  }
399  }
400 
407  {
408  QString type;
409  QString value;
410  QString formula;
411  int count_unit = 0;
412  int count_unitf = 0;
413  int count_ten = 0;
414  int count_tenf = 0;
415  int count_hundred = 0;
416  int count_hundredf = 0;
417 
418  for(int i=0 ; i<nc.size() ; i++)
419  {
420  type = nc.itemAt(i).at(0);
421  value = nc.itemAt(i).at(1);
422 
423  if (type == "idfolio") {
424  formula.append("%id");
425  }
426  else if (type == "folio") {
427  formula.append("%F");
428  }
429  else if (type == "plant") {
430  formula.append("%M");
431  }
432  else if (type == "locmach") {
433  formula.append("%LM");
434  }
435 
436 
437  else if (type == "elementcolumn") {
438  formula.append("%c");
439  }
440  else if (type == "elementline") {
441  formula.append("%l");
442  }
443  else if (type == "elementprefix") {
444  formula.append("%prefix");
445  }
446  else if (type == "string") {
447  formula.append(value);
448  }
449  else if (type == "unit") {
450  count_unit++;
451  formula.append("%sequ_" + QString::number(count_unit));
452  }
453  else if (type == "unitfolio") {
454  count_unitf++;
455  formula.append("%sequf_" + QString::number(count_unitf));
456  }
457  else if (type == "ten") {
458  count_ten++;
459  formula.append("%seqt_" + QString::number(count_ten));
460  }
461  else if (type == "tenfolio") {
462  count_tenf++;
463  formula.append("%seqtf_" + QString::number(count_tenf));
464  }
465  else if (type == "hundred") {
466  count_hundred++;
467  formula.append("%seqh_" + QString::number(count_hundred));
468  }
469  else if (type == "hundredfolio") {
470  count_hundredf++;
471  formula.append("%seqhf_" + QString::number(count_hundredf));
472  }
473  }
474 
475  return formula;
476  }
477 
485  {
486  if (!location.isProject())
487  return QString();
488 
489  QXmlStreamReader rxml;
490  QString path[10];
491  int i = -1;
492  ElementsLocation current_location = location;
493  int dirLevel = -1;
494 
495  //Add location name to path array
496  while((current_location.parent() != current_location) && (current_location.parent().fileName() != "import"))
497  {
498  i++;
499  path[i]=current_location.fileName();
500  current_location = current_location.parent();
501  dirLevel++;
502  }
503  //User Element without folder treatment
504  if (i == -1)
505  {
506  i = 0;
507  path[i]=current_location.fileName();
508  current_location = current_location.parent();
509  dirLevel = 0;
510  }
511 
512  // Create Custom labels if qet_labels.xml if exit in customElementsDir
513  if (current_location.fileName() != "10_electric"){
514  QString custom_labels = "qet_labels.xml";
515  QString customfilepath = QETApp::customElementsDir().append(custom_labels);
516 
517  QFile file(customfilepath);
518  file.isReadable();
519  if (!file.open(QFile::ReadOnly | QFile::Text))
520  return QString();
521  rxml.setDevice(&file);
522  rxml.readNext();
523 
524  while(!rxml.atEnd())
525  {
526  if (rxml.attributes().value("name").toString() == path[i])
527  {
528  rxml.readNext();
529  i=i-1;
530  //reached element directory
531  if (i==0)
532  {
533  for (int j=i; j<= dirLevel; j = j +1)
534  {
535  //if there is a prefix available apply prefix
536  if(rxml.name()=="prefix")
537  {
538  return rxml.readElementText();
539  }
540  //if there isn't a prefix available, find parent prefix in parent folder
541  else
542  {
543  while (rxml.readNextStartElement() && rxml.name()!="prefix")
544  {
545  rxml.skipCurrentElement();
546  rxml.readNext();
547  }
548  }
549  }
550  }
551  }
552  rxml.readNext();
553  }
554  }
555  else
556  {
557  QString qet_labels = "10_electric/qet_labels.xml";
558  QString filepath = QETApp::commonElementsDir().append(qet_labels);
559  QFile file(filepath);
560  file.isReadable();
561  if (!file.open(QFile::ReadOnly | QFile::Text))
562  return QString();
563 
564  rxml.setDevice(&file);
565  rxml.readNext();
566 
567  while(!rxml.atEnd())
568  {
569  if (rxml.attributes().value("name").toString() == path[i])
570  {
571  rxml.readNext();
572  i=i-1;
573  //reached element directory
574  if (i==0)
575  {
576  for (int j=i; j<= dirLevel; j = j +1)
577  {
578  //if there is a prefix available apply prefix
579  if(rxml.name()=="prefix")
580  {
581  return rxml.readElementText();
582  }
583  //if there isn't a prefix available, find parent prefix in parent folder
584  else
585  {
586  while (rxml.readNextStartElement() && rxml.name()!="prefix")
587  {
588  rxml.skipCurrentElement();
589  rxml.readNext();
590  }
591  }
592  }
593  }
594  }
595  rxml.readNext();
596  }
597 }
598  return QString();
599  }
600 }
const Element * m_element
bool isProject() const
ElementsLocation::isProject.
static QString formulaToLabel(QString formula, sequentialNumbers &seqStruct, Diagram *diagram, const Element *elmt=nullptr)
AssignVariables::formulaToLabel Return the with variable assigned (ready to be displayed) ...
QStringList itemAt(const int) const
NumerotationContext::itemAt.
int folioTotal() const
static QString customElementsDir()
QETApp::customElementsDir.
Definition: qetapp.cpp:555
int folioIndex() const
Definition: diagram.cpp:1738
void setSequentialToList(QStringList &list, NumerotationContext &context, const QString &type)
setSequentialToList Append all sequential of type owned by in list
QHash< QString, QStringList > m_elmt_unitfolio_max
Hash containing max values for folio sequential autonums in this diagram.
Definition: diagram.h:93
QString fileName() const
ElementLocation::fileName.
QVariant value(const QString &key) const
QDomElement toXml(QDomDocument &document, const QString &tag_name=QString("sequentialNumbers")) const
sequentialNumbers::toXml export this sequential numbers into a QDomElement.
void setSequential(const QString &label, sequentialNumbers &seqStruct, NumerotationContext &context, Diagram *diagram, const QString &hashKey)
setSequential Fill seqStruct
QString numerotationContextToFormula(const NumerotationContext &nc)
numerotationContextToFormula
bool operator==(const sequentialNumbers &other) const
AssignVariables(const QString &formula, const sequentialNumbers &seqStruct, Diagram *diagram, const Element *elmt=nullptr)
QList< QString > keys(KeyOrder=None) const
int size() const
NumerotationContext::size.
DiagramContext projectProperties()
bool operator!=(const sequentialNumbers &other) const
DiagramContext additionalFields() const
QString elementPrefixForLocation(const ElementsLocation &location)
elementPrefixForLocation
sequentialNumbers & operator=(const sequentialNumbers &other)
DiagramPosition convertPosition(const QPointF &)
Definition: diagram.cpp:1637
QHash< QString, QStringList > m_elmt_hundredfolio_max
Definition: diagram.h:95
void setFolioSequentialToHash(QStringList &list, QHash< QString, QStringList > &hash, const QString &autoNumName)
setFolioSequentialToHash Insert all value of in with key
QHash< QString, QStringList > m_elmt_tenfolio_max
Definition: diagram.h:94
bool isEmpty() const
NumerotationContext::isEmpty.
sequentialNumbers m_seq_struct
void fromXml(const QDomElement &element)
sequentialNumbers::fromXml Import sequential values from a QDomElement
QString getPrefix() const
Element::getPrefix get Element Prefix.
Definition: element.cpp:1529
QDomElement textToDomElement(QDomDocument &document, const QString &tag_name, const QString &value)
QETXML::textToDomElement Return a QDomElement, created from , with tag name and text ...
Definition: qetxml.cpp:269
static QString commonElementsDir()
QETApp::commonElementsDir.
Definition: qetapp.cpp:511
BorderTitleBlock border_and_titleblock
Diagram dimensions and title block.
Definition: diagram.h:74
The AssignVariables class This class assign variable of a formula string. Return the final string use...
QETProject * project() const
Definition: diagram.cpp:1716
ElementsLocation parent() const
sequentialNumbers()
sequentialNumbers::sequentialNumbers
static QString replaceVariable(const QString &formula, const DiagramContext &dc)
AssignVariables::replaceVariable Replace the variables in in form %{my-var} to the corresponding val...