[qet] [4581] Allow copy of elements from a project to another with the actions copy/ paste

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


Revision: 4581
Author:   blacksun
Date:     2016-07-18 19:37:02 +0200 (Mon, 18 Jul 2016)
Log Message:
-----------
Allow copy of elements from a project to another with the actions copy/paste

Modified Paths:
--------------
    trunk/sources/ElementsCollection/elementcollectionhandler.cpp
    trunk/sources/ElementsCollection/elementcollectionhandler.h
    trunk/sources/ElementsCollection/xmlelementcollection.cpp
    trunk/sources/diagram.cpp

Modified: trunk/sources/ElementsCollection/elementcollectionhandler.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementcollectionhandler.cpp	2016-07-18 15:23:30 UTC (rev 4580)
+++ trunk/sources/ElementsCollection/elementcollectionhandler.cpp	2016-07-18 17:37:02 UTC (rev 4581)
@@ -18,6 +18,7 @@
 #include "elementcollectionhandler.h"
 #include "renamedialog.h"
 #include "xmlelementcollection.h"
+#include "qetproject.h"
 #include "qetxml.h"
 #include <QFile>
 #include <QDir>
@@ -361,6 +362,49 @@
 }
 
 /**
+ * @brief ElementCollectionHandler::importFromProject
+ * Import the element represented by @location to the embedded collection of @project at the same path.
+ * @location must represente an element owned by a project embedded collection
+ * @param project : project where copy the element
+ * @param location : location to copy
+ * @return true if import with success
+ */
+bool ElementCollectionHandler::importFromProject(QETProject *project, ElementsLocation &location)
+{
+	if (!(location.isElement() && location.exist() && location.isProject())) return false;
+
+	ElementsLocation destination(location.collectionPath(false), project);
+	if (destination.exist()) return true;
+
+	QList <QString> names;
+
+		//Get the parent of location and find if exist in embedded collection of project
+	ElementsLocation source = location.parent();
+	names.append(location.fileName());
+
+	destination = ElementsLocation(source.collectionPath(), project);
+
+		//Go back until to find an existing directory in destination
+	while (!destination.exist()) {
+		names.append(source.fileName());
+		source = source.parent();
+		destination = ElementsLocation(source.collectionPath(), project);
+	}
+
+	XmlElementCollection *collection = project->embeddedElementCollection();
+
+	while (!names.isEmpty()) {
+		source.addToPath(names.takeLast());
+		destination = collection->copy(source, destination, QString(), false);
+
+		if (!destination.exist())
+			return false;
+	}
+
+	return true;
+}
+
+/**
  * @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

Modified: trunk/sources/ElementsCollection/elementcollectionhandler.h
===================================================================
--- trunk/sources/ElementsCollection/elementcollectionhandler.h	2016-07-18 15:23:30 UTC (rev 4580)
+++ trunk/sources/ElementsCollection/elementcollectionhandler.h	2016-07-18 17:37:02 UTC (rev 4581)
@@ -92,6 +92,7 @@
 
 		ElementsLocation copy(ElementsLocation &source, ElementsLocation &destination);
 		ElementsLocation createDir(ElementsLocation &parent, const QString &name, const NamesList &name_list);
+		bool importFromProject (QETProject *project, ElementsLocation &location);
 		bool setNames(ElementsLocation &location, const NamesList &name_list);
 
     private:

Modified: trunk/sources/ElementsCollection/xmlelementcollection.cpp
===================================================================
--- trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-07-18 15:23:30 UTC (rev 4580)
+++ trunk/sources/ElementsCollection/xmlelementcollection.cpp	2016-07-18 17:37:02 UTC (rev 4581)
@@ -657,11 +657,11 @@
 
 		//Remove the previous directory with the same path
 	QDomElement element = child(destination.collectionPath(false) + "/" + new_dir_name);
-	if (!element.isNull())
+	if (!element.isNull()) {
 		element.parentNode().removeChild(element);
+		emit directoryRemoved(destination.collectionPath(false) + "/" + new_dir_name);
+	}
 
-
-
 	ElementsLocation created_location;
 
 		//Copy with a file system collection source
@@ -703,7 +703,15 @@
 	{
 		if (!source.projectCollection()) return ElementsLocation();
 
-		QDomNode other_collection_node = source.projectCollection()->child(source.collectionPath(false)).cloneNode(true);
+		QDomNode other_collection_node = source.projectCollection()->child(source.collectionPath(false)).cloneNode(deep_copy);
+
+			//We don't make a deep copy, but we must to get the local names of the copied directory
+		if (!deep_copy) {
+			QDomNode names = source.projectCollection()->child(source.collectionPath(false)).namedItem("names");
+			if (!names.isNull() && names.isElement())
+				other_collection_node.appendChild(names.cloneNode(true));
+		}
+
 		if (other_collection_node.isNull()) return ElementsLocation();
 
 		QDomElement other_collection_dom_dir = other_collection_node.toElement();
@@ -713,6 +721,7 @@
 		created_location.setPath(destination.projectCollectionPath() + "/" + new_dir_name);
 	}
 
+	emit directorieAdded(created_location.collectionPath(false));
 	return created_location;
 }
 

Modified: trunk/sources/diagram.cpp
===================================================================
--- trunk/sources/diagram.cpp	2016-07-18 15:23:30 UTC (rev 4580)
+++ trunk/sources/diagram.cpp	2016-07-18 17:37:02 UTC (rev 4581)
@@ -34,6 +34,8 @@
 #include "terminal.h"
 #include "elementtextsmover.h"
 #include "diagrameventinterface.h"
+#include "qetapp.h"
+#include "elementcollectionhandler.h"
 
 const int   Diagram::xGrid  = 10;
 const int   Diagram::yGrid  = 10;
@@ -466,6 +468,12 @@
 			racine.setAttribute("conductorAutonum", m_conductors_autonum_name);
 		}
 	}
+	else {
+			//this method with whole_content to false,
+			//is often use to copy and paste the current selection
+			//so we add the id of the project where copy occur.
+		racine.setAttribute("projectId", QETApp::projectId(m_project));
+	}
 	document.appendChild(racine);
 	
 	// si le schema ne contient pas d'element (et donc pas de conducteurs), on retourne de suite le document XML
@@ -659,7 +667,28 @@
 		project_qet_version != -1 && project_qet_version < 0.3 &&
 		m_project -> state() == QETProject::ProjectParsingRunning
 	);
-	
+
+		//If paste from another project
+	if (root.hasAttribute("projectId")) {
+		QETProject *other_project = QETApp::project(root.attribute("projectId", "-1").toInt());
+
+			//We try to paste from another project, then befor paste elements,
+			//we must to import the definition of the pasted elements (owned by other project)
+			//in the embedded collection of this project
+		if (other_project && other_project != m_project) {
+			ElementCollectionHandler ech;
+			foreach (QDomElement element_xml, QET::findInDomElement(root, "elements", "element")) {
+				if (!Element::valideXml(element_xml)) continue;
+
+				QString type_id = element_xml.attribute("type");
+
+				if (type_id.startsWith("embed://")) {
+					ElementsLocation location(type_id, other_project);
+					ech.importFromProject(m_project, location);
+				}
+			}
+		}
+	}
 		//Load all elements from the XML
 	QList<Element *> added_elements;
 	QHash<int, Terminal *> table_adr_id;


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