1
0
mirror of https://github.com/altlinux/admc.git synced 2024-10-27 01:55:37 +03:00

add EntryWidget

containers and contents widgets now inherit from it
move column visiblity manipulation into it
move get_selected_dn() into it
move context menu request into it
This commit is contained in:
Dmitry Degtyarev 2020-05-27 14:23:35 +04:00
parent bf4fadaf88
commit 424d223c49
9 changed files with 150 additions and 74 deletions

View File

@ -50,7 +50,8 @@ set(ADTOOL_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/containers_tree.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/contents_list.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/create_entry_dialog.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/main_window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/main_window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/entry_widget.cpp
)
add_definitions(${QT5_DEFINITIONS})

View File

@ -1,20 +1,19 @@
#include "containers_tree.h"
#include "ad_model.h"
#include "ad_filter.h"
#include <QTreeView>
ContainersTree::ContainersTree(QTreeView *view, AdModel *model, QAction *advanced_view_toggle):
proxy(model, advanced_view_toggle)
ContainersTree::ContainersTree(QTreeView *view, AdModel *model, QAction *advanced_view_toggle)
: EntryWidget(view, model, advanced_view_toggle)
{
this->view = view;
proxy->only_show_containers = true;
proxy.only_show_containers = true;
view->setModel(&proxy);
view->hideColumn(AdModel::Column::Category);
view->hideColumn(AdModel::Column::Description);
view->hideColumn(AdModel::Column::DN);
column_hidden[AdModel::Column::Category] = true;
column_hidden[AdModel::Column::Description] = true;
column_hidden[AdModel::Column::DN] = true;
update_column_visibility();
connect(
view->selectionModel(), &QItemSelectionModel::selectionChanged,
@ -28,7 +27,7 @@ void ContainersTree::on_selection_changed(const QItemSelection &selected, const
if (indexes.size() > 0) {
QModelIndex index = indexes[0];
QModelIndex source_index = proxy.mapToSource(index);
QModelIndex source_index = proxy->mapToSource(index);
emit selected_container_changed(source_index);
}

View File

@ -2,17 +2,12 @@
#ifndef CONTAINERS_VIEW_H
#define CONTAINERS_VIEW_H
#include "ad_filter.h"
#include "entry_widget.h"
#include <QObject>
class QTreeView;
class AdModel;
class QAction;
class QModelIndex;
class QItemSelection;
// Shows names of AdModel as a tree
class ContainersTree : public QObject {
class ContainersTree : public EntryWidget {
Q_OBJECT
public:
@ -25,8 +20,6 @@ private slots:
void on_selection_changed(const QItemSelection &selected, const QItemSelection &);
private:
QTreeView *view;
AdFilter proxy;
};

View File

@ -12,25 +12,19 @@
#include <QMimeData>
#include <QTreeView>
ContentsList::ContentsList(QTreeView *view, AdModel* model, QAction *advanced_view_toggle) :
proxy(model, advanced_view_toggle)
ContentsList::ContentsList(QTreeView *view, AdModel* model, QAction *advanced_view_toggle)
: EntryWidget(view, model, advanced_view_toggle)
{
this->view = view;
view->setModel(&proxy);
this->update_column_visibility();
column_hidden[AdModel::Column::DN] = true;
update_column_visibility();
};
void ContentsList::update_column_visibility() {
view->setColumnHidden(AdModel::Column::DN, dn_column_hidden);
}
// 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::on_selected_container_changed(const QModelIndex &source_index) {
QModelIndex index = proxy.mapFromSource(source_index);
QModelIndex index = proxy->mapFromSource(source_index);
view->setRootIndex(index);
// NOTE: have to hide columns after model update
this->update_column_visibility();
// NOTE: have to hide columns after setRootIndex
update_column_visibility();
}

View File

@ -2,32 +2,20 @@
#ifndef CONTENTS_VIEW_H
#define CONTENTS_VIEW_H
#include "ad_filter.h"
#include <QObject>
class QTreeView;
class AdModel;
class QAction;
class QItemSelection;
#include "entry_widget.h"
// Shows name, category and description of children of entry selected in containers view
class ContentsList : public QObject {
class ContentsList : public EntryWidget {
Q_OBJECT
public:
ContentsList(QTreeView *view, AdModel *model, QAction *advanced_view);
bool dn_column_hidden = true;
void update_column_visibility();
public slots:
void on_selected_container_changed(const QModelIndex &source_index);
private:
QTreeView *view;
AdFilter proxy;
};
#endif /* CONTENTS_VIEW_H */

76
src/entry_widget.cpp Normal file
View File

@ -0,0 +1,76 @@
#include "entry_widget.h"
#include "ad_interface.h"
#include "ad_model.h"
#include "ad_filter.h"
#include <QApplication>
#include <QItemSelection>
#include <QSortFilterProxyModel>
#include <QMouseEvent>
#include <QDrag>
#include <QMimeData>
#include <QTreeView>
EntryWidget::EntryWidget(QTreeView *view_, AdModel* model, QAction *advanced_view_toggle) :
QWidget()
{
view = view_;
proxy = new AdFilter(model, advanced_view_toggle);
view->setModel(proxy);
// Init column visibility
for (int column_i = AdModel::Column::Name; column_i < AdModel::Column::COUNT; column_i++) {
auto column = static_cast<AdModel::Column>(column_i);
column_hidden[column] = false;
}
update_column_visibility();
// Convert view's customContextMenuRequested
// to context_menu_requested signal with global pos
connect(
view, &QWidget::customContextMenuRequested,
[this] (const QPoint &pos) {
QModelIndex index = view->indexAt(pos);
if (index.isValid()) {
QPoint global_pos = view->mapToGlobal(pos);
emit context_menu_requested(global_pos);
}
});
}
QString EntryWidget::get_selected_dn() const {
// Return dn of selected entry, if any is selected and view
// has focus
const auto selection_model = view->selectionModel();
if (view->hasFocus() && selection_model->hasSelection()) {
auto selected_indexes = selection_model->selectedIndexes();
auto selected = selected_indexes[0];
QModelIndex dn_index = selected.siblingAtColumn(AdModel::Column::DN);
return dn_index.data().toString();
} else {
return "";
}
}
void EntryWidget::on_action_toggle_dn(bool checked) {
const bool dn_column_hidden = !checked;
column_hidden[AdModel::Column::DN] = dn_column_hidden;
update_column_visibility();
}
void EntryWidget::update_column_visibility() {
// Set column visiblity to current values in column_hidden
for (int column_i = AdModel::Column::Name; column_i < AdModel::Column::COUNT; column_i++) {
auto column = static_cast<AdModel::Column>(column_i);
view->setColumnHidden(column, column_hidden[column]);
}
}

39
src/entry_widget.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef ENTRY_WIDGET_H
#define ENTRY_WIDGET_H
#include "ad_model.h"
#include <QWidget>
#include <QMap>
class QTreeView;
class AdModel;
class QAction;
class AdFilter;
// Shows names of AdModel as a tree
class EntryWidget : public QWidget {
Q_OBJECT
public:
EntryWidget(QTreeView *view_, AdModel *model, QAction *advanced_view_toggle);
QString get_selected_dn() const;
signals:
void context_menu_requested(const QPoint &pos);
public slots:
void on_action_toggle_dn(bool checked);
protected:
QTreeView *view = nullptr;
AdFilter *proxy = nullptr;
QMap<AdModel::Column, bool> column_hidden;
void update_column_visibility();
};
#endif /* ENTRY_WIDGET_H */

View File

@ -57,10 +57,18 @@ MainWindow::MainWindow(): QMainWindow() {
menuView->addAction(action_toggle_dn);
QObject::connect(
action_toggle_dn, &QAction::triggered,
this, &MainWindow::on_action_toggle_dn);
containers_tree, &EntryWidget::on_action_toggle_dn);
QObject::connect(
action_toggle_dn, &QAction::triggered,
contents_list, &EntryWidget::on_action_toggle_dn);
connect_view_to_entry_context_menu(containers_view);
connect_view_to_entry_context_menu(contents_view);
QObject::connect(
containers_tree, &EntryWidget::context_menu_requested,
this, &MainWindow::popup_entry_context_menu);
QObject::connect(
contents_list, &EntryWidget::context_menu_requested,
this, &MainWindow::popup_entry_context_menu);
// Set root index of contents view to selection of containers view
QObject::connect(
@ -133,20 +141,6 @@ void MainWindow::retranslateUi(QMainWindow *MainWindow) {
menuView->setTitle(tr("View"));
}
void MainWindow::connect_view_to_entry_context_menu(QTreeView *view) {
QObject::connect(
view, &QWidget::customContextMenuRequested,
[this, view] (const QPoint &pos) {
// Get DN of clicked entry
QModelIndex index = view->indexAt(pos);
if (index.isValid()) {
QPoint global_pos = view->mapToGlobal(pos);
this->popup_entry_context_menu(global_pos);
}
});
}
QString MainWindow::get_selected_dn() {
QTreeView *focus_view = nullptr;
@ -201,10 +195,3 @@ void MainWindow::popup_entry_context_menu(const QPoint &pos) {
menu.exec(pos, action_attributes);
}
void MainWindow::on_action_toggle_dn(bool checked) {
// TODO: maybe add update_column_visibility() to containers tree as well, and make visibility state an array for all columns
containers_view->setColumnHidden(AdModel::Column::DN, !checked);
contents_list->dn_column_hidden = !checked;
contents_list->update_column_visibility();
}

View File

@ -31,7 +31,6 @@ public:
private:
void setupUi();
void retranslateUi(QMainWindow *MainWindow);
void connect_view_to_entry_context_menu(QTreeView *view);
QString get_selected_dn();
void on_action_attributes();