[qet] [4568] Project embedded collection, Clean unused elements and empty directory work again

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


Revision: 4568
Author:   blacksun
Date:     2016-07-09 20:24:40 +0200 (Sat, 09 Jul 2016)
Log Message:
-----------
Project embedded collection, Clean unused elements and empty directory work again

Modified Paths:
--------------
    trunk/sources/ElementsCollection/elementscollectionmodel.cpp
    trunk/sources/ElementsCollection/elementscollectionmodel.h
    trunk/sources/ElementsCollection/xmlelementcollection.cpp
    trunk/sources/ElementsCollection/xmlelementcollection.h
    trunk/sources/projectview.cpp

Modified: trunk/sources/ElementsCollection/elementscollectionmodel.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionmodel.cpp	2016-07-07 12:45:01 UTC (rev 4567)
+++ trunk/sources/ElementsCollection/elementscollectionmodel.cpp	2016-07-09 18:24:40 UTC (rev 4568)
@@ -311,6 +311,8 @@
 		xpeci->setUpData();
 	connect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection);
 	connect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem);
+	connect(project->embeddedElementCollection(), &XmlElementCollection::elementRemoved, this, &ElementsCollectionModel::itemRemovedFromCollection);
+	connect(project->embeddedElementCollection(), &XmlElementCollection::directoryRemoved, this, &ElementsCollectionModel::itemRemovedFromCollection);
 }
 
 /**
@@ -329,6 +331,8 @@
 		m_project_hash.remove(project);
 		disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection);
 		disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem);
+		disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementRemoved, this, &ElementsCollectionModel::itemRemovedFromCollection);
+		disconnect(project->embeddedElementCollection(), &XmlElementCollection::directoryRemoved, this, &ElementsCollectionModel::itemRemovedFromCollection);
 	}
 }
 
@@ -407,9 +411,6 @@
  */
 QModelIndex ElementsCollectionModel::indexFromLocation(const ElementsLocation &location)
 {
-	if (!location.exist())
-		return QModelIndex();
-
 	QList <ElementCollectionItem *> child_list;
 
 	for (int i=0 ; i<rowCount() ; i++)
@@ -475,6 +476,34 @@
 }
 
 /**
+ * @brief ElementsCollectionModel::itemRemovedFromCollection
+ * This method must be called by a signal, to get a sender.
+ * @param path
+ */
+void ElementsCollectionModel::itemRemovedFromCollection(QString path)
+{
+	QObject *object = sender();
+	XmlElementCollection *collection = static_cast<XmlElementCollection *> (object);
+	if (!collection)
+		return;
+
+	QETProject *project = nullptr;
+
+		//Get the owner project of the collection
+	foreach (QETProject *prj, m_project_list) {
+		if (prj->embeddedElementCollection() == collection) {
+			project = prj;
+		}
+	}
+
+	if (project) {
+		QModelIndex index = indexFromLocation(ElementsLocation(path, project));
+		if (index.isValid())
+			removeRow(index.row(), index.parent());
+	}
+}
+
+/**
  * @brief ElementsCollectionModel::updateItem
  * Update the item at path
  * @param path

Modified: trunk/sources/ElementsCollection/elementscollectionmodel.h
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionmodel.h	2016-07-07 12:45:01 UTC (rev 4567)
+++ trunk/sources/ElementsCollection/elementscollectionmodel.h	2016-07-09 18:24:40 UTC (rev 4568)
@@ -28,7 +28,6 @@
 template<> class QList<ElementCollectionItem>;
 
 
-
 class ElementsCollectionModel : public QStandardItemModel
 {
 	Q_OBJECT
@@ -59,6 +58,7 @@
 
 	private:
 		void elementIntegratedToCollection (QString path);
+		void itemRemovedFromCollection (QString path);
 		void updateItem (QString path);
 
 	private:

Modified: trunk/sources/ElementsCollection/xmlelementcollection.cpp
===================================================================
--- trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-07-07 12:45:01 UTC (rev 4567)
+++ trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-07-09 18:24:40 UTC (rev 4568)
@@ -433,6 +433,26 @@
 }
 
 /**
+ * @brief XmlElementCollection::removeElement
+ * Remove the element at path @path.
+ * @param path
+ * @return True if element is removed and emit the signal elementRemoved.
+ * else false.
+ */
+bool XmlElementCollection::removeElement(QString path)
+{
+	QDomElement elmt = element(path);
+
+	if (!elmt.isNull()) {
+		elmt.parentNode().removeChild(elmt);
+		emit elementRemoved(path);
+		return true;
+	}
+
+	return false;
+}
+
+/**
  * @brief XmlElementCollection::copy
  * Copy the content represented by source (an element or a directory) to destination.
  * Destination must be a directory of this collection.
@@ -503,6 +523,24 @@
 }
 
 /**
+ * @brief XmlElementCollection::removeDir
+ * Remove the directory at path @path.
+ * @param path
+ * @return true if successfuly removed and emit directoryRemoved(QString),
+ * else false.
+ */
+bool XmlElementCollection::removeDir(QString path)
+{
+	QDomElement dir = directory(path);
+	if (!dir.isNull()) {
+		dir.parentNode().removeChild(dir);
+		emit directoryRemoved(path);
+		return true;
+	}
+	return false;
+}
+
+/**
  * @brief XmlElementCollection::elementsLocation
  * Return all locations stored in dom_element (element and directory).
  * If dom_element is null, return all location owned by this collection
@@ -572,6 +610,34 @@
 }
 
 /**
+ * @brief XmlElementCollection::cleanUnusedElement
+ * Remove elements in this collection which is not used in the owner project
+ */
+void XmlElementCollection::cleanUnusedElement()
+{
+	foreach (ElementsLocation loc, m_project->unusedElements())
+		removeElement(loc.collectionPath(false));
+}
+
+/**
+ * @brief XmlElementCollection::cleanUnusedDirectory
+ * Remove the empty directories of this collection
+ */
+void XmlElementCollection::cleanUnusedDirectory()
+{
+	QDomNodeList lst = importCategory().elementsByTagName("category");
+
+	for(int i=0 ; i<lst.size() ; i++) {
+		QDomElement dir = lst.item(i).toElement();
+			//elmt haven't got child node "element" or "category", so he is emty, we can remove it
+		if (dir.elementsByTagName("element").isEmpty() && dir.elementsByTagName("category").isEmpty()) {
+			if (removeDir(domToLocation(dir).collectionPath(false)))
+				i=-1;
+		}
+	}
+}
+
+/**
  * @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-07-07 12:45:01 UTC (rev 4567)
+++ trunk/sources/ElementsCollection/xmlelementcollection.h	2016-07-09 18:24:40 UTC (rev 4568)
@@ -50,13 +50,18 @@
 		QDomElement directory(const QString &path) const;
 		QString addElement (ElementsLocation &location);
 		bool addElementDefinition (const QString &dir_path, const QString &elmt_name, const QDomElement &xml_definition);
+		bool removeElement(QString path);
 		ElementsLocation copy (ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true);
 		bool exist (const QString &path) const;
 		bool createDir (QString path, QString name, const NamesList &name_list);
+		bool removeDir (QString path);
 
 		QList <ElementsLocation> elementsLocation (QDomElement dom_element = QDomElement(), bool childs = true) const;
 		ElementsLocation domToLocation(QDomElement dom_element) const;
 
+		void cleanUnusedElement();
+		void cleanUnusedDirectory();
+
 	private:
 		ElementsLocation copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true);
 		ElementsLocation copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename = QString());
@@ -75,11 +80,23 @@
 			 */
 		void elementChanged (QString collection_path);
 			/**
+			 * @brief elementRemoved
+			 * This signal is emited when an element is removed to this collection
+			 * @param collection_path, the path of the removed element in this collection
+			 */
+		void elementRemoved(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);
+			/**
+			 * @brief directoryRemoved
+			 * This signal is emited when a directory is removed to this collection
+			 * @param collection_path, the path of the removed directory
+			 */
+		void directoryRemoved(QString collection_path);
 
 	private:
 		QDomDocument m_dom_document;

Modified: trunk/sources/projectview.cpp
===================================================================
--- trunk/sources/projectview.cpp	2016-07-07 12:45:01 UTC (rev 4567)
+++ trunk/sources/projectview.cpp	2016-07-09 18:24:40 UTC (rev 4568)
@@ -31,6 +31,7 @@
 #include "qettemplateeditor.h"
 #include "diagramfoliolist.h"
 #include "projectpropertiesdialog.h"
+#include "xmlelementcollection.h"
 
 /**
 	Constructeur
@@ -806,6 +807,12 @@
 		if (clean_tbt -> isChecked()) {
 			m_project->embeddedTitleBlockTemplatesCollection()->deleteUnusedTitleBlocKTemplates();
 		}
+		if (clean_elements->isChecked()) {
+			m_project->embeddedElementCollection()->cleanUnusedElement();
+		}
+		if (clean_categories->isChecked()) {
+			m_project->embeddedElementCollection()->cleanUnusedDirectory();
+		}
 	}
 
 	return(clean_count);


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