2015-12-08 16:52:10 +00:00
/*
2019-01-13 16:56:12 +00:00
Copyright 2006 - 2019 The QElectroTech Team
2015-12-08 16:52:10 +00:00
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 "elementscollectionmodel.h"
# include "elementcollectionitem.h"
# include "fileelementcollectionitem.h"
2015-12-16 17:16:15 +00:00
# include "xmlprojectelementcollectionitem.h"
2016-06-05 16:34:46 +00:00
# include "qetapp.h"
2016-05-05 13:31:04 +00:00
# include "xmlelementcollection.h"
2016-06-05 16:34:46 +00:00
# include "qetproject.h"
# include "elementcollectionhandler.h"
2015-12-08 16:52:10 +00:00
2016-07-14 11:58:56 +00:00
# include <QtConcurrent>
2019-11-27 17:41:51 +01:00
# include <QFutureWatcher>
2020-01-04 15:59:27 +01:00
# include <QMessageBox>
2016-07-14 11:58:56 +00:00
2015-12-08 16:52:10 +00:00
/**
* @ brief ElementsCollectionModel : : ElementsCollectionModel
2016-06-05 16:34:46 +00:00
* Constructor
* @ param parent
2015-12-08 16:52:10 +00:00
*/
ElementsCollectionModel : : ElementsCollectionModel ( QObject * parent ) :
2016-06-05 16:34:46 +00:00
QStandardItemModel ( parent )
2015-12-08 16:52:10 +00:00
{
}
/**
2016-06-05 16:34:46 +00:00
* @ brief ElementsCollectionModel : : data
* Reimplemented from QStandardItemModel
* @ param index
* @ param role
* @ return
2015-12-08 16:52:10 +00:00
*/
2016-06-05 16:34:46 +00:00
QVariant ElementsCollectionModel : : data ( const QModelIndex & index , int role ) const
2015-12-08 16:52:10 +00:00
{
2016-06-05 16:34:46 +00:00
if ( role = = Qt : : DecorationRole ) {
QStandardItem * item = itemFromIndex ( index ) ;
2015-12-08 16:52:10 +00:00
2016-06-05 16:34:46 +00:00
if ( item - > type ( ) = = FileElementCollectionItem : : Type )
static_cast < FileElementCollectionItem * > ( item ) - > setUpIcon ( ) ;
else if ( item - > type ( ) = = XmlProjectElementCollectionItem : : Type )
static_cast < XmlProjectElementCollectionItem * > ( item ) - > setUpIcon ( ) ;
2016-05-15 14:46:01 +00:00
}
2015-12-08 16:52:10 +00:00
2016-06-05 16:34:46 +00:00
return QStandardItemModel : : data ( index , role ) ;
2015-12-08 16:52:10 +00:00
}
/**
2016-06-05 16:34:46 +00:00
* @ brief ElementsCollectionModel : : mimeData
* Reimplemented from QStandardItemModel
* @ param indexes
* @ return
2015-12-08 16:52:10 +00:00
*/
2016-06-05 16:34:46 +00:00
QMimeData * ElementsCollectionModel : : mimeData ( const QModelIndexList & indexes ) const
2015-12-08 16:52:10 +00:00
{
2016-06-05 16:34:46 +00:00
QModelIndex index = indexes . first ( ) ;
if ( index . isValid ( ) )
{
2018-07-30 15:24:29 +00:00
ElementCollectionItem * item = static_cast < ElementCollectionItem * > ( itemFromIndex ( index ) ) ;
2015-12-08 16:52:10 +00:00
2018-07-30 15:24:29 +00:00
QMimeData * mime_data = new QMimeData ( ) ;
2016-06-05 16:34:46 +00:00
mime_data - > setText ( item - > collectionPath ( ) ) ;
2016-05-15 14:46:01 +00:00
2016-06-05 16:34:46 +00:00
if ( item - > isElement ( ) )
mime_data - > setData ( " application/x-qet-element-uri " , item - > collectionPath ( ) . toLatin1 ( ) ) ;
else
mime_data - > setData ( " application/x-qet-category-uri " , item - > collectionPath ( ) . toLatin1 ( ) ) ;
2016-05-15 14:46:01 +00:00
2016-06-05 16:34:46 +00:00
return mime_data ;
2016-05-15 14:46:01 +00:00
}
2015-12-08 16:52:10 +00:00
else
2016-06-05 16:34:46 +00:00
return new QMimeData ( ) ;
2015-12-08 16:52:10 +00:00
}
/**
2016-06-05 16:34:46 +00:00
* @ brief ElementsCollectionModel : : mimeTypes
* Reimplemented from QStandardItemModel
* @ return
2015-12-08 16:52:10 +00:00
*/
2016-06-05 16:34:46 +00:00
QStringList ElementsCollectionModel : : mimeTypes ( ) const
2015-12-08 16:52:10 +00:00
{
2016-06-05 16:34:46 +00:00
QStringList mime_list = QAbstractItemModel : : mimeTypes ( ) ;
mime_list < < " application/x-qet-element-uri " < < " application/x-qet-category-uri " ;
return mime_list ;
2015-12-08 16:52:10 +00:00
}
2015-12-12 11:09:31 +00:00
/**
2016-06-05 16:34:46 +00:00
* @ brief ElementsCollectionModel : : canDropMimeData
* Reimplemented from QStandardItemModel
* @ param data
* @ param action
2015-12-12 11:09:31 +00:00
* @ param row
2016-06-05 16:34:46 +00:00
* @ param column
2015-12-12 11:09:31 +00:00
* @ param parent
2016-06-05 16:34:46 +00:00
* @ return
2015-12-12 11:09:31 +00:00
*/
2016-06-05 16:34:46 +00:00
bool ElementsCollectionModel : : canDropMimeData ( const QMimeData * data , Qt : : DropAction action , int row , int column , const QModelIndex & parent ) const
2015-12-12 11:09:31 +00:00
{
2016-06-05 16:34:46 +00:00
if ( ! ( QStandardItemModel : : canDropMimeData ( data , action , row , column , parent ) & & parent . isValid ( ) ) )
return false ;
2015-12-12 11:09:31 +00:00
2016-06-05 16:34:46 +00:00
QStandardItem * qsi = itemFromIndex ( parent . child ( row , column ) ) ;
if ( ! qsi )
qsi = itemFromIndex ( parent ) ;
2015-12-12 11:09:31 +00:00
2016-06-05 16:34:46 +00:00
//Drop in the common collection is forbiden
if ( qsi - > type ( ) = = FileElementCollectionItem : : Type )
if ( static_cast < FileElementCollectionItem * > ( qsi ) - > isCommonCollection ( ) )
return false ;
2015-12-12 11:09:31 +00:00
2018-07-30 15:24:29 +00:00
ElementCollectionItem * eci = static_cast < ElementCollectionItem * > ( qsi ) ;
2015-12-12 11:09:31 +00:00
2017-01-03 16:47:39 +00:00
if ( data - > hasFormat ( " application/x-qet-element-uri " ) | | data - > hasFormat ( " application/x-qet-category-uri " ) )
{
2016-06-05 16:34:46 +00:00
//Return false if user try to drop a item from a folder to the same folder
ElementsLocation drop_location ( data - > text ( ) ) ;
for ( int i = 0 ; i < eci - > rowCount ( ) ; i + + )
if ( static_cast < ElementCollectionItem * > ( eci - > child ( i ) ) - > collectionPath ( ) = = drop_location . collectionPath ( ) )
return false ;
return true ;
2015-12-08 16:52:10 +00:00
}
else
2016-06-05 16:34:46 +00:00
return false ;
2015-12-08 16:52:10 +00:00
}
/**
2016-06-05 16:34:46 +00:00
* @ brief ElementsCollectionModel : : dropMimeData
* Reimplemented from QStandardItemModel
* @ param data
* @ param action
* @ param row
* @ param column
* @ param parent
* @ return
2015-12-08 16:52:10 +00:00
*/
2016-06-05 16:34:46 +00:00
bool ElementsCollectionModel : : dropMimeData ( const QMimeData * data , Qt : : DropAction action , int row , int column , const QModelIndex & parent )
2015-12-08 16:52:10 +00:00
{
2020-01-04 15:59:27 +01:00
Q_UNUSED ( action )
2015-12-08 16:52:10 +00:00
2016-06-05 16:34:46 +00:00
QStandardItem * qsi = itemFromIndex ( parent . child ( row , column ) ) ;
if ( ! qsi )
qsi = itemFromIndex ( parent ) ;
2015-12-08 16:52:10 +00:00
2017-01-03 16:47:39 +00:00
if ( qsi - > type ( ) = = FileElementCollectionItem : : Type )
{
2018-07-30 15:24:29 +00:00
FileElementCollectionItem * feci = static_cast < FileElementCollectionItem * > ( qsi ) ;
2015-12-08 16:52:10 +00:00
2016-06-05 16:34:46 +00:00
if ( feci - > isCommonCollection ( ) )
return false ;
2015-12-08 16:52:10 +00:00
2016-06-05 16:34:46 +00:00
if ( feci - > isElement ( ) & & feci - > parent ( ) & & feci - > parent ( ) - > type ( ) = = FileElementCollectionItem : : Type )
feci = static_cast < FileElementCollectionItem * > ( feci - > parent ( ) ) ;
2015-12-08 16:52:10 +00:00
2016-06-05 16:34:46 +00:00
ElementCollectionHandler ech ;
2016-03-06 14:40:52 +00:00
2016-06-05 16:34:46 +00:00
ElementsLocation source ( data - > text ( ) ) ;
ElementsLocation destination ( feci - > fileSystemPath ( ) ) ;
ElementsLocation location = ech . copy ( source , destination ) ;
2016-05-22 16:28:40 +00:00
2016-06-05 16:34:46 +00:00
if ( location . exist ( ) )
{
//If feci have a child with the same path of location,
//we remove the existing child befor insert new child
for ( int i = 0 ; i < feci - > rowCount ( ) ; i + + ) {
if ( static_cast < FileElementCollectionItem * > ( feci - > child ( i ) ) - > collectionPath ( ) = = location . collectionPath ( ) )
feci - > removeRow ( i ) ;
}
feci - > addChildAtPath ( location . fileName ( ) ) ;
return true ;
}
2016-05-22 16:28:40 +00:00
2016-06-05 16:34:46 +00:00
return false ;
}
else if ( qsi - > type ( ) = = XmlProjectElementCollectionItem : : Type ) {
2018-07-30 15:24:29 +00:00
XmlProjectElementCollectionItem * xpeci = static_cast < XmlProjectElementCollectionItem * > ( qsi ) ;
2016-02-24 10:43:40 +00:00
2016-06-05 16:34:46 +00:00
if ( xpeci - > isElement ( ) & & xpeci - > parent ( ) & & xpeci - > parent ( ) - > type ( ) = = XmlProjectElementCollectionItem : : Type )
xpeci = static_cast < XmlProjectElementCollectionItem * > ( xpeci - > parent ( ) ) ;
2016-03-06 14:40:52 +00:00
2016-06-05 16:34:46 +00:00
//before do the copy, we get all collection path of xpeci child,
//for remove it if the copied item have the same path of an existing child.
//We can't do this after the copy, because at the copy if the xml collection have a DomElement with the same path,
//he was removed before the new xml DomElement is inserted
//So the existing child of this will return a null QString when call collectionPath(), because the item
//doesn't exist anymore in the xml collection.
QList < QString > child_path_list ;
for ( int i = 0 ; i < xpeci - > rowCount ( ) ; i + + )
child_path_list . append ( static_cast < XmlProjectElementCollectionItem * > ( xpeci - > child ( i , 0 ) ) - > collectionPath ( ) ) ;
2016-05-22 16:28:40 +00:00
2016-06-05 16:34:46 +00:00
ElementCollectionHandler ech ;
2016-03-06 14:40:52 +00:00
2016-06-05 16:34:46 +00:00
ElementsLocation source ( data - > text ( ) ) ;
ElementsLocation destination ( xpeci - > collectionPath ( ) ) ;
ElementsLocation location = ech . copy ( source , destination ) ;
2015-12-08 16:52:10 +00:00
2016-06-05 16:34:46 +00:00
return location . exist ( ) ;
}
2015-12-08 16:52:10 +00:00
2016-06-05 16:34:46 +00:00
return false ;
2015-12-09 20:27:31 +00:00
}
2016-07-14 11:58:56 +00:00
/**
* @ brief ElementsCollectionModel : : loadCollections
* Load the several collections in this model .
* Prefer use this method instead of addCommonCollection , addCustomCollection and addProject ,
* because it use multithreading to speed up the loading .
* This method emit loadingMaxValue ( int ) for know the maximum progress value
* This method emit loadingProgressValue ( int ) for know the current progress value
* @ param common_collection : true for load the common collection
* @ param custom_collection : true for load the custom collection
* @ param projects : list of projects to load
*/
void ElementsCollectionModel : : loadCollections ( bool common_collection , bool custom_collection , QList < QETProject * > projects )
{
2016-08-10 05:23:48 +00:00
QList < ElementCollectionItem * > list ;
2016-07-14 11:58:56 +00:00
if ( common_collection )
addCommonCollection ( false ) ;
if ( custom_collection )
addCustomCollection ( false ) ;
2016-08-10 05:23:48 +00:00
if ( common_collection | | custom_collection )
list . append ( items ( ) ) ;
2017-02-05 16:18:50 +00:00
foreach ( QETProject * project , projects ) {
2016-07-14 11:58:56 +00:00
addProject ( project , false ) ;
2016-08-10 05:23:48 +00:00
list . append ( projectItems ( project ) ) ;
}
2016-07-14 11:58:56 +00:00
2020-01-09 10:26:10 +01:00
ElementsLocation : : clearAcces ( ) ;
qDebug ( ) < < " acces count " < < ElementsLocation : : accesCount ( ) ;
2020-01-04 15:59:27 +01:00
QTime t ;
t . start ( ) ;
2016-07-14 11:58:56 +00:00
QFuture < void > futur = QtConcurrent : : map ( list , setUpData ) ;
emit loadingMaxValue ( futur . progressMaximum ( ) ) ;
while ( futur . isRunning ( ) ) {
emit loadingProgressValue ( futur . progressValue ( ) ) ;
}
2020-01-04 15:59:27 +01:00
int ms = t . elapsed ( ) ;
2020-01-09 10:26:10 +01:00
QMessageBox : : about ( nullptr , tr ( " Chargement collection d'élément " ) , tr ( " Le chargement de la collection d'éléments à été éffectué en %1 ms %2 acces " ) . arg ( ms ) . arg ( ElementsLocation : : accesCount ( ) ) ) ;
2016-07-14 11:58:56 +00:00
}
2015-12-08 16:52:10 +00:00
/**
* @ brief ElementsCollectionModel : : addCommonCollection
* Add the common elements collection to this model
*/
2016-06-05 16:34:46 +00:00
void ElementsCollectionModel : : addCommonCollection ( bool set_data )
2015-12-08 16:52:10 +00:00
{
2018-07-30 15:24:29 +00:00
FileElementCollectionItem * feci = new FileElementCollectionItem ( ) ;
2016-06-05 16:34:46 +00:00
if ( feci - > setRootPath ( QETApp : : commonElementsDirN ( ) , set_data , m_hide_element ) ) {
invisibleRootItem ( ) - > appendRow ( feci ) ;
if ( set_data )
feci - > setUpData ( ) ;
}
2015-12-08 16:52:10 +00:00
else
delete feci ;
}
/**
* @ brief ElementsCollectionModel : : addCustomCollection
* Add the custom elements collection to this model
*/
2016-06-05 16:34:46 +00:00
void ElementsCollectionModel : : addCustomCollection ( bool set_data )
2015-12-08 16:52:10 +00:00
{
2018-07-30 15:24:29 +00:00
FileElementCollectionItem * feci = new FileElementCollectionItem ( ) ;
2016-06-05 16:34:46 +00:00
if ( feci - > setRootPath ( QETApp : : customElementsDirN ( ) , set_data , m_hide_element ) ) {
invisibleRootItem ( ) - > appendRow ( feci ) ;
if ( set_data )
feci - > setUpData ( ) ;
}
2015-12-08 16:52:10 +00:00
else
delete feci ;
}
2015-12-16 17:16:15 +00:00
2016-06-17 08:41:09 +00:00
/**
* @ brief ElementsCollectionModel : : addLocation
* Add the element or directory to this model .
* If the location is already managed by this model , do nothing .
* @ param location
*/
2018-07-19 14:14:31 +00:00
void ElementsCollectionModel : : addLocation ( const ElementsLocation & location )
2016-06-17 08:41:09 +00:00
{
QModelIndex index = indexFromLocation ( location ) ;
if ( index . isValid ( ) )
return ;
ElementCollectionItem * last_item = nullptr ;
QString collection_name ;
if ( location . isProject ( ) ) {
QETProject * project = location . project ( ) ;
if ( project ) {
XmlProjectElementCollectionItem * xpeci = m_project_hash . value ( project ) ;
last_item = xpeci - > lastItemForPath ( location . collectionPath ( false ) , collection_name ) ;
}
}
else if ( location . isCustomCollection ( ) ) {
QList < ElementCollectionItem * > child_list ;
for ( int i = 0 ; i < rowCount ( ) ; i + + )
child_list . append ( static_cast < ElementCollectionItem * > ( item ( i ) ) ) ;
2017-02-05 16:18:50 +00:00
foreach ( ElementCollectionItem * eci , child_list ) {
2016-06-17 08:41:09 +00:00
if ( eci - > type ( ) = = FileElementCollectionItem : : Type ) {
2018-07-30 15:24:29 +00:00
FileElementCollectionItem * feci = static_cast < FileElementCollectionItem * > ( eci ) ;
2016-06-17 08:41:09 +00:00
if ( feci - > isCustomCollection ( ) ) {
last_item = feci - > lastItemForPath ( location . collectionPath ( false ) , collection_name ) ;
if ( last_item )
break ;
}
}
}
}
if ( last_item )
last_item - > addChildAtPath ( collection_name ) ;
}
2015-12-16 17:16:15 +00:00
/**
* @ brief ElementsCollectionModel : : addProject
2016-06-05 16:34:46 +00:00
* Add project to this model
* @ param project : project to add .
* @ param set_data : if true , setUpData is called for every ElementCollectionItem of project
2015-12-16 17:16:15 +00:00
*/
2016-06-05 16:34:46 +00:00
void ElementsCollectionModel : : addProject ( QETProject * project , bool set_data )
2015-12-16 17:16:15 +00:00
{
2016-06-05 16:34:46 +00:00
if ( m_project_list . contains ( project ) )
return ;
2015-12-16 17:16:15 +00:00
m_project_list . append ( project ) ;
int row = m_project_list . indexOf ( project ) ;
2018-07-30 15:24:29 +00:00
XmlProjectElementCollectionItem * xpeci = new XmlProjectElementCollectionItem ( ) ;
2016-06-05 16:34:46 +00:00
m_project_hash . insert ( project , xpeci ) ;
2016-07-14 11:58:56 +00:00
xpeci - > setProject ( project , set_data ) ;
2016-06-05 16:34:46 +00:00
insertRow ( row , xpeci ) ;
if ( set_data )
xpeci - > setUpData ( ) ;
2016-05-05 13:31:04 +00:00
connect ( project - > embeddedElementCollection ( ) , & XmlElementCollection : : elementAdded , this , & ElementsCollectionModel : : elementIntegratedToCollection ) ;
connect ( project - > embeddedElementCollection ( ) , & XmlElementCollection : : elementChanged , this , & ElementsCollectionModel : : updateItem ) ;
2016-07-09 18:24:40 +00:00
connect ( project - > embeddedElementCollection ( ) , & XmlElementCollection : : elementRemoved , this , & ElementsCollectionModel : : itemRemovedFromCollection ) ;
connect ( project - > embeddedElementCollection ( ) , & XmlElementCollection : : directoryRemoved , this , & ElementsCollectionModel : : itemRemovedFromCollection ) ;
2015-12-16 17:16:15 +00:00
}
2016-05-05 13:31:04 +00:00
/**
* @ brief ElementsCollectionModel : : removeProject
2016-06-05 16:34:46 +00:00
* Remove project from this model
2016-05-05 13:31:04 +00:00
* @ param project
*/
2016-06-05 16:34:46 +00:00
void ElementsCollectionModel : : removeProject ( QETProject * project )
2015-12-16 17:16:15 +00:00
{
2016-06-05 16:34:46 +00:00
if ( ! m_project_list . contains ( project ) )
return ;
2015-12-16 17:16:15 +00:00
int row = m_project_list . indexOf ( project ) ;
2016-05-05 13:31:04 +00:00
if ( removeRows ( row , 1 , QModelIndex ( ) ) ) {
2015-12-16 17:16:15 +00:00
m_project_list . removeOne ( project ) ;
2016-06-05 16:34:46 +00:00
m_project_hash . remove ( project ) ;
2016-05-05 13:31:04 +00:00
disconnect ( project - > embeddedElementCollection ( ) , & XmlElementCollection : : elementAdded , this , & ElementsCollectionModel : : elementIntegratedToCollection ) ;
2016-06-05 16:34:46 +00:00
disconnect ( project - > embeddedElementCollection ( ) , & XmlElementCollection : : elementChanged , this , & ElementsCollectionModel : : updateItem ) ;
2016-07-09 18:24:40 +00:00
disconnect ( project - > embeddedElementCollection ( ) , & XmlElementCollection : : elementRemoved , this , & ElementsCollectionModel : : itemRemovedFromCollection ) ;
disconnect ( project - > embeddedElementCollection ( ) , & XmlElementCollection : : directoryRemoved , this , & ElementsCollectionModel : : itemRemovedFromCollection ) ;
2015-12-16 17:16:15 +00:00
}
}
/**
* @ brief ElementsCollectionModel : : project
2016-06-05 16:34:46 +00:00
* @ return every project added to this model
2015-12-16 17:16:15 +00:00
*/
2016-06-05 16:34:46 +00:00
QList < QETProject * > ElementsCollectionModel : : project ( ) const
{
2015-12-16 17:16:15 +00:00
return m_project_list ;
}
2016-01-08 17:01:51 +00:00
2016-06-30 09:12:25 +00:00
/**
* @ brief ElementsCollectionModel : : highlightUnusedElement
* Highlight every unused element of managed project .
* @ See QETProject : : unusedElements ( )
*/
void ElementsCollectionModel : : highlightUnusedElement ( )
{
QList < ElementsLocation > unused ;
2017-02-05 16:18:50 +00:00
foreach ( QETProject * project , m_project_list )
2016-06-30 09:12:25 +00:00
unused . append ( project - > unusedElements ( ) ) ;
QBrush brush ;
brush . setStyle ( Qt : : Dense4Pattern ) ;
brush . setColor ( Qt : : red ) ;
2017-02-05 16:18:50 +00:00
foreach ( ElementsLocation location , unused ) {
2016-06-30 09:12:25 +00:00
QModelIndex index = indexFromLocation ( location ) ;
if ( index . isValid ( ) ) {
QStandardItem * qsi = itemFromIndex ( index ) ;
if ( qsi )
qsi - > setBackground ( brush ) ;
}
}
}
2016-05-15 14:46:01 +00:00
/**
2016-06-05 16:34:46 +00:00
* @ brief ElementsCollectionModel : : items
* @ return every ElementCollectionItem owned by this model
*/
QList < ElementCollectionItem * > ElementsCollectionModel : : items ( ) const
{
QList < ElementCollectionItem * > list ;
for ( int i = 0 ; i < rowCount ( ) ; i + + ) {
2018-07-30 15:24:29 +00:00
ElementCollectionItem * eci = static_cast < ElementCollectionItem * > ( item ( i ) ) ;
2016-06-05 16:34:46 +00:00
list . append ( eci ) ;
list . append ( eci - > items ( ) ) ;
}
return list ;
}
2016-08-10 05:23:48 +00:00
/**
* @ brief ElementsCollectionModel : : projectItems
* @ param project
* @ return return all items for project @ project . the list can be empty
*/
QList < ElementCollectionItem * > ElementsCollectionModel : : projectItems ( QETProject * project ) const
{
QList < ElementCollectionItem * > list ;
if ( m_project_list . contains ( project ) ) {
ElementCollectionItem * eci = m_project_hash . value ( project ) ;
list . append ( eci ) ;
list . append ( eci - > items ( ) ) ;
}
return list ;
}
2016-06-05 16:34:46 +00:00
/**
* @ brief ElementsCollectionModel : : hideElement
* Hide element in this model , only directory is managed
*/
void ElementsCollectionModel : : hideElement ( )
{
m_hide_element = true ;
2017-02-05 16:18:50 +00:00
foreach ( ElementCollectionItem * eci , items ( ) ) {
2016-06-05 16:34:46 +00:00
if ( eci - > isElement ( ) ) {
removeRow ( eci - > row ( ) , indexFromItem ( eci ) . parent ( ) ) ;
}
}
}
/**
* @ brief ElementsCollectionModel : : indexFromLocation
* Return the index who represent @ location .
* Index can be non valid
2016-05-15 14:46:01 +00:00
* @ param location
2016-06-05 16:34:46 +00:00
* @ return
2016-05-15 14:46:01 +00:00
*/
2016-06-05 16:34:46 +00:00
QModelIndex ElementsCollectionModel : : indexFromLocation ( const ElementsLocation & location )
2016-05-15 14:46:01 +00:00
{
QList < ElementCollectionItem * > child_list ;
2016-06-05 16:34:46 +00:00
for ( int i = 0 ; i < rowCount ( ) ; i + + )
child_list . append ( static_cast < ElementCollectionItem * > ( item ( i ) ) ) ;
2016-05-15 14:46:01 +00:00
2017-02-05 16:18:50 +00:00
foreach ( ElementCollectionItem * eci , child_list ) {
2016-05-15 14:46:01 +00:00
ElementCollectionItem * match_eci = nullptr ;
if ( eci - > type ( ) = = FileElementCollectionItem : : Type ) {
2018-07-30 15:24:29 +00:00
if ( FileElementCollectionItem * feci = static_cast < FileElementCollectionItem * > ( eci ) ) {
2016-05-15 14:46:01 +00:00
if ( ( location . isCommonCollection ( ) & & feci - > isCommonCollection ( ) ) | |
( location . isCustomCollection ( ) & & ! feci - > isCommonCollection ( ) ) ) {
match_eci = feci - > itemAtPath ( location . collectionPath ( false ) ) ;
}
}
}
else if ( eci - > type ( ) = = XmlProjectElementCollectionItem : : Type ) {
2018-07-30 15:24:29 +00:00
if ( XmlProjectElementCollectionItem * xpeci = static_cast < XmlProjectElementCollectionItem * > ( eci ) ) {
2016-05-15 14:46:01 +00:00
match_eci = xpeci - > itemAtPath ( location . collectionPath ( false ) ) ;
}
}
2016-06-05 16:34:46 +00:00
if ( match_eci )
return indexFromItem ( match_eci ) ;
2016-05-15 14:46:01 +00:00
}
return QModelIndex ( ) ;
}
/**
2016-06-05 16:34:46 +00:00
* @ brief ElementsCollectionModel : : elementIntegratedToCollection
2016-01-08 17:01:51 +00:00
* When an element is added to embedded collection of a project ,
* this method create and display the new element
2016-05-05 13:31:04 +00:00
* @ param path - The path of the new element in the embedded collection of a project
2016-01-08 17:01:51 +00:00
*/
2018-07-19 14:14:31 +00:00
void ElementsCollectionModel : : elementIntegratedToCollection ( const QString & path )
2016-01-08 17:01:51 +00:00
{
2016-05-05 13:31:04 +00:00
QObject * object = sender ( ) ;
2018-07-30 15:24:29 +00:00
XmlElementCollection * collection = static_cast < XmlElementCollection * > ( object ) ;
2016-06-05 16:34:46 +00:00
if ( ! collection )
return ;
2016-05-05 13:31:04 +00:00
QETProject * project = nullptr ;
//Get the owner project of the collection
2017-02-05 16:18:50 +00:00
foreach ( QETProject * prj , m_project_list ) {
2016-05-05 13:31:04 +00:00
if ( prj - > embeddedElementCollection ( ) = = collection ) {
project = prj ;
}
}
if ( project ) {
2016-06-05 16:34:46 +00:00
XmlProjectElementCollectionItem * xpeci = m_project_hash . value ( project ) ;
2016-05-05 13:31:04 +00:00
QString collection_name ;
ElementCollectionItem * eci = xpeci - > lastItemForPath ( path , collection_name ) ;
2016-06-05 16:34:46 +00:00
if ( ! eci )
return ;
eci - > addChildAtPath ( collection_name ) ;
2016-05-05 13:31:04 +00:00
}
}
2016-07-09 18:24:40 +00:00
/**
* @ brief ElementsCollectionModel : : itemRemovedFromCollection
* This method must be called by a signal , to get a sender .
* @ param path
*/
2018-07-19 14:14:31 +00:00
void ElementsCollectionModel : : itemRemovedFromCollection ( const QString & path )
2016-07-09 18:24:40 +00:00
{
QObject * object = sender ( ) ;
2018-07-30 15:24:29 +00:00
XmlElementCollection * collection = static_cast < XmlElementCollection * > ( object ) ;
2016-07-09 18:24:40 +00:00
if ( ! collection )
return ;
QETProject * project = nullptr ;
//Get the owner project of the collection
2017-02-05 16:18:50 +00:00
foreach ( QETProject * prj , m_project_list ) {
2016-07-09 18:24:40 +00:00
if ( prj - > embeddedElementCollection ( ) = = collection ) {
project = prj ;
}
}
if ( project ) {
QModelIndex index = indexFromLocation ( ElementsLocation ( path , project ) ) ;
if ( index . isValid ( ) )
removeRow ( index . row ( ) , index . parent ( ) ) ;
}
}
2016-05-05 13:31:04 +00:00
/**
* @ brief ElementsCollectionModel : : updateItem
* Update the item at path
* @ param path
*/
2018-07-19 14:14:31 +00:00
void ElementsCollectionModel : : updateItem ( const QString & path )
2016-05-05 13:31:04 +00:00
{
QObject * object = sender ( ) ;
2018-07-30 15:24:29 +00:00
XmlElementCollection * collection = static_cast < XmlElementCollection * > ( object ) ;
2016-06-05 16:34:46 +00:00
if ( ! collection )
return ;
2016-05-05 13:31:04 +00:00
QETProject * project = nullptr ;
//Get the owner project of the collection
2017-02-05 16:18:50 +00:00
foreach ( QETProject * prj , m_project_list ) {
2016-05-05 13:31:04 +00:00
if ( prj - > embeddedElementCollection ( ) = = collection ) {
project = prj ;
}
}
if ( project ) {
2016-06-05 16:34:46 +00:00
ElementCollectionItem * eci = m_project_hash . value ( project ) - > itemAtPath ( path ) ;
if ( ! eci )
2016-05-05 13:31:04 +00:00
return ;
eci - > clearData ( ) ;
2016-06-05 16:34:46 +00:00
eci - > setUpData ( ) ;
2016-05-05 13:31:04 +00:00
}
2016-01-08 17:01:51 +00:00
}