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 "titleblockpropertieswidget.h"
19 #include "ui_titleblockpropertieswidget.h"
20 #include "templatescollection.h"
21 #include "qeticons.h"
22 #include "titleblocktemplate.h"
23 #include "qetapp.h"
24 #include <QMenu>
25 #include <utility>
26 
27 /**
28  * @brief TitleBlockPropertiesWidget::TitleBlockPropertiesWidget
29  * default constructor
30  * @param titleblock properties to edit
31  * @param current_date if true, display the radio button "current date"
32  * @param parent parent widget
33  */
TitleBlockPropertiesWidget(const TitleBlockProperties & titleblock,bool current_date,QETProject * project,QWidget * parent)34 TitleBlockPropertiesWidget::TitleBlockPropertiesWidget(const TitleBlockProperties &titleblock, bool current_date, QETProject *project, QWidget *parent) :
35 	QWidget(parent),
36 	ui(new Ui::TitleBlockPropertiesWidget)
37 {
38 	ui->setupUi(this);
39 	initDialog(current_date, project);
40 	setProperties(titleblock);
41 }
42 
43 /**
44  * @brief TitleBlockPropertiesWidget::TitleBlockPropertiesWidget
45  * default constructor with tempalte list
46  * @param tbt_collection template list
47  * @param titleblock properties to edit
48  * @param current_date if true, display the radio button "current date"
49  * @param parent parent widget
50  */
TitleBlockPropertiesWidget(TitleBlockTemplatesCollection * tbt_collection,const TitleBlockProperties & titleblock,bool current_date,QETProject * project,QWidget * parent)51 TitleBlockPropertiesWidget::TitleBlockPropertiesWidget(TitleBlockTemplatesCollection *tbt_collection, const TitleBlockProperties &titleblock, bool current_date, QETProject *project, QWidget *parent) :
52 	QWidget(parent),
53 	ui(new Ui::TitleBlockPropertiesWidget)
54 {
55 	ui->setupUi(this);
56 	initDialog(current_date,project);
57 	addCollection(tbt_collection);
58 	updateTemplateList();
59 	setProperties(titleblock);
60 }
61 
62 /**
63  * @brief TitleBlockPropertiesWidget::TitleBlockPropertiesWidget
64  * Default constructor with several template collection
65  * @param tbt_collection template list
66  * @param titleblock properties to edit
67  * @param current_date if true, display the radio button "current date"
68  * @param parent parent widget
69  */
TitleBlockPropertiesWidget(QList<TitleBlockTemplatesCollection * > tbt_collection,const TitleBlockProperties & titleblock,bool current_date,QETProject * project,QWidget * parent)70 TitleBlockPropertiesWidget::TitleBlockPropertiesWidget(QList<TitleBlockTemplatesCollection *> tbt_collection, const TitleBlockProperties &titleblock, bool current_date, QETProject *project, QWidget *parent) :
71 	QWidget(parent),
72 	ui(new Ui::TitleBlockPropertiesWidget)
73 {
74 	ui->setupUi(this);
75 	initDialog(current_date,project);
76 	foreach (TitleBlockTemplatesCollection *c, tbt_collection)
77 		addCollection(c);
78 	updateTemplateList();
79 	setProperties(titleblock);
80 }
81 
82 /**
83  * @brief TitleBlockPropertiesWidget::~TitleBlockPropertiesWidget
84  * destructor
85  */
~TitleBlockPropertiesWidget()86 TitleBlockPropertiesWidget::~TitleBlockPropertiesWidget()
87 {
88 	delete ui;
89 }
90 
91 /**
92  * @brief TitleBlockPropertiesWidget::setProperties
93  * @param properties
94  */
setProperties(const TitleBlockProperties & properties)95 void TitleBlockPropertiesWidget::setProperties(const TitleBlockProperties &properties) {
96 	ui -> m_title_le  -> setText (properties.title);
97 	ui -> m_author_le -> setText (properties.author);
98 	ui -> m_file_le   -> setText (properties.filename);
99 	ui -> m_plant      -> setText (properties.plant);
100 	ui -> m_loc   -> setText (properties.locmach);
101 	ui -> m_indice    -> setText (properties.indexrev);
102 	ui -> m_folio_le  -> setText (properties.folio);
103 	ui -> m_display_at_cb -> setCurrentIndex(properties.display_at == Qt::BottomEdge ? 0 : 1);
104 	ui->auto_page_cb->setCurrentText(properties.auto_page_num);
105 
106 	//About date
107 	ui -> m_date_now_pb -> setDisabled(true);
108 	ui -> m_date_edit   -> setDisabled(true);
109 	ui -> m_date_edit   -> setDate(QDate::currentDate());
110 
111 	if (!ui -> m_current_date_rb -> isHidden()) {
112 		if(properties.useDate == TitleBlockProperties::CurrentDate)
113 			ui -> m_current_date_rb -> setChecked(true);
114 		else {
115 			if (properties.date.isNull())
116 				ui -> m_no_date_rb -> setChecked(true);
117 			else {
118 				ui -> m_fixed_date_rb -> setChecked(true);
119 				ui -> m_date_edit -> setDate(properties.date);
120 			}
121 		}
122 	}
123 	else {
124 		if (properties.useDate == TitleBlockProperties::CurrentDate)
125 			ui -> m_fixed_date_rb ->setChecked(true);
126 		else {
127 			if (properties.date.isNull())
128 				ui -> m_no_date_rb -> setChecked(true);
129 			else {
130 				ui -> m_fixed_date_rb -> setChecked(true);
131 				ui -> m_date_edit -> setDate(properties.date);
132 			}
133 		}
134 	} //About date
135 
136 		//Set the current titleblock if any
137 	int index = 0;
138 	if (!properties.template_name.isEmpty())
139 	{
140 		index = getIndexFor(properties.template_name, properties.collection);
141 		if (index == -1) index = 0;
142 	}
143 	ui -> m_tbt_cb -> setCurrentIndex(index);
144 
145 	m_dcw -> setContext(properties.context);
146 }
147 
148 /**
149  * @brief TitleBlockPropertiesWidget::properties
150  * @return the edited properties
151  */
properties() const152 TitleBlockProperties TitleBlockPropertiesWidget::properties() const {
153 	TitleBlockProperties prop;
154 	prop.title    = ui -> m_title_le  -> text();
155 	prop.author   = ui -> m_author_le -> text();
156 	prop.filename = ui -> m_file_le   -> text();
157 	prop.plant  = ui -> m_plant      -> text();
158 	prop.locmach  = ui -> m_loc   -> text();
159 	prop.indexrev = ui -> m_indice    -> text();
160 	prop.folio    = ui -> m_folio_le  -> text();
161 	prop.display_at = ui -> m_display_at_cb -> currentIndex() == 0 ? Qt::BottomEdge : Qt::RightEdge;
162 
163 	if (ui->m_no_date_rb->isChecked()) {
164 		prop.useDate = TitleBlockProperties::UseDateValue;
165 		prop.date = QDate();
166 	}
167 	else if (ui -> m_fixed_date_rb -> isChecked()) {
168 		prop.useDate = TitleBlockProperties::UseDateValue;
169 		prop.date = ui->m_date_edit->date();
170 	}
171 	else if (ui->m_current_date_rb->isVisible() && ui->m_current_date_rb->isChecked()) {
172 		prop.useDate = TitleBlockProperties::CurrentDate;
173 		prop.date = QDate::currentDate();
174 	}
175 
176 	if (!currentTitleBlockTemplateName().isEmpty())
177 	{
178 		prop.template_name = currentTitleBlockTemplateName();
179 		prop.collection = m_map_index_to_collection_type.at(ui->m_tbt_cb->currentIndex());
180 	}
181 
182 	prop.context = m_dcw -> context();
183 
184 	prop.auto_page_num = ui->auto_page_cb->currentText();
185 
186 	return prop;
187 }
188 
189 /**
190  * @brief TitleBlockPropertiesWidget::properties
191  * @return return properties to enable folio autonum
192  */
propertiesAutoNum(QString autoNum) const193 TitleBlockProperties TitleBlockPropertiesWidget::propertiesAutoNum(QString autoNum) const {
194 	TitleBlockProperties prop;
195 	prop.title    = ui -> m_title_le  -> text();
196 	prop.author   = ui -> m_author_le -> text();
197 	prop.filename = ui -> m_file_le   -> text();
198 	prop.plant    = ui -> m_plant      -> text();
199 	prop.locmach  = ui -> m_loc       -> text();
200 	prop.indexrev = ui -> m_indice    -> text();
201 	prop.folio    = "%autonum";
202 	prop.display_at = ui -> m_display_at_cb -> currentIndex() == 0 ? Qt::BottomEdge : Qt::RightEdge;
203 
204 	if (ui->m_no_date_rb->isChecked()) {
205 		prop.useDate = TitleBlockProperties::UseDateValue;
206 		prop.date = QDate();
207 	}
208 	else if (ui -> m_fixed_date_rb -> isChecked()) {
209 		prop.useDate = TitleBlockProperties::UseDateValue;
210 		prop.date = ui->m_date_edit->date();
211 	}
212 	else if (ui->m_current_date_rb->isVisible() && ui->m_current_date_rb->isChecked()) {
213 		prop.useDate = TitleBlockProperties::CurrentDate;
214 		prop.date = QDate::currentDate();
215 	}
216 
217 	if (!currentTitleBlockTemplateName().isEmpty())
218 	{
219 		prop.template_name = currentTitleBlockTemplateName();
220 		prop.collection = m_map_index_to_collection_type.at(ui->m_tbt_cb->currentIndex());
221 	}
222 
223 	prop.context = m_dcw -> context();
224 
225 	prop.auto_page_num = std::move(autoNum);
226 
227 	return prop;
228 }
229 
currentTitleBlockLocation() const230 TitleBlockTemplateLocation TitleBlockPropertiesWidget::currentTitleBlockLocation() const
231 {
232 	QET::QetCollection qc = m_map_index_to_collection_type.at(ui->m_tbt_cb->currentIndex());
233 	TitleBlockTemplatesCollection *collection = nullptr;
234 	foreach (TitleBlockTemplatesCollection *c, m_tbt_collection_list)
235 		if (c -> collection() == qc)
236 			collection = c;
237 
238 	if (!collection)
239 		return TitleBlockTemplateLocation();
240 
241 	return collection->location(currentTitleBlockTemplateName());
242 }
243 
244 /**
245  * @brief TitleBlockPropertiesWidget::setTitleBlockTemplatesVisible
246  * if true, title block template combo box and menu button is visible
247  */
setTitleBlockTemplatesVisible(const bool & visible)248 void TitleBlockPropertiesWidget::setTitleBlockTemplatesVisible(const bool &visible) {
249 	ui -> m_tbt_label -> setVisible(visible);
250 	ui -> m_tbt_cb    -> setVisible(visible);
251 	ui -> m_tbt_pb    -> setVisible(visible);
252 }
253 
254 /**
255  * @brief TitleBlockPropertiesWidget::setReadOnly
256  * if true, this widget is disable
257  */
setReadOnly(const bool & ro)258 void TitleBlockPropertiesWidget::setReadOnly(const bool &ro) {
259 	ui->m_tbt_gb->setDisabled(ro);
260 }
261 
262 /**
263  * @brief TitleBlockPropertiesWidget::currentTitleBlockTemplateName
264  * @return the current title block name
265  */
currentTitleBlockTemplateName() const266 QString TitleBlockPropertiesWidget::currentTitleBlockTemplateName() const {
267 	int index = ui -> m_tbt_cb -> currentIndex();
268 	if(index != -1)
269 		return (ui -> m_tbt_cb -> itemData(index).toString());
270 	return QString();
271 }
272 
273 /**
274  * @brief TitleBlockPropertiesWidget::addCollection
275  * add a collection of title block available in the combo box
276  * @param tbt_collection
277  */
addCollection(TitleBlockTemplatesCollection * tbt_collection)278 void TitleBlockPropertiesWidget::addCollection(TitleBlockTemplatesCollection *tbt_collection)
279 {
280 	if (!tbt_collection || m_tbt_collection_list.contains(tbt_collection)) return;
281 	m_tbt_collection_list << tbt_collection;
282 }
283 
284 /**
285  * @brief TitleBlockPropertiesWidget::initDialog
286  * Init this dialog
287  * @param current_date true for display current date radio button
288  */
initDialog(const bool & current_date,QETProject * project)289 void TitleBlockPropertiesWidget::initDialog(const bool &current_date,  QETProject *project) {
290 	m_dcw = new DiagramContextWidget();
291 	ui -> m_tab2_vlayout -> addWidget(m_dcw);
292 
293 	setTitleBlockTemplatesVisible(false);
294 	ui -> m_current_date_rb -> setVisible(current_date);
295 
296 	m_tbt_edit = new QAction(tr("Éditer ce modèle", "menu entry"), this);
297 	m_tbt_duplicate = new QAction(tr("Dupliquer et éditer ce modèle", "menu entry"), this);
298 
299 	connect(m_tbt_edit, SIGNAL(triggered()), this, SLOT(editCurrentTitleBlockTemplate()));
300 	connect(m_tbt_duplicate, SIGNAL(triggered()), this, SLOT(duplicateCurrentTitleBlockTemplate()));
301 
302 	m_tbt_menu = new QMenu(tr("Title block templates actions"));
303 	m_tbt_menu -> addAction(m_tbt_edit);
304 	m_tbt_menu -> addAction(m_tbt_duplicate);
305 	ui -> m_tbt_pb -> setMenu(m_tbt_menu);
306 
307 	connect(ui->m_tbt_cb, SIGNAL(currentIndexChanged(int)), this, SLOT(changeCurrentTitleBlockTemplate(int)));
308 
309 	if (project!= nullptr){
310 		keys_2 = project -> folioAutoNum().keys();
311 		foreach (QString str, keys_2) { ui -> auto_page_cb -> addItem(str); }
312 		if (ui->auto_page_cb->currentText()==nullptr)
313 			ui->auto_page_cb->addItem(tr("Créer un Folio Numérotation Auto"));
314 	}
315 	else{
316 		ui->auto_page_cb->hide();
317 		ui->m_edit_autofolionum_pb->hide();
318 		ui->label_9->hide();
319 	}
320 
321 }
322 
323 /**
324  * @brief TitleBlockPropertiesWidget::getIndexFor
325  * Find the index of the combo box for the title block @tbt_name available on the collection @collection
326  * @param tbt_name : title block name
327  * @param collection : title block collection
328  * @return the index of the title block or -1 if no match
329  */
getIndexFor(const QString & tbt_name,const QET::QetCollection collection) const330 int TitleBlockPropertiesWidget::getIndexFor(const QString &tbt_name, const QET::QetCollection collection) const
331 {
332 	for (int i = 0; i<ui->m_tbt_cb->count(); i++) {
333 		if (ui->m_tbt_cb->itemData(i).toString() == tbt_name)
334 			if (m_map_index_to_collection_type.at(i) == collection)
335 				return i;
336 	}
337 	return -1;
338 }
339 
editCurrentTitleBlockTemplate()340 void TitleBlockPropertiesWidget::editCurrentTitleBlockTemplate() {
341 	QETApp::instance()->openTitleBlockTemplate(currentTitleBlockLocation(), false);
342 }
343 
duplicateCurrentTitleBlockTemplate()344 void TitleBlockPropertiesWidget::duplicateCurrentTitleBlockTemplate() {
345 	QETApp::instance()->openTitleBlockTemplate(currentTitleBlockLocation(), true);
346 }
347 
348 /**
349  * @brief TitleBlockPropertiesWidget::updateTemplateList
350  * Update the title block template list available in the combo box
351  */
updateTemplateList()352 void TitleBlockPropertiesWidget::updateTemplateList()
353 {
354 	ui -> m_tbt_cb ->clear();
355 
356 	if (m_tbt_collection_list.isEmpty())
357 	{
358 		setTitleBlockTemplatesVisible(false);
359 		return;
360 	}
361 	setTitleBlockTemplatesVisible(true);
362 
363 		//Add the default title block
364 	m_map_index_to_collection_type.clear();
365 	m_map_index_to_collection_type.append(QET::QetCollection::Common);
366 	ui -> m_tbt_cb -> addItem(QET::Icons::QETLogo, tr("Modèle par défaut"));
367 
368 		//Add every title block stored in m_tbt_collection_list
369 	foreach (TitleBlockTemplatesCollection *tbt_c, m_tbt_collection_list)
370 	{
371 		QIcon icon;
372 		QET::QetCollection qc = tbt_c -> collection();
373 		if (qc == QET::QetCollection::Common)
374 			icon = QET::Icons::QETLogo;
375 		else if (qc == QET::QetCollection::Custom)
376 			icon = QET::Icons::Home;
377 		else if (qc == QET::QetCollection::Embedded)
378 			icon = QET::Icons::TitleBlock;
379 
380 		foreach(QString tbt_name, tbt_c -> templates())
381 		{
382 			m_map_index_to_collection_type.append(qc);
383 			ui -> m_tbt_cb -> addItem(icon, tbt_name, tbt_name);
384 		}
385 	}
386 }
387 
388 /**
389  * @brief TitleBlockPropertiesWidget::changeCurrentTitleBlockTemplate
390  * Load the additionnal field of title block "text"
391  */
changeCurrentTitleBlockTemplate(int index)392 void TitleBlockPropertiesWidget::changeCurrentTitleBlockTemplate(int index)
393 {
394 	m_dcw -> clear();
395 
396 	QET::QetCollection qc = m_map_index_to_collection_type.at(index);
397 	TitleBlockTemplatesCollection *collection = nullptr;
398 	foreach (TitleBlockTemplatesCollection *c, m_tbt_collection_list)
399 		if (c -> collection() == qc)
400 			collection = c;
401 
402 	if (!collection) return;
403 
404 		// get template
405 	TitleBlockTemplate *tpl = collection -> getTemplate(ui -> m_tbt_cb -> currentText());
406 	if(tpl != nullptr) {
407 			// get all template fields
408 		QStringList fields = tpl -> listOfVariables();
409 			// set fields to additional_fields_ widget
410 		DiagramContext templateContext;
411 		for(int i =0; i<fields.count(); i++)
412 			templateContext.addValue(fields.at(i), "");
413 		m_dcw -> setContext(templateContext);
414 	}
415 }
416 
417 /**
418  * @brief TitleBlockPropertiesWidget::on_m_date_now_pb_clicked
419  * Set the date to current date
420  */
on_m_date_now_pb_clicked()421 void TitleBlockPropertiesWidget::on_m_date_now_pb_clicked() {
422 	ui -> m_date_edit -> setDate(QDate::currentDate());
423 }
424 
425 /**
426  * @brief TitleBlockPropertiesWidget::on_m_edit_autofolionum_pb_clicked
427  * Open Auto Folio Num dialog
428  */
on_m_edit_autofolionum_pb_clicked()429 void TitleBlockPropertiesWidget::on_m_edit_autofolionum_pb_clicked() {
430     emit openAutoNumFolioEditor(ui->auto_page_cb->currentText());
431 	if (ui->auto_page_cb->currentText()!=tr("Créer un Folio Numérotation Auto"))
432 	{
433 		//still to implement: load current auto folio num settings
434 	}
435 }
436