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 "elementscategoryeditor.h"
19 #include "qet.h"
20 #include "qfilenameedit.h"
21 #include "qetmessagebox.h"
22 #include "elementcollectionhandler.h"
23 #include "namelistwidget.h"
24 
25 #include <QDialogButtonBox>
26 #include <QVBoxLayout>
27 #include <QLabel>
28 #include <QHBoxLayout>
29 
30 /**
31  * @brief ElementsCategoryEditor::ElementsCategoryEditor
32  * Constructor
33  * @param location : location of the category to edit, or parent directory/category for the creation of a new category
34  * @param edit : true = edit mode, false = creation mode
35  * @param parent : parent widget
36  */
ElementsCategoryEditor(const ElementsLocation & location,bool edit,QWidget * parent)37 ElementsCategoryEditor::ElementsCategoryEditor(const ElementsLocation &location, bool edit, QWidget *parent) :
38 	QDialog(parent),
39 	m_edit_mode(edit),
40 	m_location(location)
41 {
42 	setUpWidget();
43 
44 	if (m_location.isElement()) {
45 		QET::QetMessageBox::warning(this,
46 									tr("L'item n'est pas une catégorie", "message box title"),
47 									tr("L'item demandé n'est pas une categrie. Abandon.", "message box content"));
48 		return;
49 	}
50 
51 	if (!location.exist()) {
52 		QET::QetMessageBox::warning(this,
53 									tr("Catégorie inexistante", "message box title"),
54 									tr("La catégorie demandée n'existe pas. Abandon.", "message box content"));
55 		return;
56 	}
57 
58 	if (m_edit_mode) {
59 		setWindowTitle(tr("Éditer une catégorie", "window title"));
60 		connect(m_buttons, SIGNAL(accepted()), this, SLOT(acceptUpdate()));
61 
62 		m_names_list -> setNames(m_location.nameList());
63 		m_file_line_edit -> setText(m_location.fileSystemPath());
64 		m_file_line_edit -> setReadOnly(true);
65 	} else {
66 		setWindowTitle(tr("Créer une nouvelle catégorie", "window title"));
67 		connect(m_buttons, SIGNAL(accepted()), this, SLOT(acceptCreation()));
68 
69 		NamesList cat_names;
70 		cat_names.addName(QLocale::system().name().left(2), tr("Nom de la nouvelle catégorie", "default name when creating a new category"));
71 		m_names_list -> setNames(cat_names);
72 	}
73 
74 		//Location is ReadOnly
75 	if (!m_location.isWritable()) {
76 		QET::QetMessageBox::warning(
77 			this,
78 			tr("Édition en lecture seule", "message box title"),
79 			tr("Vous n'avez pas les privilèges nécessaires pour modifier cette catégorie. Elle sera donc ouverte en lecture seule.", "message box content")
80 		);
81 		m_names_list -> setReadOnly(true);
82 		m_file_line_edit -> setReadOnly(true);
83 	}
84 }
85 
86 /**
87  * @brief ElementsCategoryEditor::~ElementsCategoryEditor
88  * Destructor
89  */
~ElementsCategoryEditor()90 ElementsCategoryEditor::~ElementsCategoryEditor() {
91 }
92 
93 /**
94  * @brief ElementsCategoryEditor::createdLocation
95  * @return the location of the created directory
96  */
createdLocation() const97 ElementsLocation ElementsCategoryEditor::createdLocation() const
98 {
99 	return m_created_location;
100 }
101 
102 /**
103  * @brief ElementsCategoryEditor::setUpWidget
104  */
setUpWidget()105 void ElementsCategoryEditor::setUpWidget()
106 {
107 	QVBoxLayout *editor_layout = new QVBoxLayout();
108 	setLayout(editor_layout);
109 
110 	m_names_list = new NameListWidget(this);
111 	m_file_name = new QLabel(tr("Nom interne : "));
112 	m_file_line_edit = new QFileNameEdit();
113 
114 	m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
115 	connect(m_buttons, SIGNAL(rejected()), this, SLOT(reject()));
116 
117 	QHBoxLayout *internal_name_layout = new QHBoxLayout();
118 	internal_name_layout -> addWidget(m_file_name);
119 	internal_name_layout -> addWidget(m_file_line_edit);
120 
121 	editor_layout -> addLayout(internal_name_layout);
122 	editor_layout -> addWidget(new QLabel(tr("Vous pouvez spécifier un nom par langue pour la catégorie.")));
123 	editor_layout -> addWidget(m_names_list);
124 	editor_layout -> addWidget(m_buttons);
125 }
126 
127 /**
128  * @brief ElementsCategoryEditor::acceptCreation
129  * Valid the creation of the category
130  */
acceptCreation()131 void ElementsCategoryEditor::acceptCreation()
132 {
133 	if (!m_location.isWritable()) {
134 		QDialog::accept();
135 	}
136 
137 		//there must be at least one name
138 	if (m_names_list->isEmpty()) {
139 		return;
140 	}
141 
142 		//User must enter a directorie name
143 	if (!m_file_line_edit -> isValid()) {
144 		QET::QetMessageBox::critical(this,
145 									 tr("Nom interne manquant", "message box title"),
146 									 tr("Vous devez spécifier un nom interne.", "message box content"));
147 		return;
148 	}
149 	QString dirname = m_file_line_edit -> text();
150 
151 
152 		//Check if dirname already exist.
153 	ElementsLocation created_location = m_location;
154 	created_location.addToPath(dirname);
155 
156 	if (created_location.exist()) {
157 		QET::QetMessageBox::critical(this,
158 									 tr("Nom interne déjà utilisé", "message box title"),
159 									 tr("Le nom interne que vous avez choisi est déjà utilisé "
160 										"par une catégorie existante. Veuillez en choisir un autre.",
161 										"message box content"));
162 		return;
163 	}
164 
165 	ElementCollectionHandler ech_;
166 	NamesList nl = m_names_list->names();
167 	m_created_location = ech_.createDir(m_location, dirname, nl);
168 	if (m_created_location.isNull()) {
169 		QET::QetMessageBox::critical(this,
170 									 tr("Erreur", "message box title"),
171 									 tr("Impossible de créer la catégorie", "message box content"));
172 		return;
173 	}
174 
175 	QDialog::accept();
176 }
177 
178 /**
179  * @brief ElementsCategoryEditor::acceptUpdate
180  * Valid the update of the category
181  */
acceptUpdate()182 void ElementsCategoryEditor::acceptUpdate()
183 {
184 	if (!m_location.isWritable()) {
185 		QDialog::accept();
186 	}
187 
188 		//There must be at least one name
189 	if (m_names_list->isEmpty()) {
190 		return;
191 	}
192 
193 	ElementCollectionHandler ech;
194 
195 	if (ech.setNames(m_location, m_names_list->names())){
196 		QDialog::accept();
197 	}
198 	else {
199 		return;
200 	}
201 }
202