[qet] [4475] ElementsCategoryEditor : remove the use of ElementsCategory

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 4475
Author:   blacksun
Date:     2016-05-12 17:41:55 +0200 (Thu, 12 May 2016)
Log Message:
-----------
ElementsCategoryEditor : remove the use of ElementsCategory

Modified Paths:
--------------
    trunk/sources/ElementsCollection/elementcollectionhandler.cpp
    trunk/sources/ElementsCollection/elementcollectionhandler.h
    trunk/sources/ElementsCollection/elementscollectionwidget.cpp
    trunk/sources/ElementsCollection/elementslocation.cpp
    trunk/sources/ElementsCollection/xmlelementcollection.cpp
    trunk/sources/ElementsCollection/xmlelementcollection.h
    trunk/sources/elementscategoryeditor.cpp
    trunk/sources/elementscategoryeditor.h

Modified: trunk/sources/ElementsCollection/elementcollectionhandler.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementcollectionhandler.cpp	2016-05-08 17:28:13 UTC (rev 4474)
+++ trunk/sources/ElementsCollection/elementcollectionhandler.cpp	2016-05-12 15:41:55 UTC (rev 4475)
@@ -298,3 +298,117 @@
 	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;
+}

Modified: trunk/sources/ElementsCollection/elementcollectionhandler.h
===================================================================
--- trunk/sources/ElementsCollection/elementcollectionhandler.h	2016-05-08 17:28:13 UTC (rev 4474)
+++ trunk/sources/ElementsCollection/elementcollectionhandler.h	2016-05-12 15:41:55 UTC (rev 4475)
@@ -19,6 +19,7 @@
 #define ELEMENTCOLLECTIONHANDLER_H
 
 #include "elementslocation.h"
+#include "nameslist.h"
 
 class QWidget;
 
@@ -90,6 +91,8 @@
         ~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;

Modified: trunk/sources/ElementsCollection/elementscollectionwidget.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-05-08 17:28:13 UTC (rev 4474)
+++ trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-05-12 15:41:55 UTC (rev 4475)
@@ -335,8 +335,9 @@
 
 	ElementsLocation location(feci->collectionPath());
 	ElementsCategoryEditor ece(location, true, this);
+
 	if (ece.exec() == QDialog::Accepted)
-		reload();
+		eci->clearData();
 }
 
 /**

Modified: trunk/sources/ElementsCollection/elementslocation.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementslocation.cpp	2016-05-08 17:28:13 UTC (rev 4474)
+++ trunk/sources/ElementsCollection/elementslocation.cpp	2016-05-12 15:41:55 UTC (rev 4475)
@@ -495,7 +495,7 @@
 
 /**
  * @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

Modified: trunk/sources/ElementsCollection/xmlelementcollection.cpp
===================================================================
--- trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-05-08 17:28:13 UTC (rev 4474)
+++ trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-05-12 15:41:55 UTC (rev 4475)
@@ -466,6 +466,40 @@
 }
 
 /**
+ * @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.
  * if destination have a directory with the same name as source, then this directory is removed

Modified: trunk/sources/ElementsCollection/xmlelementcollection.h
===================================================================
--- trunk/sources/ElementsCollection/xmlelementcollection.h	2016-05-08 17:28:13 UTC (rev 4474)
+++ trunk/sources/ElementsCollection/xmlelementcollection.h	2016-05-12 15:41:55 UTC (rev 4475)
@@ -50,6 +50,7 @@
 		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,9 +69,13 @@
 			 * @param collection_path, the path of this element in this collection
 			 */
 		void elementChanged (QString collection_path);
+			/**
+			 * @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);
 
-	public slots:
-
 	private:
 		QDomDocument m_dom_document;
 };

Modified: trunk/sources/elementscategoryeditor.cpp
===================================================================
--- trunk/sources/elementscategoryeditor.cpp	2016-05-08 17:28:13 UTC (rev 4474)
+++ trunk/sources/elementscategoryeditor.cpp	2016-05-12 15:41:55 UTC (rev 4475)
@@ -16,161 +16,145 @@
 	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
 */
 #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;
-	} else {
-		category = category_item -> toPureCategory();
 	}
+
+	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;
+	}
 	
-	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();
+ * @brief ElementsCategoryEditor::acceptCreation
+ * Valid the creation of the category
+ */
+void ElementsCategoryEditor::acceptCreation()
+{
+	if (!m_location.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")
-		);
+		//there must be at least one name
+	if (!m_names_list -> checkOneName()) {
 		return;
 	}
-	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"
-			)
-		);
+		//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();
 	
-	// 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")
-		);
+
+		//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;
 	}
-	
-	// chargement des noms
-	NamesList names = names_list -> names();
-	foreach(QString lang, names.langs()) {
-		new_category -> addName(lang, names[lang]);
-	}
-	
-	// 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 @@
 }
 
 /**
-	Valide les donnees entrees par l'utilisateur lors d'une modification de
-	categorie
-*/
-void ElementsCategoryEditor::acceptUpdate() {
+ * @brief ElementsCategoryEditor::acceptUpdate
+ * Valid the update of the category
+ */
+void ElementsCategoryEditor::acceptUpdate()
+{
+	if (!m_location.isWritable()) {
+		QDialog::accept();
+	}
 	
-	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]);
+		//There must be at least one name
+	if (!m_names_list -> checkOneName()) {
+		return;
 	}
 	
-	category -> write();
-	
-	QDialog::accept();
+	ElementCollectionHandler ech;
+
+	if (ech.setNames(m_location, m_names_list->names())){
+		QDialog::accept();
+	}
+	else {
+		return;
+	}
 }

Modified: trunk/sources/elementscategoryeditor.h
===================================================================
--- trunk/sources/elementscategoryeditor.h	2016-05-08 17:28:13 UTC (rev 4474)
+++ trunk/sources/elementscategoryeditor.h	2016-05-12 15:41:55 UTC (rev 4475)
@@ -17,41 +17,45 @@
 */
 #ifndef ELEMENTS_CATEGORY_EDITOR_H
 #define ELEMENTS_CATEGORY_EDITOR_H
-#include <QtWidgets>
+
+#include <QDialog>
 #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


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/