diff --git a/src/ad_filter.cpp b/src/ad_filter.cpp index 1fd48bf9..9a2d327a 100644 --- a/src/ad_filter.cpp +++ b/src/ad_filter.cpp @@ -4,16 +4,17 @@ #include <QAction> -AdFilter::AdFilter(const QAction * const advanced_view_action, bool only_show_containers) { - this->only_show_containers = only_show_containers; +AdFilter::AdFilter(AdModel *model, QAction *advanced_view_toggle) { + this->setSourceModel(model); + connect(advanced_view_toggle, &QAction::toggled, + this, &AdFilter::on_advanced_view_toggled); +} + +void AdFilter::on_advanced_view_toggled(bool checked) { // On advanced view toggle, copy advanced view flag and invalidate filter - connect(advanced_view_action, &QAction::toggled, - [this](bool checked) - { - this->advanced_view = checked; - this->invalidateFilter(); - }); + advanced_view = checked; + this->invalidateFilter(); } bool AdFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { diff --git a/src/ad_filter.h b/src/ad_filter.h index 42b60b3a..0b5f19db 100644 --- a/src/ad_filter.h +++ b/src/ad_filter.h @@ -13,13 +13,18 @@ class AdModel; // Connected to advanced view toggle in menubar class AdFilter : public QSortFilterProxyModel { public: - explicit AdFilter(const QAction * const advanced_view_action, bool only_show_containers = false); + explicit AdFilter(AdModel *model, QAction *advanced_view_toggle); + + bool only_show_containers; + +private slots: + void on_advanced_view_toggled(bool checked); private: bool advanced_view = false; - bool only_show_containers; bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; + }; #endif /* AD_FILTER_H */ diff --git a/src/ad_model.cpp b/src/ad_model.cpp index 27c2951a..8185c696 100644 --- a/src/ad_model.cpp +++ b/src/ad_model.cpp @@ -120,6 +120,19 @@ AdModel::AdModel(): QStandardItemModel(0, Column::COUNT) { QStandardItem *invis_root = this->invisibleRootItem(); auto head_dn = QString(HEAD_DN); load_and_add_row(invis_root, head_dn); + + QObject::connect( + &ad_interface, &AdInterface::entry_deleted, + this, &AdModel::on_entry_deleted); + QObject::connect( + &ad_interface, &AdInterface::entry_changed, + this, &AdModel::on_entry_changed); + QObject::connect( + &ad_interface, &AdInterface::user_moved, + this, &AdModel::on_user_moved); + QObject::connect( + &ad_interface, &AdInterface::entry_created, + this, &AdModel::on_entry_created); } bool AdModel::canFetchMore(const QModelIndex &parent) const { diff --git a/src/ad_model.h b/src/ad_model.h index 47ff7a47..4818bb8e 100644 --- a/src/ad_model.h +++ b/src/ad_model.h @@ -28,7 +28,7 @@ public: void fetchMore(const QModelIndex &parent); bool hasChildren(const QModelIndex &parent) const override; -public slots: +private slots: void on_entry_changed(const QString &dn); void on_entry_deleted(const QString &dn); void on_user_moved(const QString &old_dn, const QString &new_dn, const QString &new_parent_dn); diff --git a/src/attributes_list.h b/src/attributes_list.h index b26aca02..c9e01385 100644 --- a/src/attributes_list.h +++ b/src/attributes_list.h @@ -15,7 +15,7 @@ class AttributesList : public QObject { Q_OBJECT public: - explicit AttributesList(QTreeView *view); + AttributesList(QTreeView *view); AttributesModel model; diff --git a/src/attributes_model.cpp b/src/attributes_model.cpp index b4338f13..66fa48dc 100644 --- a/src/attributes_model.cpp +++ b/src/attributes_model.cpp @@ -5,6 +5,10 @@ AttributesModel::AttributesModel(): QStandardItemModel(0, Column::COUNT) { change_target(QString("")); + + QObject::connect( + &ad_interface, &AdInterface::entry_deleted, + this, &AttributesModel::on_entry_deleted); } // This will be called when an attribute value is edited diff --git a/src/attributes_model.h b/src/attributes_model.h index e88d220a..a33ea504 100644 --- a/src/attributes_model.h +++ b/src/attributes_model.h @@ -22,7 +22,7 @@ public: signals: void entry_changed(const QString &dn); -public slots: +private slots: void on_entry_deleted(const QString &dn); private: diff --git a/src/containers_tree.cpp b/src/containers_tree.cpp index a1755f14..2afe6842 100644 --- a/src/containers_tree.cpp +++ b/src/containers_tree.cpp @@ -1,14 +1,17 @@ #include "containers_tree.h" -#include "ad_filter.h" #include "ad_model.h" #include <QTreeView> -ContainersTree::ContainersTree(QTreeView *view, AdFilter *proxy) { +ContainersTree::ContainersTree(QTreeView *view, AdModel *model, QAction *advanced_view_toggle): +proxy(model, advanced_view_toggle) +{ this->view = view; - view->setModel(proxy); + proxy.only_show_containers = true; + + view->setModel(&proxy); view->hideColumn(AdModel::Column::Category); view->hideColumn(AdModel::Column::Description); view->hideColumn(AdModel::Column::DN); diff --git a/src/containers_tree.h b/src/containers_tree.h index ba79188e..04d512c8 100644 --- a/src/containers_tree.h +++ b/src/containers_tree.h @@ -2,17 +2,21 @@ #ifndef CONTAINERS_VIEW_H #define CONTAINERS_VIEW_H +#include "ad_filter.h" + class QTreeView; -class AdFilter; +class AdModel; +class QAction; // Shows names of AdModel as a tree class ContainersTree { public: - ContainersTree(QTreeView *view, AdFilter *proxy); + ContainersTree(QTreeView *view, AdModel *model, QAction *advanced_view_toggle); private: QTreeView *view; + AdFilter proxy; }; diff --git a/src/contents_list.cpp b/src/contents_list.cpp index d85aaa73..c3d60d94 100644 --- a/src/contents_list.cpp +++ b/src/contents_list.cpp @@ -1,8 +1,8 @@ #include "contents_list.h" #include "ad_interface.h" -#include "ad_filter.h" #include "ad_model.h" +#include "ad_filter.h" #include <QApplication> #include <QItemSelection> @@ -12,10 +12,13 @@ #include <QMimeData> #include <QTreeView> -ContentsList::ContentsList(QTreeView *view, AdFilter *proxy): QWidget() { +ContentsList::ContentsList(QTreeView *view, AdModel* model, QAction *advanced_view_toggle) : +QWidget(), +proxy(model, advanced_view_toggle) +{ this->view = view; - view->setModel(proxy); + view->setModel(&proxy); view->hideColumn(AdModel::Column::DN); }; @@ -65,7 +68,7 @@ void ContentsList::set_root_index_from_selection(const QItemSelection &selected, // probably from dragging being started incorrectly void ContentsList::mousePressEvent(QMouseEvent *event) { // view->mousePressEvent(event); - + // Record drag position if (event->button() == Qt::LeftButton) { drag_start_position = event->pos(); @@ -74,7 +77,7 @@ void ContentsList::mousePressEvent(QMouseEvent *event) { void ContentsList::mouseMoveEvent(QMouseEvent *event) { // view->mouseMoveEvent(event); - + // Start drag event if holding left mouse button and dragged far enough bool holding_left_button = event->buttons() & Qt::LeftButton; @@ -112,7 +115,7 @@ void ContentsList::mouseMoveEvent(QMouseEvent *event) { void ContentsList::dragEnterEvent(QDragEnterEvent *event) { // view->dragEnterEvent(event); - + // TODO: is this needed? if (event->mimeData()->hasText()) { event->acceptProposedAction(); @@ -131,7 +134,7 @@ void ContentsList::dragMoveEvent(QDragMoveEvent *event) { // This only changes the drag icon // view->dragMoveEvent(event); - + QPoint pos = event->pos(); QModelIndex index = view->indexAt(pos); QModelIndex category_index = index.siblingAtColumn(AdModel::Column::Category); diff --git a/src/contents_list.h b/src/contents_list.h index 0f620452..6c127630 100644 --- a/src/contents_list.h +++ b/src/contents_list.h @@ -2,10 +2,13 @@ #ifndef CONTENTS_VIEW_H #define CONTENTS_VIEW_H +#include "ad_filter.h" + #include <QWidget> class QTreeView; -class AdFilter; +class AdModel; +class QAction; class QItemSelection; // Shows name, category and description of children of entry selected in containers view @@ -13,7 +16,7 @@ class ContentsList : public QWidget { Q_OBJECT public: - explicit ContentsList(QTreeView *view, AdFilter *proxy); + ContentsList(QTreeView *view, AdModel *model, QAction *advanced_view); void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; @@ -27,6 +30,7 @@ public slots: private: QPoint drag_start_position; QTreeView *view; + AdFilter proxy; }; #endif /* CONTENTS_VIEW_H */ diff --git a/src/main.cpp b/src/main.cpp index 338db2b9..5491d472 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,22 +35,11 @@ int main(int argc, char **argv) { AdModel ad_model; - // Attributes + ContainersTree containers_tree(ui.containers_view, &ad_model, ui.menubar_view_advancedView); + ContentsList contents_list(ui.contents_view, &ad_model, ui.menubar_view_advancedView); AttributesList attributes_view(ui.attributes_view); - // Containers - AdFilter containers_proxy(ui.menubar_view_advancedView, true); - containers_proxy.setSourceModel(&ad_model); - ContainersTree containers_tree(ui.containers_view, &containers_proxy); - - // Contents - AdFilter contents_proxy(ui.menubar_view_advancedView); - contents_proxy.setSourceModel(&ad_model); - ContentsList contents_list(ui.contents_view, &containers_proxy); - - // // Entry context menu - // { auto entry_context_menu = new EntryContextMenu(&main_window); @@ -70,23 +59,6 @@ int main(int argc, char **argv) { delete_entry); } - // Connect signals to update models on when entries are modified - QObject::connect( - &ad_interface, &AdInterface::entry_deleted, - &ad_model, &AdModel::on_entry_deleted); - QObject::connect( - &ad_interface, &AdInterface::entry_deleted, - &attributes_view.model, &AttributesModel::on_entry_deleted); - QObject::connect( - &ad_interface, &AdInterface::entry_changed, - &ad_model, &AdModel::on_entry_changed); - QObject::connect( - &ad_interface, &AdInterface::user_moved, - &ad_model, &AdModel::on_user_moved); - QObject::connect( - &ad_interface, &AdInterface::entry_created, - &ad_model, &AdModel::on_entry_created); - // Set root index of contents view to selection of containers view QObject::connect( ui.containers_view->selectionModel(), &QItemSelectionModel::selectionChanged,