2015-12-08 16:52:10 +00:00
|
|
|
/*
|
2021-02-06 18:33:42 +01:00
|
|
|
Copyright 2006-2021 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 "elementcollectionitem.h"
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::ElementCollectionItem
|
|
|
|
Constructor
|
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
ElementCollectionItem::ElementCollectionItem()
|
2015-12-08 16:52:10 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::clearData
|
|
|
|
Reset the data
|
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
void ElementCollectionItem::clearData()
|
2016-01-08 17:01:51 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
setText(QString());
|
|
|
|
setToolTip(QString());
|
|
|
|
setIcon(QIcon());
|
2019-01-04 22:06:34 +00:00
|
|
|
setData(QString());
|
2016-01-08 17:01:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 14:25:20 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::lastItemForPath
|
2020-08-18 20:07:55 +02:00
|
|
|
Return the last existing item in this ElementCollectionItem hierarchy
|
|
|
|
according to the given path.
|
|
|
|
Next_item is the first non existing item in this hierarchy according
|
|
|
|
to the given path.
|
|
|
|
@param path : The path to find last item.
|
|
|
|
The path must be in form : path/otherPath/.../.../myElement.elmt.
|
2020-08-16 11:19:36 +02:00
|
|
|
@param no_found_path : The first item that not exist in this hierarchy
|
2020-08-18 20:07:55 +02:00
|
|
|
@return : The last item that exist in this hierarchy,
|
|
|
|
or nullptr can't find (an error was occurred, or path already exist)
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2020-08-18 20:08:32 +02:00
|
|
|
ElementCollectionItem *ElementCollectionItem::lastItemForPath(
|
|
|
|
const QString &path,
|
|
|
|
QString &no_found_path)
|
2016-01-16 14:25:20 +00:00
|
|
|
{
|
|
|
|
QStringList str_list = path.split("/");
|
|
|
|
if (str_list.isEmpty()) return nullptr;
|
|
|
|
|
|
|
|
ElementCollectionItem *return_eci = this;
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QString str, str_list)
|
2016-01-16 14:25:20 +00:00
|
|
|
{
|
|
|
|
ElementCollectionItem *eci = return_eci->childWithCollectionName(str);
|
|
|
|
if (!eci)
|
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
no_found_path = str;
|
2016-01-16 14:25:20 +00:00
|
|
|
return return_eci;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return_eci = eci;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-05 13:31:04 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::childWithCollectionName
|
2020-08-18 20:07:55 +02:00
|
|
|
Return the child with the collection name name, else return nullptr
|
2020-08-16 11:19:36 +02:00
|
|
|
@param name
|
|
|
|
@return
|
|
|
|
*/
|
2020-08-18 20:08:32 +02:00
|
|
|
ElementCollectionItem *ElementCollectionItem::childWithCollectionName(
|
|
|
|
const QString& name) const
|
2016-05-05 13:31:04 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
rowCount();
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QStandardItem *qsi, directChilds()) {
|
2018-07-30 15:24:29 +00:00
|
|
|
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
|
2016-06-05 16:34:46 +00:00
|
|
|
if (eci->name() == name)
|
|
|
|
return eci;
|
2016-05-05 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::directChilds
|
|
|
|
Return the direct child of this item
|
|
|
|
@return
|
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QList<QStandardItem *> ElementCollectionItem::directChilds() const
|
|
|
|
{
|
|
|
|
QList <QStandardItem *> item_list;
|
|
|
|
|
|
|
|
for (int i=0 ; i<rowCount() ; i++)
|
|
|
|
item_list.append(child(i));
|
|
|
|
|
|
|
|
return item_list;
|
2016-05-05 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 14:25:20 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::rowForInsertItem
|
2020-08-18 20:07:55 +02:00
|
|
|
Return the row for insert a new child item to this item with name.
|
|
|
|
@param name
|
2020-08-16 11:19:36 +02:00
|
|
|
@return
|
2020-08-18 20:07:55 +02:00
|
|
|
If row can't be found (name is null, or already exist) return -1;
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
int ElementCollectionItem::rowForInsertItem(const QString &name)
|
2016-01-16 14:25:20 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
if (name.isEmpty())
|
|
|
|
return -1;
|
2016-01-16 14:25:20 +00:00
|
|
|
|
|
|
|
QList <ElementCollectionItem *> child;
|
2020-08-18 20:07:55 +02:00
|
|
|
//The item to insert is an element we search from element child
|
2016-06-05 16:34:46 +00:00
|
|
|
if (name.endsWith(".elmt"))
|
2016-01-16 14:25:20 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
child = elementsDirectChild();
|
2016-01-16 14:25:20 +00:00
|
|
|
//There isn't element, we insert at last position
|
|
|
|
if (child.isEmpty())
|
2016-06-05 16:34:46 +00:00
|
|
|
return rowCount();
|
2016-01-16 14:25:20 +00:00
|
|
|
}
|
2020-08-18 20:07:55 +02:00
|
|
|
//The item is a directory, we search from directory child
|
2016-01-16 14:25:20 +00:00
|
|
|
else
|
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
child = directoriesDirectChild();
|
2016-01-16 14:25:20 +00:00
|
|
|
//There isn't directory, we insert at first position
|
|
|
|
if(child.isEmpty())
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (ElementCollectionItem *eci, child)
|
2016-06-05 16:34:46 +00:00
|
|
|
if (eci->name() > name)
|
|
|
|
return model()->indexFromItem(eci).row();
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
return (model()->indexFromItem(child.last()).row() + 1);
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::itemAtPath
|
|
|
|
@param path
|
|
|
|
@return the item at path or nullptr if doesn't exist
|
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
ElementCollectionItem *ElementCollectionItem::itemAtPath(const QString &path)
|
2016-02-26 09:58:55 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
QStringList str_list = path.split("/");
|
|
|
|
if (str_list.isEmpty())
|
|
|
|
return nullptr;
|
2016-02-26 09:58:55 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
ElementCollectionItem *match_eci = this;
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QString str, str_list) {
|
2016-06-05 16:34:46 +00:00
|
|
|
ElementCollectionItem *eci = match_eci->childWithCollectionName(str);
|
|
|
|
if (!eci)
|
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
match_eci = eci;
|
2016-02-26 09:58:55 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
return match_eci;
|
2016-03-24 10:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 16:52:10 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::elementsDirectChild
|
|
|
|
@return The direct element child of this item
|
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QList<ElementCollectionItem *> ElementCollectionItem::elementsDirectChild() const
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
QList <ElementCollectionItem *> element_child;
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QStandardItem *qsi, directChilds()) {
|
2018-07-30 15:24:29 +00:00
|
|
|
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
|
2016-06-05 16:34:46 +00:00
|
|
|
if (eci->isElement())
|
|
|
|
element_child.append(eci);
|
|
|
|
}
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
return element_child;
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::directoriesDirectChild
|
|
|
|
@return the direct directory child of this item
|
|
|
|
*/
|
2016-06-05 16:34:46 +00:00
|
|
|
QList<ElementCollectionItem *> ElementCollectionItem::directoriesDirectChild() const
|
2015-12-08 16:52:10 +00:00
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
QList <ElementCollectionItem *> dir_child;
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QStandardItem *qsi, directChilds()) {
|
2018-07-30 15:24:29 +00:00
|
|
|
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(qsi);
|
2016-06-05 16:34:46 +00:00
|
|
|
if (eci->isDir())
|
|
|
|
dir_child.append(eci);
|
|
|
|
}
|
2015-12-08 16:52:10 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
return dir_child;
|
2015-12-08 16:52:10 +00:00
|
|
|
}
|
2015-12-09 20:27:31 +00:00
|
|
|
|
2016-10-15 13:39:02 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::elementsChild
|
|
|
|
@return Every elements child (direct and indirect) of this item
|
|
|
|
*/
|
2016-10-15 13:39:02 +00:00
|
|
|
QList<ElementCollectionItem *> ElementCollectionItem::elementsChild() const
|
|
|
|
{
|
|
|
|
QList <ElementCollectionItem *> list = elementsDirectChild();
|
|
|
|
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (ElementCollectionItem *eci, directoriesChild())
|
2016-10-15 13:39:02 +00:00
|
|
|
list.append(eci->elementsDirectChild());
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::directoriesChild
|
|
|
|
@return Every directories child (direct and indirect) of this item
|
|
|
|
*/
|
2016-10-15 13:39:02 +00:00
|
|
|
QList<ElementCollectionItem *> ElementCollectionItem::directoriesChild() const
|
|
|
|
{
|
|
|
|
QList<ElementCollectionItem *> list = directoriesDirectChild();
|
|
|
|
QList<ElementCollectionItem *> child_list;
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (ElementCollectionItem *eci, list) {
|
2016-10-15 13:39:02 +00:00
|
|
|
child_list.append(eci->directoriesChild());
|
|
|
|
}
|
|
|
|
|
|
|
|
list.append(child_list);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2015-12-09 20:27:31 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementCollectionItem::items
|
|
|
|
@return every childs of this item (direct and indirect childs)
|
|
|
|
*/
|
2015-12-09 20:27:31 +00:00
|
|
|
QList<ElementCollectionItem *> ElementCollectionItem::items() const
|
|
|
|
{
|
2016-06-05 16:34:46 +00:00
|
|
|
QList <ElementCollectionItem *> list;
|
2015-12-12 11:09:31 +00:00
|
|
|
|
2016-06-05 16:34:46 +00:00
|
|
|
for (int i=0 ; i<rowCount() ; i++) {
|
2018-07-30 15:24:29 +00:00
|
|
|
ElementCollectionItem *eci = static_cast<ElementCollectionItem *>(child(i));
|
2016-06-05 16:34:46 +00:00
|
|
|
list.append(eci);
|
|
|
|
list.append(eci->items());
|
|
|
|
}
|
2016-01-08 17:01:51 +00:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2016-06-06 18:34:34 +00:00
|
|
|
|
2020-01-19 11:53:40 +01:00
|
|
|
void setUpData(ElementCollectionItem *eci) {
|
2016-06-06 18:34:34 +00:00
|
|
|
eci->setUpData();
|
|
|
|
}
|