Remove elementLocation and use elementsLocation instead

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4377 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2016-03-15 15:23:11 +00:00
parent 764d50c4ac
commit 1170522d9c
28 changed files with 626 additions and 924 deletions

View File

@@ -24,7 +24,7 @@
/******************************************************/
ECHStrategy::ECHStrategy(ElementLocation &source, ElementLocation &destination) :
ECHStrategy::ECHStrategy(ElementsLocation &source, ElementsLocation &destination) :
m_source(source),
m_destination (destination)
{}
@@ -33,14 +33,14 @@ ECHStrategy::~ECHStrategy() {}
/******************************************************/
ECHSFileToFile::ECHSFileToFile(ElementLocation &source, ElementLocation &destination) :
ECHSFileToFile::ECHSFileToFile(ElementsLocation &source, ElementsLocation &destination) :
ECHStrategy(source, destination)
{}
ElementLocation ECHSFileToFile::copy()
ElementsLocation ECHSFileToFile::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());
ElementsLocation location(m_destination.fileSystemPath() + "/" + m_source.fileName());
QString rename;
if (location.exist())
{
@@ -66,7 +66,7 @@ ElementLocation ECHSFileToFile::copy()
}
}
else
return ElementLocation();
return ElementsLocation();
}
if (m_source.isElement())
@@ -75,12 +75,12 @@ ElementLocation ECHSFileToFile::copy()
return copyDirectory(m_source, m_destination, rename);
}
ElementLocation ECHSFileToFile::copyDirectory(ElementLocation &source, ElementLocation &destination, QString rename)
ElementsLocation ECHSFileToFile::copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename)
{
QDir source_dir(source.fileSystemPath());
QDir destination_dir(destination.fileSystemPath());
if (!source_dir.exists() || !destination_dir.exists()) return ElementLocation();
if (!source_dir.exists() || !destination_dir.exists()) return ElementsLocation();
QString new_dir_name = rename.isEmpty() ? source_dir.dirName() : rename;
@@ -94,10 +94,10 @@ ElementLocation ECHSFileToFile::copyDirectory(ElementLocation &source, ElementLo
QFile::copy(source_dir.canonicalPath() + "/qet_directory", created_dir.canonicalPath() + "/qet_directory");
//Copy all dirs found in source_dir to destination_dir
ElementLocation created_location(created_dir.canonicalPath());
ElementsLocation created_location(created_dir.canonicalPath());
foreach(QString str, source_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
{
ElementLocation sub_source(source.fileSystemPath() + "/" + str);
ElementsLocation sub_source(source.fileSystemPath() + "/" + str);
copyDirectory(sub_source, created_location);
}
@@ -105,36 +105,36 @@ ElementLocation ECHSFileToFile::copyDirectory(ElementLocation &source, ElementLo
source_dir.setNameFilters(QStringList() << "*.elmt");
foreach(QString str, source_dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
{
ElementLocation sub_source(source.fileSystemPath() + "/" + str);
ElementsLocation sub_source(source.fileSystemPath() + "/" + str);
copyElement(sub_source, created_location);
}
return created_location;
}
return ElementLocation();
return ElementsLocation();
}
ElementLocation ECHSFileToFile::copyElement(ElementLocation &source, ElementLocation &destination, QString rename)
ElementsLocation ECHSFileToFile::copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename)
{
QString new_elmt_name = rename.isEmpty() ? source.fileName() : rename;
bool rb = QFile::copy(source.fileSystemPath(), destination.fileSystemPath() + "/" + new_elmt_name);
if (rb)
return ElementLocation (destination.fileSystemPath() + "/" + new_elmt_name);
return ElementsLocation (destination.fileSystemPath() + "/" + new_elmt_name);
else
return ElementLocation();
return ElementsLocation();
}
/******************************************************/
ECHSXmlToFile::ECHSXmlToFile(ElementLocation &source, ElementLocation &destination) :
ECHSXmlToFile::ECHSXmlToFile(ElementsLocation &source, ElementsLocation &destination) :
ECHStrategy(source, destination)
{}
ElementLocation ECHSXmlToFile::copy()
ElementsLocation 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());
ElementsLocation location(m_destination.fileSystemPath() + "/" + m_source.fileName());
QString rename;
if (location.exist())
{
@@ -160,7 +160,7 @@ ElementLocation ECHSXmlToFile::copy()
}
}
else
return ElementLocation();
return ElementsLocation();
}
if (m_source.isElement())
@@ -169,11 +169,11 @@ ElementLocation ECHSXmlToFile::copy()
return copyDirectory(m_source, m_destination, rename);
}
ElementLocation ECHSXmlToFile::copyDirectory(ElementLocation &source, ElementLocation &destination, QString rename)
ElementsLocation ECHSXmlToFile::copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename)
{
QDir destination_dir(destination.fileSystemPath());
if (!(destination_dir.exists() && source.exist())) return ElementLocation();
if (!(destination_dir.exists() && source.exist())) return ElementsLocation();
QString new_dir_name = rename.isEmpty() ? source.fileName() : rename;
@@ -181,7 +181,7 @@ ElementLocation ECHSXmlToFile::copyDirectory(ElementLocation &source, ElementLoc
if (destination_dir.mkdir(new_dir_name))
{
QDir created_dir(destination_dir.canonicalPath() + "/" + new_dir_name);
ElementLocation created_location(created_dir.canonicalPath());
ElementsLocation created_location(created_dir.canonicalPath());
//Create the qet-directory file
QDomDocument document;
@@ -198,7 +198,7 @@ ElementLocation ECHSXmlToFile::copyDirectory(ElementLocation &source, ElementLoc
QStringList directories_names = project_collection->directoriesNames( project_collection->directory(source.collectionPath(false)) );
foreach(QString name, directories_names)
{
ElementLocation sub_source_dir(source.projectCollectionPath() + "/" + name);
ElementsLocation sub_source_dir(source.projectCollectionPath() + "/" + name);
copyDirectory(sub_source_dir, created_location);
}
@@ -206,19 +206,19 @@ ElementLocation ECHSXmlToFile::copyDirectory(ElementLocation &source, ElementLoc
QStringList elements_names = project_collection->elementsNames( project_collection->directory(source.collectionPath(false))) ;
foreach (QString name, elements_names)
{
ElementLocation source_element(source.projectCollectionPath() + "/" + name);
ElementsLocation source_element(source.projectCollectionPath() + "/" + name);
copyElement(source_element, created_location);
}
return created_location;
}
return ElementLocation();
return ElementsLocation();
}
ElementLocation ECHSXmlToFile::copyElement(ElementLocation &source, ElementLocation &destination, QString rename)
ElementsLocation ECHSXmlToFile::copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename)
{
if (!(destination.exist() && source.exist())) return ElementLocation();
if (!(destination.exist() && source.exist())) return ElementsLocation();
QString new_element_name = rename.isEmpty() ? source.fileName() : rename;
@@ -229,23 +229,23 @@ ElementLocation ECHSXmlToFile::copyElement(ElementLocation &source, ElementLocat
//Create the .elmt file
QString filepath = destination.fileSystemPath() + "/" + new_element_name;
if (QET::writeXmlFile(document, filepath))
return ElementLocation(filepath);
return ElementsLocation(filepath);
else
return ElementLocation();
return ElementsLocation();
}
/******************************************************/
ECHSToXml::ECHSToXml(ElementLocation &source, ElementLocation &destination) :
ECHSToXml::ECHSToXml(ElementsLocation &source, ElementsLocation &destination) :
ECHStrategy(source, destination)
{}
ElementLocation ECHSToXml::copy()
ElementsLocation ECHSToXml::copy()
{
if (!(m_source.exist() && m_destination.isDirectory() && m_destination.isProject())) return ElementLocation();
if (!(m_source.exist() && m_destination.isDirectory() && m_destination.isProject())) return ElementsLocation();
//Check if the destination already have an item with the same name of the item to copy
ElementLocation location(m_destination.projectCollectionPath() + "/" + m_source.fileName());
ElementsLocation location(m_destination.projectCollectionPath() + "/" + m_source.fileName());
QString rename;
if (location.exist())
@@ -257,7 +257,7 @@ ElementLocation ECHSToXml::copy()
rename = rd.newName();
}
else
return ElementLocation();
return ElementsLocation();
}
return m_destination.projectCollection()->copy(m_source, m_destination, rename);
@@ -285,9 +285,9 @@ ElementCollectionHandler::~ElementCollectionHandler()
* @param destination
* @return
*/
ElementLocation ElementCollectionHandler::copy(ElementLocation &source, ElementLocation &destination)
ElementsLocation ElementCollectionHandler::copy(ElementsLocation &source, ElementsLocation &destination)
{
if (!source.exist() || !destination.exist() || destination.isElement()) return ElementLocation();
if (!source.exist() || !destination.exist() || destination.isElement()) return ElementsLocation();
if (source.isFileSystem() && destination.isFileSystem()) m_strategy = new ECHSFileToFile(source, destination);
if (source.isProject() && destination.isFileSystem()) m_strategy = new ECHSXmlToFile(source, destination);
@@ -296,5 +296,5 @@ ElementLocation ElementCollectionHandler::copy(ElementLocation &source, ElementL
if (m_strategy)
return m_strategy->copy();
else
return ElementLocation();
return ElementsLocation();
}

View File

@@ -18,7 +18,7 @@
#ifndef ELEMENTCOLLECTIONHANDLER_H
#define ELEMENTCOLLECTIONHANDLER_H
#include "elementlocation.h"
#include "elementslocation.h"
class QWidget;
@@ -29,11 +29,11 @@ class QWidget;
class ECHStrategy
{
public:
ECHStrategy(ElementLocation &source, ElementLocation &destination);
ECHStrategy(ElementsLocation &source, ElementsLocation &destination);
virtual ~ECHStrategy();
virtual ElementLocation copy() =0;
virtual ElementsLocation copy() =0;
ElementLocation m_source, m_destination;
ElementsLocation m_source, m_destination;
};
/**
@@ -43,12 +43,12 @@ class ECHStrategy
class ECHSFileToFile : public ECHStrategy
{
public:
ECHSFileToFile (ElementLocation &source, ElementLocation &destination);
ElementLocation copy();
ECHSFileToFile (ElementsLocation &source, ElementsLocation &destination);
ElementsLocation copy();
private:
ElementLocation copyDirectory(ElementLocation &source, ElementLocation &destination, QString rename = QString());
ElementLocation copyElement(ElementLocation &source, ElementLocation &destination, QString rename = QString());
ElementsLocation copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename = QString());
ElementsLocation copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename = QString());
};
/**
@@ -58,12 +58,12 @@ class ECHSFileToFile : public ECHStrategy
class ECHSXmlToFile : public ECHStrategy
{
public:
ECHSXmlToFile (ElementLocation &source, ElementLocation &destination);
ElementLocation copy();
ECHSXmlToFile (ElementsLocation &source, ElementsLocation &destination);
ElementsLocation copy();
private:
ElementLocation copyDirectory(ElementLocation &source, ElementLocation &destination, QString rename = QString());
ElementLocation copyElement(ElementLocation &source, ElementLocation &destination, QString rename = QString());
ElementsLocation copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename = QString());
ElementsLocation copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename = QString());
};
/**
@@ -74,8 +74,8 @@ class ECHSXmlToFile : public ECHStrategy
class ECHSToXml : public ECHStrategy
{
public:
ECHSToXml (ElementLocation &source, ElementLocation &destination);
ElementLocation copy();
ECHSToXml (ElementsLocation &source, ElementsLocation &destination);
ElementsLocation copy();
};
/**
@@ -89,7 +89,7 @@ class ElementCollectionHandler
ElementCollectionHandler();
~ElementCollectionHandler();
ElementLocation copy(ElementLocation &source, ElementLocation &destination);
ElementsLocation copy(ElementsLocation &source, ElementsLocation &destination);
private:
ECHStrategy *m_strategy = nullptr;

View File

@@ -19,7 +19,6 @@
#define ELEMENTCOLLECTIONITEM_H
#include <QVariant>
#include "elementlocation.h"
class QMimeData;
class ElementCollectionItem;

View File

@@ -1,504 +0,0 @@
/*
Copyright 2006-2015 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "elementlocation.h"
#include "qetapp.h"
#include "qetproject.h"
#include "elementscollectioncache.h"
#include "xmlelementcollection.h"
#include "elementfactory.h"
#include "element.h"
/**
* @brief ElementLocation::ElementLocation
* @param path : path of item in file system
*/
ElementLocation::ElementLocation(QString path)
{
if (!path.isEmpty())
setPath(path);
}
/**
* @brief ElementLocation::ElementLocation
* @param path : path of item embedded in @project
* The path must be in form : embed://dir/subdir/myElement.elmt
* @param project : project
*/
ElementLocation::ElementLocation(QString path, QETProject *project) :
m_project(project)
{
if (!path.isEmpty())
setPath(path);
}
/**
* @brief ElementLocation::ElementLocation
* Constructor, build an ElementLocation from a QMimeData, the mime data format
* must be "application/x-qet-element-uri" or "application/x-qet-category-uri".
* This location can be null even if format is valid.
* @param data
*/
ElementLocation::ElementLocation(const QMimeData *data)
{
if (data->hasFormat("application/x-qet-element-uri") || data->hasFormat("application/x-qet-category-uri"))
setPath(data->text());
}
ElementLocation &ElementLocation::operator=(const ElementLocation &other)
{
m_collection_path = other.m_collection_path;
m_file_system_path = other.m_file_system_path;
m_project = other.m_project;
m_xml = other.m_xml;
m_uuid = other.m_uuid;
m_icon = other.m_icon;
return(*this);
}
ElementLocation::~ElementLocation()
{}
/**
* @brief ElementLocation::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://).
* @param path
* @return true if the element pointed by path exist, else false
*/
bool ElementLocation::setPath(QString path)
{
QString tmp_path = path;
//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;
}
//The path start with project, we get the project and the path from the string
else if (tmp_path.startsWith("project"))
{
QRegExp rx("^project([0-9]+)\\+(embed:\\/\\/.*)$", Qt::CaseInsensitive);
if (rx.exactMatch(tmp_path))
{
bool conv_ok;
uint project_id = rx.capturedTexts().at(1).toUInt(&conv_ok);
if (conv_ok)
{
QETProject *project = QETApp::project(project_id);
if (project)
{
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
else if (path.startsWith("common://") || path.startsWith("custom://"))
{
QString p;
if (path.startsWith("common://"))
{
tmp_path.remove("common://");
p = QETApp::commonElementsDirN() + "/" + tmp_path;
}
else
{
tmp_path.remove("custom://");
p = QETApp::customElementsDirN() + "/" + tmp_path;
}
//This is an element
if (path.endsWith(".elmt"))
{
QFile file(p);
if (file.exists())
{
m_file_system_path = p;
m_collection_path = path;
return true;
}
return false;
}
//They must be a directory
else
{
QDir dir(p);
if(dir.exists())
{
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.
else
{
if(path.endsWith(".elmt"))
{
m_file_system_path = path;
if (path.startsWith(QETApp::commonElementsDirN()))
{
path.remove(QETApp::commonElementsDirN()+="/");
path.prepend("common://");
m_collection_path = path;
}
else if (path.startsWith(QETApp::customElementsDirN()))
{
path.remove(QETApp::customElementsDirN()+="/");
path.prepend("custom://");
m_collection_path = path;
}
return true;
}
else
{
m_file_system_path = path;
if (path.startsWith(QETApp::commonElementsDirN()))
{
path.remove(QETApp::commonElementsDirN()+="/");
path.prepend("common://");
m_collection_path = path;
}
else if (path.startsWith(QETApp::customElementsDirN()))
{
path.remove(QETApp::customElementsDirN()+="/");
path.prepend("custom://");
m_collection_path = path;
}
return true;
}
}
return false;
}
/**
* @brief ElementLocation::isNull
* @return True represent nothing
*/
bool ElementLocation::isNull() const
{
if (isFileSystem() || isProject())
return false;
else
return true;
}
/**
* @brief ElementLocation::setProject
* @param project : set the project of this location to @project.
*/
void ElementLocation::setProject(QETProject *project) {
m_project = project;
}
/**
* @brief ElementLocation::isElement
* @return true if this location represent an element
*/
bool ElementLocation::isElement() const {
return m_collection_path.endsWith(".elmt");
}
/**
* @brief ElementLocation::isDirectory
* @return true if this location represent a directory
*/
bool ElementLocation::isDirectory() const
{
return (!isElement() && !m_collection_path.isEmpty());
}
/**
* @brief ElementLocation::isFileSystem
* @return True if this location represent a file system item.
*/
bool ElementLocation::isFileSystem() const
{
if (m_project) return false;
if (m_file_system_path.isEmpty()) return false;
return true;
}
/**
* @brief ElementLocation::isProject
* @return True if this location represent an item from a project.
*/
bool ElementLocation::isProject() const
{
if (m_project && !m_collection_path.isEmpty())
return true;
else
return false;
}
/**
* @brief ElementLocation::exist
* @return True if this location represent an existing directory or element.
*/
bool ElementLocation::exist() const
{
if (m_project)
return m_project->embeddedElementCollection()->exist(collectionPath(false));
else
{
if (fileSystemPath().isEmpty()) return false;
if (isDirectory())
{
QDir dir(fileSystemPath());
return dir.exists();
}
else if (isElement())
return QFile::exists(fileSystemPath());
else
return false;
}
}
/**
* @brief ElementLocation::projectCollection
* @return If this location represente a item in an embedded project collection, return this collection
* else return nullptr.
*/
XmlElementCollection *ElementLocation::projectCollection() const
{
if (m_project)
return m_project->embeddedElementCollection();
else
return nullptr;
}
/**
* @brief ElementLocation::nameList
* @return the namelist of the represented element or directory.
* If namelist can't be set, return a empty namelist
*/
NamesList ElementLocation::nameList()
{
NamesList nl;
if (isElement())
nl.fromXml(xml());
if (isDirectory())
{
if (m_project)
nl.fromXml(m_project->embeddedElementCollection()->directory(collectionPath(false)));
else
{
//Open the qet_directory file, to get the traductions name of this dir
QFile dir_conf(fileSystemPath() + "/qet_directory");
if (dir_conf.exists() && dir_conf.open(QIODevice::ReadOnly | QIODevice::Text))
{
//Get the content of the file
QDomDocument document;
if (document.setContent(&dir_conf))
{
QDomElement root = document.documentElement();
if (root.tagName() == "qet-directory")
nl.fromXml(root);
}
}
}
}
return nl;
}
/**
* @brief ElementLocation::collectionPath
* Return the path of the represented element relative to collection
* if @protocol is true the path is prepended by the collection type (common://, custom:// or embed://)
* else if false, only the collection path is returned without the collection type.
* @param protocol
* @return the path
*/
QString ElementLocation::collectionPath(bool protocol) const
{
if (protocol)
return m_collection_path;
else
{
QString path = m_collection_path;
return path.remove(QRegularExpression("common://|custom://|embed://"));
}
}
/**
* @brief ElementLocation::projectCollectionPath
* @return The path is in form : project0+embed://dir/subdir/myElement.elmt
* If this item represent a file system thing, return a null QString;
*/
QString ElementLocation::projectCollectionPath() const
{
if (isFileSystem())
return QString();
else
return QString("project" + QString::number(QETApp::projectId(m_project)) + "+" + collectionPath());
}
/**
* @brief ElementLocation::fileSystemPath
* @return The file system path of this element, (the separator is always '/' see QDir::toNativeSeparators())
* If this element is embedded in a project return an empty string;
*/
QString ElementLocation::fileSystemPath() const
{
if (!m_project)
return m_file_system_path;
else
return QString();
}
/**
* @brief ElementLocation::project
* @return the project of this location if he was set.
*/
QETProject *ElementLocation::project() const {
return m_project;
}
/**
* @brief ElementLocation::xml
* @return The definition of this element.
* The definition can be null.
*/
QDomElement ElementLocation::xml()
{
if (!m_xml.isNull())
return m_xml;
if (!m_project)
{
QFile file (m_file_system_path);
QDomDocument docu;
if (docu.setContent(&file))
m_xml = docu.documentElement().cloneNode().toElement();
}
else
{
QString str = m_collection_path;
if (isElement())
{
QDomElement element = m_project->embeddedElementCollection()->element(str.remove("embed://"));
m_xml = element.firstChildElement("definition");
}
else
{
QDomElement element = m_project->embeddedElementCollection()->directory(str.remove("embed://"));
m_xml = element;
}
}
return m_xml;
}
/**
* @brief ElementLocation::uuid
* @return The uuid of the pointed element
*/
QUuid ElementLocation::uuid()
{
if (!m_uuid.isNull()) return m_uuid;
//Get the uuid of element
QList<QDomElement> list_ = QET::findInDomElement(xml(), "uuid");
if (!list_.isEmpty())
m_uuid = QUuid(list_.first().attribute("uuid"));
return m_uuid;
}
/**
* @brief ElementLocation::icon
* @return The icon of the represented element.
* If icon can't be set, return a null QIcon
*/
QIcon ElementLocation::icon()
{
if (!m_icon.isNull()) return m_icon;
if (!m_project)
{
ElementsCollectionCache *cache = QETApp::collectionCache();
if (cache->fetchElement(*this))
m_icon = QIcon(cache->pixmap());
}
else
{
ElementFactory *factory = ElementFactory::Instance();
int state;
Element *elmt = factory->createElement(*this, 0, &state);
if (state == 0)
m_icon = QIcon(elmt->pixmap());
}
return m_icon;
}
/**
* @brief ElementLocation::name
* @return The name of the represented element in the current local
*/
QString ElementLocation::name()
{
if (!m_project)
{
ElementsCollectionCache *cache = QETApp::collectionCache();
if (cache->fetchElement(*this))
return cache->name();
else
return QString();
}
else
{
NamesList nl;
nl.fromXml(xml());
return nl.name(fileName());
}
}
/**
* @brief ElementLocation::fileName
* @return Return the file name of this element whatever the storage system (file system, xml collection)
*/
QString ElementLocation::fileName() const
{
if (m_collection_path.isEmpty()) return QString();
QStringList qsl = m_collection_path.split("/");
if (qsl.isEmpty()) return QString();
else return qsl.last();
}

View File

@@ -1,78 +0,0 @@
/*
Copyright 2006-2015 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ELEMENTLOCATION_H
#define ELEMENTLOCATION_H
#include "nameslist.h"
#include <QString>
#include <QDomElement>
#include <QUuid>
#include <QIcon>
class QETProject;
class QMimeData;
class XmlElementCollection;
/**
* @brief The ElementLocation class
* This class represent the location of an element or a directory in the file system
* or an embedded collection of a project.
* They also provide common things about an element, like the icon, uuid etc...
*/
class ElementLocation
{
public:
ElementLocation(QString path = QString());
ElementLocation(QString path, QETProject *project);
ElementLocation(const QMimeData *data);
ElementLocation &operator=(const ElementLocation &other);
~ElementLocation();
bool setPath(QString path);
bool isNull() const;
void setProject(QETProject *project);
bool isElement() const;
bool isDirectory() const;
bool isFileSystem() const;
bool isProject() const;
bool exist() const;
XmlElementCollection *projectCollection() const;
NamesList nameList();
QString collectionPath(bool protocol = true) const;
QString projectCollectionPath() const;
QString fileSystemPath() const;
QETProject *project() const;
QDomElement xml();
QUuid uuid();
QIcon icon();
QString name();
QString fileName() const;
private:
QString m_collection_path;
QString m_file_system_path;
QETProject *m_project = nullptr;
QDomElement m_xml;
QUuid m_uuid;
QIcon m_icon;
};
#endif // ELEMENTLOCATION_H

View File

@@ -167,7 +167,7 @@ QVariant FileElementCollectionItem::data(int column, int role)
{
if (m_icon.isNull())
{
ElementLocation loc(collectionPath());
ElementsLocation loc(collectionPath());
m_icon = loc.icon();
}
return m_icon;
@@ -214,7 +214,7 @@ bool FileElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropA
if (data->hasFormat("application/x-qet-element-uri") || data->hasFormat("application/x-qet-category-uri"))
{
//Return false if user try to drop a item from a folder to the same folder
ElementLocation drop_location(data->text());
ElementsLocation drop_location(data->text());
for (int i=0 ; i<childCount() ; i++)
{
if (static_cast<FileElementCollectionItem *>(child(i))->collectionPath() == drop_location.collectionPath())
@@ -245,9 +245,9 @@ bool FileElementCollectionItem::dropMimeData(const QMimeData *data, Qt::DropActi
ElementCollectionHandler ech;
ElementLocation source(data->text());
ElementLocation destination(feci->fileSystemPath());
ElementLocation location = ech.copy(source, destination);
ElementsLocation source(data->text());
ElementsLocation destination(feci->fileSystemPath());
ElementsLocation location = ech.copy(source, destination);
if (location.exist())
{
@@ -375,7 +375,7 @@ QString FileElementCollectionItem::name()
}
else if (isElement())
{
ElementLocation loc(collectionPath());
ElementsLocation loc(collectionPath());
m_name = loc.name();
}
return m_name;

View File

@@ -19,9 +19,9 @@
#define FILEELEMENTCOLLECTIONITEM_H
#include "elementcollectionitem.h"
#include "elementlocation.h"
#include <QString>
#include <QDir>
#include <QIcon>
/**
* @brief The FileElementCollectionItem class
@@ -69,7 +69,6 @@ class FileElementCollectionItem : public ElementCollectionItem
private:
QString m_path;
ElementLocation m_location;
QIcon m_icon;
};

View File

@@ -19,6 +19,7 @@
#include "nameslist.h"
#include "elementlocation.h"
#include "qetxml.h"
#include "elementslocation.h"
/**
* @brief XmlElementCollection::XmlElementCollection
@@ -322,8 +323,7 @@ QDomElement XmlElementCollection::directory(const QString &path)
*/
QString XmlElementCollection::addElement(const QString &path)
{
ElementLocation location(path);
ElementsLocation location(path);
if (!location.isElement() || location.fileSystemPath().isEmpty()) return QString();
if (exist(QString("import/" + location.collectionPath(false)))) return QString();
@@ -388,20 +388,16 @@ QString XmlElementCollection::addElement(const QString &path)
* @brief XmlElementCollection::copy
* Copy the content represented by source (an element or a directory) to destination.
* Destination must be a directory of this collection.
*
* WARNING :
* for now, only work if source represent a file or directory from filesystem.
*
* @param source : content to copy
* @param destination : destination of the copy, must be a directory of this collection
* @param rename : rename the copy with @rename else use the name of source
* @param deep_copy : if true copy all childs of source (only if source is directory)
* @return the ElementLocation that represent the copy, if copy failed return a null ElementLocation
*/
ElementLocation XmlElementCollection::copy(ElementLocation &source, ElementLocation &destination, QString rename, bool deep_copy)
ElementsLocation XmlElementCollection::copy(ElementsLocation &source, ElementsLocation &destination, QString rename, bool deep_copy)
{
if (!(source.exist() && destination.isDirectory() && destination.isProject() && destination.projectCollection() == this))
return ElementLocation();
return ElementsLocation();
if (source.isElement())
return copyElement(source, destination, rename);
@@ -447,13 +443,13 @@ bool XmlElementCollection::exist(const QString &path)
* @param deep_copy :if true copy all childs of source
* @return the ElementLocation that represent the copy, if copy failed return a null ElementLocation
*/
ElementLocation XmlElementCollection::copyDirectory(ElementLocation &source, ElementLocation &destination, QString rename, bool deep_copy)
ElementsLocation XmlElementCollection::copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename, bool deep_copy)
{
QString new_dir_name = rename.isEmpty() ? source.fileName() : rename;
//Get the xml directory where the new directory must be added
QDomElement parent_dir_dom = directory(destination.collectionPath(false));
if (parent_dir_dom.isNull()) return ElementLocation();
if (parent_dir_dom.isNull()) return ElementsLocation();
//Remove the previous directory with the same path
QDomElement element = child(destination.collectionPath(false) + "/" + new_dir_name);
@@ -462,18 +458,18 @@ ElementLocation XmlElementCollection::copyDirectory(ElementLocation &source, Ele
ElementLocation created_location;
ElementsLocation created_location;
//Copy with a file system collection source
if (source.isFileSystem())
{
QDir source_dir(source.fileSystemPath());
if (!source_dir.exists()) return ElementLocation();
if (!source_dir.exists()) return ElementsLocation();
QDir dir(source.fileSystemPath());
QDomElement elmt_dom = QETXML::fileSystemDirToXmlCollectionDir(m_dom_document, dir, new_dir_name);
if (elmt_dom.isNull()) return ElementLocation();
if (elmt_dom.isNull()) return ElementsLocation();
parent_dir_dom.appendChild(elmt_dom);
@@ -484,7 +480,7 @@ ElementLocation XmlElementCollection::copyDirectory(ElementLocation &source, Ele
//Append all directories of source to the new created directory
foreach(QString str, source_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
{
ElementLocation sub_source(source.fileSystemPath() + "/" + str);
ElementsLocation sub_source(source.fileSystemPath() + "/" + str);
copyDirectory(sub_source, created_location);
}
@@ -492,7 +488,7 @@ ElementLocation XmlElementCollection::copyDirectory(ElementLocation &source, Ele
source_dir.setNameFilters(QStringList() << "*.elmt");
foreach(QString str, source_dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
{
ElementLocation sub_source(source.fileSystemPath() + "/" + str);
ElementsLocation sub_source(source.fileSystemPath() + "/" + str);
copyElement(sub_source, created_location);
}
}
@@ -501,10 +497,10 @@ ElementLocation XmlElementCollection::copyDirectory(ElementLocation &source, Ele
//Copy with a xml collection source
else
{
if (!source.projectCollection()) return ElementLocation();
if (!source.projectCollection()) return ElementsLocation();
QDomNode other_collection_node = source.projectCollection()->child(source.collectionPath(false)).cloneNode(true);
if (other_collection_node.isNull()) return ElementLocation();
if (other_collection_node.isNull()) return ElementsLocation();
QDomElement other_collection_dom_dir = other_collection_node.toElement();
other_collection_dom_dir.setAttribute("name", new_dir_name);
@@ -523,7 +519,7 @@ ElementLocation XmlElementCollection::copyDirectory(ElementLocation &source, Ele
* @param rename : rename the copy with @rename else use the name of source
* @return
*/
ElementLocation XmlElementCollection::copyElement(ElementLocation &source, ElementLocation &destination, QString rename)
ElementsLocation XmlElementCollection::copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename)
{
QString new_elmt_name = rename.isEmpty() ? source.fileName() : rename;
@@ -534,7 +530,7 @@ ElementLocation XmlElementCollection::copyElement(ElementLocation &source, Eleme
{
QFile file(source.fileSystemPath());
elmt_dom = QETXML::fileSystemElementToXmlCollectionElement(m_dom_document, file, new_elmt_name);
if (elmt_dom.isNull()) return ElementLocation();
if (elmt_dom.isNull()) return ElementsLocation();
}
//Copy with a xml collection source
else
@@ -553,8 +549,8 @@ ElementLocation XmlElementCollection::copyElement(ElementLocation &source, Eleme
//Get the xml directory where the new element must be added
QDomElement dir_dom = directory(destination.collectionPath(false));
if (dir_dom.isNull()) return ElementLocation();
if (dir_dom.isNull()) return ElementsLocation();
dir_dom.appendChild(elmt_dom);
return ElementLocation(destination.projectCollectionPath() + "/" + new_elmt_name);
return ElementsLocation(destination.projectCollectionPath() + "/" + new_elmt_name);
}

View File

@@ -18,7 +18,6 @@
#ifndef XMLELEMENTCOLLECTION_H
#define XMLELEMENTCOLLECTION_H
#include "elementlocation.h"
#include <QObject>
#include <QDomElement>
@@ -47,12 +46,12 @@ class XmlElementCollection : public QObject
QDomElement element(const QString &path);
QDomElement directory(const QString &path);
QString addElement (const QString &path);
ElementLocation copy (ElementLocation &source, ElementLocation &destination, QString rename = QString(), bool deep_copy = true);
ElementsLocation copy (ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true);
bool exist (const QString &path);
private:
ElementLocation copyDirectory(ElementLocation &source, ElementLocation &destination, QString rename = QString(), bool deep_copy = true);
ElementLocation copyElement(ElementLocation &source, ElementLocation &destination, QString rename = QString());
ElementsLocation copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true);
ElementsLocation copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename = QString());
signals:
/**

View File

@@ -21,7 +21,6 @@
#include "xmlelementcollection.h"
#include "nameslist.h"
#include "qetapp.h"
#include "elementlocation.h"
#include "elementcollectionhandler.h"
#include <algorithm>
@@ -86,7 +85,7 @@ QVariant XmlProjectElementCollectionItem::data(int column, int role)
{
if (m_icon.isNull())
{
ElementLocation loc(embeddedPath(), m_project);
ElementsLocation loc(embeddedPath(), m_project);
m_icon = loc.icon();
}
return m_icon;
@@ -135,7 +134,7 @@ bool XmlProjectElementCollectionItem::canDropMimeData(const QMimeData *data, Qt:
if (data->hasFormat("application/x-qet-element-uri") || data->hasFormat("application/x-qet-category-uri"))
{
//Return false if user try to drop a item from a folder to the same folder
ElementLocation drop_location(data->text());
ElementsLocation drop_location(data->text());
for (int i=0 ; i<childCount() ; i++)
{
if (static_cast<XmlProjectElementCollectionItem *>(child(i))->collectionPath() == drop_location.collectionPath())
@@ -176,9 +175,9 @@ bool XmlProjectElementCollectionItem::dropMimeData(const QMimeData *data, Qt::Dr
ElementCollectionHandler ech;
ElementLocation source(data->text());
ElementLocation destination(xpeci->collectionPath());
ElementLocation location = ech.copy(source, destination);
ElementsLocation source(data->text());
ElementsLocation destination(xpeci->collectionPath());
ElementsLocation location = ech.copy(source, destination);
if (location.exist())
{
@@ -234,7 +233,7 @@ QString XmlProjectElementCollectionItem::name()
}
else
{
ElementLocation location (embeddedPath(), m_project);
ElementsLocation location (embeddedPath(), m_project);
m_name = location.name();
return m_name;
}
@@ -283,7 +282,7 @@ bool XmlProjectElementCollectionItem::isElement() const
*/
QString XmlProjectElementCollectionItem::collectionPath() const
{
ElementLocation loc (embeddedPath(), m_project);
ElementsLocation loc (embeddedPath(), m_project);
return loc.projectCollectionPath();
}