2015-12-08 16:52:10 +00:00
|
|
|
/*
|
2017-01-20 10:55:49 +00:00
|
|
|
Copyright 2006-2017 The QElectroTech Team
|
2016-06-05 16:34:46 +00:00
|
|
|
This file is part of QElectroTech.
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
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.
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
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.
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
#include "fileelementcollectionitem.h"
|
|
|
|
#include "elementslocation.h"
|
2016-06-05 16:34:46 +00:00
|
|
|
#include "qetapp.h"
|
2015-12-08 16:52:10 +00:00
|
|
|
#include "qeticons.h"
|
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
#include <QDir>
|
2015-12-08 16:52:10 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief FileElementCollectionItem::FileElementCollectionItem
|
|
|
|
* Constructor
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
FileElementCollectionItem::FileElementCollectionItem()
|
2015-12-08 16:52:10 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::setRootPath
|
|
|
|
* Set path has root path for this file item.
|
|
|
|
* Use this function only to set the beginning of a file collection.
|
|
|
|
* @param path
|
|
|
|
* @return true if path exist.
|
|
|
|
*/
|
2018-07-19 14:14:31 +00:00
|
|
|
bool FileElementCollectionItem::setRootPath(const QString& path, bool set_data, bool hide_element)
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
QDir dir(path);
|
|
|
|
if (dir.exists()) {
|
2015-12-08 16:52:10 +00:00
|
|
|
m_path = path;
|
2016-06-05 16:34:46 +00:00
|
|
|
populate(set_data, hide_element);
|
2015-12-08 16:52:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-05 16:34:46 +00:00
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::fileSystemPath
|
|
|
|
* @return the file system path of this item
|
|
|
|
*/
|
|
|
|
QString FileElementCollectionItem::fileSystemPath() const
|
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (isCollectionRoot())
|
2015-12-08 16:52:10 +00:00
|
|
|
return m_path;
|
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
FileElementCollectionItem *feci = static_cast<FileElementCollectionItem *> (parent());
|
|
|
|
if (feci)
|
|
|
|
return feci->fileSystemPath() + "/" + m_path;
|
|
|
|
else
|
|
|
|
return QString();
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::dirPath
|
2016-06-05 16:34:46 +00:00
|
|
|
* @return the dir path of this item (if this item is a dir return the path,
|
|
|
|
* if item is an element return the path of the parent directory)
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
|
|
|
QString FileElementCollectionItem::dirPath() const
|
|
|
|
{
|
|
|
|
if (isDir())
|
|
|
|
return fileSystemPath();
|
2016-06-05 16:34:46 +00:00
|
|
|
else if (parent() && parent()->type() == FileElementCollectionItem::Type)
|
|
|
|
return static_cast<FileElementCollectionItem*>(parent())->fileSystemPath();
|
|
|
|
else
|
|
|
|
return QString();
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief FileElementCollectionItem::isDir
|
|
|
|
* @return true if this item represent a directory
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
bool FileElementCollectionItem::isDir() const
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (m_path.endsWith(".elmt"))
|
|
|
|
return false;
|
2015-12-08 16:52:10 +00:00
|
|
|
else
|
2016-06-05 16:34:46 +00:00
|
|
|
return true;
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 14:25:20 +00:00
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief FileElementCollectionItem::isElement
|
|
|
|
* @return true if this item represent an element
|
2016-01-16 14:25:20 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
bool FileElementCollectionItem::isElement() const
|
2016-01-16 14:25:20 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
return (!isDir());
|
2016-01-16 14:25:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief FileElementCollectionItem::localName
|
|
|
|
* @return the located name of this item
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QString FileElementCollectionItem::localName()
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (!text().isNull())
|
|
|
|
return text();
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
else if (isDir()) {
|
|
|
|
if (isCollectionRoot()) {
|
|
|
|
if (m_path == QETApp::commonElementsDirN())
|
|
|
|
setText(QObject::tr("Collection QET"));
|
|
|
|
else if (m_path == QETApp::customElementsDirN())
|
|
|
|
setText(QObject::tr("Collection utilisateur"));
|
|
|
|
else
|
|
|
|
setText(QObject::tr("Collection inconnue"));
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
2016-06-05 16:34:46 +00:00
|
|
|
else {
|
|
|
|
//Open the qet_directory file, to get the traductions name of this dir
|
|
|
|
QFile dir_conf(fileSystemPath() + "/qet_directory");
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
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") {
|
|
|
|
NamesList nl;
|
|
|
|
nl.fromXml(root);
|
|
|
|
setText(nl.name());
|
|
|
|
}
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-05 16:34:46 +00:00
|
|
|
else if (isElement()) {
|
|
|
|
ElementsLocation loc(collectionPath());
|
|
|
|
setText(loc.name());
|
|
|
|
}
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
return text();
|
2016-03-24 10:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief FileElementCollectionItem::name
|
|
|
|
* @return The collection name of this item
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QString FileElementCollectionItem::name() const
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (isCollectionRoot())
|
|
|
|
return QString();
|
2015-12-09 20:27:31 +00:00
|
|
|
else
|
2016-06-05 16:34:46 +00:00
|
|
|
return m_path;
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief FileElementCollectionItem::collectionPath
|
|
|
|
* @return The path of this item relative to the collection.
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QString FileElementCollectionItem::collectionPath() const
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (isCollectionRoot()) {
|
|
|
|
if (m_path == QETApp::commonElementsDirN())
|
|
|
|
return "common://";
|
|
|
|
else
|
|
|
|
return "custom://";
|
2016-02-13 12:51:56 +00:00
|
|
|
}
|
2016-06-05 16:34:46 +00:00
|
|
|
else if (parent() && parent()->type() == FileElementCollectionItem::Type) {
|
|
|
|
ElementCollectionItem *eci = static_cast<ElementCollectionItem*>(parent());
|
|
|
|
if (eci->isCollectionRoot())
|
|
|
|
return eci->collectionPath() + m_path;
|
|
|
|
else
|
|
|
|
return eci->collectionPath() + "/" + m_path;
|
2016-02-13 12:51:56 +00:00
|
|
|
}
|
2015-12-08 16:52:10 +00:00
|
|
|
else
|
2016-06-05 16:34:46 +00:00
|
|
|
return QString();
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::isCollectionRoot
|
|
|
|
* @return true if this item represent the root of collection
|
|
|
|
*/
|
|
|
|
bool FileElementCollectionItem::isCollectionRoot() const
|
|
|
|
{
|
2016-02-13 12:51:56 +00:00
|
|
|
if (m_path == QETApp::commonElementsDirN() || m_path == QETApp::customElementsDirN())
|
2015-12-08 16:52:10 +00:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::isCommonCollection
|
2016-06-05 16:34:46 +00:00
|
|
|
* @return True if this item represent the common collection
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
bool FileElementCollectionItem::isCommonCollection() const
|
|
|
|
{
|
2016-02-13 12:51:56 +00:00
|
|
|
return fileSystemPath().startsWith(QETApp::commonElementsDirN());
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 08:41:09 +00:00
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::isCustomCollection
|
|
|
|
* @return True if this item represent the custom collection
|
|
|
|
*/
|
|
|
|
bool FileElementCollectionItem::isCustomCollection() const
|
|
|
|
{
|
|
|
|
return fileSystemPath().startsWith(QETApp::customElementsDirN());
|
|
|
|
}
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief FileElementCollectionItem::addChildAtPath
|
|
|
|
* Ask to this item item to add a child with collection name @collection_name
|
|
|
|
* @param collection_name
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
void FileElementCollectionItem::addChildAtPath(const QString &collection_name)
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (collection_name.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
FileElementCollectionItem *feci = new FileElementCollectionItem();
|
|
|
|
insertRow(rowForInsertItem(collection_name), feci);
|
|
|
|
feci->setPathName(collection_name);
|
|
|
|
feci->setUpData();
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief FileElementCollectionItem::setUpData
|
|
|
|
* SetUp the data of this item
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
void FileElementCollectionItem::setUpData()
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
//Setup the displayed name
|
|
|
|
localName();
|
2015-12-09 20:27:31 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
if (isDir())
|
|
|
|
setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
|
|
|
|
else
|
|
|
|
setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
|
2015-12-09 20:27:31 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
setToolTip(collectionPath());
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::setUpIcon
|
|
|
|
* SetUp the icon of this item.
|
|
|
|
* Because icon use several memory, we use this method for setup icon instead setUpData.
|
|
|
|
*/
|
|
|
|
void FileElementCollectionItem::setUpIcon()
|
2016-01-16 14:25:20 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (!icon().isNull())
|
|
|
|
return;
|
2016-01-16 14:25:20 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
if (isCollectionRoot()) {
|
|
|
|
if (m_path == QETApp::commonElementsDirN())
|
|
|
|
setIcon(QIcon(":/ico/16x16/qet.png"));
|
|
|
|
else
|
|
|
|
setIcon(QIcon(":/ico/16x16/go-home.png"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (isDir())
|
|
|
|
setIcon(QET::Icons::Folder);
|
|
|
|
else {
|
|
|
|
ElementsLocation loc(collectionPath());
|
|
|
|
setIcon(loc.icon());
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 14:25:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::setPathName
|
|
|
|
* Set the name of this item in the file system path.
|
|
|
|
* This item must have a parent, because they should be a child item of another.
|
|
|
|
* For create a new file collection see setRootPath.
|
|
|
|
* @param path_name
|
|
|
|
*/
|
2018-07-19 14:14:31 +00:00
|
|
|
void FileElementCollectionItem::setPathName(const QString& path_name, bool set_data, bool hide_element)
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
|
|
|
m_path = path_name;
|
|
|
|
|
|
|
|
//This isn't an element, we create the childs
|
|
|
|
if (!path_name.endsWith(".elmt"))
|
2016-06-05 16:34:46 +00:00
|
|
|
populate(set_data, hide_element);
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::populate
|
2016-06-05 16:34:46 +00:00
|
|
|
* Create the childs of this item
|
|
|
|
* @param set_data : if true, call setUpData for every child of this item
|
2015-12-08 16:52:10 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
void FileElementCollectionItem::populate(bool set_data, bool hide_element)
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
|
|
|
QDir dir (fileSystemPath());
|
|
|
|
|
|
|
|
//Get all directory in this directory.
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach(QString str, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
FileElementCollectionItem *feci = new FileElementCollectionItem();
|
|
|
|
appendRow(feci);
|
|
|
|
feci->setPathName(str, set_data, hide_element);
|
|
|
|
if (set_data)
|
|
|
|
feci->setUpData();
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
if (hide_element)
|
|
|
|
return;
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
//Get all elmt file in this directory
|
|
|
|
dir.setNameFilters(QStringList() << "*.elmt");
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach(QString str, dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
FileElementCollectionItem *feci = new FileElementCollectionItem();
|
|
|
|
appendRow(feci);
|
|
|
|
feci->setPathName(str, set_data);
|
|
|
|
if (set_data)
|
|
|
|
feci->setUpData();
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
}
|