diff --git a/sources/ElementsCollection/elementscollectionmodel.cpp b/sources/ElementsCollection/elementscollectionmodel.cpp index 10f0b421c..6299e2326 100644 --- a/sources/ElementsCollection/elementscollectionmodel.cpp +++ b/sources/ElementsCollection/elementscollectionmodel.cpp @@ -311,6 +311,8 @@ void ElementsCollectionModel::addProject(QETProject *project, bool set_data) xpeci->setUpData(); connect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection); connect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem); + connect(project->embeddedElementCollection(), &XmlElementCollection::elementRemoved, this, &ElementsCollectionModel::itemRemovedFromCollection); + connect(project->embeddedElementCollection(), &XmlElementCollection::directoryRemoved, this, &ElementsCollectionModel::itemRemovedFromCollection); } /** @@ -329,6 +331,8 @@ void ElementsCollectionModel::removeProject(QETProject *project) m_project_hash.remove(project); disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection); disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem); + disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementRemoved, this, &ElementsCollectionModel::itemRemovedFromCollection); + disconnect(project->embeddedElementCollection(), &XmlElementCollection::directoryRemoved, this, &ElementsCollectionModel::itemRemovedFromCollection); } } @@ -407,9 +411,6 @@ void ElementsCollectionModel::hideElement() */ QModelIndex ElementsCollectionModel::indexFromLocation(const ElementsLocation &location) { - if (!location.exist()) - return QModelIndex(); - QList child_list; for (int i=0 ; i (object); + if (!collection) + return; + + QETProject *project = nullptr; + + //Get the owner project of the collection + foreach (QETProject *prj, m_project_list) { + if (prj->embeddedElementCollection() == collection) { + project = prj; + } + } + + if (project) { + QModelIndex index = indexFromLocation(ElementsLocation(path, project)); + if (index.isValid()) + removeRow(index.row(), index.parent()); + } +} + /** * @brief ElementsCollectionModel::updateItem * Update the item at path diff --git a/sources/ElementsCollection/elementscollectionmodel.h b/sources/ElementsCollection/elementscollectionmodel.h index e64449cb8..e7002dab7 100644 --- a/sources/ElementsCollection/elementscollectionmodel.h +++ b/sources/ElementsCollection/elementscollectionmodel.h @@ -28,7 +28,6 @@ template<> class QHash; template<> class QList; - class ElementsCollectionModel : public QStandardItemModel { Q_OBJECT @@ -59,6 +58,7 @@ class ElementsCollectionModel : public QStandardItemModel private: void elementIntegratedToCollection (QString path); + void itemRemovedFromCollection (QString path); void updateItem (QString path); private: diff --git a/sources/ElementsCollection/xmlelementcollection.cpp b/sources/ElementsCollection/xmlelementcollection.cpp index 3d2282df7..d06732036 100644 --- a/sources/ElementsCollection/xmlelementcollection.cpp +++ b/sources/ElementsCollection/xmlelementcollection.cpp @@ -432,6 +432,26 @@ bool XmlElementCollection::addElementDefinition(const QString &dir_path, const Q return true; } +/** + * @brief XmlElementCollection::removeElement + * Remove the element at path @path. + * @param path + * @return True if element is removed and emit the signal elementRemoved. + * else false. + */ +bool XmlElementCollection::removeElement(QString path) +{ + QDomElement elmt = element(path); + + if (!elmt.isNull()) { + elmt.parentNode().removeChild(elmt); + emit elementRemoved(path); + return true; + } + + return false; +} + /** * @brief XmlElementCollection::copy * Copy the content represented by source (an element or a directory) to destination. @@ -502,6 +522,24 @@ bool XmlElementCollection::createDir(QString path, QString name, const NamesList return true; } +/** + * @brief XmlElementCollection::removeDir + * Remove the directory at path @path. + * @param path + * @return true if successfuly removed and emit directoryRemoved(QString), + * else false. + */ +bool XmlElementCollection::removeDir(QString path) +{ + QDomElement dir = directory(path); + if (!dir.isNull()) { + dir.parentNode().removeChild(dir); + emit directoryRemoved(path); + return true; + } + return false; +} + /** * @brief XmlElementCollection::elementsLocation * Return all locations stored in dom_element (element and directory). @@ -571,6 +609,34 @@ ElementsLocation XmlElementCollection::domToLocation(QDomElement dom_element) co return ElementsLocation(); } +/** + * @brief XmlElementCollection::cleanUnusedElement + * Remove elements in this collection which is not used in the owner project + */ +void XmlElementCollection::cleanUnusedElement() +{ + foreach (ElementsLocation loc, m_project->unusedElements()) + removeElement(loc.collectionPath(false)); +} + +/** + * @brief XmlElementCollection::cleanUnusedDirectory + * Remove the empty directories of this collection + */ +void XmlElementCollection::cleanUnusedDirectory() +{ + QDomNodeList lst = importCategory().elementsByTagName("category"); + + for(int i=0 ; i elementsLocation (QDomElement dom_element = QDomElement(), bool childs = true) const; ElementsLocation domToLocation(QDomElement dom_element) const; + void cleanUnusedElement(); + void cleanUnusedDirectory(); + private: ElementsLocation copyDirectory(ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true); ElementsLocation copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename = QString()); @@ -74,12 +79,24 @@ class XmlElementCollection : public QObject * @param collection_path, the path of this element in this collection */ void elementChanged (QString collection_path); + /** + * @brief elementRemoved + * This signal is emited when an element is removed to this collection + * @param collection_path, the path of the removed element in this collection + */ + void elementRemoved(QString collection_path); /** * @brief directorieAdded * This signal is emited when a directorie is added to this collection * @param collection_path, the path of the new directorie */ void directorieAdded(QString collection_path); + /** + * @brief directoryRemoved + * This signal is emited when a directory is removed to this collection + * @param collection_path, the path of the removed directory + */ + void directoryRemoved(QString collection_path); private: QDomDocument m_dom_document; diff --git a/sources/projectview.cpp b/sources/projectview.cpp index d43932f36..75da2dca8 100644 --- a/sources/projectview.cpp +++ b/sources/projectview.cpp @@ -31,6 +31,7 @@ #include "qettemplateeditor.h" #include "diagramfoliolist.h" #include "projectpropertiesdialog.h" +#include "xmlelementcollection.h" /** Constructeur @@ -806,6 +807,12 @@ int ProjectView::cleanProject() { if (clean_tbt -> isChecked()) { m_project->embeddedTitleBlockTemplatesCollection()->deleteUnusedTitleBlocKTemplates(); } + if (clean_elements->isChecked()) { + m_project->embeddedElementCollection()->cleanUnusedElement(); + } + if (clean_categories->isChecked()) { + m_project->embeddedElementCollection()->cleanUnusedDirectory(); + } } return(clean_count);