qelectrotech-source-mirror/sources/ElementsCollection/fileelementcollectionitem.cpp

380 lines
8.9 KiB
C++
Raw Normal View History

/*
2020-06-15 17:42:37 +02:00
Copyright 2006-2020 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 "elementslocation.h"
#include "qetapp.h"
#include "qeticons.h"
#include <QDir>
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::FileElementCollectionItem
Constructor
*/
FileElementCollectionItem::FileElementCollectionItem()
{}
/**
2020-07-24 22:44:10 +02: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
2020-08-20 21:57:35 +02:00
@param set_data
@param hide_element
2020-07-24 22:44:10 +02:00
@return true if path exist.
*/
bool FileElementCollectionItem::setRootPath(const QString& path,
bool set_data,
bool hide_element)
{
QDir dir(path);
if (dir.exists())
{
m_path = path;
populate(set_data, hide_element);
return true;
}
return false;
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::fileSystemPath
@return the file system path of this item
*/
QString FileElementCollectionItem::fileSystemPath() const
{
if (isCollectionRoot())
return m_path;
2020-07-24 22:44:32 +02:00
FileElementCollectionItem *feci =
static_cast<FileElementCollectionItem *> (parent());
if (feci)
return feci->fileSystemPath() + "/" + m_path;
else
2020-07-24 22:44:10 +02:00
return QString();//Null string
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::dirPath
@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)
*/
QString FileElementCollectionItem::dirPath() const
{
if (isDir())
return fileSystemPath();
else if (parent() && parent()->type() == FileElementCollectionItem::Type)
return static_cast<FileElementCollectionItem*>(parent())->fileSystemPath();
else
2020-07-24 22:44:10 +02:00
return QString();//Null string
}
/**
2020-07-24 22:44:10 +02:00
@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;
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::isElement
@return true if this item represent an element
*/
bool FileElementCollectionItem::isElement() const
{
return (!isDir());
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::localName
@return the located name of this item
*/
QString FileElementCollectionItem::localName()
{
if (!text().isNull())
return text();
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"));
}
else
{
2020-01-17 19:33:13 +01:00
QString str(fileSystemPath() + "/qet_directory");
pugi::xml_document docu;
if(docu.load_file(str.toStdString().c_str()))
{
2020-07-24 22:44:32 +02:00
if (QString(docu.document_element().name())
== "qet-directory")
{
2020-01-17 19:33:13 +01:00
NamesList nl;
nl.fromXml(docu.document_element());
setText(nl.name());
}
}
}
}
else if (isElement()) {
2020-01-23 10:45:14 +01:00
ElementsLocation loc(collectionPath());
setText(loc.name());
}
return text();
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::localName
Surcharged method, unlike the default method,
avoid to create an elementLocation and so, gain time.
@param location
@return
*/
2020-01-23 10:45:14 +01:00
QString FileElementCollectionItem::localName(const ElementsLocation &location)
{
if (!text().isNull())
return text();
else if (isDir()) {
localName();
}
else if (isElement()) {
setText(location.name());
}
return text();
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::name
@return The collection name of this item
*/
QString FileElementCollectionItem::name() const
{
if (isCollectionRoot())
return QString();
else
return m_path;
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::collectionPath
@return The path of this item relative to the collection.
*/
QString FileElementCollectionItem::collectionPath() const
{
if (isCollectionRoot()) {
if (m_path == QETApp::commonElementsDirN())
return "common://";
else
return "custom://";
}
2020-07-24 22:44:32 +02: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;
}
else
return QString();
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::isCollectionRoot
@return true if this item represent the root of collection
*/
bool FileElementCollectionItem::isCollectionRoot() const
{
2020-07-24 22:44:32 +02:00
if (m_path == QETApp::commonElementsDirN()
|| m_path == QETApp::customElementsDirN())
return true;
else
return false;
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::isCommonCollection
@return True if this item represent the common collection
*/
bool FileElementCollectionItem::isCommonCollection() const
{
return fileSystemPath().startsWith(QETApp::commonElementsDirN());
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::isCustomCollection
@return True if this item represent the custom collection
*/
bool FileElementCollectionItem::isCustomCollection() const
{
return fileSystemPath().startsWith(QETApp::customElementsDirN());
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::addChildAtPath
Ask to this item item to add a child
2020-08-18 20:07:55 +02:00
with collection name collection_name
2020-07-24 22:44:10 +02:00
@param collection_name
*/
void FileElementCollectionItem::addChildAtPath(const QString &collection_name)
{
if (collection_name.isEmpty())
return;
FileElementCollectionItem *feci = new FileElementCollectionItem();
insertRow(rowForInsertItem(collection_name), feci);
feci->setPathName(collection_name);
feci->setUpData();
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::setUpData
SetUp the data of this item
*/
void FileElementCollectionItem::setUpData()
{
if (isDir())
2020-01-18 20:03:24 +01:00
{
localName();
2020-07-24 22:44:32 +02:00
setFlags(Qt::ItemIsSelectable
| Qt::ItemIsDragEnabled
| Qt::ItemIsDropEnabled
| Qt::ItemIsEnabled);
2020-01-18 20:03:24 +01:00
}
else
{
2020-07-24 22:44:32 +02:00
setFlags(Qt::ItemIsSelectable
| Qt::ItemIsDragEnabled
| Qt::ItemIsEnabled);
2020-07-24 22:44:10 +02:00
//Set the local name and all informations of the element
//in the data Qt::UserRole+1, these data will be use for search.
2020-01-23 10:45:14 +01:00
ElementsLocation loc(collectionPath());
DiagramContext context = loc.elementInformations();
QStringList search_list;
for (QString key : context.keys()) {
search_list.append(context.value(key).toString());
}
2020-01-23 10:45:14 +01:00
search_list.append(localName(loc));
setData(search_list.join(" "));
}
setToolTip(collectionPath());
}
/**
2020-07-24 22:44:10 +02: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()
{
if (!icon().isNull())
return;
if (isCollectionRoot()) {
if (m_path == QETApp::commonElementsDirN())
setIcon(QIcon(":/ico/16x16/qet.png"));
else
setIcon(QIcon(":/ico/16x16/go-home.png"));
}
2020-01-17 19:33:13 +01:00
else
{
if (isDir()) {
setIcon(QET::Icons::Folder);
2020-01-17 19:33:13 +01:00
} else {
2020-01-23 10:45:14 +01:00
ElementsLocation loc(collectionPath());
setIcon(loc.icon());
}
}
}
/**
2020-07-24 22:44:10 +02: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
@param set_data
@param hide_element
*/
void FileElementCollectionItem::setPathName(const QString& path_name,
bool set_data,
bool hide_element)
{
m_path = path_name;
2020-07-24 22:44:10 +02:00
//This isn't an element, we create the childs
if (!path_name.endsWith(".elmt"))
populate(set_data, hide_element);
}
/**
2020-07-24 22:44:10 +02:00
@brief FileElementCollectionItem::populate
Create the childs of this item
@param set_data : if true, call setUpData for every child of this item
@param hide_element
*/
void FileElementCollectionItem::populate(bool set_data, bool hide_element)
{
QDir dir (fileSystemPath());
//Get all directory in this directory.
2020-07-24 22:44:32 +02:00
for(auto str : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot,
QDir::Name))
{
FileElementCollectionItem *feci = new FileElementCollectionItem();
appendRow(feci);
feci->setPathName(str, set_data, hide_element);
if (set_data)
feci->setUpData();
}
if (hide_element)
return;
//Get all elmt file in this directory
dir.setNameFilters(QStringList() << "*.elmt");
2020-07-24 22:44:32 +02:00
for(auto str : dir.entryList(QDir::Files | QDir::NoDotAndDotDot,
QDir::Name))
{
FileElementCollectionItem *feci = new FileElementCollectionItem();
appendRow(feci);
feci->setPathName(str, set_data);
if (set_data)
feci->setUpData();
}
}