diff --git a/src/ad_filter.h b/src/ad_filter.h index 0b5f19db..c68fb604 100644 --- a/src/ad_filter.h +++ b/src/ad_filter.h @@ -15,7 +15,7 @@ class AdFilter : public QSortFilterProxyModel { public: explicit AdFilter(AdModel *model, QAction *advanced_view_toggle); - bool only_show_containers; + bool only_show_containers = false; private slots: void on_advanced_view_toggled(bool checked); diff --git a/src/containers_tree.cpp b/src/containers_tree.cpp index 2afe6842..9802a7a2 100644 --- a/src/containers_tree.cpp +++ b/src/containers_tree.cpp @@ -15,4 +15,22 @@ proxy(model, advanced_view_toggle) view->hideColumn(AdModel::Column::Category); view->hideColumn(AdModel::Column::Description); view->hideColumn(AdModel::Column::DN); + + connect( + view->selectionModel(), &QItemSelectionModel::selectionChanged, + this, &ContainersTree::on_selection_changed); }; + +void ContainersTree::on_selection_changed(const QItemSelection &selected, const QItemSelection &) { + // Transform selected index into source index and pass it on + // to selected_container_changed() signal + const QList indexes = selected.indexes(); + + if (indexes.size() > 0) { + QModelIndex index = indexes[0]; + QModelIndex source_index = proxy.mapToSource(index); + + emit selected_container_changed(source_index); + } +} + diff --git a/src/containers_tree.h b/src/containers_tree.h index 04d512c8..189a5bc9 100644 --- a/src/containers_tree.h +++ b/src/containers_tree.h @@ -4,16 +4,26 @@ #include "ad_filter.h" +#include + class QTreeView; class AdModel; class QAction; +class QModelIndex; // Shows names of AdModel as a tree -class ContainersTree { +class ContainersTree : public QObject { +Q_OBJECT public: ContainersTree(QTreeView *view, AdModel *model, QAction *advanced_view_toggle); +signals: + void selected_container_changed(const QModelIndex &selected); + +private slots: + void on_selection_changed(const QItemSelection &selected, const QItemSelection &); + private: QTreeView *view; AdFilter proxy; diff --git a/src/contents_list.cpp b/src/contents_list.cpp index c3d60d94..fe159f41 100644 --- a/src/contents_list.cpp +++ b/src/contents_list.cpp @@ -24,38 +24,9 @@ proxy(model, advanced_view_toggle) // Both contents and containers share the same source model, but have different proxy's to it // So need to map from containers proxy to source then back to proxy of contents -void ContentsList::set_root_index_from_selection(const QItemSelection &selected, const QItemSelection &) { - - const QList indexes = selected.indexes(); - - if (indexes.size() == 0) { - return; - } - - // Map from proxy model of given index to source model of this view (if needed) - QModelIndex source_index = indexes[0]; - { - auto proxy_model = qobject_cast(source_index.model()); - if (proxy_model != nullptr) { - source_index = proxy_model->mapToSource(source_index); - } - } - - // Map from source model of this view to proxy model of this view (if needed) - QModelIndex contents_index = source_index; - { - auto proxy_model = qobject_cast(view->model()); - if (proxy_model != nullptr) { - contents_index = proxy_model->mapFromSource(contents_index); - } - } - - if (!view->model()->checkIndex(contents_index)) { - printf("ContentsList::set_root_index_from_selection received bad index!\n"); - return; - } - - view->setRootIndex(contents_index); +void ContentsList::on_selected_container_changed(const QModelIndex &source_index) { + QModelIndex index = proxy.mapFromSource(source_index); + view->setRootIndex(index); // NOTE: have to hide columns after model update view->hideColumn(AdModel::Column::DN); diff --git a/src/contents_list.h b/src/contents_list.h index 6c127630..d620d61d 100644 --- a/src/contents_list.h +++ b/src/contents_list.h @@ -25,7 +25,7 @@ public: void dropEvent(QDropEvent *event) override; public slots: - void set_root_index_from_selection(const QItemSelection &selected, const QItemSelection &); + void on_selected_container_changed(const QModelIndex &source_index); private: QPoint drag_start_position; diff --git a/src/main.cpp b/src/main.cpp index 5491d472..ed0c015d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,8 +61,8 @@ int main(int argc, char **argv) { // Set root index of contents view to selection of containers view QObject::connect( - ui.containers_view->selectionModel(), &QItemSelectionModel::selectionChanged, - &contents_list, &ContentsList::set_root_index_from_selection); + &containers_tree, &ContainersTree::selected_container_changed, + &contents_list, &ContentsList::on_selected_container_changed); // Connect menubar "New" submenu's to entry creation dialogs QObject::connect(