mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 20:50:34 +01:00
New element panel : drag and drop an item from a project collection to a files system collection work.
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4375 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -22,6 +22,8 @@
|
|||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
|
||||||
|
/******************************************************/
|
||||||
|
|
||||||
ECHStrategy::ECHStrategy(ElementLocation &source, ElementLocation &destination) :
|
ECHStrategy::ECHStrategy(ElementLocation &source, ElementLocation &destination) :
|
||||||
m_source(source),
|
m_source(source),
|
||||||
m_destination (destination)
|
m_destination (destination)
|
||||||
@@ -125,6 +127,115 @@ ElementLocation ECHSFileToFile::copyElement(ElementLocation &source, ElementLoca
|
|||||||
|
|
||||||
/******************************************************/
|
/******************************************************/
|
||||||
|
|
||||||
|
ECHSXmlToFile::ECHSXmlToFile(ElementLocation &source, ElementLocation &destination) :
|
||||||
|
ECHStrategy(source, destination)
|
||||||
|
{}
|
||||||
|
|
||||||
|
ElementLocation ECHSXmlToFile::copy()
|
||||||
|
{
|
||||||
|
//Check if the destination already have an item with the same name of the item to copy
|
||||||
|
ElementLocation location(m_destination.fileSystemPath() + "/" + m_source.fileName());
|
||||||
|
QString rename;
|
||||||
|
if (location.exist())
|
||||||
|
{
|
||||||
|
RenameDialog rd(location.fileSystemPath());
|
||||||
|
if (rd.exec() == QDialog::Accepted)
|
||||||
|
{
|
||||||
|
if (rd.selectedAction() == QET::Erase)
|
||||||
|
{
|
||||||
|
if (location.isDirectory())
|
||||||
|
{
|
||||||
|
QDir dir(location.fileSystemPath());
|
||||||
|
dir.removeRecursively();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QFile file(location.fileSystemPath());
|
||||||
|
file.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (rd.selectedAction() == QET::Rename)
|
||||||
|
{
|
||||||
|
rename = rd.newName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return ElementLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_source.isElement())
|
||||||
|
return copyElement(m_source, m_destination, rename);
|
||||||
|
else
|
||||||
|
return copyDirectory(m_source, m_destination, rename);
|
||||||
|
}
|
||||||
|
|
||||||
|
ElementLocation ECHSXmlToFile::copyDirectory(ElementLocation &source, ElementLocation &destination, QString rename)
|
||||||
|
{
|
||||||
|
QDir destination_dir(destination.fileSystemPath());
|
||||||
|
|
||||||
|
if (!(destination_dir.exists() && source.exist())) return ElementLocation();
|
||||||
|
|
||||||
|
QString new_dir_name = rename.isEmpty() ? source.fileName() : rename;
|
||||||
|
|
||||||
|
//Create new dir
|
||||||
|
if (destination_dir.mkdir(new_dir_name))
|
||||||
|
{
|
||||||
|
QDir created_dir(destination_dir.canonicalPath() + "/" + new_dir_name);
|
||||||
|
ElementLocation created_location(created_dir.canonicalPath());
|
||||||
|
|
||||||
|
//Create the qet-directory file
|
||||||
|
QDomDocument document;
|
||||||
|
QDomElement root = document.createElement("qet-directory");
|
||||||
|
document.appendChild(root);
|
||||||
|
root.appendChild(source.nameList().toXml(document));
|
||||||
|
|
||||||
|
QString filepath = created_dir.canonicalPath() + "/qet_directory";
|
||||||
|
QET::writeXmlFile(document, filepath);
|
||||||
|
|
||||||
|
//Create all directory found in source to created_dir
|
||||||
|
XmlElementCollection *project_collection = source.projectCollection();
|
||||||
|
|
||||||
|
QStringList directories_names = project_collection->directoriesNames( project_collection->directory(source.collectionPath(false)) );
|
||||||
|
foreach(QString name, directories_names)
|
||||||
|
{
|
||||||
|
ElementLocation sub_source_dir(source.projectCollectionPath() + "/" + name);
|
||||||
|
copyDirectory(sub_source_dir, created_location);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create all elements found in source to destination
|
||||||
|
QStringList elements_names = project_collection->elementsNames( project_collection->directory(source.collectionPath(false))) ;
|
||||||
|
foreach (QString name, elements_names)
|
||||||
|
{
|
||||||
|
ElementLocation source_element(source.projectCollectionPath() + "/" + name);
|
||||||
|
copyElement(source_element, created_location);
|
||||||
|
}
|
||||||
|
|
||||||
|
return created_location;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ElementLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
ElementLocation ECHSXmlToFile::copyElement(ElementLocation &source, ElementLocation &destination, QString rename)
|
||||||
|
{
|
||||||
|
if (!(destination.exist() && source.exist())) return ElementLocation();
|
||||||
|
|
||||||
|
QString new_element_name = rename.isEmpty() ? source.fileName() : rename;
|
||||||
|
|
||||||
|
//Get the xml descrption of the element
|
||||||
|
QDomDocument document;
|
||||||
|
document.appendChild(document.importNode(source.xml(), true));
|
||||||
|
|
||||||
|
//Create the .elmt file
|
||||||
|
QString filepath = destination.fileSystemPath() + "/" + new_element_name;
|
||||||
|
if (QET::writeXmlFile(document, filepath))
|
||||||
|
return ElementLocation(filepath);
|
||||||
|
else
|
||||||
|
return ElementLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************/
|
||||||
|
|
||||||
ECHSToXml::ECHSToXml(ElementLocation &source, ElementLocation &destination) :
|
ECHSToXml::ECHSToXml(ElementLocation &source, ElementLocation &destination) :
|
||||||
ECHStrategy(source, destination)
|
ECHStrategy(source, destination)
|
||||||
{}
|
{}
|
||||||
@@ -152,6 +263,8 @@ ElementLocation ECHSToXml::copy()
|
|||||||
return m_destination.projectCollection()->copy(m_source, m_destination, rename);
|
return m_destination.projectCollection()->copy(m_source, m_destination, rename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/******************************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief ElementCollectionHandler::ElementCollectionHandler
|
* @brief ElementCollectionHandler::ElementCollectionHandler
|
||||||
* @param widget
|
* @param widget
|
||||||
@@ -177,6 +290,7 @@ ElementLocation ElementCollectionHandler::copy(ElementLocation &source, ElementL
|
|||||||
if (!source.exist() || !destination.exist() || destination.isElement()) return ElementLocation();
|
if (!source.exist() || !destination.exist() || destination.isElement()) return ElementLocation();
|
||||||
|
|
||||||
if (source.isFileSystem() && destination.isFileSystem()) m_strategy = new ECHSFileToFile(source, destination);
|
if (source.isFileSystem() && destination.isFileSystem()) m_strategy = new ECHSFileToFile(source, destination);
|
||||||
|
if (source.isProject() && destination.isFileSystem()) m_strategy = new ECHSXmlToFile(source, destination);
|
||||||
else if (destination.isProject()) m_strategy = new ECHSToXml(source, destination);
|
else if (destination.isProject()) m_strategy = new ECHSToXml(source, destination);
|
||||||
|
|
||||||
if (m_strategy)
|
if (m_strategy)
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ class ECHStrategy
|
|||||||
/**
|
/**
|
||||||
* @brief The ECHSFileToFile class
|
* @brief The ECHSFileToFile class
|
||||||
* Manage the copy of directory or element from a file system collection to another file system collection
|
* Manage the copy of directory or element from a file system collection to another file system collection
|
||||||
* (Work only if the source is the common collection and the destination is the custom collection)
|
|
||||||
*/
|
*/
|
||||||
class ECHSFileToFile : public ECHStrategy
|
class ECHSFileToFile : public ECHStrategy
|
||||||
{
|
{
|
||||||
@@ -52,6 +51,21 @@ class ECHSFileToFile : public ECHStrategy
|
|||||||
ElementLocation copyElement(ElementLocation &source, ElementLocation &destination, QString rename = QString());
|
ElementLocation copyElement(ElementLocation &source, ElementLocation &destination, QString rename = QString());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The ECHSXmlToFile class
|
||||||
|
* Manage the copy of a directory or element from an xml collection to a file.
|
||||||
|
*/
|
||||||
|
class ECHSXmlToFile : public ECHStrategy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ECHSXmlToFile (ElementLocation &source, ElementLocation &destination);
|
||||||
|
ElementLocation copy();
|
||||||
|
|
||||||
|
private:
|
||||||
|
ElementLocation copyDirectory(ElementLocation &source, ElementLocation &destination, QString rename = QString());
|
||||||
|
ElementLocation copyElement(ElementLocation &source, ElementLocation &destination, QString rename = QString());
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The ECHSToXml class
|
* @brief The ECHSToXml class
|
||||||
* Manage the copy of a directory or element from a collection (no matter if the source is a file system collection or an xml collection)
|
* Manage the copy of a directory or element from a collection (no matter if the source is a file system collection or an xml collection)
|
||||||
|
|||||||
@@ -177,6 +177,26 @@ QList<QDomElement> XmlElementCollection::directories(const QDomElement &parent_e
|
|||||||
return directory_list;
|
return directory_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief XmlElementCollection::directoriesNames
|
||||||
|
* @param parent_element
|
||||||
|
* @return a list of names for every child directories of @parent_element
|
||||||
|
*/
|
||||||
|
QStringList XmlElementCollection::directoriesNames(const QDomElement &parent_element)
|
||||||
|
{
|
||||||
|
QList <QDomElement> childs = directories(parent_element);
|
||||||
|
QStringList names;
|
||||||
|
|
||||||
|
foreach (QDomElement child, childs)
|
||||||
|
{
|
||||||
|
QString name = child.attribute("name");
|
||||||
|
if (!name.isEmpty())
|
||||||
|
names.append(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief XmlElementCollection::elements
|
* @brief XmlElementCollection::elements
|
||||||
* @param parent_element
|
* @param parent_element
|
||||||
@@ -198,6 +218,26 @@ QList<QDomElement> XmlElementCollection::elements(const QDomElement &parent_elem
|
|||||||
return element_list;
|
return element_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief XmlElementCollection::elementsNames
|
||||||
|
* @param parent_element
|
||||||
|
* @return A list of names fr every childs element of @parent_element
|
||||||
|
*/
|
||||||
|
QStringList XmlElementCollection::elementsNames(const QDomElement &parent_element)
|
||||||
|
{
|
||||||
|
QList <QDomElement> childs = elements(parent_element);
|
||||||
|
QStringList names;
|
||||||
|
|
||||||
|
foreach (QDomElement child, childs)
|
||||||
|
{
|
||||||
|
QString name = child.attribute("name");
|
||||||
|
if (!name.isEmpty())
|
||||||
|
names.append(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief XmlElementCollection::element
|
* @brief XmlElementCollection::element
|
||||||
* @param path : path of element : the path must start by "import/",
|
* @param path : path of element : the path must start by "import/",
|
||||||
|
|||||||
@@ -41,7 +41,9 @@ class XmlElementCollection : public QObject
|
|||||||
QDomElement child(const QDomElement &parent_element, const QString &child_name) const;
|
QDomElement child(const QDomElement &parent_element, const QString &child_name) const;
|
||||||
QDomElement child(const QString &path) const;
|
QDomElement child(const QString &path) const;
|
||||||
QList<QDomElement> directories(const QDomElement &parent_element);
|
QList<QDomElement> directories(const QDomElement &parent_element);
|
||||||
|
QStringList directoriesNames(const QDomElement &parent_element);
|
||||||
QList<QDomElement> elements(const QDomElement &parent_element);
|
QList<QDomElement> elements(const QDomElement &parent_element);
|
||||||
|
QStringList elementsNames(const QDomElement &parent_element);
|
||||||
QDomElement element(const QString &path);
|
QDomElement element(const QString &path);
|
||||||
QDomElement directory(const QString &path);
|
QDomElement directory(const QString &path);
|
||||||
QString addElement (const QString &path);
|
QString addElement (const QString &path);
|
||||||
|
|||||||
Reference in New Issue
Block a user