QElectroTech  0.70
elementscategoryeditor.cpp
Go to the documentation of this file.
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"
23 #include "namelistwidget.h"
24 
25 #include <QDialogButtonBox>
26 #include <QVBoxLayout>
27 #include <QLabel>
28 #include <QHBoxLayout>
29 
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()) {
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()) {
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());
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()) {
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 
91 }
92 
98 {
99  return m_created_location;
100 }
101 
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 : "));
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 
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()) {
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()) {
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 
167  m_created_location = ech_.createDir(m_location, dirname, nl);
168  if (m_created_location.isNull()) {
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 
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 
194 
195  if (ech.setNames(m_location, m_names_list->names())){
196  QDialog::accept();
197  }
198  else {
199  return;
200  }
201 }
NamesList nameList()
ElementsLocation::nameList.
ElementsLocation m_created_location
NamesList names() const
NameListWidget::names.
bool isEmpty() const
NameListWidget::isEmpty.
ElementsLocation createDir(ElementsLocation &parent, const QString &name, const NamesList &name_list)
void addName(const QString &, const QString &)
Definition: nameslist.cpp:50
bool addToPath(const QString &)
ElementsLocation::addToPath Add a string to the actual path of this location.
void acceptUpdate()
ElementsCategoryEditor::acceptUpdate Valid the update of the category.
QIcon nl
Definition: qeticons.cpp:202
QDialogButtonBox * m_buttons
ElementsCategoryEditor(const ElementsLocation &location, bool edit=true, QWidget *parent=nullptr)
ElementsCategoryEditor::ElementsCategoryEditor Constructor.
QIcon tr
Definition: qeticons.cpp:204
~ElementsCategoryEditor() override
ElementsCategoryEditor::~ElementsCategoryEditor Destructor.
void acceptCreation()
ElementsCategoryEditor::acceptCreation Valid the creation of the category.
bool exist() const
ElementsLocation::exist.
void setUpWidget()
ElementsCategoryEditor::setUpWidget.
QString fileSystemPath() const
ElementsLocation::fileSystemPath.
bool setNames(ElementsLocation &location, const NamesList &name_list)
ElementCollectionHandler::setNames Set the names stored in as the names of the item represented by l...
QIcon Cancel
Definition: qeticons.cpp:34
ElementsLocation createdLocation() const
ElementsCategoryEditor::createdLocation.
bool isElement() const
ElementsLocation::isElement.
QMessageBox::StandardButton critical(QWidget *, const QString &, const QString &, QMessageBox::StandardButtons=QMessageBox::Ok, QMessageBox::StandardButton=QMessageBox::NoButton)
bool isWritable() const
ElementsLocation::isWritable.
QMessageBox::StandardButton warning(QWidget *, const QString &, const QString &, QMessageBox::StandardButtons=QMessageBox::Ok, QMessageBox::StandardButton=QMessageBox::NoButton)
The NameListWidget class Provide a widget for let user define localized string;.
The ElementCollectionHandler class Provide several method to copy element or directory from a collect...