[qet] [4388] Enable the drag & drop of an element from the element panel ( an embedded element of a project) to the folio of another project.

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


Revision: 4388
Author:   blacksun
Date:     2016-03-19 15:18:00 +0100 (Sat, 19 Mar 2016)
Log Message:
-----------
Enable the drag & drop of an element from the element panel (an embedded element of a project) to the folio of another project.

Modified Paths:
--------------
    trunk/sources/ElementsCollection/elementslocation.cpp
    trunk/sources/ElementsCollection/elementslocation.h
    trunk/sources/ElementsCollection/xmlelementcollection.cpp
    trunk/sources/ElementsCollection/xmlelementcollection.h
    trunk/sources/qetproject.cpp

Modified: trunk/sources/ElementsCollection/elementslocation.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementslocation.cpp	2016-03-19 10:16:28 UTC (rev 4387)
+++ trunk/sources/ElementsCollection/elementslocation.cpp	2016-03-19 14:18:00 UTC (rev 4388)
@@ -186,12 +186,10 @@
 /**
  * @brief ElementsLocation::setPath
  * Set the path of this item.
- * If the path is for a file collection, the path can be in file system or relative to the beginning
- * of the colection, in this case the path must start with (common:// or custom://).
+ * The path can be relative to a collection (start by common:// , custom:// or embed://) or not.
  * @param path
- * @return true if the element pointed by path exist, else false
  */
-bool ElementsLocation::setPath(const QString &path)
+void ElementsLocation::setPath(const QString &path)
 {
 		QString tmp_path = path;
 #ifdef Q_OS_WIN32
@@ -203,13 +201,11 @@
 		//There is a project, the path is for an embedded coolection.
 	if (m_project)
 	{
-		if (path.startsWith("embed://"))
-		{
-			m_collection_path = path;
-			return true;
-		}
-		else
-			return false;
+		m_collection_path = path;
+			//Add the protocol to the collection path
+		if (!path.startsWith("embed://"))
+			m_collection_path.prepend("embed://");
+
 	}
 
 		//The path start with project, we get the project and the path from the string
@@ -227,11 +223,9 @@
 				{
 					m_collection_path = rx.capturedTexts().at(2);
 					m_project = project;
-					return true;
 				}
 			}
 		}
-		return false;
 	}
 
 		//The path is in file system, the given path is relative to common or custom collection
@@ -257,9 +251,7 @@
 			{
 				m_file_system_path = p;
 				m_collection_path = path;
-				return true;
 			}
-			return false;
 		}
 			//They must be a directory
 		else
@@ -269,9 +261,7 @@
 			{
 				m_file_system_path = p;
 				m_collection_path = path;
-				return true;
 			}
-			return false;
 		}
 	}
 		//In this case, the path is supposed to be relative to the file system.
@@ -293,7 +283,6 @@
 				path_.prepend("custom://");
 				m_collection_path = path_;
 			}
-			return true;
 		}
 		else
 		{
@@ -310,11 +299,8 @@
 				path_.prepend("custom://");
 				m_collection_path = path_;
 			}
-			return true;
 		}
 	}
-
-	return false;
 }
 
 /**
@@ -467,7 +453,9 @@
 bool ElementsLocation::exist() const
 {
 	if (m_project)
+	{
 		return m_project->embeddedElementCollection()->exist(collectionPath(false));
+	}
 	else
 	{
 		if (fileSystemPath().isEmpty()) return false;

Modified: trunk/sources/ElementsCollection/elementslocation.h
===================================================================
--- trunk/sources/ElementsCollection/elementslocation.h	2016-03-19 10:16:28 UTC (rev 4387)
+++ trunk/sources/ElementsCollection/elementslocation.h	2016-03-19 14:18:00 UTC (rev 4388)
@@ -49,7 +49,7 @@
 		QString projectCollectionPath() const;
 		QString fileSystemPath() const;
 		QString path() const;
-		bool setPath(const QString &path);
+		void setPath(const QString &path);
 		bool addToPath(const QString &);
 
 		ElementsLocation parent() const;

Modified: trunk/sources/ElementsCollection/xmlelementcollection.cpp
===================================================================
--- trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-03-19 10:16:28 UTC (rev 4387)
+++ trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-03-19 14:18:00 UTC (rev 4388)
@@ -79,7 +79,7 @@
 
 /**
  * @brief XmlElementCollection::root
- * The root is the beginning of the xml collection, the tag name
+ * The root is the first DOM-Element the xml collection, the tag name
  * of the dom element is : collection
  * @return The root QDomElement of the collection
  */
@@ -109,7 +109,7 @@
 
 /**
  * @brief XmlElementCollection::child
- * If parent_element have child element with name "child_name", return it, else return a null QDomElement.
+ * If parent_element have child element with an attribute name = @child_name, return it, else return a null QDomElement.
  * Only search for element with tag-name "category" and "element" (if child_name end with ".elmt")
  * @param parent_element : the parent DomElement where we search for child.
  * @parent_element must be a child node of this XmlElementCollection.
@@ -118,7 +118,7 @@
  */
 QDomElement XmlElementCollection::child(const QDomElement &parent_element, const QString &child_name) const
 {
-	if (parent_element.ownerDocument() != m_dom_document && parent_element.tagName() != "category") return QDomElement();
+	if (parent_element.ownerDocument() != m_dom_document) return QDomElement();
 
 		//Get all childs element of parent_element
 	QDomNodeList child_list = parent_element.childNodes();
@@ -314,75 +314,113 @@
 
 /**
  * @brief XmlElementCollection::addElement
- * Add the element at path @path to this xml collection.
- * The path must be a common or custom collection (a file system element).
- * The element is copied in this collection with the same path of @path, in other word,
- * if the path is dir1/dir2/dir3/myElement.elmt, myElement is copied to this collection at the path : dir1/dir2/dir3/myElement.elmt
+ * Add the element at location to this collection.
+ * The element is copied in this collection in "import" dir with the same path, in other word
+ * if the path is dir1/dir2/dir3/myElement.elmt, myElement is copied to this collection at the path : import/dir1/dir2/dir3/myElement.elmt
  * If the path doesn't exist, he was created.
- * @param path, path of the element
- * @return the xml collection path of the added item or a null QString if element can't be added.
+ * If the element already exist, do nothing.
+ * @param location, location of the element
+ * @return the collection path of the added item or a null QString if element can't be added.
  */
-QString XmlElementCollection::addElement(const QString &path)
+QString XmlElementCollection::addElement(ElementsLocation &location)
 {
-	ElementsLocation location(path);
-	if (!location.isElement() || location.fileSystemPath().isEmpty()) return QString();
-	if (exist(QString("import/" + location.collectionPath(false)))) return QString();
+		//location must be an element and exist
+	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();
 
+		//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 (!dir.exists()) return QString();
+	if (location.isFileSystem() && !dir.exists()) return QString();
 
+		//Get the import dir of this collection
 	QDomElement parent_element = importCategory();
 	if (parent_element.isNull()) return QString();
 
-	QStringList str_list = location.collectionPath(false).split("/");
-	if (str_list.isEmpty()) return QString();
+	QString integrated_path = parent_element.attribute("name");
 
-	QString collection_path(parent_element.attribute("name"));
+		//Split the path
+	QStringList splitted_path = location.collectionPath(false).split("/");
+	if (splitted_path.isEmpty()) return QString();
 
-	foreach(QString str, str_list)
+	foreach(QString str, splitted_path)
 	{
 		QDomElement child_element = child(parent_element, str);
 
-			//Child doesn't exist
+			//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();
+					//The location represent a file system element
+				if (location.isFileSystem())
+				{
+					QFile element_file(dir.filePath(str));
+					if (!element_file.exists()) return QString();
 
-				QDomElement element_dom = QETXML::fileSystemElementToXmlCollectionElement(m_dom_document, element_file);
-				if (element_dom.isNull()) 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);
 
-				parent_element.appendChild(element_dom);
-				parent_element = element_dom;
+					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();
-				QDomElement dir_element = QETXML::fileSystemDirToXmlCollectionDir(m_dom_document, dir);
-					//Creation of a xml collection dir failed
-				if (dir_element.isNull()) return QString();
+					//The location represent a file system directory
+				if (location.isFileSystem())
+				{
+						//Dir doesn't exist.
+					if (!dir.cd(str)) return QString();
 
-				parent_element.appendChild(dir_element);
-				parent_element = dir_element;
+					created_child = QETXML::fileSystemDirToXmlCollectionDir(m_dom_document, dir);
+				}
+					//The location represent a xml collection directory
+				else
+				{
+					created_child = m_dom_document.createElement("category");
+					created_child.setAttribute("name", str);
+
+					ElementsLocation sub_dir_location(integrated_path + str, location.project());
+					QDomElement names_element = sub_dir_location.nameList().toXml(m_dom_document);
+					created_child.appendChild(names_element);
+				}
 			}
+
+			if(created_child.isNull()) return QString();
+
+			parent_element.appendChild(created_child);
+			parent_element = created_child;
 		}
 			//Child exist
 		else
 		{
-			if (!dir.cd(str)) return QString();
+			if (location.isFileSystem())
+				if (!dir.cd(str)) return QString();
+
 			parent_element = child_element;
 		}
 
-		collection_path.append("/"+str);
+		integrated_path.append("/"+str);
 	}
 
-	emit elementAdded(collection_path);
-	return collection_path;
+	emit elementAdded(integrated_path);
+	return integrated_path;
 }
 
 /**
@@ -417,14 +455,11 @@
 	QStringList str_list = path.split("/");
 	if (str_list.isEmpty()) return false;
 
-		//The first category of a XmlElementCollection is always "import"
-	if (str_list.first() != "import") return false;
-	str_list.removeFirst();
-
-	QDomElement parent_element = importCategory();
+	QDomElement parent_element = root();
 	foreach (QString str, str_list)
 	{
 		QDomElement child_element = child(parent_element, str);
+
 		if (child_element.isNull())
 			return false;
 		else

Modified: trunk/sources/ElementsCollection/xmlelementcollection.h
===================================================================
--- trunk/sources/ElementsCollection/xmlelementcollection.h	2016-03-19 10:16:28 UTC (rev 4387)
+++ trunk/sources/ElementsCollection/xmlelementcollection.h	2016-03-19 14:18:00 UTC (rev 4388)
@@ -46,7 +46,7 @@
 		QStringList elementsNames(const QDomElement &parent_element);
 		QDomElement element(const QString &path);
 		QDomElement directory(const QString &path);
-		QString addElement (const QString &path);
+		QString addElement (ElementsLocation &location);
 		ElementsLocation copy (ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true);
 		bool exist (const QString &path);
 

Modified: trunk/sources/qetproject.cpp
===================================================================
--- trunk/sources/qetproject.cpp	2016-03-19 10:16:28 UTC (rev 4387)
+++ trunk/sources/qetproject.cpp	2016-03-19 14:18:00 UTC (rev 4388)
@@ -789,7 +789,9 @@
 	{
 		// integre l'element normalement
 		result = copyElementWithHandler(integ_elmt, target_cat, handler, error_message);
-		QString xml_path = m_elements_collection->addElement(elmt_path);
+
+		ElementsLocation location(elmt_path);
+		QString xml_path = m_elements_collection->addElement(location);
 		if (!xml_path.isNull()) emit elementIntegratedToCollection(this, xml_path);
 	}
 	


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