2020-01-09 10:26:10 +01:00
|
|
|
/*
|
2020-06-15 17:42:37 +02:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2009-04-03 19:30:25 +00:00
|
|
|
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 "elementslocation.h"
|
|
|
|
#include "qetapp.h"
|
2016-03-15 15:23:11 +00:00
|
|
|
#include "xmlelementcollection.h"
|
|
|
|
#include "qetproject.h"
|
|
|
|
#include "elementscollectioncache.h"
|
2018-08-23 19:41:58 +00:00
|
|
|
#include "elementpicturefactory.h"
|
2016-03-15 15:23:11 +00:00
|
|
|
#include "element.h"
|
2016-03-31 17:28:44 +00:00
|
|
|
#include "qetxml.h"
|
2018-08-23 19:41:58 +00:00
|
|
|
#include <QPicture>
|
2009-04-03 19:30:25 +00:00
|
|
|
|
2012-02-05 19:24:42 +00:00
|
|
|
// make this class usable with QVariant
|
|
|
|
int ElementsLocation::MetaTypeId = qRegisterMetaType<ElementsLocation>("ElementsLocation");
|
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::ElementsLocation
|
|
|
|
Constructor
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation::ElementsLocation()
|
|
|
|
{}
|
2009-04-03 19:30:25 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::ElementsLocation
|
|
|
|
@param path :
|
|
|
|
Chemin de l'emplacement de l'element
|
|
|
|
@param project :
|
|
|
|
Projet de l'emplacement de l'element
|
2009-04-03 19:30:25 +00:00
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation::ElementsLocation(const QString &path, QETProject *project) :
|
|
|
|
m_project(project)
|
2009-04-03 19:30:25 +00:00
|
|
|
{
|
2016-03-15 15:23:11 +00:00
|
|
|
setPath(path);
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Destructeur
|
|
|
|
*/
|
|
|
|
ElementsLocation::~ElementsLocation() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Constructeur de copie
|
|
|
|
@param other Autre emplacement d'element a copier
|
|
|
|
*/
|
|
|
|
ElementsLocation::ElementsLocation(const ElementsLocation &other) :
|
2016-03-15 15:23:11 +00:00
|
|
|
m_collection_path(other.m_collection_path),
|
2016-03-16 16:39:23 +00:00
|
|
|
m_file_system_path(other.m_file_system_path),
|
2016-03-29 17:23:58 +00:00
|
|
|
m_project(other.m_project)
|
2016-03-16 16:39:23 +00:00
|
|
|
{}
|
2016-03-15 15:23:11 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::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
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation::ElementsLocation(const QMimeData *data)
|
2009-04-03 19:30:25 +00:00
|
|
|
{
|
2020-08-12 21:53:02 +02:00
|
|
|
if (data->hasFormat("application/x-qet-element-uri")
|
|
|
|
|| data->hasFormat("application/x-qet-category-uri"))
|
2016-03-15 15:23:11 +00:00
|
|
|
setPath(data->text());
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Operateur d'affectation
|
|
|
|
@param other Autre emplacement d'element a affecter
|
|
|
|
*/
|
|
|
|
ElementsLocation &ElementsLocation::operator=(const ElementsLocation &other) {
|
2016-03-15 15:23:11 +00:00
|
|
|
m_collection_path = other.m_collection_path;
|
2016-03-15 18:42:18 +00:00
|
|
|
m_file_system_path = other.m_file_system_path;
|
2016-03-15 15:23:11 +00:00
|
|
|
m_project = other.m_project;
|
2009-04-03 19:30:25 +00:00
|
|
|
return(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::operator ==
|
2009-04-03 19:30:25 +00:00
|
|
|
Operateur de comparaison
|
|
|
|
@param other Autre emplacement d'element a comparer
|
2020-08-12 21:53:02 +02:00
|
|
|
@return true si other et cet ElementsLocation sont identiques,
|
|
|
|
false sinon
|
2009-04-03 19:30:25 +00:00
|
|
|
*/
|
|
|
|
bool ElementsLocation::operator==(const ElementsLocation &other) const {
|
|
|
|
return(
|
2016-03-15 15:23:11 +00:00
|
|
|
m_collection_path == other.m_collection_path &&\
|
|
|
|
m_project == other.m_project
|
2009-04-03 19:30:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::operator !=
|
2009-04-03 19:30:25 +00:00
|
|
|
Operateur de comparaison
|
|
|
|
@param other Autre emplacement d'element a comparer
|
2020-08-12 21:53:02 +02:00
|
|
|
@return true si other et cet ElementsLocation sont differents,
|
|
|
|
false sinon
|
2009-04-03 19:30:25 +00:00
|
|
|
*/
|
|
|
|
bool ElementsLocation::operator!=(const ElementsLocation &other) const {
|
|
|
|
return(
|
2016-03-15 15:23:11 +00:00
|
|
|
m_collection_path != other.m_collection_path ||\
|
|
|
|
m_project != other.m_project
|
2009-04-03 19:30:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::baseName
|
|
|
|
@return The base name of the element or directory.
|
|
|
|
Unlike ElementsLocation::fileName,
|
|
|
|
this method don't return the extension name.
|
|
|
|
For exemple if this location represent an element they return myElement.
|
|
|
|
@see fileName()
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2009-04-03 19:30:25 +00:00
|
|
|
QString ElementsLocation::baseName() const {
|
|
|
|
QRegExp regexp("^.*([^/]+)\\.elmt$");
|
2016-03-15 15:23:11 +00:00
|
|
|
if (regexp.exactMatch(m_collection_path)) {
|
2009-04-03 19:30:25 +00:00
|
|
|
return(regexp.capturedTexts().at(1));
|
|
|
|
}
|
|
|
|
return(QString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::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
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
QString ElementsLocation::collectionPath(bool protocol) const
|
|
|
|
{
|
|
|
|
if (protocol)
|
|
|
|
return m_collection_path;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString path = m_collection_path;
|
|
|
|
return path.remove(QRegularExpression("common://|custom://|embed://"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::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;
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
QString ElementsLocation::projectCollectionPath() const
|
|
|
|
{
|
|
|
|
if (isFileSystem())
|
|
|
|
return QString();
|
|
|
|
else
|
2020-08-12 21:53:02 +02:00
|
|
|
return QString("project"
|
|
|
|
+ QString::number(QETApp::projectId(m_project))
|
|
|
|
+ "+"
|
|
|
|
+ collectionPath());
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::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;
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
QString ElementsLocation::fileSystemPath() const
|
|
|
|
{
|
|
|
|
if (!m_project)
|
|
|
|
return m_file_system_path;
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::path
|
|
|
|
@return The path of this location.
|
|
|
|
@deprecated use instead collectionPath(true)
|
|
|
|
*/
|
2009-04-03 19:30:25 +00:00
|
|
|
QString ElementsLocation::path() const {
|
2016-03-15 15:23:11 +00:00
|
|
|
return(m_collection_path);
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::setPath
|
|
|
|
Set the path of this item.
|
|
|
|
The path can be relative to a collection
|
|
|
|
(start by common:// , custom:// or embed://) or not.
|
|
|
|
@param path
|
|
|
|
*/
|
2016-03-19 14:18:00 +00:00
|
|
|
void ElementsLocation::setPath(const QString &path)
|
2016-03-15 15:23:11 +00:00
|
|
|
{
|
|
|
|
QString tmp_path = path;
|
2009-04-03 19:30:25 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
2016-03-15 15:23:11 +00:00
|
|
|
//On windows, we convert backslash to slash
|
|
|
|
tmp_path = QDir::fromNativeSeparators(path);
|
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
#endif
|
2016-03-15 15:23:11 +00:00
|
|
|
|
|
|
|
//There is a project, the path is for an embedded coolection.
|
|
|
|
if (m_project)
|
|
|
|
{
|
2016-03-19 14:18:00 +00:00
|
|
|
m_collection_path = path;
|
|
|
|
//Add the protocol to the collection path
|
|
|
|
if (!path.startsWith("embed://"))
|
|
|
|
m_collection_path.prepend("embed://");
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 21:53:02 +02:00
|
|
|
//The path start with project, we get the project and the path from the string
|
2016-03-15 15:23:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:53:02 +02:00
|
|
|
// The path is in file system,
|
|
|
|
// the given path is relative to common or custom collection
|
2016-03-15 15:23:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-19 09:29:40 +00:00
|
|
|
m_file_system_path = p;
|
|
|
|
m_collection_path = path;
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
2020-08-12 21:53:02 +02:00
|
|
|
//In this case, the path is supposed to be relative to the file system.
|
2016-03-15 15:23:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
QString path_ = path;
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::addToPath
|
|
|
|
Add a string to the actual path of this location
|
|
|
|
@param string
|
|
|
|
@return True if the operation success
|
|
|
|
*/
|
2016-04-09 10:45:15 +00:00
|
|
|
bool ElementsLocation::addToPath(const QString &string)
|
|
|
|
{
|
|
|
|
if (m_collection_path.endsWith(".elmt", Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
qDebug() << "ElementsLocation::addToPath : Can't add string to the path of an element";
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString added_path = string;
|
|
|
|
|
|
|
|
if (!m_collection_path.endsWith("/") && !added_path.startsWith("/"))
|
|
|
|
added_path.prepend("/");
|
|
|
|
|
|
|
|
if (isFileSystem())
|
|
|
|
m_file_system_path += added_path;
|
|
|
|
|
|
|
|
m_collection_path += added_path;
|
2009-04-03 19:30:25 +00:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
2012-04-28 16:45:16 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::parent
|
2012-04-28 16:45:16 +00:00
|
|
|
@return the location of the parent category, or a copy of this location
|
|
|
|
when it represents a root category.
|
|
|
|
*/
|
|
|
|
ElementsLocation ElementsLocation::parent() const {
|
|
|
|
ElementsLocation copy(*this);
|
|
|
|
QRegExp re1("^([a-z]+://)(.*)/*$");
|
2016-03-15 15:23:11 +00:00
|
|
|
if (re1.exactMatch(m_collection_path)) {
|
2012-04-28 16:45:16 +00:00
|
|
|
QString path_proto = re1.capturedTexts().at(1);
|
|
|
|
QString path_path = re1.capturedTexts().at(2);
|
|
|
|
QString parent_path = path_path.remove(QRegExp("/*[^/]+$"));
|
|
|
|
copy.setPath(path_proto + parent_path);
|
|
|
|
}
|
|
|
|
return(copy);
|
|
|
|
}
|
|
|
|
|
2009-04-03 19:30:25 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::project
|
2009-04-03 19:30:25 +00:00
|
|
|
@return le projet de cet emplacement ou 0 si celui-ci n'est pas lie a
|
|
|
|
un projet.
|
|
|
|
*/
|
|
|
|
QETProject *ElementsLocation::project() const {
|
2016-03-15 15:23:11 +00:00
|
|
|
return(m_project);
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::setProject
|
|
|
|
@param project :
|
|
|
|
le nouveau projet pointe par cet emplacement
|
2009-04-03 19:30:25 +00:00
|
|
|
Indiquer 0 pour que cet emplacement ne soit plus lie a un projet.
|
|
|
|
*/
|
|
|
|
void ElementsLocation::setProject(QETProject *project) {
|
2016-03-15 15:23:11 +00:00
|
|
|
m_project = project;
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::isNull
|
2009-04-03 19:30:25 +00:00
|
|
|
@return true si l'emplacement semble utilisable (chemin virtuel non vide).
|
|
|
|
*/
|
|
|
|
bool ElementsLocation::isNull() const {
|
2016-03-15 15:23:11 +00:00
|
|
|
return(m_collection_path.isEmpty());
|
2009-04-03 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::toString
|
2009-04-03 19:30:25 +00:00
|
|
|
@return Une chaine de caracteres representant l'emplacement
|
|
|
|
*/
|
|
|
|
QString ElementsLocation::toString() const {
|
|
|
|
QString result;
|
2016-03-15 15:23:11 +00:00
|
|
|
if (m_project) {
|
|
|
|
int project_id = QETApp::projectId(m_project);
|
2009-04-03 19:30:25 +00:00
|
|
|
if (project_id != -1) {
|
|
|
|
result += "project" + QString().setNum(project_id) + "+";
|
|
|
|
}
|
|
|
|
}
|
2016-03-15 15:23:11 +00:00
|
|
|
result += m_collection_path;
|
2009-04-03 19:30:25 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::isElement
|
|
|
|
@return true if this location represent an element
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
bool ElementsLocation::isElement() const {
|
|
|
|
return m_collection_path.endsWith(".elmt");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::isDirectory
|
|
|
|
@return true if this location represent a directory
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
bool ElementsLocation::isDirectory() const {
|
|
|
|
return (!isElement() && !m_collection_path.isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::isFileSystem
|
|
|
|
@return true if
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
bool ElementsLocation::isFileSystem() const
|
|
|
|
{
|
|
|
|
if (m_project) return false;
|
|
|
|
if (m_file_system_path.isEmpty()) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-15 14:46:01 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::isCommonCollection
|
|
|
|
@return
|
|
|
|
True if this location represent an item from the common collection
|
|
|
|
*/
|
2016-05-15 14:46:01 +00:00
|
|
|
bool ElementsLocation::isCommonCollection() const
|
|
|
|
{
|
|
|
|
return fileSystemPath().startsWith(QETApp::commonElementsDirN());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::isCustomCollection
|
|
|
|
@return
|
|
|
|
True if this location represent an item from the custom collection
|
|
|
|
*/
|
2016-05-15 14:46:01 +00:00
|
|
|
bool ElementsLocation::isCustomCollection() const
|
|
|
|
{
|
|
|
|
return fileSystemPath().startsWith(QETApp::customElementsDirN());
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::isProject
|
|
|
|
@return True if this location represent an item from a project.
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
bool ElementsLocation::isProject() const
|
|
|
|
{
|
|
|
|
if (m_project && !m_collection_path.isEmpty())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::exist
|
|
|
|
@return
|
|
|
|
True if this location represent an existing directory or element.
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
bool ElementsLocation::exist() const
|
|
|
|
{
|
|
|
|
if (m_project)
|
2016-03-19 14:18:00 +00:00
|
|
|
{
|
2016-03-15 15:23:11 +00:00
|
|
|
return m_project->embeddedElementCollection()->exist(collectionPath(false));
|
2016-03-19 14:18:00 +00:00
|
|
|
}
|
2016-03-15 15:23:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fileSystemPath().isEmpty()) return false;
|
|
|
|
|
|
|
|
if (isDirectory())
|
|
|
|
{
|
|
|
|
QDir dir(fileSystemPath());
|
|
|
|
return dir.exists();
|
|
|
|
}
|
|
|
|
else if (isElement())
|
|
|
|
return QFile::exists(fileSystemPath());
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:28:44 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::isWritable
|
|
|
|
@return True if this element can be writable (can use set xml)
|
|
|
|
*/
|
2016-03-31 17:28:44 +00:00
|
|
|
bool ElementsLocation::isWritable() const
|
|
|
|
{
|
|
|
|
if (m_project)
|
|
|
|
return !m_project->isReadOnly();
|
|
|
|
else if (isFileSystem())
|
|
|
|
{
|
|
|
|
if (fileSystemPath().startsWith(QETApp::commonElementsDirN()))
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::projectCollection
|
|
|
|
@return
|
|
|
|
If this location represente a item in an embedded project collection,
|
|
|
|
return this collection else return nullptr.
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
XmlElementCollection *ElementsLocation::projectCollection() const
|
|
|
|
{
|
|
|
|
if (m_project)
|
|
|
|
return m_project->embeddedElementCollection();
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::nameList
|
|
|
|
@return the namelist of the represented element or directory.
|
|
|
|
If namelist can't be set, return a empty namelist
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
NamesList ElementsLocation::nameList()
|
|
|
|
{
|
|
|
|
NamesList nl;
|
|
|
|
|
2020-01-18 20:03:24 +01:00
|
|
|
if (isElement()) {
|
|
|
|
nl.fromXml(pugiXml());
|
2020-01-04 15:59:27 +01:00
|
|
|
}
|
2016-03-15 15:23:11 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::xml
|
|
|
|
@return The definition of this element or directory.
|
|
|
|
The definition can be null.
|
|
|
|
*/
|
2016-03-29 17:23:58 +00:00
|
|
|
QDomElement ElementsLocation::xml() const
|
2016-03-15 15:23:11 +00:00
|
|
|
{
|
|
|
|
if (!m_project)
|
|
|
|
{
|
|
|
|
QFile file (m_file_system_path);
|
|
|
|
QDomDocument docu;
|
|
|
|
if (docu.setContent(&file))
|
2016-03-31 17:28:44 +00:00
|
|
|
return docu.documentElement();
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString str = m_collection_path;
|
|
|
|
if (isElement())
|
|
|
|
{
|
|
|
|
QDomElement element = m_project->embeddedElementCollection()->element(str.remove("embed://"));
|
2016-03-29 17:23:58 +00:00
|
|
|
return element.firstChildElement("definition");
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QDomElement element = m_project->embeddedElementCollection()->directory(str.remove("embed://"));
|
2016-03-29 17:23:58 +00:00
|
|
|
return element;
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 17:23:58 +00:00
|
|
|
return QDomElement();
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 11:30:43 +01:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::pugiXml
|
|
|
|
@return the xml document of this element or directory
|
|
|
|
The definition can be null
|
|
|
|
*/
|
2020-01-04 11:30:43 +01:00
|
|
|
pugi::xml_document ElementsLocation::pugiXml() const
|
|
|
|
{
|
2020-08-12 21:53:02 +02:00
|
|
|
/* Except for linux OS (because linux keep in cache the file),
|
|
|
|
* we keep in memory the xml
|
|
|
|
* to avoid multiple access to file.
|
|
|
|
* keep in memory the XML,
|
|
|
|
* consumes a little more RAM,
|
|
|
|
* for this reason we don't use it for linux to minimize the RAM footprint.
|
|
|
|
*/
|
2020-01-09 10:26:10 +01:00
|
|
|
#ifndef Q_OS_LINUX
|
|
|
|
if (!m_string_stream.str().empty())
|
|
|
|
{
|
|
|
|
pugi::xml_document docu;
|
|
|
|
docu.load_string(m_string_stream.str().c_str());
|
|
|
|
return docu;
|
|
|
|
}
|
|
|
|
#endif
|
2020-01-04 11:30:43 +01:00
|
|
|
if (!m_project)
|
|
|
|
{
|
|
|
|
pugi::xml_document docu;
|
|
|
|
if (docu.load_file(m_file_system_path.toStdString().c_str()))
|
2020-01-09 10:26:10 +01:00
|
|
|
{
|
|
|
|
#ifndef Q_OS_LINUX
|
|
|
|
docu.save(m_string_stream);
|
|
|
|
#endif
|
2020-01-04 11:30:43 +01:00
|
|
|
return docu;
|
2020-01-09 10:26:10 +01:00
|
|
|
}
|
2020-01-04 11:30:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString str = m_collection_path;
|
|
|
|
if (isElement())
|
|
|
|
{
|
2020-08-12 21:53:02 +02:00
|
|
|
//Get the xml dom from Qt xml and copie to pugi xml
|
2020-01-04 11:30:43 +01:00
|
|
|
QDomElement element = m_project->embeddedElementCollection()->element(str.remove("embed://"));
|
|
|
|
QDomDocument qdoc;
|
|
|
|
qdoc.appendChild(qdoc.importNode(element.firstChildElement("definition"), true));
|
|
|
|
|
|
|
|
pugi::xml_document docu;
|
|
|
|
docu.load_string(qdoc.toString(4).toStdString().c_str());
|
|
|
|
return docu;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QDomElement element = m_project->embeddedElementCollection()->directory(str.remove("embed://"));
|
|
|
|
QDomDocument qdoc;
|
|
|
|
qdoc.appendChild(qdoc.importNode(element, true));
|
|
|
|
|
|
|
|
pugi::xml_document docu;
|
|
|
|
docu.load_string(qdoc.toString(4).toStdString().c_str());
|
|
|
|
return docu;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pugi::xml_document();
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:28:44 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::setXml
|
|
|
|
Replace the current xml description by xml_document;
|
|
|
|
The document element of xml_document must have
|
|
|
|
tagname "definition" to be written
|
|
|
|
This definition must be writable
|
|
|
|
@param xml_document
|
|
|
|
@return true if success
|
|
|
|
*/
|
2016-03-31 17:28:44 +00:00
|
|
|
bool ElementsLocation::setXml(const QDomDocument &xml_document) const
|
|
|
|
{
|
|
|
|
if (!isWritable())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (xml_document.documentElement().tagName() != "definition")
|
|
|
|
{
|
|
|
|
qDebug() << "ElementsLocation::setXml : tag name of document element isn't 'definition'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFileSystem())
|
|
|
|
{
|
|
|
|
QString error;
|
|
|
|
QETXML::writeXmlFile(xml_document, fileSystemPath(), &error);
|
|
|
|
|
2016-04-19 09:29:40 +00:00
|
|
|
if (!error.isEmpty()) {
|
2016-03-31 17:28:44 +00:00
|
|
|
qDebug() << "ElementsLocation::setXml error : " << error;
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-19 09:29:40 +00:00
|
|
|
else {
|
2016-03-31 17:28:44 +00:00
|
|
|
return true;
|
2016-04-19 09:29:40 +00:00
|
|
|
}
|
2016-03-31 17:28:44 +00:00
|
|
|
}
|
2016-04-19 09:29:40 +00:00
|
|
|
else if (isProject())
|
2016-03-31 17:28:44 +00:00
|
|
|
{
|
2016-04-19 09:29:40 +00:00
|
|
|
//Element exist, we overwrite the existing element.
|
|
|
|
if (exist())
|
|
|
|
{
|
|
|
|
QDomElement dom_element = xml();
|
|
|
|
QDomNode parent_node = dom_element.parentNode();
|
|
|
|
parent_node.removeChild(dom_element);
|
|
|
|
parent_node.appendChild(xml_document.documentElement().cloneNode(true));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//Element doesn't exist, we create the element
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString path_ = collectionPath(false);
|
|
|
|
QRegExp rx ("^(.*)/(.*\\.elmt)$");
|
2016-03-31 17:28:44 +00:00
|
|
|
|
2016-04-19 09:29:40 +00:00
|
|
|
if (rx.exactMatch(path_)) {
|
|
|
|
return project()->embeddedElementCollection()->addElementDefinition(rx.cap(1), rx.cap(2), xml_document.documentElement());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qDebug() << "ElementsLocation::setXml : rx don't match";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-03-31 17:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::uuid
|
|
|
|
@return The uuid of the pointed element
|
|
|
|
Uuid can be null
|
|
|
|
*/
|
2016-03-29 17:23:58 +00:00
|
|
|
QUuid ElementsLocation::uuid() const
|
2016-03-15 15:23:11 +00:00
|
|
|
{
|
2020-01-04 11:30:43 +01:00
|
|
|
if (!isElement()) {
|
|
|
|
return QUuid();
|
|
|
|
}
|
|
|
|
|
2020-01-17 19:33:13 +01:00
|
|
|
auto document = pugiXml();
|
|
|
|
auto uuid_node = document.document_element().child("uuid");
|
|
|
|
if (uuid_node.empty()) {
|
|
|
|
return QUuid();
|
2020-01-04 11:30:43 +01:00
|
|
|
}
|
2020-01-17 19:33:13 +01:00
|
|
|
return QUuid(uuid_node.attribute("uuid").as_string());
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementLocation::icon
|
|
|
|
@return The icon of the represented element.
|
|
|
|
If icon can't be set, return a null QIcon
|
|
|
|
*/
|
2016-03-29 17:23:58 +00:00
|
|
|
QIcon ElementsLocation::icon() const
|
2016-03-15 15:23:11 +00:00
|
|
|
{
|
|
|
|
if (!m_project)
|
|
|
|
{
|
|
|
|
ElementsCollectionCache *cache = QETApp::collectionCache();
|
2020-08-12 21:53:02 +02:00
|
|
|
// Make a copy of this to keep this method const
|
|
|
|
ElementsLocation loc(*this);
|
2016-03-29 17:23:58 +00:00
|
|
|
if (cache->fetchElement(loc))
|
|
|
|
return QIcon(cache->pixmap());
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
2018-08-23 19:41:58 +00:00
|
|
|
else {
|
|
|
|
return QIcon(ElementPictureFactory::instance()->pixmap(*this));
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 17:23:58 +00:00
|
|
|
return QIcon();
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementLocation::name
|
|
|
|
@return The name of the represented element in the current local
|
|
|
|
*/
|
2016-03-29 17:23:58 +00:00
|
|
|
QString ElementsLocation::name() const
|
2016-03-15 15:23:11 +00:00
|
|
|
{
|
2018-08-23 19:41:58 +00:00
|
|
|
NamesList nl;
|
2020-01-17 19:33:13 +01:00
|
|
|
nl.fromXml(pugiXml().document_element());
|
2018-08-23 19:41:58 +00:00
|
|
|
return nl.name(fileName());
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementLocation::fileName
|
|
|
|
@return Return the file name of the represented item,
|
|
|
|
whatever the storage system (file system, xml collection)
|
|
|
|
with is file extension.
|
|
|
|
For example if this location represent an element,
|
|
|
|
they return myElement.elmt.
|
|
|
|
For a directory return myDirectory.
|
|
|
|
@see baseName
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
QString ElementsLocation::fileName() const
|
|
|
|
{
|
2016-03-16 16:39:23 +00:00
|
|
|
if (m_collection_path.isEmpty()) return QString();
|
2016-03-15 15:23:11 +00:00
|
|
|
|
2016-03-16 16:39:23 +00:00
|
|
|
QStringList qsl = m_collection_path.split("/");
|
|
|
|
if (qsl.isEmpty()) return QString();
|
|
|
|
else return qsl.last();
|
2016-03-15 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 22:06:34 +00:00
|
|
|
/**
|
2020-08-12 21:53:02 +02:00
|
|
|
@brief ElementsLocation::elementInformations
|
|
|
|
@return
|
|
|
|
the element information of the element represented by this location.
|
|
|
|
If the location is a directory, the returned diagram context is empty
|
|
|
|
*/
|
2019-01-04 22:06:34 +00:00
|
|
|
DiagramContext ElementsLocation::elementInformations() const
|
|
|
|
{
|
|
|
|
DiagramContext context;
|
|
|
|
if (isDirectory()) {
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:53:02 +02:00
|
|
|
context.fromXml(pugiXml().document_element().child(
|
|
|
|
"elementInformations"),
|
|
|
|
"elementInformation");
|
2020-01-17 19:33:13 +01:00
|
|
|
return context;
|
2020-01-09 10:26:10 +01:00
|
|
|
}
|
|
|
|
|
2020-08-12 21:53:02 +02:00
|
|
|
/**
|
|
|
|
@brief operator <<
|
|
|
|
@param debug
|
|
|
|
@param location
|
|
|
|
@return
|
|
|
|
*/
|
2016-05-05 13:31:04 +00:00
|
|
|
QDebug operator<< (QDebug debug, const ElementsLocation &location)
|
|
|
|
{
|
|
|
|
QDebugStateSaver saver(debug);
|
|
|
|
debug.noquote();
|
|
|
|
|
|
|
|
QString msg;
|
|
|
|
msg += "ElementsLocation(";
|
2020-08-12 21:53:02 +02:00
|
|
|
msg += (location.isProject()? location.projectCollectionPath()
|
|
|
|
: location.collectionPath(true));
|
2016-05-05 13:31:04 +00:00
|
|
|
msg += location.exist()? ", true" : ", false";
|
|
|
|
msg +=")";
|
|
|
|
|
|
|
|
debug << msg;
|
|
|
|
|
|
|
|
return debug;
|
|
|
|
}
|