diff --git a/sources/ElementsCollection/elementscollectionmodel.cpp b/sources/ElementsCollection/elementscollectionmodel.cpp index 0aa1bdcdb..5b0be8806 100644 --- a/sources/ElementsCollection/elementscollectionmodel.cpp +++ b/sources/ElementsCollection/elementscollectionmodel.cpp @@ -241,6 +241,54 @@ void ElementsCollectionModel::addCustomCollection(bool set_data) delete feci; } +/** + * @brief ElementsCollectionModel::addLocation + * Add the element or directory to this model. + * If the location is already managed by this model, do nothing. + * @param location + */ +void ElementsCollectionModel::addLocation(ElementsLocation location) +{ + 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 child_list; + + for (int i=0 ; i(item(i))); + + foreach(ElementCollectionItem *eci, child_list) { + + if (eci->type() == FileElementCollectionItem::Type) { + FileElementCollectionItem *feci = static_cast(eci); + + if (feci->isCustomCollection()) { + last_item = feci->lastItemForPath(location.collectionPath(false), collection_name); + if(last_item) + break; + } + } + } + } + + if (last_item) + last_item->addChildAtPath(collection_name); +} + /** * @brief ElementsCollectionModel::addProject * Add project to this model diff --git a/sources/ElementsCollection/elementscollectionmodel.h b/sources/ElementsCollection/elementscollectionmodel.h index 19a0c183f..18ead9a5a 100644 --- a/sources/ElementsCollection/elementscollectionmodel.h +++ b/sources/ElementsCollection/elementscollectionmodel.h @@ -42,6 +42,7 @@ class ElementsCollectionModel : public QStandardItemModel void addCommonCollection(bool set_data = true); void addCustomCollection(bool set_data = true); + void addLocation(ElementsLocation location); void addProject(QETProject *project, bool set_data = true); void removeProject(QETProject *project); diff --git a/sources/ElementsCollection/elementscollectionwidget.cpp b/sources/ElementsCollection/elementscollectionwidget.cpp index 307ce2100..641ff6461 100644 --- a/sources/ElementsCollection/elementscollectionwidget.cpp +++ b/sources/ElementsCollection/elementscollectionwidget.cpp @@ -263,10 +263,7 @@ void ElementsCollectionWidget::editElement() app->openElementLocations(QList() << location); foreach (QETElementEditor *element_editor, app->elementEditors()) - { - if (element_editor->isEditing(location)) - connect(element_editor, &QETElementEditor::destroyed, [eci](){ eci->clearData(); eci->setUpData();}); - } + connect(element_editor, &QETElementEditor::saveToLocation, this, &ElementsCollectionWidget::locationWasSaved); } /** @@ -481,6 +478,33 @@ void ElementsCollectionWidget::reload() m_progress_bar->hide(); } +/** + * @brief ElementsCollectionWidget::locationWasSaved + * This method is connected with the signal savedToLocation of Element editor (see ElementsCollectionWidget::editElement()) + * Update or add the item represented by location to m_model + * @param location + */ +void ElementsCollectionWidget::locationWasSaved(ElementsLocation location) +{ + //Because this method update an item in the model, location must + //represente an existing element (in file system of project) + if (!location.exist()) + return; + + QModelIndex index = m_model->indexFromLocation(location); + + if (index.isValid()) { + QStandardItem *item = m_model->itemFromIndex(index); + if (item) { + static_cast(item)->clearData(); + static_cast(item)->setUpData(); + } + } + else { + m_model->addLocation(location); + } +} + /** * @brief ElementsCollectionWidget::search * Search every item (directory or element) that match the text of m_search_field diff --git a/sources/ElementsCollection/elementscollectionwidget.h b/sources/ElementsCollection/elementscollectionwidget.h index 717cd74ca..3bd79cde2 100644 --- a/sources/ElementsCollection/elementscollectionwidget.h +++ b/sources/ElementsCollection/elementscollectionwidget.h @@ -18,6 +18,8 @@ #ifndef ELEMENTSCOLLECTIONWIDGET_H #define ELEMENTSCOLLECTIONWIDGET_H +#include "elementslocation.h" + #include #include #include @@ -75,6 +77,9 @@ class ElementsCollectionWidget : public QWidget private slots: void reload(); + private: + void locationWasSaved(ElementsLocation location); + private: ElementsCollectionModel *m_model; diff --git a/sources/ElementsCollection/fileelementcollectionitem.cpp b/sources/ElementsCollection/fileelementcollectionitem.cpp index 5bba95029..521633692 100644 --- a/sources/ElementsCollection/fileelementcollectionitem.cpp +++ b/sources/ElementsCollection/fileelementcollectionitem.cpp @@ -203,6 +203,15 @@ bool FileElementCollectionItem::isCommonCollection() const return fileSystemPath().startsWith(QETApp::commonElementsDirN()); } +/** + * @brief FileElementCollectionItem::isCustomCollection + * @return True if this item represent the custom collection + */ +bool FileElementCollectionItem::isCustomCollection() const +{ + return fileSystemPath().startsWith(QETApp::customElementsDirN()); +} + /** * @brief FileElementCollectionItem::addChildAtPath * Ask to this item item to add a child with collection name @collection_name diff --git a/sources/ElementsCollection/fileelementcollectionitem.h b/sources/ElementsCollection/fileelementcollectionitem.h index d54f80055..adcf04581 100644 --- a/sources/ElementsCollection/fileelementcollectionitem.h +++ b/sources/ElementsCollection/fileelementcollectionitem.h @@ -44,6 +44,7 @@ class FileElementCollectionItem : public ElementCollectionItem virtual QString collectionPath() const; virtual bool isCollectionRoot() const; bool isCommonCollection() const; + bool isCustomCollection() const; virtual void addChildAtPath(const QString &collection_name); void setUpData(); diff --git a/sources/editor/qetelementeditor.cpp b/sources/editor/qetelementeditor.cpp index 073231d83..42211d770 100644 --- a/sources/editor/qetelementeditor.cpp +++ b/sources/editor/qetelementeditor.cpp @@ -1131,7 +1131,10 @@ bool QETElementEditor::slot_save() //Else save to the known location bool result_save = toLocation(location_); - if (result_save) ce_scene -> undoStack().setClean(); + if (result_save) { + ce_scene -> undoStack().setClean(); + emit saveToLocation(location_); + } return(result_save); } } @@ -1147,20 +1150,19 @@ bool QETElementEditor::slot_save() * @return true if save with success */ bool QETElementEditor::slot_saveAs() { - // Check element befor writing + // Check element befor writing if (checkElement()) { - // demande une localisation a l'utilisateur + //Ask a location to user ElementsLocation location = ElementDialog::getSaveElementLocation(this); if (location.isNull()) return(false); - - // tente l'enregistrement + bool result_save = toLocation(location); if (result_save) { setLocation(location); ce_scene -> undoStack().setClean(); + emit saveToLocation(location); } - - // retourne un booleen representatif de la reussite de l'enregistrement + return(result_save); } QMessageBox::critical(this, tr("Echec de l'enregistrement"), tr("L'enregistrement à échoué,\nles conditions requises ne sont pas valides")); @@ -1173,9 +1175,9 @@ bool QETElementEditor::slot_saveAs() { * @return true if save with success */ bool QETElementEditor::slot_saveAsFile() { - // Check element befor writing + // Check element befor writing if (checkElement()) { - // demande un nom de fichier a l'utilisateur pour enregistrer l'element + //Ask a filename to user, for save the element QString fn = QFileDialog::getSaveFileName( this, tr("Enregistrer sous", "dialog title"), @@ -1185,19 +1187,22 @@ bool QETElementEditor::slot_saveAsFile() { "filetypes allowed when saving an element file" ) ); - // si aucun nom n'est entre, renvoie faux. - if (fn.isEmpty()) return(false); - // si le nom ne se termine pas par l'extension .elmt, celle-ci est ajoutee - if (!fn.endsWith(".elmt", Qt::CaseInsensitive)) fn += ".elmt"; - // tente d'enregistrer le fichier + + if (fn.isEmpty()) + return(false); + + //If the name doesn't end by .elmt, we add it + if (!fn.endsWith(".elmt", Qt::CaseInsensitive)) + fn += ".elmt"; + bool result_save = toFile(fn); - // si l'enregistrement reussit, le nom du fichier est conserve + //If the save success, the filename is keep if (result_save) { setFileName(fn); QETApp::elementsRecentFiles() -> fileWasOpened(fn); ce_scene -> undoStack().setClean(); } - // retourne un booleen representatif de la reussite de l'enregistrement + return(result_save); } QMessageBox::critical(this, tr("Echec de l'enregistrement"), tr("L'enregistrement à échoué,\nles conditions requises ne sont pas valides")); diff --git a/sources/editor/qetelementeditor.h b/sources/editor/qetelementeditor.h index 5571d5cc9..72993a504 100644 --- a/sources/editor/qetelementeditor.h +++ b/sources/editor/qetelementeditor.h @@ -114,6 +114,9 @@ class QETElementEditor : public QETMainWindow { static QString getOpenElementFileName(QWidget * = 0, const QString & = QString()); void contextMenu(QPoint p); + signals: + void saveToLocation(ElementsLocation loc); + protected: void closeEvent(QCloseEvent *); virtual void firstActivation(QEvent *);