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 "numerotationcontextcommands.h"
19 #include "diagram.h"
20 
21 /**
22  * @brief Constructor
23  */
NumerotationContextCommands(const NumerotationContext & nc,Diagram * d)24 NumerotationContextCommands::NumerotationContextCommands(const NumerotationContext &nc, Diagram *d):
25 	diagram_ (d),
26 	context_ (nc),
27 	strategy_ (nullptr)
28 {}
29 
30 /**
31  * @brief Destructor
32  */
~NumerotationContextCommands()33 NumerotationContextCommands::~NumerotationContextCommands() {
34 	if (strategy_) delete strategy_;
35 }
36 
37 /**
38  * @brief NumerotationContextCommands::next
39  * @return the next numerotation context
40  */
next()41 NumerotationContext NumerotationContextCommands::next() {
42 	NumerotationContext contextnum;
43 
44 	for (int i=0; i<context_.size(); ++i) {
45 		QStringList str = context_.itemAt(i);
46 		setNumStrategy(str.at(0));
47 		contextnum << strategy_ -> next(context_, i);
48 	}
49 	return contextnum;
50 }
51 
52 /**
53  * @brief NumerotationContextCommands::previous
54  * @return the previous numerotation context
55  */
previous()56 NumerotationContext NumerotationContextCommands::previous() {
57 	NumerotationContext contextnum;
58 
59 	for (int i=0; i<context_.size(); ++i) {
60 		QStringList str = context_.itemAt(i);
61 		setNumStrategy(str.at(0));
62 		contextnum << strategy_ -> previous(context_, i);
63 	}
64 	return contextnum;
65 }
66 
67 /**
68  * @brief NumerotationContextCommands::toFinalString
69  * @return the string represented by the numerotation context
70  */
toRepresentedString()71 QString NumerotationContextCommands::toRepresentedString() {
72 	QString num;
73 	if (context_.size()) {
74 		for (int i=0; i<context_.size(); i++) {
75 			QStringList  str = context_.itemAt(i);
76 			setNumStrategy(str.at(0));
77 			num += strategy_ -> toRepresentedString(str.at(1));
78 		}
79 		return num;
80 	}
81 	if (diagram_) return (diagram_ -> defaultConductorProperties.text);
82 	return QString();
83 }
84 
85 /**
86  * @brief NumerotationContextCommands::setNumStrategy
87  * apply the good strategy relative to @str
88  */
setNumStrategy(const QString & str)89 void NumerotationContextCommands::setNumStrategy(const QString &str) {
90 	if (strategy_) delete strategy_;
91 	if (str == "unit") {
92 		strategy_ = new UnitNum(diagram_);
93 		return;
94 	}
95 	else if (str == "unitfolio") {
96 		strategy_ = new UnitFNum (diagram_);
97 		return;
98 	}
99 	else if (str == "ten") {
100 		strategy_ = new TenNum (diagram_);
101 		return;
102 	}
103 	else if (str == "tenfolio") {
104 		strategy_ = new TenFNum (diagram_);
105 		return;
106 	}
107 	else if (str == "hundred") {
108 		strategy_ = new HundredNum (diagram_);
109 		return;
110 	}
111 	else if (str == "hundredfolio") {
112 		strategy_ = new HundredFNum (diagram_);
113 		return;
114 	}
115 	else if (str == "string") {
116 		strategy_ = new StringNum (diagram_);
117 		return;
118 	}
119 	else if (str == "idfolio") {
120 		strategy_ = new IdFolioNum (diagram_);
121 		return;
122 	}
123 	else if (str=="folio"){
124 		strategy_ = new FolioNum (diagram_);
125 		return;
126 	}
127 	else if (str=="plant"){
128 		strategy_ = new PlantNum (diagram_);
129 		return;
130 	}
131 	else if (str=="locmach"){
132 		strategy_ = new LocmachNum (diagram_);
133 		return;
134 	}
135 	else if (str=="elementline"){
136 		strategy_ = new ElementLineNum (diagram_);
137 		return;
138 	}
139 	else if (str=="elementcolumn"){
140 		strategy_ = new ElementColumnNum (diagram_);
141 		return;
142 	}
143 	else if (str=="elementprefix"){
144 		strategy_ = new ElementPrefixNum (diagram_);
145 		return;
146 	}
147 }
148 
149 
150 
151 /**
152  * Constructor
153  */
NumStrategy(Diagram * d)154 NumStrategy::NumStrategy (Diagram *d):
155 	diagram_ (d)
156 {}
157 
~NumStrategy()158 NumStrategy::~NumStrategy() {}
159 
160 /**
161  * @brief NumStrategy::nextString
162  * @return the next value of @nc at position @i
163  */
nextString(const NumerotationContext & nc,const int i) const164 NumerotationContext NumStrategy::nextString (const NumerotationContext &nc, const int i) const {
165 	QStringList strl = nc.itemAt(i);
166 	NumerotationContext newnc;
167 	newnc.addValue(strl.at(0), strl.at(1), strl.at(2).toInt());
168 	return (newnc);
169 }
170 
171 /**
172  * @brief NumStrategy::nextNumber
173  * @return the next value of @nc at position @i
174  */
nextNumber(const NumerotationContext & nc,const int i) const175 NumerotationContext NumStrategy::nextNumber (const NumerotationContext &nc, const int i) const {
176 	QStringList strl = nc.itemAt(i);
177 	NumerotationContext newnc;
178 	QString value = QString::number( (strl.at(1).toInt()) + (strl.at(2).toInt()) );
179 	newnc.addValue(strl.at(0), value, strl.at(2).toInt(), strl.at(3).toInt());
180 	return (newnc);
181 }
182 
183 /**
184  * @brief NumStrategy::previousNumber
185  * @return  the previous value of @nc at position @i
186  */
previousNumber(const NumerotationContext & nc,const int i) const187 NumerotationContext NumStrategy::previousNumber(const NumerotationContext &nc, const int i) const {
188 	QStringList strl = nc.itemAt(i);
189 	NumerotationContext newnc;
190 	QString value = QString::number( (strl.at(1).toInt()) - (strl.at(2).toInt()) );
191 	newnc.addValue(strl.at(0), value, strl.at(2).toInt(), strl.at(3).toInt());
192 	return (newnc);
193 }
194 
195 /**
196  * Constructor
197  */
UnitNum(Diagram * d)198 UnitNum::UnitNum(Diagram *d):
199 	NumStrategy(d)
200 {}
201 
202 /**
203  * @brief UnitNum::toRepresentedString
204  * @return the represented string of num
205  */
toRepresentedString(const QString num) const206 QString UnitNum::toRepresentedString(const QString num) const {
207 	return (num);
208 }
209 
210 /**
211  * @brief UnitNum::next
212  * @return the next NumerotationContext nc at position i
213  */
next(const NumerotationContext & nc,const int i) const214 NumerotationContext UnitNum::next (const NumerotationContext &nc, const int i) const {
215 	return (nextNumber(nc, i));
216 }
217 
218 /**
219  * @brief UnitNum::previous
220  * @return the previous NumerotationContext nc at posiiton i
221  */
previous(const NumerotationContext & nc,const int i) const222 NumerotationContext UnitNum::previous(const NumerotationContext &nc, const int i) const {
223 	return (previousNumber(nc, i));
224 }
225 
226 /**
227  * Constructor
228  */
UnitFNum(Diagram * d)229 UnitFNum::UnitFNum(Diagram *d):
230 	NumStrategy(d)
231 {}
232 
233 /**
234  * @brief UnitFNum::toRepresentedString
235  * @return the represented string of num
236  */
toRepresentedString(const QString num) const237 QString UnitFNum::toRepresentedString(const QString num) const {
238 	return (num);
239 }
240 
241 /**
242  * @brief UnitFNum::next
243  * @return the next NumerotationContext nc at position i
244  */
next(const NumerotationContext & nc,const int i) const245 NumerotationContext UnitFNum::next (const NumerotationContext &nc, const int i) const {
246 	return (nextNumber(nc, i));
247 }
248 
249 /**
250  * @brief UnitFNum::previous
251  * @return the previous NumerotationContext nc at posiiton i
252  */
previous(const NumerotationContext & nc,const int i) const253 NumerotationContext UnitFNum::previous(const NumerotationContext &nc, const int i) const {
254 	return (previousNumber(nc, i));
255 }
256 
257 /**
258  * Constructor
259  */
TenNum(Diagram * d)260 TenNum::TenNum (Diagram *d):
261 	NumStrategy (d)
262 {}
263 
264 /**
265  * @brief TenNum::toRepresentedString
266  * @return the represented string of num
267  */
toRepresentedString(const QString num) const268 QString TenNum::toRepresentedString(const QString num) const {
269 	int numint = num.toInt();
270 	QString numstr = num;
271 	if (numint<10) numstr.prepend("0");
272 	return (numstr);
273 }
274 
275 /**
276  * @brief TenNum::next
277  * @return the next NumerotationContext nc at position i
278  */
next(const NumerotationContext & nc,const int i) const279 NumerotationContext TenNum::next (const NumerotationContext &nc, const int i) const {
280 	return (nextNumber(nc, i));
281 }
282 
283 /**
284  * @brief TenNum::previous
285  * @return the previous NumerotationContext nc at posiiton i
286  */
previous(const NumerotationContext & nc,const int i) const287 NumerotationContext TenNum::previous(const NumerotationContext &nc, const int i) const {
288 	return (previousNumber(nc, i));
289 }
290 
291 /**
292  * Constructor
293  */
TenFNum(Diagram * d)294 TenFNum::TenFNum (Diagram *d):
295 	NumStrategy (d)
296 {}
297 
298 /**
299  * @brief TenFNum::toRepresentedString
300  * @return the represented string of num
301  */
toRepresentedString(const QString num) const302 QString TenFNum::toRepresentedString(const QString num) const {
303 	int numint = num.toInt();
304 	QString numstr = num;
305 	if (numint<10) numstr.prepend("0");
306 	return (numstr);
307 }
308 
309 /**
310  * @brief TenFNum::next
311  * @return the next NumerotationContext nc at position i
312  */
next(const NumerotationContext & nc,const int i) const313 NumerotationContext TenFNum::next (const NumerotationContext &nc, const int i) const {
314 	return (nextNumber(nc, i));
315 }
316 
317 /**
318  * @brief TenFNum::previous
319  * @return the previous NumerotationContext nc at posiiton i
320  */
previous(const NumerotationContext & nc,const int i) const321 NumerotationContext TenFNum::previous(const NumerotationContext &nc, const int i) const {
322 	return (previousNumber(nc, i));
323 }
324 
325 
326 /**
327  * Constructor
328  */
HundredNum(Diagram * d)329 HundredNum::HundredNum (Diagram *d):
330 	NumStrategy (d)
331 {}
332 
333 /**
334  * @brief HundredNum::toRepresentedString
335  * @return the represented string of num
336  */
toRepresentedString(const QString num) const337 QString HundredNum::toRepresentedString(const QString num) const {
338 	int numint = num.toInt();
339 	QString numstr = num;
340 	if (numint<100) {
341 		if (numint<10) {
342 			numstr.prepend("00");
343 		}
344 		else numstr.prepend("0");
345 	}
346 	return (numstr);
347 }
348 
349 /**
350  * @brief HundredNum::next
351  * @return the next NumerotationContext nc at position i
352  */
next(const NumerotationContext & nc,const int i) const353 NumerotationContext HundredNum::next (const NumerotationContext &nc, const int i) const {
354 	return (nextNumber(nc, i));
355 }
356 
357 /**
358  * @brief HundredNum::previous
359  * @return the previous NumerotationContext nc at posiiton i
360  */
previous(const NumerotationContext & nc,const int i) const361 NumerotationContext HundredNum::previous(const NumerotationContext &nc, const int i) const {
362 	return (previousNumber(nc, i));
363 }
364 
365 /**
366  * Constructor
367  */
HundredFNum(Diagram * d)368 HundredFNum::HundredFNum (Diagram *d):
369 	NumStrategy (d)
370 {}
371 
372 /**
373  * @brief HundredFNum::toRepresentedString
374  * @return the represented string of num
375  */
toRepresentedString(const QString num) const376 QString HundredFNum::toRepresentedString(const QString num) const {
377 	int numint = num.toInt();
378 	QString numstr = num;
379 	if (numint<100) {
380 		if (numint<10) {
381 			numstr.prepend("00");
382 		}
383 		else numstr.prepend("0");
384 	}
385 	return (numstr);
386 }
387 
388 /**
389  * @brief HundredFNum::next
390  * @return the next NumerotationContext nc at position i
391  */
next(const NumerotationContext & nc,const int i) const392 NumerotationContext HundredFNum::next (const NumerotationContext &nc, const int i) const {
393 	return (nextNumber(nc, i));
394 }
395 
396 /**
397  * @brief HundredFNum::previous
398  * @return the previous NumerotationContext nc at posiiton i
399  */
previous(const NumerotationContext & nc,const int i) const400 NumerotationContext HundredFNum::previous(const NumerotationContext &nc, const int i) const {
401 	return (previousNumber(nc, i));
402 }
403 
404 /**
405  * Constructor
406  */
StringNum(Diagram * d)407 StringNum::StringNum (Diagram *d):
408 	NumStrategy (d)
409 {}
410 
411 /**
412  * @brief StringNum::toRepresentedString
413  * @return the represented string of num
414  */
toRepresentedString(const QString str) const415 QString StringNum::toRepresentedString(const QString str) const {
416 	return (str);
417 }
418 
419 /**
420  * @brief StringNum::next
421  * @return the next NumerotationContext nc at position i
422  */
next(const NumerotationContext & nc,const int i) const423 NumerotationContext StringNum::next (const NumerotationContext &nc, const int i) const {
424 	return (nextString(nc, i));
425 }
426 
427 /**
428  * @brief StringNum::previous
429  * @return the previous NumerotationContext nc at posiiton i
430  */
previous(const NumerotationContext & nc,const int i) const431 NumerotationContext StringNum::previous(const NumerotationContext &nc, const int i) const {
432 	return (nextString(nc, i));
433 }
434 
435 /**
436  * Constructor
437  */
IdFolioNum(Diagram * d)438 IdFolioNum::IdFolioNum (Diagram *d):
439 	NumStrategy (d)
440 {}
441 
442 /**
443  * @brief IdFolioNum::toRepresentedString
444  * @return the represented string of num
445  */
toRepresentedString(const QString str) const446 QString IdFolioNum::toRepresentedString(const QString str) const {
447 	Q_UNUSED(str);
448 	return ("%id");
449 }
450 
451 /**
452  * @brief IdFolioNum::next
453  * @return the next NumerotationContext nc at position i
454  */
next(const NumerotationContext & nc,const int i) const455 NumerotationContext IdFolioNum::next (const NumerotationContext &nc, const int i) const {
456 	return (nextString(nc, i));
457 }
458 
459 /**
460  * @brief IdFolioNum::previous
461  * @return the previous NumerotationContext nc at posiiton i
462  */
previous(const NumerotationContext & nc,const int i) const463 NumerotationContext IdFolioNum::previous(const NumerotationContext &nc, const int i) const {
464 	return (nextString(nc, i));
465 }
466 
467 /**
468  * Constructor
469  */
FolioNum(Diagram * d)470 FolioNum::FolioNum (Diagram *d):
471 	NumStrategy (d)
472 {}
473 
474 /**
475  * @brief FolioNum::toRepresentedString
476  * @return the represented string of folio
477  */
toRepresentedString(const QString str) const478 QString FolioNum::toRepresentedString(const QString str) const {
479 	Q_UNUSED(str);
480 	return ("%F");
481 }
482 
483 /**
484  * @brief FolioNum::next
485  * @return the next NumerotationContext nc at position i
486  */
next(const NumerotationContext & nc,const int i) const487 NumerotationContext FolioNum::next (const NumerotationContext &nc, const int i) const {
488 	return (nextString(nc, i));
489 }
490 
491 /**
492  * @brief FolioNum::previous
493  * @return the previous NumerotationContext nc at posiiton i
494  */
previous(const NumerotationContext & nc,const int i) const495 NumerotationContext FolioNum::previous(const NumerotationContext &nc, const int i) const {
496 	return (nextString(nc, i));
497 }
498 
499 /**
500  * Constructor
501  */
PlantNum(Diagram * d)502 PlantNum::PlantNum (Diagram *d):
503 	NumStrategy (d)
504 {}
505 
506 /**
507  * @brief PlantNum::toRepresentedString
508  * @return the represented string of folio
509  */
toRepresentedString(const QString str) const510 QString PlantNum::toRepresentedString(const QString str) const {
511 	Q_UNUSED(str);
512 	return "%M";
513 }
514 
515 /**
516  * @brief PlantNum::next
517  * @return the next NumerotationContext nc at position i
518  */
next(const NumerotationContext & nc,const int i) const519 NumerotationContext PlantNum::next (const NumerotationContext &nc, const int i) const {
520 	return (nextString(nc, i));
521 }
522 
523 /**
524  * @brief PlantNum::previous
525  * @return the previous NumerotationContext nc at posiiton i
526  */
previous(const NumerotationContext & nc,const int i) const527 NumerotationContext PlantNum::previous(const NumerotationContext &nc, const int i) const {
528 	return (nextString(nc, i));
529 }
530 
531 
532 /**
533  * Constructor
534  */
LocmachNum(Diagram * d)535 LocmachNum::LocmachNum (Diagram *d):
536 	NumStrategy (d)
537 {}
538 
539 /**
540  * @brief LocmachNum::toRepresentedString
541  * @return the represented string of folio
542  */
toRepresentedString(const QString str) const543 QString LocmachNum::toRepresentedString(const QString str) const {
544 	Q_UNUSED(str);
545 	return "%LM";
546 }
547 
548 /**
549  * @brief LocmachNum::next
550  * @return the next NumerotationContext nc at position i
551  */
next(const NumerotationContext & nc,const int i) const552 NumerotationContext LocmachNum::next (const NumerotationContext &nc, const int i) const {
553 	return (nextString(nc, i));
554 }
555 
556 /**
557  * @brief LocmachNum::previous
558  * @return the previous NumerotationContext nc at posiiton i
559  */
previous(const NumerotationContext & nc,const int i) const560 NumerotationContext LocmachNum::previous(const NumerotationContext &nc, const int i) const {
561 	return (nextString(nc, i));
562 }
563 
564 
565 /**
566  * Constructor
567  */
ElementLineNum(Diagram * d)568 ElementLineNum::ElementLineNum (Diagram *d):
569 	NumStrategy (d)
570 {}
571 
572 /**
573  * @brief ElementLineNum::toRepresentedString
574  * @return the represented string of folio
575  */
toRepresentedString(const QString str) const576 QString ElementLineNum::toRepresentedString(const QString str) const {
577 	Q_UNUSED(str);
578 	return "%l";
579 }
580 
581 /**
582  * @brief ElementLineNum::next
583  * @return the next NumerotationContext nc at position i
584  */
next(const NumerotationContext & nc,const int i) const585 NumerotationContext ElementLineNum::next (const NumerotationContext &nc, const int i) const {
586 	return (nextString(nc, i));
587 }
588 
589 /**
590  * @brief ElementLineNum::previous
591  * @return the previous NumerotationContext nc at posiiton i
592  */
previous(const NumerotationContext & nc,const int i) const593 NumerotationContext ElementLineNum::previous(const NumerotationContext &nc, const int i) const {
594 	return (nextString(nc, i));
595 }
596 
597 /**
598  * Constructor
599  */
ElementColumnNum(Diagram * d)600 ElementColumnNum::ElementColumnNum (Diagram *d):
601 	NumStrategy (d)
602 {}
603 
604 /**
605  * @brief ElementColumnNum::toRepresentedString
606  * @return the represented string of folio
607  */
toRepresentedString(const QString str) const608 QString ElementColumnNum::toRepresentedString(const QString str) const {
609 	Q_UNUSED(str);
610 	return "%c";
611 }
612 
613 /**
614  * @brief ElementColumnNum::next
615  * @return the next NumerotationContext nc at position i
616  */
next(const NumerotationContext & nc,const int i) const617 NumerotationContext ElementColumnNum::next (const NumerotationContext &nc, const int i) const {
618 	return (nextString(nc, i));
619 }
620 
621 /**
622  * @brief ElementColumnNum::previous
623  * @return the previous NumerotationContext nc at posiiton i
624  */
previous(const NumerotationContext & nc,const int i) const625 NumerotationContext ElementColumnNum::previous(const NumerotationContext &nc, const int i) const {
626 	return (nextString(nc, i));
627 }
628 
629 /**
630  * Constructor
631  */
ElementPrefixNum(Diagram * d)632 ElementPrefixNum::ElementPrefixNum (Diagram *d):
633 	NumStrategy (d)
634 {}
635 
636 /**
637  * @brief ElementPrefixNum::toRepresentedString
638  * @return the represented string of folio
639  */
toRepresentedString(const QString str) const640 QString ElementPrefixNum::toRepresentedString(const QString str) const {
641 	Q_UNUSED(str);
642 	return "%prefix";
643 }
644 
645 /**
646  * @brief ElementPrefixNum::next
647  * @return the next NumerotationContext nc at position i
648  */
next(const NumerotationContext & nc,const int i) const649 NumerotationContext ElementPrefixNum::next (const NumerotationContext &nc, const int i) const {
650 	return (nextString(nc, i));
651 }
652 
653 /**
654  * @brief ElementPrefixNum::previous
655  * @return the previous NumerotationContext nc at posiiton i
656  */
previous(const NumerotationContext & nc,const int i) const657 NumerotationContext ElementPrefixNum::previous(const NumerotationContext &nc, const int i) const {
658 	return (nextString(nc, i));
659 }
660 
661