mirror of
https://github.com/altlinux/admc.git
synced 2025-03-22 14:50:36 +03:00
improve Containers->Contents selection connection
create selected_container_changed signal/slot change Containers to QObject for signals/slots transform selection to source index in Containers transform back to proxy index in Contents default AdFilter::only_show_containers to false
This commit is contained in:
parent
887bc71d19
commit
0a80ab0ec1
@ -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);
|
||||
|
@ -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<QModelIndex> indexes = selected.indexes();
|
||||
|
||||
if (indexes.size() > 0) {
|
||||
QModelIndex index = indexes[0];
|
||||
QModelIndex source_index = proxy.mapToSource(index);
|
||||
|
||||
emit selected_container_changed(source_index);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,16 +4,26 @@
|
||||
|
||||
#include "ad_filter.h"
|
||||
|
||||
#include <QObject>
|
||||
|
||||
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;
|
||||
|
@ -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<QModelIndex> 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<const QSortFilterProxyModel *>(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<const QSortFilterProxyModel *>(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);
|
||||
|
@ -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;
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user