2015-12-08 16:52:10 +00:00
|
|
|
/*
|
|
|
|
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 "fileelementcollectionitem.h"
|
|
|
|
#include "qetapp.h"
|
|
|
|
#include "elementslocation.h"
|
|
|
|
#include "nameslist.h"
|
|
|
|
#include "qeticons.h"
|
2016-02-13 12:51:56 +00:00
|
|
|
#include "elementcollectionhandler.h"
|
2015-12-08 16:52:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::FileElementCollectionItem
|
|
|
|
* Default constructor
|
|
|
|
* @param parent : parent item of this item
|
|
|
|
*/
|
|
|
|
FileElementCollectionItem::FileElementCollectionItem(ElementCollectionItem *parent) :
|
|
|
|
ElementCollectionItem(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::~FileElementCollectionItem
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
FileElementCollectionItem::~FileElementCollectionItem()
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
bool FileElementCollectionItem::setRootPath(QString path)
|
|
|
|
{
|
|
|
|
QDir dir(path);
|
|
|
|
|
|
|
|
if (dir.exists())
|
|
|
|
{
|
|
|
|
m_path = path;
|
|
|
|
populate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::fileSystemPath
|
|
|
|
* @return the file system path of this item
|
|
|
|
*/
|
|
|
|
QString FileElementCollectionItem::fileSystemPath() const
|
|
|
|
{
|
|
|
|
//Parent must be a file element collection item
|
|
|
|
if (!m_parent_item || m_parent_item->type() != FileElementCollectionItem::Type)
|
|
|
|
return m_path;
|
|
|
|
|
|
|
|
FileElementCollectionItem *parent = static_cast<FileElementCollectionItem*>(m_parent_item);
|
|
|
|
|
|
|
|
//Get the path of the parent.
|
2016-02-13 12:51:56 +00:00
|
|
|
// if (parent->isCollectionRoot())
|
|
|
|
// return parent->fileSystemPath() + m_path;
|
|
|
|
// else
|
|
|
|
// return parent->fileSystemPath() + "/" + m_path;
|
|
|
|
return parent->fileSystemPath() + "/" + m_path;
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::dirPath
|
|
|
|
* @return the dir path of this item
|
|
|
|
*/
|
|
|
|
QString FileElementCollectionItem::dirPath() const
|
|
|
|
{
|
|
|
|
if (isDir())
|
|
|
|
return fileSystemPath();
|
|
|
|
|
|
|
|
//Parent must be a file element collection item
|
|
|
|
if (m_parent_item->type() != FileElementCollectionItem::Type) return QString();
|
|
|
|
|
|
|
|
FileElementCollectionItem *parent = static_cast<FileElementCollectionItem*>(m_parent_item);
|
|
|
|
//Get the path of the parent.
|
|
|
|
return parent->fileSystemPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::collectionPath
|
|
|
|
* @return The path of this item relative to the collection.
|
|
|
|
*/
|
|
|
|
QString FileElementCollectionItem::collectionPath() const
|
|
|
|
{
|
|
|
|
//Parent must be a file element collection item
|
|
|
|
//else this item is the root of collection path.
|
|
|
|
if (!m_parent_item || m_parent_item->type() != FileElementCollectionItem::Type)
|
|
|
|
{
|
2016-02-13 12:51:56 +00:00
|
|
|
if (m_path == QETApp::commonElementsDirN())
|
2015-12-08 16:52:10 +00:00
|
|
|
return "common://";
|
|
|
|
else
|
|
|
|
return "custom://";
|
|
|
|
}
|
|
|
|
else if (m_parent_item->type() == FileElementCollectionItem::Type)
|
|
|
|
{
|
|
|
|
FileElementCollectionItem *feci = static_cast<FileElementCollectionItem*>(m_parent_item);
|
|
|
|
if (feci->isCollectionRoot())
|
|
|
|
return feci->collectionPath() + m_path;
|
|
|
|
else
|
|
|
|
return feci->collectionPath() + "/" + m_path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2016-01-16 14:25:20 +00:00
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::collectionName
|
|
|
|
* @return The collection name of this item
|
|
|
|
*/
|
|
|
|
QString FileElementCollectionItem::collectionName() const
|
|
|
|
{
|
|
|
|
if (isCollectionRoot()) return QString();
|
|
|
|
else return m_path;
|
|
|
|
}
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::data
|
|
|
|
* @param column
|
|
|
|
* @param role
|
|
|
|
* @return the item data at column and role
|
|
|
|
*/
|
|
|
|
QVariant FileElementCollectionItem::data(int column, int role)
|
|
|
|
{
|
|
|
|
//element collection have only one column
|
2015-12-16 17:16:15 +00:00
|
|
|
if (column > 0)
|
2015-12-08 16:52:10 +00:00
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch (role)
|
|
|
|
{
|
2015-12-09 20:27:31 +00:00
|
|
|
case Qt::DisplayRole: {
|
2015-12-08 16:52:10 +00:00
|
|
|
return name();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
{
|
|
|
|
//This item have no parent or parent isn't a file element, so it is the root of a collection
|
|
|
|
if (!m_parent_item || m_parent_item->type() != FileElementCollectionItem::Type)
|
|
|
|
{
|
2016-02-13 12:51:56 +00:00
|
|
|
if (m_path == QETApp::commonElementsDirN())
|
2015-12-08 16:52:10 +00:00
|
|
|
return QIcon(":/ico/16x16/qet.png");
|
|
|
|
else
|
|
|
|
return QIcon(":/ico/16x16/go-home.png");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDir())
|
|
|
|
return QET::Icons::Folder;
|
|
|
|
else if (isElement())
|
|
|
|
{
|
|
|
|
if (m_icon.isNull())
|
|
|
|
{
|
|
|
|
ElementLocation loc(collectionPath());
|
|
|
|
m_icon = loc.icon();
|
|
|
|
}
|
|
|
|
return m_icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
return collectionPath();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::mimeData
|
|
|
|
* @return the mime data of this item
|
|
|
|
*/
|
|
|
|
QMimeData *FileElementCollectionItem::mimeData()
|
|
|
|
{
|
|
|
|
QMimeData *mime_data = new QMimeData();
|
|
|
|
mime_data->setText(collectionPath());
|
2015-12-09 20:27:31 +00:00
|
|
|
|
|
|
|
if (isElement())
|
|
|
|
mime_data->setData("application/x-qet-element-uri", collectionPath().toLatin1());
|
|
|
|
else
|
|
|
|
mime_data->setData("application/x-qet-category-uri", collectionPath().toLatin1());
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
return mime_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::canDropMimeData
|
|
|
|
* @param data
|
|
|
|
* @param action
|
|
|
|
* @param column
|
|
|
|
* @return True if the data can be dropped
|
|
|
|
*/
|
2016-01-16 14:25:20 +00:00
|
|
|
bool FileElementCollectionItem::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column) const
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-01-16 14:25:20 +00:00
|
|
|
Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column);
|
2015-12-09 20:27:31 +00:00
|
|
|
if (isCommonCollection()) return false;
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2015-12-09 20:27:31 +00:00
|
|
|
if (data->hasFormat("application/x-qet-element-uri") || data->hasFormat("application/x-qet-category-uri"))
|
2016-02-13 12:51:56 +00:00
|
|
|
{
|
|
|
|
//Return false if user try to drop a item from a folder to the same folder
|
|
|
|
ElementLocation drop_location(data->text());
|
|
|
|
for (int i=0 ; i<childCount() ; i++)
|
|
|
|
{
|
|
|
|
if (static_cast<FileElementCollectionItem *>(child(i))->collectionPath() == drop_location.collectionPath())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
return true;
|
2016-02-13 12:51:56 +00:00
|
|
|
}
|
2015-12-08 16:52:10 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::dropMimeData
|
|
|
|
* @param data
|
|
|
|
* @param action
|
|
|
|
* @param column
|
|
|
|
* @return Handle the drop of a data
|
|
|
|
*/
|
2016-01-16 14:25:20 +00:00
|
|
|
bool FileElementCollectionItem::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column)
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-01-16 14:25:20 +00:00
|
|
|
Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(column);
|
2015-12-09 20:27:31 +00:00
|
|
|
if (isCommonCollection()) return false;
|
|
|
|
|
|
|
|
FileElementCollectionItem *feci = this;
|
|
|
|
if (isElement() && parent() && parent()->type() == FileElementCollectionItem::Type)
|
|
|
|
feci = static_cast<FileElementCollectionItem *>(parent());
|
|
|
|
|
2016-02-13 12:51:56 +00:00
|
|
|
ElementCollectionHandler ech;
|
|
|
|
|
|
|
|
ElementLocation source(data->text());
|
|
|
|
ElementLocation destination(feci->fileSystemPath());
|
|
|
|
ElementLocation location = ech.copy(source, destination);
|
|
|
|
|
|
|
|
if (location.exist())
|
|
|
|
{
|
|
|
|
//If this item have a child with the same path of location,
|
|
|
|
//we remove the existing child befor insert new child
|
|
|
|
for (int i=0 ; i<childCount() ; i++)
|
|
|
|
if (static_cast<FileElementCollectionItem *>(child(i))->collectionPath() == location.collectionPath())
|
|
|
|
removeChild(i, 1);
|
|
|
|
|
|
|
|
insertNewItem(location.fileName());
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-09 20:27:31 +00:00
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::flags
|
|
|
|
* @return the flags of this item
|
|
|
|
*/
|
|
|
|
Qt::ItemFlags FileElementCollectionItem::flags()
|
|
|
|
{
|
|
|
|
if (isDir())
|
|
|
|
return (Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
|
|
|
|
else
|
|
|
|
return (Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::isDir
|
|
|
|
* @return true if this item represent a directory
|
|
|
|
*/
|
|
|
|
bool FileElementCollectionItem::isDir() const
|
|
|
|
{
|
|
|
|
if (m_path.endsWith(".elmt"))
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::isElement
|
|
|
|
* @return true if this item represent an element
|
|
|
|
*/
|
|
|
|
bool FileElementCollectionItem::isElement() const {
|
|
|
|
return (!isDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
* @return True if this item is part of the common element collection item
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::isValid
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool FileElementCollectionItem::isValid() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
if (m_path.isEmpty())
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::name
|
|
|
|
* @return the located name of this item
|
|
|
|
*/
|
|
|
|
QString FileElementCollectionItem::name()
|
|
|
|
{
|
|
|
|
if (!m_name.isNull()) return m_name;
|
|
|
|
|
|
|
|
else if (isDir())
|
|
|
|
{
|
2015-12-09 20:27:31 +00:00
|
|
|
if (isCollectionRoot())
|
|
|
|
{
|
2016-02-13 12:51:56 +00:00
|
|
|
if (m_path == QETApp::commonElementsDirN())
|
2015-12-09 20:27:31 +00:00
|
|
|
m_name = QObject::tr("Collection QET");
|
2016-02-13 12:51:56 +00:00
|
|
|
else if (m_path == QETApp::customElementsDirN())
|
2015-12-09 20:27:31 +00:00
|
|
|
m_name = QObject::tr("Collection utilisateur");
|
|
|
|
else
|
|
|
|
m_name = QObject::tr("Collection inconnue");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Open the qet_directory file, to get the traductions name of this dir
|
|
|
|
QFile dir_conf(fileSystemPath() + "/qet_directory");
|
|
|
|
if (!dir_conf.exists())
|
|
|
|
m_name = QString("");
|
|
|
|
|
|
|
|
if (!dir_conf.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
m_name = QString("");
|
|
|
|
|
|
|
|
//Get the content of the file
|
|
|
|
QDomDocument document;
|
|
|
|
if (!document.setContent(&dir_conf))
|
|
|
|
m_name = QString("");
|
|
|
|
|
|
|
|
QDomElement root = document.documentElement();
|
|
|
|
if (root.tagName() != "qet-directory")
|
|
|
|
m_name = QString("");
|
|
|
|
|
|
|
|
//Return the name for the current langage.
|
|
|
|
NamesList nl;
|
|
|
|
nl.fromXml(root);
|
|
|
|
m_name = nl.name();
|
|
|
|
}
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
else if (isElement())
|
|
|
|
{
|
|
|
|
ElementLocation loc(collectionPath());
|
|
|
|
m_name = loc.name();
|
|
|
|
}
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
2015-12-12 11:09:31 +00:00
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::canRemoveContent
|
|
|
|
* Reimplemented from ElementCollectionItem
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool FileElementCollectionItem::canRemoveContent()
|
|
|
|
{
|
|
|
|
if (isCommonCollection()) return false;
|
|
|
|
else if (isDir() && isCollectionRoot()) return false;
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::removeContent
|
|
|
|
* Reimplemented from ElementCollectionItem
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool FileElementCollectionItem::removeContent()
|
|
|
|
{
|
|
|
|
if (!canRemoveContent()) return false;
|
|
|
|
|
|
|
|
if (isElement())
|
|
|
|
{
|
|
|
|
QFile file(fileSystemPath());
|
|
|
|
return file.remove();
|
|
|
|
}
|
|
|
|
else if (isDir() && !isCollectionRoot())
|
|
|
|
{
|
|
|
|
QDir dir (fileSystemPath());
|
|
|
|
return dir.removeRecursively();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-16 14:25:20 +00:00
|
|
|
void FileElementCollectionItem::insertNewItem(const QString &collection_name)
|
|
|
|
{
|
|
|
|
if (collection_name.isEmpty()) return;
|
|
|
|
|
|
|
|
FileElementCollectionItem *feci = new FileElementCollectionItem(this);
|
|
|
|
feci->setPathName(collection_name);
|
|
|
|
insertChild(rowForInsertItem(collection_name), feci);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
void FileElementCollectionItem::setPathName(QString path_name)
|
|
|
|
{
|
|
|
|
if (!m_parent_item) return;
|
|
|
|
|
|
|
|
m_path = path_name;
|
|
|
|
|
|
|
|
//This isn't an element, we create the childs
|
|
|
|
if (!path_name.endsWith(".elmt"))
|
|
|
|
populate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FileElementCollectionItem::populate
|
|
|
|
* Item populate itself with childs found in the system path.
|
|
|
|
*/
|
|
|
|
void FileElementCollectionItem::populate()
|
|
|
|
{
|
|
|
|
QDir dir (fileSystemPath());
|
|
|
|
|
|
|
|
//Get all directory in this directory.
|
|
|
|
foreach(QString str, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
|
|
|
|
{
|
|
|
|
FileElementCollectionItem *feci = new FileElementCollectionItem(this);
|
|
|
|
feci->setPathName(str);
|
|
|
|
appendChild(feci);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get all elmt file in this directory
|
|
|
|
dir.setNameFilters(QStringList() << "*.elmt");
|
|
|
|
foreach(QString str, dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
|
|
|
|
{
|
|
|
|
FileElementCollectionItem *feci = new FileElementCollectionItem(this);
|
|
|
|
feci->setPathName(str);
|
|
|
|
appendChild(feci);
|
|
|
|
}
|
|
|
|
}
|