From ee27c528257336e9c29acda365f8907df5281007 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 23 Feb 2025 16:33:47 +0100 Subject: [PATCH] improved save/restore of selection/expanded items --- retroshare-gui/src/gui/Identity/IdDialog.cpp | 101 +++++++++++++++++- retroshare-gui/src/gui/Identity/IdDialog.h | 11 +- .../src/gui/Identity/IdentityListModel.cpp | 8 ++ .../src/gui/Identity/IdentityListModel.h | 2 + 4 files changed, 114 insertions(+), 8 deletions(-) diff --git a/retroshare-gui/src/gui/Identity/IdDialog.cpp b/retroshare-gui/src/gui/Identity/IdDialog.cpp index 3081e8424..405ab9479 100644 --- a/retroshare-gui/src/gui/Identity/IdDialog.cpp +++ b/retroshare-gui/src/gui/Identity/IdDialog.cpp @@ -238,6 +238,9 @@ IdDialog::IdDialog(QWidget *parent) /* Setup UI helper */ mStateHelper = new UIStateHelper(this); + connect(ui->idTreeWidget,SIGNAL(expanded(const QModelIndex&)),this,SLOT(trace_expanded(const QModelIndex&)),Qt::DirectConnection); + connect(ui->idTreeWidget,SIGNAL(collapsed(const QModelIndex&)),this,SLOT(trace_collapsed(const QModelIndex&)),Qt::DirectConnection); + mStateHelper->addWidget(IDDIALOG_IDDETAILS, ui->lineEdit_PublishTS); mStateHelper->addWidget(IDDIALOG_IDDETAILS, ui->lineEdit_KeyId); mStateHelper->addWidget(IDDIALOG_IDDETAILS, ui->lineEdit_Type); @@ -2596,6 +2599,7 @@ void IdDialog::restoreExpandedCircleItems(const std::vector& expanded_root restoreTopLevel(mMyCircleItem,2); } +#ifdef TO_REMOVE void IdDialog::saveExpandedPathsAndSelection_idTreeView(std::set& expanded_indexes, std::set >& selected_indices) { @@ -2685,13 +2689,13 @@ void IdDialog::restoreExpandedPathsAndSelection_idTreeView(const std::setidTreeWidget->blockSignals(false) ; ui->idTreeWidget->selectionModel()->blockSignals(false); } +#endif void IdDialog::applyWhileKeepingTree(std::function predicate) { - std::set expanded_indexes; - std::set > selected; + std::set expanded,selected; - saveExpandedPathsAndSelection_idTreeView(expanded_indexes, selected); + saveExpandedPathsAndSelection_idTreeView(expanded, selected); #ifdef DEBUG_NEW_FRIEND_LIST std::cerr << "After collecting selection, selected paths is: \"" << selected.toStdString() << "\", " ; @@ -2724,7 +2728,7 @@ void IdDialog::applyWhileKeepingTree(std::function predicate) predicate(); mProxyModel->setSourceModel(mIdListModel); - restoreExpandedPathsAndSelection_idTreeView(expanded_indexes,selected); + restoreExpandedPathsAndSelection_idTreeView(expanded,selected); // restore hidden columns for(uint32_t i=0;i predicate) // if(selected_index.isValid()) // ui->idTreeWidget->scrollTo(selected_index); } +#define DEBUG_ID_DIALOG + +void IdDialog::saveExpandedPathsAndSelection_idTreeView(std::set& expanded, std::set& selected) +{ +// QModelIndexList selectedIndexes = ui->idTreeWidget->selectionModel()->selectedIndexes(); +// QModelIndex current_index = selectedIndexes.empty()?QModelIndex():(*selectedIndexes.begin()); + +#ifdef DEBUG_ID_DIALOG + std::cerr << "Saving expended paths and selection..." << std::endl; +#endif + + for(int row = 0; row < mProxyModel->rowCount(); ++row) + recursSaveExpandedItems_idTreeView(mProxyModel->index(row,0),QStringList(),expanded,selected); +} + +void IdDialog::restoreExpandedPathsAndSelection_idTreeView(const std::set& expanded, const std::set& selected) +{ + ui->idTreeWidget->blockSignals(true) ; + + for(int row = 0; row < mProxyModel->rowCount(); ++row) + recursRestoreExpandedItems_idTreeView(mProxyModel->index(row,0),QStringList(),expanded,selected); + + ui->idTreeWidget->blockSignals(false) ; +} + +void IdDialog::recursSaveExpandedItems_idTreeView(const QModelIndex& index,const QStringList& parent_path,std::set& expanded,std::set& selected) +{ + QStringList local_path = parent_path; + local_path.push_back(index.sibling(index.row(),RsIdentityListModel::COLUMN_THREAD_NAME).data(RsIdentityListModel::TreePathRole).toString()) ; + + if(ui->idTreeWidget->isExpanded(index)) + { +#ifdef DEBUG_ID_DIALOG + std::cerr << "Adding expanded path "; + for(auto L:local_path) std::cerr << "\"" << L.toStdString() << "\" " ; std::cerr << std::endl; +#endif + if(index.isValid()) + expanded.insert(local_path) ; + + for(int row=0;rowrowCount(index);++row) + recursSaveExpandedItems_idTreeView(index.child(row,0),local_path,expanded,selected) ; + } + + if(ui->idTreeWidget->selectionModel()->isSelected(index)) + { +#ifdef DEBUG_ID_DIALOG + std::cerr << "Adding selected path "; + for(auto L:local_path) std::cerr << "\"" << L.toStdString() << "\" " ; std::cerr << std::endl; +#endif + selected.insert(local_path); + } +} + +void IdDialog::recursRestoreExpandedItems_idTreeView(const QModelIndex& index,const QStringList& parent_path,const std::set& expanded,const std::set& selected) +{ + QStringList local_path = parent_path; + local_path.push_back(index.sibling(index.row(),RsIdentityListModel::COLUMN_THREAD_NAME).data(RsIdentityListModel::TreePathRole).toString()) ; + + if(expanded.find(local_path) != expanded.end()) + { +#ifdef DEBUG_ID_DIALOG + std::cerr << " re expanding " ; + for(auto L:local_path) std::cerr << "\"" << L.toStdString() << "\" " ; std::cerr << std::endl; +#endif + + ui->idTreeWidget->setExpanded(index,true) ; + + for(int row=0;rowrowCount(index);++row) + recursRestoreExpandedItems_idTreeView(index.child(row,0),local_path,expanded,selected) ; + } + + if(selected.find(local_path) != selected.end()) + { +#ifdef DEBUG_ID_DIALOG + std::cerr << "Restoring selected path "; + for(auto L:local_path) std::cerr << "\"" << L.toStdString() << "\" " ; std::cerr << std::endl; +#endif + ui->idTreeWidget->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows); + } +} +void IdDialog::trace_collapsed(const QModelIndex& i) +{ +std::cerr << "Collapsed " << i << std::endl; +} + +void IdDialog::trace_expanded(const QModelIndex& i) +{ +std::cerr << "Expanded " << i << std::endl; +} diff --git a/retroshare-gui/src/gui/Identity/IdDialog.h b/retroshare-gui/src/gui/Identity/IdDialog.h index 594f4f5e3..dfb85a0bb 100644 --- a/retroshare-gui/src/gui/Identity/IdDialog.h +++ b/retroshare-gui/src/gui/Identity/IdDialog.h @@ -71,8 +71,9 @@ protected: private slots: void updateIdList(); void updateCircles(); - - void createExternalCircle(); +void trace_expanded(const QModelIndex&); +void trace_collapsed(const QModelIndex& i); + void createExternalCircle(); void showEditExistingCircle(); void updateCirclesDisplay(); void toggleAutoBanIdentities(bool b); @@ -123,8 +124,10 @@ private: void processSettings(bool load); QString createUsageString(const RsIdentityUsage& u) const; - void restoreExpandedPathsAndSelection_idTreeView(const std::set& expanded_indexes, const std::set >& selected_indices); - void saveExpandedPathsAndSelection_idTreeView(std::set& expanded_indexes, std::set >& selected_indices); + void saveExpandedPathsAndSelection_idTreeView(std::set &expanded, std::set &selected); + void restoreExpandedPathsAndSelection_idTreeView(const std::set& expanded, const std::set& selelected); + void recursSaveExpandedItems_idTreeView(const QModelIndex& index, const QStringList& parent_path, std::set& expanded, std::set& selected); + void recursRestoreExpandedItems_idTreeView(const QModelIndex& index,const QStringList& parent_path,const std::set& expanded,const std::set& selected); void requestIdData(std::list &ids); bool fillIdListItem(const RsGxsIdGroup& data, QTreeWidgetItem *&item, const RsPgpId &ownPgpId, int accept); diff --git a/retroshare-gui/src/gui/Identity/IdentityListModel.cpp b/retroshare-gui/src/gui/Identity/IdentityListModel.cpp index 3d4b579d7..829dd7d73 100644 --- a/retroshare-gui/src/gui/Identity/IdentityListModel.cpp +++ b/retroshare-gui/src/gui/Identity/IdentityListModel.cpp @@ -352,6 +352,7 @@ QVariant RsIdentityListModel::data(const QModelIndex &index, int role) const case FilterRole: return filterRole(entry,index.column()) ; case SortRole: return sortRole(entry,index.column()) ; + case TreePathRole: return treePathRole(entry,index.column()) ; default: return QVariant(); @@ -483,6 +484,13 @@ QVariant RsIdentityListModel::sizeHintRole(const EntryIndex& e,int col) const } } +QVariant RsIdentityListModel::treePathRole(const EntryIndex& entry,int column) const +{ + if(entry.type == ENTRY_TYPE_CATEGORY) + return QString::number((int)entry.category_index); + else + return QString::fromStdString(mIdentities[entry.identity_index].id.toStdString()); +} QVariant RsIdentityListModel::sortRole(const EntryIndex& entry,int column) const { switch(column) diff --git a/retroshare-gui/src/gui/Identity/IdentityListModel.h b/retroshare-gui/src/gui/Identity/IdentityListModel.h index 9bf96205f..9c5d8eb27 100644 --- a/retroshare-gui/src/gui/Identity/IdentityListModel.h +++ b/retroshare-gui/src/gui/Identity/IdentityListModel.h @@ -54,6 +54,7 @@ public: StatusRole = Qt::UserRole+2, UnreadRole = Qt::UserRole+3, FilterRole = Qt::UserRole+4, + TreePathRole = Qt::UserRole+5, }; enum FilterType{ FILTER_TYPE_NONE = 0x00, @@ -157,6 +158,7 @@ private: QVariant foregroundRole(const EntryIndex& e, int col) const; QVariant textColorRole (const EntryIndex& e, int col) const; QVariant filterRole (const EntryIndex& e, int col) const; + QVariant treePathRole (const EntryIndex& entry,int column) const; /*! * \brief debug_dump