diff --git a/sources/ElementsCollection/elementscollectionmodel.cpp b/sources/ElementsCollection/elementscollectionmodel.cpp index 1397c42d5..7b31c5c65 100644 --- a/sources/ElementsCollection/elementscollectionmodel.cpp +++ b/sources/ElementsCollection/elementscollectionmodel.cpp @@ -61,10 +61,22 @@ QModelIndex ElementsCollectionModel::index(int row, int column, const QModelInde parent_item = static_cast(parent.internalPointer()); ElementCollectionItem *child_item = parent_item->child(row); - if (child_item->isValid()) - return createIndex(row, column, child_item); - else + if (child_item->isValid()) { + if (m_hide_element) { + if (child_item->isDir()) { + return createIndex(row, column, child_item); + } + else { + return QModelIndex(); + } + } + else { + return createIndex(row, column, child_item); + } + } + else { return QModelIndex(); + } } /** @@ -102,7 +114,21 @@ int ElementsCollectionModel::rowCount(const QModelIndex &parent) const else parent_item = static_cast (parent.internalPointer()); - return parent_item->childCount(); + if (m_hide_element) { + int count_ = 0; + + for (int i = 0 ; ichildCount() ; i++) + { + if (parent_item->child(i)->isDir()) { + count_ ++; + } + } + + return count_; + } + else { + return parent_item->childCount(); + } } /** @@ -324,6 +350,62 @@ QList ElementsCollectionModel::project() const { return m_project_list; } +/** + * @brief ElementsCollectionModel::index + * @param location + * @return Return the index of the item represented by location. + * index can be no valid + */ +QModelIndex ElementsCollectionModel::index(const ElementsLocation &location) const +{ + if (!location.exist()) { + return QModelIndex(); + } + + QList child_list; + + for (int i=0 ; ichildCount() ; i++) { + child_list.append(m_root_item->child(i)); + } + + foreach(ElementCollectionItem *eci, child_list) { + + ElementCollectionItem *match_eci = nullptr; + + if (eci->type() == FileElementCollectionItem::Type) { + FileElementCollectionItem *feci = static_cast(eci); + if (feci) { + if ( (location.isCommonCollection() && feci->isCommonCollection()) || + (location.isCustomCollection() && !feci->isCommonCollection()) ) { + match_eci = feci->itemAtPath(location.collectionPath(false)); + } + } + } + else if (eci->type() == XmlProjectElementCollectionItem::Type) { + XmlProjectElementCollectionItem *xpeci = static_cast(eci); + if (xpeci) { + match_eci = xpeci->itemAtPath(location.collectionPath(false)); + } + } + + if (match_eci) { + return createIndex(match_eci->row(), 0, match_eci); + } + } + + return QModelIndex(); +} + +/** + * @brief ElementsCollectionModel::hideElement + * Hide element. + * Only directory is provided by the model + */ +void ElementsCollectionModel::hideElement() +{ + m_hide_element = true; +} + /** * @brief ElementsCollectionModel::itemForProject * @param project diff --git a/sources/ElementsCollection/elementscollectionmodel.h b/sources/ElementsCollection/elementscollectionmodel.h index 3fc32b4ae..bde496463 100644 --- a/sources/ElementsCollection/elementscollectionmodel.h +++ b/sources/ElementsCollection/elementscollectionmodel.h @@ -19,6 +19,7 @@ #define ELEMENTSCOLLECTIONMODEL_H #include +#include "elementslocation.h" class ElementCollectionItem; class QETProject; @@ -58,6 +59,8 @@ class ElementsCollectionModel : public QAbstractItemModel bool addProject(QETProject *project); bool removeProject(QETProject *project); QList project() const; + QModelIndex index(const ElementsLocation &location) const; + void hideElement(); private: XmlProjectElementCollectionItem *itemForProject(QETProject *project); @@ -71,6 +74,7 @@ class ElementsCollectionModel : public QAbstractItemModel ElementCollectionItem *m_root_item; QList m_project_list; QModelIndex m_parent_at_drop; + bool m_hide_element = false; }; #endif // ELEMENTSCOLLECTIONMODEL_H diff --git a/sources/ElementsCollection/elementscollectionwidget.cpp b/sources/ElementsCollection/elementscollectionwidget.cpp index 0ce5bcf4c..ff1642aa2 100644 --- a/sources/ElementsCollection/elementscollectionwidget.cpp +++ b/sources/ElementsCollection/elementscollectionwidget.cpp @@ -25,7 +25,6 @@ #include "qetmessagebox.h" #include "elementscategoryeditor.h" #include "newelementwizard.h" -#include "elementscategory.h" #include "xmlprojectelementcollectionitem.h" #include "qetproject.h" #include "qetelementeditor.h" @@ -368,16 +367,18 @@ void ElementsCollectionWidget::newElement() { ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); - if (eci->type() != FileElementCollectionItem::Type) return; + if (eci->type() != FileElementCollectionItem::Type) { + return; + } FileElementCollectionItem *feci = static_cast(eci); - if(feci->isCommonCollection()) return; - - ElementsCollectionItem *category = QETApp::collectionItem(ElementsLocation(feci->collectionPath()), false); - ElementsCategory *selected_category = category -> toCategory(); - if (!selected_category) return; + if(feci->isCommonCollection()) { + return; + } NewElementWizard elmt_wizard(this); + ElementsLocation loc(feci->collectionPath()); + elmt_wizard.preselectedLocation(loc); elmt_wizard.exec(); } diff --git a/sources/ElementsCollection/elementslocation.cpp b/sources/ElementsCollection/elementslocation.cpp index ea14bd304..d5e637380 100644 --- a/sources/ElementsCollection/elementslocation.cpp +++ b/sources/ElementsCollection/elementslocation.cpp @@ -387,6 +387,24 @@ bool ElementsLocation::isFileSystem() const return true; } +/** + * @brief ElementsLocation::isCommonCollection + * @return True if this location represent an item from the common collection + */ +bool ElementsLocation::isCommonCollection() const +{ + return fileSystemPath().startsWith(QETApp::commonElementsDirN()); +} + +/** + * @brief ElementsLocation::isCustomCollection + * @return True if this location represent an item from the custom collection + */ +bool ElementsLocation::isCustomCollection() const +{ + return fileSystemPath().startsWith(QETApp::customElementsDirN()); +} + /** * @brief ElementsLocation::isProject * @return True if this location represent an item from a project. diff --git a/sources/ElementsCollection/elementslocation.h b/sources/ElementsCollection/elementslocation.h index 0333eb715..7209e2664 100644 --- a/sources/ElementsCollection/elementslocation.h +++ b/sources/ElementsCollection/elementslocation.h @@ -61,6 +61,8 @@ class ElementsLocation bool isElement() const; bool isDirectory() const; bool isFileSystem() const; + bool isCommonCollection() const; + bool isCustomCollection() const; bool isProject() const; bool exist() const; bool isWritable() const; diff --git a/sources/newelementwizard.cpp b/sources/newelementwizard.cpp index 05f9d63b0..8cc15aeae 100644 --- a/sources/newelementwizard.cpp +++ b/sources/newelementwizard.cpp @@ -54,11 +54,26 @@ NewElementWizard::NewElementWizard(QWidget *parent, Qt::WindowFlags f) : NewElementWizard::~NewElementWizard() { } +/** + * @brief NewElementWizard::preselectedLocation + * Select item in the tree view represented by location, + * @param location + */ +void NewElementWizard::preselectedLocation(const ElementsLocation &location) +{ + QModelIndex index = m_model->index(location); + if (index.isValid()) { + m_tree_view->scrollTo(index); + m_tree_view->setCurrentIndex(index); + } +} + /** * @brief NewElementWizard::buildStep1 * @return */ -QWizardPage *NewElementWizard::buildStep1() { +QWizardPage *NewElementWizard::buildStep1() +{ QWizardPage *page = new QWizardPage(); page -> setProperty("WizardState", Category); page -> setTitle(tr("Étape 1/3 : Catégorie parente", "wizard page title")); @@ -66,14 +81,17 @@ QWizardPage *NewElementWizard::buildStep1() { QVBoxLayout *layout = new QVBoxLayout(); m_tree_view = new QTreeView(this); - ElementsCollectionModel *model_ = new ElementsCollectionModel(m_tree_view); - model_->addCustomCollection(); - m_tree_view->setModel(model_); - m_tree_view->setHeaderHidden(true); - layout->addWidget(m_tree_view); + m_model = new ElementsCollectionModel(m_tree_view); + m_model->hideElement(); + m_model->addCustomCollection(); + + m_tree_view->setModel(m_model); + m_tree_view->setHeaderHidden(true); + m_tree_view->setAnimated(true); + layout->addWidget(m_tree_view); - page -> setLayout(layout); + page->setLayout(layout); return(page); } diff --git a/sources/newelementwizard.h b/sources/newelementwizard.h index e5d532b1a..ded66c10e 100644 --- a/sources/newelementwizard.h +++ b/sources/newelementwizard.h @@ -24,6 +24,7 @@ class NamesListWidget; class QFileNameEdit; class QTreeView; +class ElementsCollectionModel; /** This class provides a wizard dialog enabling users to to specify the basic @@ -34,34 +35,38 @@ class QTreeView; - the filename the element should be saved to - localized names */ -class NewElementWizard : public QWizard { +class NewElementWizard : public QWizard +{ Q_OBJECT - // constructors, destructor + // constructors, destructor public: - NewElementWizard(QWidget * = 0, Qt::WindowFlags = 0); - virtual ~NewElementWizard(); + NewElementWizard(QWidget * = 0, Qt::WindowFlags = 0); + virtual ~NewElementWizard(); + + void preselectedLocation(const ElementsLocation &location); private: - NewElementWizard(const NewElementWizard &); + NewElementWizard(const NewElementWizard &); - // attributes + // attributes private: - enum WizardState { Category, Filename, Names }; - QFileNameEdit *m_qle_filename; - NamesListWidget *m_names_list; - QString m_chosen_file; - QTreeView *m_tree_view = nullptr; - ElementsLocation m_chosen_location; + enum WizardState { Category, Filename, Names }; + QFileNameEdit *m_qle_filename; + NamesListWidget *m_names_list; + QString m_chosen_file; + QTreeView *m_tree_view = nullptr; + ElementsLocation m_chosen_location; + ElementsCollectionModel *m_model = nullptr; - // methods + // methods private: - QWizardPage *buildStep1(); - QWizardPage *buildStep2(); - QWizardPage *buildStep3(); - bool validStep1(); - bool validStep2(); - bool validateCurrentPage(); - void createNewElement(); + QWizardPage *buildStep1(); + QWizardPage *buildStep2(); + QWizardPage *buildStep3(); + bool validStep1(); + bool validStep2(); + bool validateCurrentPage(); + void createNewElement(); }; #endif