From 5f939669846e12d4c982cafad7af425b43b581a1 Mon Sep 17 00:00:00 2001 From: blacksun Date: Thu, 12 May 2016 15:41:55 +0000 Subject: [PATCH] ElementsCategoryEditor : remove the use of ElementsCategory git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4475 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- .../elementcollectionhandler.cpp | 114 +++++++++ .../elementcollectionhandler.h | 3 + .../elementscollectionwidget.cpp | 3 +- .../ElementsCollection/elementslocation.cpp | 2 +- .../xmlelementcollection.cpp | 34 +++ .../ElementsCollection/xmlelementcollection.h | 9 +- sources/elementscategoryeditor.cpp | 224 ++++++++---------- sources/elementscategoryeditor.h | 48 ++-- 8 files changed, 292 insertions(+), 145 deletions(-) diff --git a/sources/ElementsCollection/elementcollectionhandler.cpp b/sources/ElementsCollection/elementcollectionhandler.cpp index 1f4810e86..8a4141022 100644 --- a/sources/ElementsCollection/elementcollectionhandler.cpp +++ b/sources/ElementsCollection/elementcollectionhandler.cpp @@ -298,3 +298,117 @@ ElementsLocation ElementCollectionHandler::copy(ElementsLocation &source, Elemen else 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; +} diff --git a/sources/ElementsCollection/elementcollectionhandler.h b/sources/ElementsCollection/elementcollectionhandler.h index e0fc1abad..f3ceacdc3 100644 --- a/sources/ElementsCollection/elementcollectionhandler.h +++ b/sources/ElementsCollection/elementcollectionhandler.h @@ -19,6 +19,7 @@ #define ELEMENTCOLLECTIONHANDLER_H #include "elementslocation.h" +#include "nameslist.h" class QWidget; @@ -90,6 +91,8 @@ class ElementCollectionHandler ~ElementCollectionHandler(); 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: ECHStrategy *m_strategy = nullptr; diff --git a/sources/ElementsCollection/elementscollectionwidget.cpp b/sources/ElementsCollection/elementscollectionwidget.cpp index fe32a54d3..8a1aa1b94 100644 --- a/sources/ElementsCollection/elementscollectionwidget.cpp +++ b/sources/ElementsCollection/elementscollectionwidget.cpp @@ -335,8 +335,9 @@ void ElementsCollectionWidget::editDirectory() ElementsLocation location(feci->collectionPath()); ElementsCategoryEditor ece(location, true, this); + if (ece.exec() == QDialog::Accepted) - reload(); + eci->clearData(); } /** diff --git a/sources/ElementsCollection/elementslocation.cpp b/sources/ElementsCollection/elementslocation.cpp index b6bd53104..c3786f1c3 100644 --- a/sources/ElementsCollection/elementslocation.cpp +++ b/sources/ElementsCollection/elementslocation.cpp @@ -495,7 +495,7 @@ NamesList ElementsLocation::nameList() /** * @brief ElementsLocation::xml - * @return The definition of this element. + * @return The definition of this element or directory. * The definition can be null. */ QDomElement ElementsLocation::xml() const diff --git a/sources/ElementsCollection/xmlelementcollection.cpp b/sources/ElementsCollection/xmlelementcollection.cpp index 518ccc34d..75865bf03 100644 --- a/sources/ElementsCollection/xmlelementcollection.cpp +++ b/sources/ElementsCollection/xmlelementcollection.cpp @@ -465,6 +465,40 @@ bool XmlElementCollection::exist(const QString &path) 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 * Copy the directory represented by source to destination. diff --git a/sources/ElementsCollection/xmlelementcollection.h b/sources/ElementsCollection/xmlelementcollection.h index 62018364c..2fb7782d7 100644 --- a/sources/ElementsCollection/xmlelementcollection.h +++ b/sources/ElementsCollection/xmlelementcollection.h @@ -50,6 +50,7 @@ class XmlElementCollection : public QObject 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); bool exist (const QString &path); + bool createDir (QString path, QString name, const NamesList &name_list); private: 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 */ 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: QDomDocument m_dom_document; diff --git a/sources/elementscategoryeditor.cpp b/sources/elementscategoryeditor.cpp index de5abbd69..9ecf74afd 100644 --- a/sources/elementscategoryeditor.cpp +++ b/sources/elementscategoryeditor.cpp @@ -16,161 +16,145 @@ along with QElectroTech. If not, see . */ #include "elementscategoryeditor.h" -#include "elementscollection.h" -#include "elementscategory.h" #include "nameslistwidget.h" #include "qet.h" -#include "qetapp.h" #include "qfilenameedit.h" #include "qetmessagebox.h" +#include "elementcollectionhandler.h" /** - Constructeur fournissant un dialogue d'edition de categorie. - @param category_path Chemin de la categorie a editer ou de la categorie parente en cas de creation - @param edit booleen a true pour le mode edition, a false pour le mode creation - @param parent QWidget parent du dialogue -*/ -ElementsCategoryEditor::ElementsCategoryEditor(const ElementsLocation &category_path, bool edit, QWidget *parent) : + * @brief ElementsCategoryEditor::ElementsCategoryEditor + * Constructor + * @param location : location of the category to edit, or parent directory/category for the creation of a new category + * @param edit : true = edit mode, false = creation mode + * @param parent : parent widget + */ +ElementsCategoryEditor::ElementsCategoryEditor(const ElementsLocation &location, bool edit, QWidget *parent) : QDialog(parent), - mode_edit(edit) + m_edit_mode(edit), + m_location(location) { - // dialogue basique - buildDialog(); - - // recupere la categorie a editer - ElementsCollectionItem *category_item = QETApp::collectionItem(category_path); - if (category_item) category_item = category_item -> toCategory(); - - 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") - ); + setUpWidget(); + + if (m_location.isElement()) { + QET::QetMessageBox::warning(this, + 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")); + return; + } + + if (!location.exist()) { + 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; - } else { - category = category_item -> toPureCategory(); } - if (mode_edit) { + if (m_edit_mode) { setWindowTitle(tr("Éditer une catégorie", "window title")); - connect(buttons, SIGNAL(accepted()), this, SLOT(acceptUpdate())); + connect(m_buttons, SIGNAL(accepted()), this, SLOT(acceptUpdate())); - // edition de categorie = affichage des noms deja existants - names_list -> setNames(category -> categoryNames()); - internal_name_ -> setText(category -> pathName()); - 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 { 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; 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 - if (!category -> isWritable()) { + + //Location is ReadOnly + if (!m_location.isWritable()) { QET::QetMessageBox::warning( this, 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") ); - names_list -> setReadOnly(true); - internal_name_ -> setReadOnly(true); + m_names_list -> setReadOnly(true); + m_file_line_edit -> setReadOnly(true); } } /** - Destructeur -*/ + * @brief ElementsCategoryEditor::~ElementsCategoryEditor + * Destructor + */ ElementsCategoryEditor::~ElementsCategoryEditor() { } /** - Bases du dialogue de creation / edition -*/ -void ElementsCategoryEditor::buildDialog() { + * @brief ElementsCategoryEditor::setUpWidget + */ +void ElementsCategoryEditor::setUpWidget() +{ QVBoxLayout *editor_layout = new QVBoxLayout(); setLayout(editor_layout); - names_list = new NamesListWidget(); - internal_name_label_ = new QLabel(tr("Nom interne : ")); - internal_name_ = new QFileNameEdit(); + m_names_list = new NamesListWidget(); + m_file_name = new QLabel(tr("Nom interne : ")); + m_file_line_edit = new QFileNameEdit(); - buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); - connect(buttons, SIGNAL(rejected()), this, SLOT(reject())); + m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + connect(m_buttons, SIGNAL(rejected()), this, SLOT(reject())); QHBoxLayout *internal_name_layout = new QHBoxLayout(); - internal_name_layout -> addWidget(internal_name_label_); - internal_name_layout -> addWidget(internal_name_); + internal_name_layout -> addWidget(m_file_name); + internal_name_layout -> addWidget(m_file_line_edit); 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(names_list); - editor_layout -> addWidget(buttons); + editor_layout -> addWidget(m_names_list); + editor_layout -> addWidget(m_buttons); } /** - Valide les donnees entrees par l'utilisateur lors d'une creation de - categorie -*/ -void ElementsCategoryEditor::acceptCreation() { - if (!category -> isWritable()) QDialog::accept(); - - // il doit y avoir au moins un nom - 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; + * @brief ElementsCategoryEditor::acceptCreation + * Valid the creation of the category + */ +void ElementsCategoryEditor::acceptCreation() +{ + if (!m_location.isWritable()) { + QDialog::accept(); } - QString dirname = internal_name_ -> text(); - // verifie que le nom interne n'est pas deja pris - if (category -> category(dirname)) { - 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" - ) - ); + //there must be at least one name + if (!m_names_list -> checkOneName()) { return; } - // cree la nouvelle categorie - ElementsCategory *new_category = category -> createCategory(dirname); - if (!new_category) { - QET::QetMessageBox::critical( - this, - tr("Erreur", "message box title"), - tr("Impossible de créer la catégorie", "message box content") - ); + //User must enter a directorie name + if (!m_file_line_edit -> 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 = m_file_line_edit -> text(); - // chargement des noms - NamesList names = names_list -> names(); - foreach(QString lang, names.langs()) { - new_category -> addName(lang, names[lang]); + + //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; } - - // ecriture de la - if (!new_category -> write()) { - QET::QetMessageBox::critical( - this, - tr("Erreur", "message box title"), - tr("Impossible d'enregistrer la catégorie", "message box content") - ); + + ElementCollectionHandler ech_; + NamesList nl = m_names_list->names(); + ElementsLocation loc = ech_.createDir(m_location, dirname, nl); + if (loc.isNull()) { + QET::QetMessageBox::critical(this, + tr("Erreur", "message box title"), + tr("Impossible de créer la catégorie", "message box content")); return; } @@ -178,24 +162,26 @@ void ElementsCategoryEditor::acceptCreation() { } /** - Valide les donnees entrees par l'utilisateur lors d'une modification de - categorie -*/ -void ElementsCategoryEditor::acceptUpdate() { - - if (!category -> 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]); + * @brief ElementsCategoryEditor::acceptUpdate + * Valid the update of the category + */ +void ElementsCategoryEditor::acceptUpdate() +{ + if (!m_location.isWritable()) { + QDialog::accept(); } - 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; + } } diff --git a/sources/elementscategoryeditor.h b/sources/elementscategoryeditor.h index d09eb9984..9033324c5 100644 --- a/sources/elementscategoryeditor.h +++ b/sources/elementscategoryeditor.h @@ -17,41 +17,45 @@ */ #ifndef ELEMENTS_CATEGORY_EDITOR_H #define ELEMENTS_CATEGORY_EDITOR_H -#include + +#include #include "elementslocation.h" + class ElementsCategory; class NamesListWidget; class QFileNameEdit; +class QDialogButtonBox; +class QLabel; + /** - This class provides a dialog to edit an existing category or create a new - one. -*/ -class ElementsCategoryEditor : public QDialog { + * @brief The ElementsCategoryEditor class + * This class provides a dialog to edit an existing category or create a new one. + */ +class ElementsCategoryEditor : public QDialog +{ Q_OBJECT - - // constructors, destructor + public: - ElementsCategoryEditor(const ElementsLocation &, bool = true, QWidget * = 0); - virtual ~ElementsCategoryEditor(); + ElementsCategoryEditor(const ElementsLocation &location, bool edit = true, QWidget *parent = nullptr); + virtual ~ElementsCategoryEditor(); private: - ElementsCategoryEditor(const ElementsCategoryEditor &); + ElementsCategoryEditor(const ElementsCategoryEditor &); - // attributes private: - ElementsCategory *category; - QDialogButtonBox *buttons; - NamesListWidget *names_list; - QLabel *internal_name_label_; - QFileNameEdit *internal_name_; - bool mode_edit; - - // methods + ElementsCategory *category; + QDialogButtonBox *m_buttons; + NamesListWidget *m_names_list; + QLabel *m_file_name; + QFileNameEdit *m_file_line_edit; + bool m_edit_mode; + ElementsLocation m_location; + private: - void buildDialog(); + void setUpWidget(); public slots: - void acceptCreation(); - void acceptUpdate(); + void acceptCreation(); + void acceptUpdate(); }; #endif