[qet] [4629] Bug fix : Crash when drag an item from the element panel, which represent an element embedded by a project, and drop it in a folio of another project.

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


Revision: 4629
Author:   blacksun
Date:     2016-08-16 22:25:27 +0200 (Tue, 16 Aug 2016)
Log Message:
-----------
Bug fix : Crash when drag an item from the element panel, which represent an element embedded by a project, and drop it in a folio of another project.

Modified Paths:
--------------
    trunk/sources/ElementsCollection/xmlelementcollection.cpp
    trunk/sources/ElementsCollection/xmlprojectelementcollectionitem.cpp

Modified: trunk/sources/ElementsCollection/xmlelementcollection.cpp
===================================================================
--- trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-08-16 13:43:46 UTC (rev 4628)
+++ trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-08-16 20:25:27 UTC (rev 4629)
@@ -296,98 +296,121 @@
 QString XmlElementCollection::addElement(ElementsLocation &location)
 {
 		//location must be an element and exist
-	if (!(location.exist() && location.isElement())) return QString();
+	if (!(location.exist() && location.isElement()))
+		return QString();
+
 		//Add an element from this collection to this collection have no sense
-	if (location.isProject() && location.projectCollection() == this) return QString();
+	if (location.isProject() && location.projectCollection() == this)
+		return QString();
 
 		//First we check if this location exist in this collection if so, we do nothing
 	if ( exist("import/" + location.collectionPath(false)) )
 		return QString();
 
-		//Get the root dir of the filesystem collection
-	QDir dir(location.fileSystemPath().remove(location.collectionPath(false)));
-	if (location.isFileSystem() && !dir.exists()) return QString();
-
 		//Get the import dir of this collection
 	QDomElement parent_element = importCategory();
-	if (parent_element.isNull()) return QString();
+	if (parent_element.isNull())
+		return QString();
 
 	QString integrated_path = parent_element.attribute("name");
 
 		//Split the path
 	QStringList splitted_path = location.collectionPath(false).split("/");
-	if (splitted_path.isEmpty()) return QString();
+	if (splitted_path.isEmpty())
+		return QString();
 
-	foreach(QString str, splitted_path)
-	{
-		QDomElement child_element = child(parent_element, str);
+	if (location.isFileSystem()) {
+			//Get the root dir of the filesystem collection
+		QDir dir(location.fileSystemPath().remove(location.collectionPath(false)));
+		if (!dir.exists())
+			return QString();
 
-			//Child doesn't exist, we create it
-		if (child_element.isNull())
-		{
-			QDomElement created_child;
+		foreach(QString str, splitted_path) {
+			QDomElement child_element = child(parent_element, str);
 
-				//str is the path of an element, we integrate an element
-			if (str.endsWith(".elmt"))
-			{
-					//The location represent a file system element
-				if (location.isFileSystem())
-				{
+				//Child doesn't exist, we create it
+			if (child_element.isNull()) {
+				QDomElement created_child;
+
+					//str is the path of an element, we integrate an element
+				if (str.endsWith(".elmt")) {
 					QFile element_file(dir.filePath(str));
-					if (!element_file.exists()) return QString();
+					if (!element_file.exists())
+						return QString();
 
 					created_child = QETXML::fileSystemElementToXmlCollectionElement(m_dom_document, element_file);
 				}
-					//The location represent a xml collection element
-				else
-				{
-					created_child = m_dom_document.createElement("element");
-					created_child.setAttribute("name", str);
 
-					ElementsLocation element_location(integrated_path + str, location.project());
-					QDomElement imported_element = element_location.xml();
-					created_child.appendChild(imported_element.cloneNode());
+					//str is the path of a directory, we integrate a directory.
+				else {
+						//Dir doesn't exist.
+					if (!dir.cd(str))
+						return QString();
+
+					created_child = QETXML::fileSystemDirToXmlCollectionDir(m_dom_document, dir);
 				}
+
+				if(created_child.isNull())
+					return QString();
+
+				parent_element.appendChild(created_child);
+				parent_element = created_child;
 			}
+				//Child exist
+			else {
+				if (!dir.cd(str))
+					return QString();
 
-				//str is the path of a directory, we integrate a directory.
+				parent_element = child_element;
+			}
+
+			integrated_path.append("/"+str);
+		}
+	}
+	else if (location.isProject()) {
+		QString path;
+		foreach(QString str, splitted_path) {
+			if (path.isEmpty())
+				path = str;
 			else
-			{
-					//The location represent a file system directory
-				if (location.isFileSystem())
-				{
-						//Dir doesn't exist.
-					if (!dir.cd(str)) return QString();
+				path = path + "/" + str;
 
-					created_child = QETXML::fileSystemDirToXmlCollectionDir(m_dom_document, dir);
+			QDomElement child_element = child(parent_element, str);
+
+				//Child doesn't exist, we create it
+			if (child_element.isNull()) {
+				QDomElement created_child;
+
+					//str is the path of an element, we integrate an element
+				if (str.endsWith(".elmt")) {
+					created_child = m_dom_document.createElement("element");
+					created_child.setAttribute("name", str);
+
+					created_child.appendChild(location.xml().cloneNode(true));
 				}
-					//The location represent a xml collection directory
-				else
-				{
+
+					//str is the path of a directory, we integrate a directory.
+				else {
 					created_child = m_dom_document.createElement("category");
 					created_child.setAttribute("name", str);
 
-					ElementsLocation sub_dir_location(integrated_path + str, location.project());
+					ElementsLocation sub_dir_location(path, location.project());
 					QDomElement names_element = sub_dir_location.nameList().toXml(m_dom_document);
 					created_child.appendChild(names_element);
 				}
-			}
 
-			if(created_child.isNull()) return QString();
+				if(created_child.isNull())
+					return QString();
 
-			parent_element.appendChild(created_child);
-			parent_element = created_child;
-		}
-			//Child exist
-		else
-		{
-			if (location.isFileSystem())
-				if (!dir.cd(str)) return QString();
+				parent_element.appendChild(created_child);
+				parent_element = created_child;
+			}
+				//Child exist
+			else
+				parent_element = child_element;
 
-			parent_element = child_element;
+			integrated_path.append("/"+str);
 		}
-
-		integrated_path.append("/"+str);
 	}
 
 	emit elementAdded(integrated_path);

Modified: trunk/sources/ElementsCollection/xmlprojectelementcollectionitem.cpp
===================================================================
--- trunk/sources/ElementsCollection/xmlprojectelementcollectionitem.cpp	2016-08-16 13:43:46 UTC (rev 4628)
+++ trunk/sources/ElementsCollection/xmlprojectelementcollectionitem.cpp	2016-08-16 20:25:27 UTC (rev 4629)
@@ -129,35 +129,29 @@
 
 /**
  * @brief XmlProjectElementCollectionItem::addChildAtPath
- * Ask to this item item to add a child with collection name @collection_name
- * @param collection_name
+ * Ask to this item item to add a new child with collection name @collection_name
+ * (the child must exist in the xml element collection)
+ * @param collection_name : name of the child item to add.
  */
 void XmlProjectElementCollectionItem::addChildAtPath(const QString &collection_name)
 {
 	if (collection_name.isEmpty())
 		return;
 
-	QDomNodeList node_list;
-	if (collection_name.endsWith(".elmt"))
-		node_list = m_dom_element.elementsByTagName("element");
-	else
-		node_list = m_dom_element.elementsByTagName("category");
+	QString str (collection_name.endsWith(".elmt")? "element" : "category");
+	QDomElement child_element = m_dom_element.firstChildElement(str);
 
-	QDomElement child_element;
-	for(int i=0 ; i<node_list.count() ; i++)
-	{
-		QDomElement dom_elmt = node_list.at(i).toElement();
-		if (dom_elmt.attribute("name") == collection_name)
-		{
-			child_element = dom_elmt;
-			i = node_list.count();
+	while (!child_element.isNull()) {
+		if (child_element.attribute("name") == collection_name) {
+			XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem ();
+			insertRow(rowForInsertItem(collection_name), xpeci);
+			xpeci->setXmlElement(child_element, m_project);
+			xpeci->setUpData();
+			return;
 		}
+		else
+			child_element = child_element.nextSiblingElement(str);
 	}
-
-	XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem ();
-	insertRow(rowForInsertItem(collection_name), xpeci);
-	xpeci->setXmlElement(child_element, m_project);
-	xpeci->setUpData();
 }
 
 /**


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