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 "newelementwizard.h"
19 #include "namelistwidget.h"
20 #include "qetelementeditor.h"
21 #include "qfilenameedit.h"
22 #include "qetmessagebox.h"
23 #include "elementscollectionmodel.h"
24 #include "elementcollectionitem.h"
25
26 /**
27 Constructeur
28 @param parent QWidget parent de ce dialogue
29 @param f flags pour le dialogue
30 */
NewElementWizard(QWidget * parent,Qt::WindowFlags f)31 NewElementWizard::NewElementWizard(QWidget *parent, Qt::WindowFlags f) :
32 QWizard(parent, f)
33 {
34 setOptions(options() & ~QWizard::NoCancelButton);
35
36 #ifdef Q_OS_WIN
37 setWizardStyle(QWizard::AeroStyle);
38 #elif defined(Q_OS_MAC)
39 setWizardStyle(QWizard::MacStyle);
40 #endif
41
42 setPixmap(LogoPixmap, QPixmap(":/ico/256x256/qelectrotech.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
43 setWindowTitle(tr("Créer un nouvel élément : Assistant", "window title"));
44 setButtonText(QWizard::NextButton, tr("&Suivant >"));
45 addPage(buildStep1());
46 addPage(buildStep2());
47 addPage(buildStep3());
48 setFixedSize(705, 325);
49 }
50
51 /**
52 Destructeur
53 */
~NewElementWizard()54 NewElementWizard::~NewElementWizard() {
55 }
56
57 /**
58 * @brief NewElementWizard::preselectedLocation
59 * Select item in the tree view represented by location,
60 * @param location
61 */
preselectedLocation(const ElementsLocation & location)62 void NewElementWizard::preselectedLocation(const ElementsLocation &location)
63 {
64 QModelIndex index = m_model->indexFromLocation(location);
65 if (index.isValid()) {
66 m_tree_view->scrollTo(index);
67 m_tree_view->setCurrentIndex(index);
68 }
69 }
70
71 /**
72 * @brief NewElementWizard::buildStep1
73 * @return
74 */
buildStep1()75 QWizardPage *NewElementWizard::buildStep1()
76 {
77 QWizardPage *page = new QWizardPage();
78 page -> setProperty("WizardState", Category);
79 page -> setTitle(tr("Étape 1/3 : Catégorie parente", "wizard page title"));
80 page -> setSubTitle(tr("Sélectionnez une catégorie dans laquelle enregistrer le nouvel élément.", "wizard page subtitle"));
81 QVBoxLayout *layout = new QVBoxLayout();
82
83 m_tree_view = new QTreeView(this);
84
85 m_model = new ElementsCollectionModel(m_tree_view);
86 m_model->hideElement();
87 m_model->addCustomCollection();
88
89 m_tree_view->setModel(m_model);
90 m_tree_view->setHeaderHidden(true);
91 m_tree_view->setAnimated(true);
92 layout->addWidget(m_tree_view);
93
94 page->setLayout(layout);
95 return(page);
96 }
97
98 /**
99 * @brief NewElementWizard::buildStep2
100 * @return
101 */
buildStep2()102 QWizardPage *NewElementWizard::buildStep2() {
103 QWizardPage *page = new QWizardPage();
104 page -> setProperty("WizardState", Filename);
105 page -> setTitle(tr("Étape 2/3 : Nom du fichier", "wizard page title"));
106 page -> setSubTitle(tr("Indiquez le nom du fichier dans lequel enregistrer le nouvel élément.", "wizard page subtitle"));
107 QVBoxLayout *layout = new QVBoxLayout();
108
109 m_qle_filename = new QFileNameEdit(tr("nouvel_element"));
110 m_qle_filename -> selectAll();
111 QLabel *explication2 = new QLabel(tr("Vous n'êtes pas obligé de préciser l'extension *.elmt. Elle sera ajoutée automatiquement."));
112 explication2 -> setAlignment(Qt::AlignJustify | Qt::AlignVCenter);
113 explication2 -> setWordWrap(true);
114 layout -> addWidget(m_qle_filename);
115 layout -> addWidget(explication2);
116 layout -> addSpacing(100);
117
118 page -> setLayout(layout);
119 return(page);
120 }
121
122 /**
123 * @brief NewElementWizard::buildStep3
124 * @return
125 */
buildStep3()126 QWizardPage *NewElementWizard::buildStep3() {
127 QWizardPage *page = new QWizardPage();
128 page -> setProperty("WizardState", Names);
129 page -> setTitle(tr("Étape 3/3 : Noms de l'élément", "wizard page title"));
130 page -> setSubTitle(tr("Indiquez le ou les noms de l'élément.", "wizard page subtitle"));
131 QVBoxLayout *layout = new QVBoxLayout();
132
133 m_names_list = new NameListWidget(this);
134 NamesList hash_name;
135 hash_name.addName(QLocale::system().name().left(2), tr("Nom du nouvel élément", "default name when creating a new element"));
136 m_names_list -> setNames(hash_name);
137 layout -> addWidget(m_names_list);
138
139 page -> setLayout(layout);
140 return(page);
141 }
142
143 /**
144 * @brief NewElementWizard::validateCurrentPage
145 * @return true if the current step is valid
146 */
validateCurrentPage()147 bool NewElementWizard::validateCurrentPage()
148 {
149 WizardState wizard_state = static_cast<WizardState>(currentPage() -> property("WizardState").toInt());
150
151 if (wizard_state == Category) {
152 return(validStep1());
153 }
154 else if (wizard_state == Filename) {
155 return(validStep2());
156 }
157 else if (wizard_state == Names)
158 {
159 // must have one name minimum
160 if (!m_names_list->isEmpty()) {
161 createNewElement();
162 }
163 return true;
164 }
165 else return(true);
166 }
167
168 /**
169 * @brief NewElementWizard::validStep1
170 * Valid the setp 1
171 * @return trie if the step is valid.
172 */
validStep1()173 bool NewElementWizard::validStep1()
174 {
175 //They must be one directory selected
176 bool step1_ok = false;
177
178 QModelIndex index = m_tree_view->currentIndex();
179 if (index.isValid()) {
180
181 ElementCollectionItem *eci = static_cast<ElementCollectionItem*>(m_model->itemFromIndex(index));
182 if (eci && eci->isDir()) {
183 ElementsLocation loc(eci->collectionPath());
184 if (loc.exist()) {
185 m_chosen_location = loc;
186 step1_ok = true;
187 }
188 }
189 }
190
191 if (!step1_ok) {
192 QET::QetMessageBox::critical(parentWidget(),
193 tr("Erreur", "message box title"),
194 tr("Vous devez sélectionner une catégorie.", "message box content"));
195 }
196
197 return(step1_ok);
198 }
199
200 /**
201 * @brief NewElementWizard::validStep2
202 * Valid the step 2
203 * @return true if step is valid
204 */
validStep2()205 bool NewElementWizard::validStep2() {
206 QString file_name = m_qle_filename -> text();
207
208 if (file_name.isEmpty()) {
209 QET::QetMessageBox::critical(this,
210 tr("Erreur", "message box title"),
211 tr("Vous devez entrer un nom de fichier", "message box content"));
212 return false;
213 }
214
215 if (!file_name.endsWith(".elmt")) {
216 file_name += ".elmt";
217 }
218
219 ElementsLocation loc_ = m_chosen_location;
220 loc_.addToPath(file_name);
221 if (loc_.exist()) {
222 QET::QetMessageBox::critical(this,
223 tr("Erreur", "message box title"),
224 tr("Un élément portant le même nom existe déjà"));
225 return false;
226 }
227
228 m_chosen_file = file_name;
229 return(true);
230 }
231
232 /**
233 * @brief NewElementWizard::createNewElement
234 * Lauch an element editor for create the new element
235 */
createNewElement()236 void NewElementWizard::createNewElement() {
237 QETElementEditor *edit_new_element = new QETElementEditor(parentWidget());
238 edit_new_element -> setNames(m_names_list -> names());
239
240 ElementsLocation loc_ = m_chosen_location;
241 loc_.addToPath(m_chosen_file);
242 edit_new_element -> setLocation(loc_);
243 edit_new_element -> show();
244 }
245