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 <QRegExp>
19 #include "numparteditorw.h"
20 #include "ui_numparteditorw.h"
21 
22 /**
23  * Constructor
24  */
NumPartEditorW(int type,QWidget * parent)25 NumPartEditorW::NumPartEditorW(int type, QWidget *parent) :
26 	QWidget(parent),
27 	ui(new Ui::NumPartEditorW),
28 	intValidator (new QIntValidator(0,99999,this)),
29 	m_edited_type(type)
30 {
31 	ui -> setupUi(this);
32 	setVisibleItems();
33 	setType(NumPartEditorW::unit, true);
34 }
35 
36 /**
37  * Constructor
38  * Build with value of @context at position i
39  */
NumPartEditorW(NumerotationContext & context,int i,int type,QWidget * parent)40 NumPartEditorW::NumPartEditorW (NumerotationContext &context, int i, int type, QWidget *parent):
41 	QWidget(parent),
42 	ui(new Ui::NumPartEditorW),
43 	intValidator (new QIntValidator(0,99999,this)),
44 	m_edited_type(type)
45 {
46 	ui -> setupUi(this);
47 	setVisibleItems();
48 	if(context.size()==0) setType(NumPartEditorW::unit, true);
49 
50 	else {
51 		QStringList strl = context.itemAt(i);
52 		if (strl.at(0)=="unit") setType(NumPartEditorW::unit, true);
53 		else if (strl.at(0)=="unitfolio") setType(NumPartEditorW::unitfolio, true);
54 		else if (strl.at(0)=="ten") setType(NumPartEditorW::ten, true);
55 		else if (strl.at(0)=="tenfolio") setType(NumPartEditorW::tenfolio, true);
56 		else if (strl.at(0)=="hundred") setType(NumPartEditorW::hundred, true);
57 		else if (strl.at(0)=="hundredfolio") setType(NumPartEditorW::hundredfolio, true);
58 		else if (strl.at(0)=="string") setType(NumPartEditorW::string);
59 		else if (strl.at(0)=="idfolio") setType(NumPartEditorW::idfolio);
60 		else if (strl.at(0)=="folio") setType(NumPartEditorW::folio);
61 		else if (strl.at(0)=="plant") setType(NumPartEditorW::plant);
62 		else if (strl.at(0)=="locmach") setType(NumPartEditorW::locmach);
63 		else if (strl.at(0)=="elementline") setType(NumPartEditorW::elementline);
64 		else if (strl.at(0)=="elementcolumn") setType(NumPartEditorW::elementcolumn);
65 		else if (strl.at(0)=="elementprefix") setType(NumPartEditorW::elementprefix);
66 		ui -> value_field -> setText(strl.at(1));
67 		ui -> increase_spinBox -> setValue(strl.at(2).toInt());
68 	}
69 }
70 
71 /**
72  * Destructor
73  */
~NumPartEditorW()74 NumPartEditorW::~NumPartEditorW()
75 {
76 	delete intValidator;
77 	delete ui;
78 }
79 
setVisibleItems()80 void NumPartEditorW::setVisibleItems()
81 {
82 	ui->type_cb->setInsertPolicy(QComboBox::InsertAtBottom);
83 	QStringList items;
84 	if (m_edited_type == 2)
85 	{
86 		items	<< tr("Chiffre 1") << tr("Chiffre 01")
87 				<< tr("Chiffre 001")
88 				<< tr("Texte");
89 	}
90 	else if (m_edited_type == 1)
91 	{
92 		items	<< tr("Chiffre 1") << tr("Chiffre 1 - Folio") << tr("Chiffre 01")
93 				<< tr("Chiffre 01 - Folio") << tr("Chiffre 001") << tr("Chiffre 001 - Folio")
94 				<< tr("Texte") << tr("N° folio") << tr("Folio") << tr("Installation") << tr("Locmach");
95 	}
96 	else
97 		items << tr("Chiffre 1") << tr("Chiffre 1 - Folio") << tr("Chiffre 01")
98 			  << tr("Chiffre 01 - Folio") << tr("Chiffre 001") << tr("Chiffre 001 - Folio")
99 			  << tr("Texte") << tr("N° folio") << tr("Folio") << tr("Installation") << tr("Locmach")
100 			  << tr("Element Line") << tr("Element Column") << tr("Element Prefix");
101 	ui->type_cb->insertItems(0,items);
102 }
103 
104 /**
105  * @brief NumPartEditorW::toNumContext
106  * @return the display to NumerotationContext
107  */
toNumContext()108 NumerotationContext NumPartEditorW::toNumContext() {
109 	NumerotationContext nc;
110 	QString type_str;
111 	switch (type_) {
112 		case unit:
113 			type_str = "unit";
114 			break;
115 		case unitfolio:
116 			type_str = "unitfolio";
117 			break;
118 		case ten:
119 			type_str = "ten";
120 			break;
121 		case tenfolio:
122 			type_str = "tenfolio";
123 			break;
124 		case hundred:
125 			type_str = "hundred";
126 			break;
127 		case hundredfolio:
128 			type_str = "hundredfolio";
129 			break;
130 		case string:
131 			type_str = "string";
132 			break;
133 		case idfolio:
134 			type_str = "idfolio";
135 			break;
136 		case folio:
137 			type_str = "folio";
138 			break;
139 		case plant:
140 			type_str = "plant";
141 			break;
142 		case locmach:
143 			type_str = "locmach";
144 			break;
145 		case elementline:
146 			type_str = "elementline";
147 			break;
148 		case elementcolumn:
149 			type_str = "elementcolumn";
150 			break;
151 		case elementprefix:
152 			type_str = "elementprefix";
153 			break;
154 	}
155 	if (type_str == "unitfolio" || type_str == "tenfolio" || type_str == "hundredfolio")
156 		nc.addValue(type_str, ui -> value_field -> displayText(), ui -> increase_spinBox -> value(), ui->value_field->displayText().toInt());
157 	else
158 	nc.addValue(type_str, ui -> value_field -> displayText(), ui -> increase_spinBox -> value());
159 	return nc;
160 }
161 
162 /**
163  * @brief NumPartEditorW::isValid
164  * @return true if value field isn't empty or if type is folio
165  */
isValid()166 bool NumPartEditorW::isValid() {
167 	if (type_ == folio || type_ == idfolio || type_ == elementline || type_ == plant || type_ == locmach ||
168 		type_ == elementcolumn || type_ == elementprefix) {return true;}
169 	else if(ui -> value_field -> text().isEmpty()) {return false;}
170 	else return true;
171 }
172 
173 /**
174  * @brief NumPartEditorW::on_type_cb_activated
175  * Action when user change the type comboBox
176  */
on_type_cb_activated(int)177 void NumPartEditorW::on_type_cb_activated(int) {
178 	if (ui->type_cb->currentText() == tr("Chiffre 1"))
179 		setType(unit);
180 	else if (ui->type_cb->currentText() == tr("Chiffre 1 - Folio"))
181 		setType(unitfolio);
182 	else if (ui->type_cb->currentText() ==  tr("Chiffre 01"))
183 		setType(ten);
184 	else if (ui->type_cb->currentText() == tr("Chiffre 01 - Folio"))
185 		setType(tenfolio);
186 	else if (ui->type_cb->currentText() == tr("Chiffre 001"))
187 		setType(hundred);
188 	else if (ui->type_cb->currentText() == tr("Chiffre 001 - Folio"))
189 		setType(hundredfolio);
190 	else if (ui->type_cb->currentText() == tr("Texte"))
191 		setType(string);
192 	else if (ui->type_cb->currentText() == tr("N° folio"))
193 		setType(idfolio);
194 	else if (ui->type_cb->currentText() == tr("Folio"))
195 		setType(folio);
196 	else if (ui->type_cb->currentText() == tr("Installation"))
197 		setType(plant);
198 	else if (ui->type_cb->currentText() == tr("Locmach"))
199 		setType(locmach);
200 	else if (ui->type_cb->currentText() == tr("Element Line"))
201 		setType(elementline);
202 	else if (ui->type_cb->currentText() == tr("Element Column"))
203 		setType(elementcolumn);
204 	else if (ui->type_cb->currentText() == tr("Element Prefix"))
205 		setType(elementprefix);
206 	emit changed();
207 }
208 
209 /**
210  * @brief NumPartEditorW::on_value_field_textChanged
211  * emit changed when @value_field text changed
212  */
on_value_field_textEdited()213 void NumPartEditorW::on_value_field_textEdited() {
214 	emit changed();
215 }
216 
217 /**
218  * @brief NumPartEditorW::on_increase_spinBox_valueChanged
219  * emit changed when @increase_spinBox value changed
220  */
on_increase_spinBox_valueChanged(int)221 void NumPartEditorW::on_increase_spinBox_valueChanged(int) {
222 	if (!ui -> value_field -> text().isEmpty()) emit changed();
223 }
224 
225 /**
226  * @brief NumPartEditorW::setType
227  * Set good behavior by type @t
228  * @param t, type used
229  * @param fnum, force the behavior of numeric type
230  */
setType(NumPartEditorW::type t,bool fnum)231 void NumPartEditorW::setType(NumPartEditorW::type t, bool fnum) {
232 	setCurrentIndex(t);
233 
234 	//if @t is a numeric type and preview type @type_ isn't a numeric type
235 	//or @fnum is true, we set numeric behavior
236 	if ( ((t==unit || t==unitfolio || t==ten || t==tenfolio || t==hundred || t==hundredfolio) &&
237 		  (type_==string || type_==folio || type_==plant || type_==locmach ||type_==idfolio ||
238 		   type_==elementcolumn || type_==elementline || type_==elementprefix))
239 		 || fnum) {
240 		ui -> value_field -> clear();
241 		ui -> value_field -> setEnabled(true);
242 		ui -> value_field -> setValidator(intValidator);
243 		ui -> increase_spinBox -> setEnabled(true);
244 		ui -> increase_spinBox -> setValue(1);
245 	}
246 	//@t isn't a numeric type
247 	else if (t == string || t == folio || t == idfolio || t == elementline || t == plant || t == locmach ||
248 			 t == elementcolumn || t == elementprefix) {
249 		ui -> value_field -> clear();
250 		ui -> increase_spinBox -> setDisabled(true);
251 		if (t==string) {
252 			ui -> value_field -> setValidator(nullptr);
253 			ui -> value_field -> setEnabled(true);
254 		}
255 		else if (t==folio) {
256 			ui -> value_field -> setDisabled(true);
257 			ui -> increase_spinBox -> setDisabled(true);
258 		}
259 		else if (t==plant) {
260 			ui -> value_field -> setDisabled(true);
261 			ui -> increase_spinBox -> setDisabled(true);
262 		}
263 		else if (t==locmach) {
264 			ui -> value_field -> setDisabled(true);
265 			ui -> increase_spinBox -> setDisabled(true);
266 		}
267 		else if (t==idfolio) {
268 			ui -> value_field -> setDisabled(true);
269 			ui -> increase_spinBox -> setDisabled(true);
270 		}
271 		else if (t==elementcolumn) {
272 			ui -> value_field -> setDisabled(true);
273 			ui -> increase_spinBox -> setDisabled(true);
274 		}
275 		else if (t==elementline) {
276 			ui -> value_field -> setDisabled(true);
277 			ui -> increase_spinBox -> setDisabled(true);
278 		}
279 		else if (t==elementprefix) {
280 			ui -> value_field -> setDisabled(true);
281 			ui -> increase_spinBox -> setDisabled(true);
282 		}
283 	}
284 	type_= t;
285 }
286 
287 /**
288  * @brief NumPartEditorW::setCurrentIndex
289  * Set Current Index of type_cb
290  * @param t, type used
291  */
setCurrentIndex(NumPartEditorW::type t)292 void NumPartEditorW::setCurrentIndex(NumPartEditorW::type t) {
293 	int i=-1;
294 	if (t == unit)
295 		i = ui->type_cb->findText(tr("Chiffre 1"));
296 	else if (t == unitfolio)
297 		i = ui->type_cb->findText(tr("Chiffre 1 - Folio"));
298 	else if (t == ten)
299 		i = ui->type_cb->findText(tr("Chiffre 01"));
300 	else if (t == tenfolio)
301 		i = ui->type_cb->findText(tr("Chiffre 01 - Folio"));
302 	else if (t == hundred)
303 		i = ui->type_cb->findText(tr("Chiffre 001"));
304 	else if (t == hundredfolio)
305 		i = ui->type_cb->findText(tr("Chiffre 001 - Folio"));
306 	else if (t == string)
307 		i = ui->type_cb->findText(tr("Texte"));
308 	else if (t == idfolio)
309 		i = ui->type_cb->findText(tr("N° folio"));
310 	else if (t == folio)
311 		i = ui->type_cb->findText(tr("Folio"));
312 	else if (t == plant)
313 		i = ui->type_cb->findText(tr("Installation"));
314 	else if (t == locmach)
315 		i = ui->type_cb->findText(tr("Locmach"));
316 	else if (t == elementline)
317 			i = ui->type_cb->findText(tr("Element Line"));
318 	else if (t == elementcolumn)
319 		i = ui->type_cb->findText(tr("Element Column"));
320 	else if (t == elementprefix)
321 		i = ui->type_cb->findText(tr("Element Prefix"));
322 	ui->type_cb->setCurrentIndex(i);
323 }
324