2015-12-16 17:16:15 +00:00
|
|
|
/*
|
2020-06-15 17:42:37 +02:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2016-06-05 16:34:46 +00:00
|
|
|
This file is part of QElectroTech.
|
2015-12-16 17:16:15 +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-16 17:16:15 +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-16 17:16:15 +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-16 17:16:15 +00:00
|
|
|
*/
|
|
|
|
#include "qetproject.h"
|
|
|
|
#include "xmlelementcollection.h"
|
2016-06-05 16:34:46 +00:00
|
|
|
#include "qeticons.h"
|
|
|
|
#include "xmlprojectelementcollectionitem.h"
|
2015-12-16 17:16:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief XmlProjectElementCollectionItem::XmlProjectElementCollectionItem
|
2016-06-05 16:34:46 +00:00
|
|
|
* Constructor
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
XmlProjectElementCollectionItem::XmlProjectElementCollectionItem()
|
|
|
|
{}
|
2015-12-16 17:16:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::isDir
|
|
|
|
* @return true if this item represent a directory
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
bool XmlProjectElementCollectionItem::isDir() const
|
2015-12-16 17:16:15 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (m_dom_element.tagName() == "category") return true;
|
|
|
|
else return false;
|
2015-12-16 17:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::isElement
|
|
|
|
* @return true if this item represent an element
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
bool XmlProjectElementCollectionItem::isElement() const
|
2016-03-31 17:28:44 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (m_dom_element.tagName() == "element") return true;
|
|
|
|
else return false;
|
2016-03-31 17:28:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-16 17:16:15 +00:00
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::localName
|
|
|
|
* @return the located name of this item
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QString XmlProjectElementCollectionItem::localName()
|
2015-12-16 17:16:15 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (!text().isNull())
|
|
|
|
return text();
|
2015-12-16 17:16:15 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
if (isCollectionRoot()) {
|
|
|
|
if (m_project->title().isEmpty())
|
|
|
|
setText(QObject::tr("Projet sans titre"));
|
|
|
|
else
|
|
|
|
setText(m_project->title());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ElementsLocation location (embeddedPath(), m_project);
|
|
|
|
setText(location.name());
|
|
|
|
}
|
2015-12-16 17:16:15 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
return text();
|
2015-12-16 17:16:15 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 18:53:40 +00:00
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::name
|
|
|
|
* @return The collection name of this item
|
2016-02-21 18:53:40 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QString XmlProjectElementCollectionItem::name() const
|
2016-02-21 18:53:40 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
return m_dom_element.attribute("name");
|
2016-02-21 18:53:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::collectionPath
|
|
|
|
* @return The path of this item relative to the collection.
|
2016-02-21 18:53:40 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QString XmlProjectElementCollectionItem::collectionPath() const
|
2016-02-21 18:53:40 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
ElementsLocation loc (embeddedPath(), m_project);
|
|
|
|
QString p = loc.projectCollectionPath();
|
|
|
|
if (p.isEmpty())
|
|
|
|
p = QObject::tr("Collection");
|
|
|
|
return p;
|
2016-02-21 18:53:40 +00:00
|
|
|
}
|
|
|
|
|
2015-12-16 17:16:15 +00:00
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::embeddedPath
|
|
|
|
* @return The embedde path of this item
|
|
|
|
* The path is in form : embed://dir/subdir/myElement.elmt
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QString XmlProjectElementCollectionItem::embeddedPath() const
|
2015-12-16 17:16:15 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (isCollectionRoot())
|
|
|
|
return "embed://";
|
|
|
|
else if (parent()){
|
2018-07-30 15:24:29 +00:00
|
|
|
XmlProjectElementCollectionItem *xpeci = static_cast<XmlProjectElementCollectionItem *>(parent());
|
2016-06-05 16:34:46 +00:00
|
|
|
|
|
|
|
if (xpeci->isCollectionRoot())
|
|
|
|
return xpeci->embeddedPath() + name();
|
|
|
|
else
|
|
|
|
return xpeci->embeddedPath() + "/" + name();
|
|
|
|
}
|
2015-12-16 17:16:15 +00:00
|
|
|
else
|
2016-06-05 16:34:46 +00:00
|
|
|
return QString();
|
2015-12-16 17:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief XmlProjectElementCollectionItem::isCollectionRoot
|
2016-06-05 16:34:46 +00:00
|
|
|
* @return true if this item represent the root of collection
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
|
|
|
bool XmlProjectElementCollectionItem::isCollectionRoot() const
|
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (!parent())
|
|
|
|
return true;
|
|
|
|
else if (parent()->type() != XmlProjectElementCollectionItem::Type)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2015-12-16 17:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::addChildAtPath
|
2016-08-16 20:25:27 +00:00
|
|
|
* Ask to this item item to add a new child with collection name @collection_name
|
|
|
|
* (the child must exist in the xml element collection)
|
|
|
|
* @param collection_name : name of the child item to add.
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
void XmlProjectElementCollectionItem::addChildAtPath(const QString &collection_name)
|
2015-12-16 17:16:15 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (collection_name.isEmpty())
|
|
|
|
return;
|
2015-12-16 17:16:15 +00:00
|
|
|
|
2016-08-16 20:25:27 +00:00
|
|
|
QString str (collection_name.endsWith(".elmt")? "element" : "category");
|
|
|
|
QDomElement child_element = m_dom_element.firstChildElement(str);
|
|
|
|
|
|
|
|
while (!child_element.isNull()) {
|
|
|
|
if (child_element.attribute("name") == collection_name) {
|
2018-07-30 15:24:29 +00:00
|
|
|
XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem ();
|
2016-08-16 20:25:27 +00:00
|
|
|
insertRow(rowForInsertItem(collection_name), xpeci);
|
|
|
|
xpeci->setXmlElement(child_element, m_project);
|
|
|
|
xpeci->setUpData();
|
|
|
|
return;
|
2016-06-05 16:34:46 +00:00
|
|
|
}
|
2016-08-16 20:25:27 +00:00
|
|
|
else
|
|
|
|
child_element = child_element.nextSiblingElement(str);
|
2015-12-16 17:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief XmlProjectElementCollectionItem::project
|
2016-06-05 16:34:46 +00:00
|
|
|
* @return the paretn project of the managed collection
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QETProject *XmlProjectElementCollectionItem::project() const
|
|
|
|
{
|
2015-12-16 17:16:15 +00:00
|
|
|
return m_project;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::setProject
|
|
|
|
* Set the project for this item.
|
|
|
|
* Use this method for set this item the root of the collection
|
|
|
|
* @param project : project to manage the collection
|
|
|
|
* @param set_data : if true, call setUpData for every child of this item
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
void XmlProjectElementCollectionItem::setProject(QETProject *project, bool set_data, bool hide_element)
|
2015-12-16 17:16:15 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (m_project)
|
|
|
|
return;
|
2015-12-16 17:16:15 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
m_project = project;
|
|
|
|
m_dom_element = project->embeddedElementCollection()->root();
|
|
|
|
populate(set_data, hide_element);
|
2015-12-16 17:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::setUpData
|
|
|
|
* SetUp the data of this item
|
2015-12-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
void XmlProjectElementCollectionItem::setUpData()
|
2015-12-16 17:16:15 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
//Setup the displayed name
|
|
|
|
localName();
|
2015-12-30 09:39:22 +00:00
|
|
|
|
2019-01-04 22:06:34 +00:00
|
|
|
if (isDir()) {
|
2016-06-05 16:34:46 +00:00
|
|
|
setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
|
2019-01-04 22:06:34 +00:00
|
|
|
}
|
2015-12-30 09:39:22 +00:00
|
|
|
else
|
2019-01-04 22:06:34 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
|
2019-01-04 22:06:34 +00:00
|
|
|
|
|
|
|
//Set the local name and all informations of the element
|
|
|
|
//in the data Qt::UserRole+1, these data will be use for search.
|
|
|
|
ElementsLocation location(embeddedPath(), m_project);
|
|
|
|
DiagramContext context = location.elementInformations();
|
|
|
|
QStringList search_list;
|
|
|
|
for (QString key : context.keys()) {
|
|
|
|
search_list.append(context.value(key).toString());
|
|
|
|
}
|
|
|
|
search_list.append(localName());
|
|
|
|
setData(search_list.join(" "));
|
|
|
|
}
|
2015-12-30 09:39:22 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
setToolTip(collectionPath());
|
2016-01-08 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 16:34:46 +00:00
|
|
|
* @brief XmlProjectElementCollectionItem::setUpIcon
|
|
|
|
* SetUp the icon of this item.
|
|
|
|
* Because icon use several memory, we use this method for setup icon instead setUpData.
|
2016-01-08 17:01:51 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
void XmlProjectElementCollectionItem::setUpIcon()
|
2016-01-08 17:01:51 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (!icon().isNull())
|
|
|
|
return;
|
2016-01-08 17:01:51 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
if (isCollectionRoot())
|
|
|
|
setIcon(QET::Icons::ProjectFileGP);
|
|
|
|
else if (isDir())
|
|
|
|
setIcon(QET::Icons::Folder);
|
|
|
|
else {
|
|
|
|
ElementsLocation loc(embeddedPath(), m_project);
|
|
|
|
setIcon(loc.icon());
|
2015-12-16 17:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief XmlProjectElementCollectionItem::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-16 17:16:15 +00:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
void XmlProjectElementCollectionItem::populate(bool set_data, bool hide_element)
|
2015-12-16 17:16:15 +00:00
|
|
|
{
|
2016-01-08 17:01:51 +00:00
|
|
|
QList <QDomElement> dom_category = m_project->embeddedElementCollection()->directories(m_dom_element);
|
2015-12-16 17:16:15 +00:00
|
|
|
std::sort(dom_category.begin(), dom_category.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));});
|
|
|
|
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QDomElement element, dom_category)
|
2015-12-16 17:16:15 +00:00
|
|
|
{
|
2018-07-30 15:24:29 +00:00
|
|
|
XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem();
|
2016-06-05 16:34:46 +00:00
|
|
|
appendRow(xpeci);
|
|
|
|
xpeci->setXmlElement(element, m_project, set_data, hide_element);
|
|
|
|
if (set_data)
|
|
|
|
xpeci->setUpData();
|
2015-12-16 17:16:15 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
if (hide_element)
|
|
|
|
return;
|
|
|
|
|
2015-12-16 17:16:15 +00:00
|
|
|
QList <QDomElement> dom_elements = m_project->embeddedElementCollection()->elements(m_dom_element);
|
|
|
|
std::sort(dom_elements.begin(), dom_elements.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));});
|
|
|
|
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QDomElement element, dom_elements)
|
2015-12-16 17:16:15 +00:00
|
|
|
{
|
2018-07-30 15:24:29 +00:00
|
|
|
XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem();
|
2016-06-05 16:34:46 +00:00
|
|
|
appendRow(xpeci);
|
|
|
|
xpeci->setXmlElement(element, m_project, set_data);
|
|
|
|
if (set_data)
|
|
|
|
xpeci->setUpData();
|
2015-12-16 17:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 16:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief XmlProjectElementCollectionItem::setXmlElement
|
|
|
|
* Set the managed content of this item
|
|
|
|
* @param element : the dom element (directory or element), to be managed by this item
|
|
|
|
* @param project : the parent project of managed collection
|
|
|
|
* @param set_data : if true, call setUpData for every child of this item
|
|
|
|
*/
|
2018-07-19 14:14:31 +00:00
|
|
|
void XmlProjectElementCollectionItem::setXmlElement(const QDomElement& element, QETProject *project, bool set_data, bool hide_element)
|
2016-06-05 16:34:46 +00:00
|
|
|
{
|
|
|
|
m_dom_element = element;
|
|
|
|
m_project = project;
|
|
|
|
populate(set_data, hide_element);
|
|
|
|
}
|