From 11ecd6fbd15b45038fa88d206531216154a22517 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 29 Aug 2020 14:13:03 +0200 Subject: [PATCH] keep focus on current item when changing view mode --- .../GxsChannelPostsWidgetWithModel.cpp | 24 +++++++++++++++++++ .../GxsChannelPostsWidgetWithModel.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp index bd3a45d8d..380d9467b 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp @@ -460,8 +460,29 @@ void GxsChannelPostsWidgetWithModel::postContextMenu(const QPoint&) menu.exec(QCursor::pos()); } +RsGxsMessageId GxsChannelPostsWidgetWithModel::getCurrentItemId() const +{ + RsGxsMessageId selected_msg_id ; + QModelIndex index = ui->postsTree->selectionModel()->currentIndex(); + + if(index.isValid()) + selected_msg_id = index.data(Qt::UserRole).value().mMeta.mMsgId ; + + return selected_msg_id; +} + +void GxsChannelPostsWidgetWithModel::selectItem(const RsGxsMessageId& msg_id) +{ + auto index = mChannelPostsModel->getIndexOfMessage(msg_id); + + ui->postsTree->selectionModel()->setCurrentIndex(index,QItemSelectionModel::ClearAndSelect); + ui->postsTree->scrollTo(index);//May change if model reloaded +} + void GxsChannelPostsWidgetWithModel::switchView() { + auto msg_id = getCurrentItemId(); + if(mChannelPostsModel->getMode() == RsGxsChannelPostsModel::TREE_MODE_GRID) { whileBlocking(ui->list_TB)->setChecked(true); @@ -484,6 +505,9 @@ void GxsChannelPostsWidgetWithModel::switchView() for(int i=0;icolumnCount();++i) ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(i,font(),ui->postsTree->width())); + selectItem(msg_id); + ui->postsTree->setFocus(); + ui->postsTree->dataChanged(QModelIndex(),QModelIndex()); // forces update of the whole tree } diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h index 6e645642a..095db9698 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h @@ -159,6 +159,8 @@ public slots: private: void processSettings(bool load); + RsGxsMessageId getCurrentItemId() const; + void selectItem(const RsGxsMessageId& msg_id); void setAutoDownload(bool autoDl); static bool filterItem(FeedItem *feedItem, const QString &text, int filter);