ElementsCategoryEditor : remove the use of ElementsCategory

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4475 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2016-05-12 15:41:55 +00:00
parent 432ab44c28
commit 5f93966984
8 changed files with 292 additions and 145 deletions

View File

@@ -298,3 +298,117 @@ ElementsLocation ElementCollectionHandler::copy(ElementsLocation &source, Elemen
else else
return ElementsLocation(); return ElementsLocation();
} }
/**
* @brief ElementCollectionHandler::createDir
* Create a directorie with name @name as child of @parent.
* Parent must be a directory
* @param parent : parent of the dir to create
* @param name : name of directorie to create
* @param name_list : translations of the directorie name
* @return : ElementsLocation that represent the new directorie, location can be null if an error was occured
*/
ElementsLocation ElementCollectionHandler::createDir(ElementsLocation &parent, const QString &name, const NamesList &name_list)
{
//Parent must be a directorie and writable
if (!(parent.isDirectory() && parent.isWritable() && parent.exist())) {
qDebug() << "ElementCollectionHandler::createDir : the prerequisites are not valid. " << parent;
return ElementsLocation();
}
//Directorie to create must not already exist
ElementsLocation created_dir = parent;
created_dir.addToPath(name);
if (created_dir.exist()) {
return ElementsLocation();
}
//Location is a file system
if (parent.isFileSystem()) {
QDir parent_dir(parent.fileSystemPath());
if (parent_dir.mkdir(name)) {
//Create the qet-directory file
QDomDocument document;
QDomElement root = document.createElement("qet-directory");
document.appendChild(root);
root.appendChild(name_list.toXml(document));
QString filepath = created_dir.fileSystemPath() + "/qet_directory";
if (!QET::writeXmlFile(document, filepath)) {
qDebug() << "ElementCollectionHandler::createDir : write qet-directory file failed";
}
return created_dir;
}
else {
qDebug() << "ElementCollectionHandler::createDir : error was occured at creation of new directories in file system. ";
return ElementsLocation();
}
}
else if (parent.isProject()) {
XmlElementCollection *xmlec = parent.projectCollection();
if (xmlec->createDir(parent.collectionPath(false), name, name_list)) {
return created_dir;
}
else {
qDebug() << "ElementCollectionHandler::createDir : error was occured at creation of new directories in embbeded collection.";
return ElementsLocation();
}
}
return ElementsLocation();
}
/**
* @brief ElementCollectionHandler::setNames
* Set the names stored in @name_list as the names of the item represented by location
* @param location : location to change the names
* @param name_list : NamesList to use
* @return return true if success
*/
bool ElementCollectionHandler::setNames(ElementsLocation &location, const NamesList &name_list)
{
if ( !(location.exist() && location.isWritable()) ) {
return false;
}
if (location.isFileSystem()) {
if (location.isDirectory()) {
QDomDocument document;
QDomElement root = document.createElement("qet-directory");
document.appendChild(root);
root.appendChild(name_list.toXml(document));
QString filepath = location.fileSystemPath() + "/qet_directory";
if (!QET::writeXmlFile(document, filepath)) {
qDebug() << "ElementCollectionHandler::setNames : write qet-directory file failed";
return false;
}
return true;
}
if (location.isElement()) {
QDomDocument document;
document.appendChild(document.importNode(location.xml(), true));
if (document.isNull()) {
qDebug() << "ElementCollectionHandler::setNames : failed to load xml document from file";
return false;
}
QDomElement document_element = document.documentElement();
document_element.replaceChild(name_list.toXml(document), document_element.firstChildElement("names"));
return true;
}
}
if (location.isProject()) {
QDomElement element = location.xml();
QDomDocument document = element.ownerDocument();
element.replaceChild(name_list.toXml(document), element.firstChildElement("names"));
return true;
}
return false;
}

View File

@@ -19,6 +19,7 @@
#define ELEMENTCOLLECTIONHANDLER_H #define ELEMENTCOLLECTIONHANDLER_H
#include "elementslocation.h" #include "elementslocation.h"
#include "nameslist.h"
class QWidget; class QWidget;
@@ -90,6 +91,8 @@ class ElementCollectionHandler
~ElementCollectionHandler(); ~ElementCollectionHandler();
ElementsLocation copy(ElementsLocation &source, ElementsLocation &destination); ElementsLocation copy(ElementsLocation &source, ElementsLocation &destination);
ElementsLocation createDir(ElementsLocation &parent, const QString &name, const NamesList &name_list);
bool setNames(ElementsLocation &location, const NamesList &name_list);
private: private:
ECHStrategy *m_strategy = nullptr; ECHStrategy *m_strategy = nullptr;

View File

@@ -335,8 +335,9 @@ void ElementsCollectionWidget::editDirectory()
ElementsLocation location(feci->collectionPath()); ElementsLocation location(feci->collectionPath());
ElementsCategoryEditor ece(location, true, this); ElementsCategoryEditor ece(location, true, this);
if (ece.exec() == QDialog::Accepted) if (ece.exec() == QDialog::Accepted)
reload(); eci->clearData();
} }
/** /**

View File

@@ -495,7 +495,7 @@ NamesList ElementsLocation::nameList()
/** /**
* @brief ElementsLocation::xml * @brief ElementsLocation::xml
* @return The definition of this element. * @return The definition of this element or directory.
* The definition can be null. * The definition can be null.
*/ */
QDomElement ElementsLocation::xml() const QDomElement ElementsLocation::xml() const

View File

@@ -465,6 +465,40 @@ bool XmlElementCollection::exist(const QString &path)
return true; return true;
} }
/**
* @brief XmlElementCollection::createDir
* Create a child directorie at path @path with the name @name.
* Emit directorieAdded if success.
* @param path : path of parent diectorie
* @param name : name of the directori to create.
* @param name_list : translation of the directorie name.
* @return true if creation success, if directorie already exist return true.
*/
bool XmlElementCollection::createDir(QString path, QString name, const NamesList &name_list)
{
QString new_dir_path = path + "/" + name;
if (!directory(new_dir_path).isNull()) {
return true;
}
QDomElement parent_dir = directory(path);
if (parent_dir.isNull()) {
qDebug() << "XmlElementCollection::createDir : directorie at path doesn't exist";
return false;
}
QDomElement new_dir = m_dom_document.createElement("category");
new_dir.setAttribute("name", name);
new_dir.appendChild(name_list.toXml(m_dom_document));
parent_dir.appendChild(new_dir);
emit directorieAdded(new_dir_path);
return true;
}
/** /**
* @brief XmlElementCollection::copyDirectory * @brief XmlElementCollection::copyDirectory
* Copy the directory represented by source to destination. * Copy the directory represented by source to destination.

View File

@@ -50,6 +50,7 @@ class XmlElementCollection : public QObject
bool addElementDefinition (const QString &dir_path, const QString &elmt_name, const QDomElement &xml_definition); bool addElementDefinition (const QString &dir_path, const QString &elmt_name, const QDomElement &xml_definition);
ElementsLocation copy (ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true); ElementsLocation copy (ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true);
bool exist (const QString &path); bool exist (const QString &path);
bool createDir (QString path, QString name, const NamesList &name_list);
private: private:
ElementsLocation copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true); ElementsLocation copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true);
@@ -68,8 +69,12 @@ class XmlElementCollection : public QObject
* @param collection_path, the path of this element in this collection * @param collection_path, the path of this element in this collection
*/ */
void elementChanged (QString collection_path); void elementChanged (QString collection_path);
/**
public slots: * @brief directorieAdded
* This signal is emited when a directorie is added to this collection
* @param collection_path, the path of the new directorie
*/
void directorieAdded(QString collection_path);
private: private:
QDomDocument m_dom_document; QDomDocument m_dom_document;

View File

@@ -16,161 +16,145 @@
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "elementscategoryeditor.h" #include "elementscategoryeditor.h"
#include "elementscollection.h"
#include "elementscategory.h"
#include "nameslistwidget.h" #include "nameslistwidget.h"
#include "qet.h" #include "qet.h"
#include "qetapp.h"
#include "qfilenameedit.h" #include "qfilenameedit.h"
#include "qetmessagebox.h" #include "qetmessagebox.h"
#include "elementcollectionhandler.h"
/** /**
Constructeur fournissant un dialogue d'edition de categorie. * @brief ElementsCategoryEditor::ElementsCategoryEditor
@param category_path Chemin de la categorie a editer ou de la categorie parente en cas de creation * Constructor
@param edit booleen a true pour le mode edition, a false pour le mode creation * @param location : location of the category to edit, or parent directory/category for the creation of a new category
@param parent QWidget parent du dialogue * @param edit : true = edit mode, false = creation mode
*/ * @param parent : parent widget
ElementsCategoryEditor::ElementsCategoryEditor(const ElementsLocation &category_path, bool edit, QWidget *parent) : */
ElementsCategoryEditor::ElementsCategoryEditor(const ElementsLocation &location, bool edit, QWidget *parent) :
QDialog(parent), QDialog(parent),
mode_edit(edit) m_edit_mode(edit),
m_location(location)
{ {
// dialogue basique setUpWidget();
buildDialog();
// recupere la categorie a editer if (m_location.isElement()) {
ElementsCollectionItem *category_item = QETApp::collectionItem(category_path); QET::QetMessageBox::warning(this,
if (category_item) category_item = category_item -> toCategory(); tr("L'item n'est pas une catégorie", "message box title"),
tr("L'item demandé n'est pas une categrie. Abandon.", "message box content"));
if (!category_item || !category_item -> isCategory()) {
QET::QetMessageBox::warning(
this,
tr("Catégorie inexistante", "message box title"),
tr("La catégorie demandée n'existe pas. Abandon.", "message box content")
);
return; return;
} else {
category = category_item -> toPureCategory();
} }
if (mode_edit) { if (!location.exist()) {
setWindowTitle(tr("Éditer une catégorie", "window title")); QET::QetMessageBox::warning(this,
connect(buttons, SIGNAL(accepted()), this, SLOT(acceptUpdate())); tr("Catégorie inexistante", "message box title"),
tr("La catégorie demandée n'existe pas. Abandon.", "message box content"));
return;
}
// edition de categorie = affichage des noms deja existants if (m_edit_mode) {
names_list -> setNames(category -> categoryNames()); setWindowTitle(tr("Éditer une catégorie", "window title"));
internal_name_ -> setText(category -> pathName()); connect(m_buttons, SIGNAL(accepted()), this, SLOT(acceptUpdate()));
internal_name_ -> setReadOnly(true);
m_names_list -> setNames(m_location.nameList());
m_file_line_edit -> setText(m_location.fileSystemPath());
m_file_line_edit -> setReadOnly(true);
} else { } else {
setWindowTitle(tr("Créer une nouvelle catégorie", "window title")); setWindowTitle(tr("Créer une nouvelle catégorie", "window title"));
connect(buttons, SIGNAL(accepted()), this, SLOT(acceptCreation())); connect(m_buttons, SIGNAL(accepted()), this, SLOT(acceptCreation()));
// nouvelle categorie = une ligne pre-machee
NamesList cat_names; NamesList cat_names;
cat_names.addName(QLocale::system().name().left(2), tr("Nom de la nouvelle catégorie", "default name when creating a new category")); cat_names.addName(QLocale::system().name().left(2), tr("Nom de la nouvelle catégorie", "default name when creating a new category"));
names_list -> setNames(cat_names); m_names_list -> setNames(cat_names);
} }
// gestion de la lecture seule //Location is ReadOnly
if (!category -> isWritable()) { if (!m_location.isWritable()) {
QET::QetMessageBox::warning( QET::QetMessageBox::warning(
this, this,
tr("Édition en lecture seule", "message box title"), tr("Édition en lecture seule", "message box title"),
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") 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")
); );
names_list -> setReadOnly(true); m_names_list -> setReadOnly(true);
internal_name_ -> setReadOnly(true); m_file_line_edit -> setReadOnly(true);
} }
} }
/** /**
Destructeur * @brief ElementsCategoryEditor::~ElementsCategoryEditor
*/ * Destructor
*/
ElementsCategoryEditor::~ElementsCategoryEditor() { ElementsCategoryEditor::~ElementsCategoryEditor() {
} }
/** /**
Bases du dialogue de creation / edition * @brief ElementsCategoryEditor::setUpWidget
*/ */
void ElementsCategoryEditor::buildDialog() { void ElementsCategoryEditor::setUpWidget()
{
QVBoxLayout *editor_layout = new QVBoxLayout(); QVBoxLayout *editor_layout = new QVBoxLayout();
setLayout(editor_layout); setLayout(editor_layout);
names_list = new NamesListWidget(); m_names_list = new NamesListWidget();
internal_name_label_ = new QLabel(tr("Nom interne : ")); m_file_name = new QLabel(tr("Nom interne : "));
internal_name_ = new QFileNameEdit(); m_file_line_edit = new QFileNameEdit();
buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttons, SIGNAL(rejected()), this, SLOT(reject())); connect(m_buttons, SIGNAL(rejected()), this, SLOT(reject()));
QHBoxLayout *internal_name_layout = new QHBoxLayout(); QHBoxLayout *internal_name_layout = new QHBoxLayout();
internal_name_layout -> addWidget(internal_name_label_); internal_name_layout -> addWidget(m_file_name);
internal_name_layout -> addWidget(internal_name_); internal_name_layout -> addWidget(m_file_line_edit);
editor_layout -> addLayout(internal_name_layout); editor_layout -> addLayout(internal_name_layout);
editor_layout -> addWidget(new QLabel(tr("Vous pouvez spécifier un nom par langue pour la catégorie."))); editor_layout -> addWidget(new QLabel(tr("Vous pouvez spécifier un nom par langue pour la catégorie.")));
editor_layout -> addWidget(names_list); editor_layout -> addWidget(m_names_list);
editor_layout -> addWidget(buttons); editor_layout -> addWidget(m_buttons);
} }
/** /**
Valide les donnees entrees par l'utilisateur lors d'une creation de * @brief ElementsCategoryEditor::acceptCreation
categorie * Valid the creation of the category
*/ */
void ElementsCategoryEditor::acceptCreation() { void ElementsCategoryEditor::acceptCreation()
if (!category -> isWritable()) QDialog::accept(); {
if (!m_location.isWritable()) {
// il doit y avoir au moins un nom QDialog::accept();
if (!names_list -> checkOneName()) return;
// exige un nom de dossier de la part de l'utilisateur
if (!internal_name_ -> isValid()) {
QET::QetMessageBox::critical(
this,
tr("Nom interne manquant", "message box title"),
tr("Vous devez spécifier un nom interne.", "message box content")
);
return;
} }
QString dirname = internal_name_ -> text();
// verifie que le nom interne n'est pas deja pris //there must be at least one name
if (category -> category(dirname)) { if (!m_names_list -> checkOneName()) {
QET::QetMessageBox::critical(
this,
tr("Nom interne déjà utilisé", "message box title"),
tr(
"Le nom interne que vous avez choisi est déjà utilisé "
"par une catégorie existante. Veuillez en choisir un autre.",
"message box content"
)
);
return; return;
} }
// cree la nouvelle categorie //User must enter a directorie name
ElementsCategory *new_category = category -> createCategory(dirname); if (!m_file_line_edit -> isValid()) {
if (!new_category) { QET::QetMessageBox::critical(this,
QET::QetMessageBox::critical( tr("Nom interne manquant", "message box title"),
this, tr("Vous devez spécifier un nom interne.", "message box content"));
tr("Erreur", "message box title"), return;
tr("Impossible de créer la catégorie", "message box content") }
); QString dirname = m_file_line_edit -> text();
//Check if dirname already exist.
ElementsLocation created_location = m_location;
created_location.addToPath(dirname);
if (created_location.exist()) {
QET::QetMessageBox::critical(this,
tr("Nom interne déjà utilisé", "message box title"),
tr("Le nom interne que vous avez choisi est déjà utilisé "
"par une catégorie existante. Veuillez en choisir un autre.",
"message box content"));
return; return;
} }
// chargement des noms ElementCollectionHandler ech_;
NamesList names = names_list -> names(); NamesList nl = m_names_list->names();
foreach(QString lang, names.langs()) { ElementsLocation loc = ech_.createDir(m_location, dirname, nl);
new_category -> addName(lang, names[lang]); if (loc.isNull()) {
} QET::QetMessageBox::critical(this,
tr("Erreur", "message box title"),
// ecriture de la tr("Impossible de créer la catégorie", "message box content"));
if (!new_category -> write()) {
QET::QetMessageBox::critical(
this,
tr("Erreur", "message box title"),
tr("Impossible d'enregistrer la catégorie", "message box content")
);
return; return;
} }
@@ -178,24 +162,26 @@ void ElementsCategoryEditor::acceptCreation() {
} }
/** /**
Valide les donnees entrees par l'utilisateur lors d'une modification de * @brief ElementsCategoryEditor::acceptUpdate
categorie * Valid the update of the category
*/ */
void ElementsCategoryEditor::acceptUpdate() { void ElementsCategoryEditor::acceptUpdate()
{
if (!category -> isWritable()) QDialog::accept(); if (!m_location.isWritable()) {
QDialog::accept();
// il doit y avoir au moins un nom
if (!names_list -> checkOneName()) return;
// chargement des noms
category -> clearNames();
NamesList names = names_list -> names();
foreach(QString lang, names.langs()) {
category -> addName(lang, names[lang]);
} }
category -> write(); //There must be at least one name
if (!m_names_list -> checkOneName()) {
return;
}
QDialog::accept(); ElementCollectionHandler ech;
if (ech.setNames(m_location, m_names_list->names())){
QDialog::accept();
}
else {
return;
}
} }

View File

@@ -17,41 +17,45 @@
*/ */
#ifndef ELEMENTS_CATEGORY_EDITOR_H #ifndef ELEMENTS_CATEGORY_EDITOR_H
#define ELEMENTS_CATEGORY_EDITOR_H #define ELEMENTS_CATEGORY_EDITOR_H
#include <QtWidgets>
#include <QDialog>
#include "elementslocation.h" #include "elementslocation.h"
class ElementsCategory; class ElementsCategory;
class NamesListWidget; class NamesListWidget;
class QFileNameEdit; class QFileNameEdit;
class QDialogButtonBox;
class QLabel;
/** /**
This class provides a dialog to edit an existing category or create a new * @brief The ElementsCategoryEditor class
one. * This class provides a dialog to edit an existing category or create a new one.
*/ */
class ElementsCategoryEditor : public QDialog { class ElementsCategoryEditor : public QDialog
{
Q_OBJECT Q_OBJECT
// constructors, destructor
public: public:
ElementsCategoryEditor(const ElementsLocation &, bool = true, QWidget * = 0); ElementsCategoryEditor(const ElementsLocation &location, bool edit = true, QWidget *parent = nullptr);
virtual ~ElementsCategoryEditor(); virtual ~ElementsCategoryEditor();
private: private:
ElementsCategoryEditor(const ElementsCategoryEditor &); ElementsCategoryEditor(const ElementsCategoryEditor &);
// attributes
private: private:
ElementsCategory *category; ElementsCategory *category;
QDialogButtonBox *buttons; QDialogButtonBox *m_buttons;
NamesListWidget *names_list; NamesListWidget *m_names_list;
QLabel *internal_name_label_; QLabel *m_file_name;
QFileNameEdit *internal_name_; QFileNameEdit *m_file_line_edit;
bool mode_edit; bool m_edit_mode;
ElementsLocation m_location;
// methods
private: private:
void buildDialog(); void setUpWidget();
public slots: public slots:
void acceptCreation(); void acceptCreation();
void acceptUpdate(); void acceptUpdate();
}; };
#endif #endif