From 23302ea469d1ca8f728408243047bbf8d9aad1cb Mon Sep 17 00:00:00 2001 From: csoler Date: Tue, 27 Nov 2018 19:21:49 +0100 Subject: [PATCH] partly resurrected post versions --- .../src/gui/gxsforums/GxsForumModel.cpp | 64 ++++++++++++------- .../src/gui/gxsforums/GxsForumModel.h | 7 +- .../gui/gxsforums/GxsForumThreadWidget.cpp | 35 +++++----- .../src/gui/gxsforums/GxsForumThreadWidget.h | 2 - 4 files changed, 65 insertions(+), 43 deletions(-) diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp index 04fbcb67e..2e960df90 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp @@ -48,11 +48,22 @@ int RsGxsForumModel::rowCount(const QModelIndex& parent) const else return getChildrenCount(parent.internalPointer()); } + int RsGxsForumModel::columnCount(const QModelIndex &parent) const { return COLUMN_THREAD_NB_COLUMNS ; } +std::vector > RsGxsForumModel::getPostVersions(const RsGxsMessageId& mid) const +{ + auto it = mPostVersions.find(mid); + + if(it != mPostVersions.end()) + return it->second; + else + return std::vector >(); +} + bool RsGxsForumModel::getPostData(const QModelIndex& i,ForumModelPostEntry& fmpe) const { if(!i.isValid()) @@ -488,12 +499,13 @@ void RsGxsForumModel::setForum(const RsGxsGroupId& forum_group_id) update_posts(forum_group_id); } -void RsGxsForumModel::setPosts(const RsGxsForumGroup& group, const std::vector& posts) +void RsGxsForumModel::setPosts(const RsGxsForumGroup& group, const std::vector& posts,const std::map > >& post_versions) { emit dataChanged(createIndex(0,0,(void*)NULL), createIndex(0,COLUMN_THREAD_NB_COLUMNS-1,(void*)NULL)); mForumGroup = group; mPosts = posts; + mPostVersions = post_versions; // now update prow for all posts @@ -539,14 +551,15 @@ void RsGxsForumModel::update_posts(const RsGxsGroupId& group_id) // 2 - sort the messages into a proper hierarchy + auto post_versions = new std::map > >() ; std::vector *vect = new std::vector(); RsGxsForumGroup group = groups[0]; - computeMessagesHierarchy(group,messages,*vect); + computeMessagesHierarchy(group,messages,*vect,*post_versions); // 3 - update the model in the UI thread. - RsQThreadUtils::postToObject( [group,vect,this]() + RsQThreadUtils::postToObject( [group,vect,post_versions,this]() { /* Here it goes any code you want to be executed on the Qt Gui * thread, for example to update the data model with new information @@ -554,8 +567,10 @@ void RsGxsForumModel::update_posts(const RsGxsGroupId& group_id) * Qt::QueuedConnection is important! */ - setPosts(group,*vect) ; + setPosts(group,*vect,*post_versions) ; + delete vect; + delete post_versions; }, this ); @@ -651,11 +666,13 @@ void RsGxsForumModel::convertMsgToPostEntry(const RsGxsForumGroup& mForumGroup,c #endif } -static bool decreasing_time_comp(const QPair& e1,const QPair& e2) { return e2.first < e1.first ; } +static bool decreasing_time_comp(const std::pair& e1,const std::pair& e2) { return e2.first < e1.first ; } void RsGxsForumModel::computeMessagesHierarchy(const RsGxsForumGroup& forum_group, const std::vector& msgs_array, - std::vector& posts) + std::vector& posts, + std::map > >& mPostVersions + ) { std::cerr << "updating messages data with " << msgs_array.size() << " messages" << std::endl; @@ -687,7 +704,6 @@ void RsGxsForumModel::computeMessagesHierarchy(const RsGxsForumGroup& forum_grou int step = 0; initEmptyHierarchy(posts); - QMap > > mPostVersions ; // ThreadList contains the list of parent threads. The algorithm below iterates through all messages // and tries to establish parenthood relationships between them, given that we only know the @@ -740,31 +756,31 @@ void RsGxsForumModel::computeMessagesHierarchy(const RsGxsForumGroup& forum_grou // always add the post a self version if(mPostVersions[msgIt->second.mMeta.mOrigMsgId].empty()) - mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(QPair(msgIt2->second.mMeta.mPublishTs,msgIt2->second.mMeta.mMsgId)) ; + mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(std::make_pair(msgIt2->second.mMeta.mPublishTs,msgIt2->second.mMeta.mMsgId)) ; - mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(QPair(msgIt->second.mMeta.mPublishTs,msgIt->second.mMeta.mMsgId)) ; + mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(std::make_pair(msgIt->second.mMeta.mPublishTs,msgIt->second.mMeta.mMsgId)) ; } } // The following code assembles all new versions of a given post into the same array, indexed by the oldest version of the post. - for(QMap > >::iterator it(mPostVersions.begin());it!=mPostVersions.end();++it) + for(auto it(mPostVersions.begin());it!=mPostVersions.end();++it) { - QVector >& v(*it) ; + auto& v(it->second) ; for(int32_t i=0;ifirst) { RsGxsMessageId sub_msg_id = v[i].second ; - QMap > >::iterator it2 = mPostVersions.find(sub_msg_id); + auto it2 = mPostVersions.find(sub_msg_id); if(it2 != mPostVersions.end()) { - for(int32_t j=0;j<(*it2).size();++j) - if((*it2)[j].second != sub_msg_id) // dont copy it, since it is already present at slot i - v.append((*it2)[j]) ; + for(int32_t j=0;jsecond.size();++j) + if(it2->second[j].second != sub_msg_id) // dont copy it, since it is already present at slot i + v.push_back(it2->second[j]) ; mPostVersions.erase(it2) ; // it2 is never equal to it } @@ -779,37 +795,37 @@ void RsGxsForumModel::computeMessagesHierarchy(const RsGxsForumGroup& forum_grou #ifdef DEBUG_FORUMS std::cerr << "Final post versions: " << std::endl; #endif - QMap > > mTmp; + std::map > > mTmp; std::map most_recent_versions ; - for(QMap > >::iterator it(mPostVersions.begin());it!=mPostVersions.end();++it) + for(auto it(mPostVersions.begin());it!=mPostVersions.end();++it) { #ifdef DEBUG_FORUMS std::cerr << "Original post: " << it.key() << std::endl; #endif // Finally, sort the posts from newer to older - qSort((*it).begin(),(*it).end(),decreasing_time_comp) ; + std::sort(it->second.begin(),it->second.end(),decreasing_time_comp) ; #ifdef DEBUG_FORUMS std::cerr << " most recent version " << (*it)[0].first << " " << (*it)[0].second << std::endl; #endif - for(int32_t i=1;i<(*it).size();++i) + for(int32_t i=1;isecond.size();++i) { - msgs.erase((*it)[i].second) ; + msgs.erase(it->second[i].second) ; #ifdef DEBUG_FORUMS std::cerr << " older version " << (*it)[i].first << " " << (*it)[i].second << std::endl; #endif } - mTmp[(*it)[0].second] = *it ; // index the versions map by the ID of the most recent post. + mTmp[it->second[0].second] = it->second ; // index the versions map by the ID of the most recent post. // Now make sure that message parents are consistent. Indeed, an old post may have the old version of a post as parent. So we need to change that parent // to the newest version. So we create a map of which is the most recent version of each message, so that parent messages can be searched in it. - for(int i=1;i<(*it).size();++i) - most_recent_versions[(*it)[i].second] = (*it)[0].second ; + for(int i=1;isecond.size();++i) + most_recent_versions[it->second[i].second] = it->second[0].second ; } mPostVersions = mTmp ; diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumModel.h b/retroshare-gui/src/gui/gxsforums/GxsForumModel.h index c5ada2d88..78fd9f6e3 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumModel.h +++ b/retroshare-gui/src/gui/gxsforums/GxsForumModel.h @@ -83,6 +83,8 @@ public: QModelIndex root() const{ return createIndex(0,0,(void*)NULL) ;} QModelIndex getIndexOfMessage(const RsGxsMessageId& mid) const; + std::vector > getPostVersions(const RsGxsMessageId& mid) const; + #ifdef TO_REMOVE QModelIndex getNextIndex(const QModelIndex& i,bool unread_only) const; @@ -176,11 +178,12 @@ private: static ForumModelIndex addEntry(std::vector& posts,const ForumModelPostEntry& entry,ForumModelIndex parent); static void convertMsgToPostEntry(const RsGxsForumGroup &mForumGroup, const RsGxsForumMsg& msg, bool useChildTS, uint32_t filterColumn, ForumModelPostEntry& fentry); - void computeMessagesHierarchy(const RsGxsForumGroup& forum_group,const std::vector& msgs_array,std::vector& posts); - void setPosts(const RsGxsForumGroup &group, const std::vector& posts); // this method *must* be called from UI thread. + void computeMessagesHierarchy(const RsGxsForumGroup& forum_group, const std::vector& msgs_array, std::vector& posts, std::map > > &mPostVersions); + void setPosts(const RsGxsForumGroup& group, const std::vector& posts,const std::map > >& post_versions); void initEmptyHierarchy(std::vector& posts); std::vector mPosts ; // store the list of posts updated from rsForums. + std::map > > mPostVersions; QColor mTextColorRead ; QColor mTextColorUnread ; diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp index b7df1cd07..a5c5c4ce7 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp @@ -484,7 +484,6 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget ui->threadTreeWidget->enableColumnCustomize(true); #endif - ui->threadTreeWidget->sortByColumn(RsGxsForumModel::COLUMN_THREAD_DATE, Qt::DescendingOrder); } void GxsForumThreadWidget::blank() @@ -1883,9 +1882,9 @@ void GxsForumThreadWidget::insertMessage() if (index.isValid()) { - QModelIndex parentIndex = mThreadProxyModel->mapToSource(index).parent(); + QModelIndex parentIndex = index.parent(); int curr_index = index.row(); - int count = mThreadModel->rowCount(parentIndex); + int count = mThreadProxyModel->rowCount(parentIndex); ui->previousButton->setEnabled(curr_index > 0); ui->nextButton->setEnabled(curr_index < count - 1); @@ -1911,32 +1910,31 @@ void GxsForumThreadWidget::insertMessage() // add/show combobox for versions, if applicable, and enable it. If no older versions of the post available, hide the combobox. - std::cerr << "Looking into existing versions for post " << mThreadId << ", thread history: " << mPostVersions.size() << std::endl; -#ifdef TODO - QMap > >::const_iterator it = mPostVersions.find(mOrigThreadId) ; + std::vector > post_versions = mThreadModel->getPostVersions(mThreadId); + std::cerr << "Looking into existing versions for post " << mThreadId << ", thread history: " << post_versions.size() << std::endl; ui->versions_CB->blockSignals(true) ; while(ui->versions_CB->count() > 0) ui->versions_CB->removeItem(0); - if(it != mPostVersions.end()) + if(!post_versions.empty()) { - std::cerr << (*it).size() << " versions found " << std::endl; + std::cerr << post_versions.size() << " versions found " << std::endl; ui->versions_CB->setVisible(true) ; ui->time_label->hide(); int current_index = 0 ; - for(int i=0;i<(*it).size();++i) + for(int i=0;iversions_CB->insertItem(i, ((i==0)?tr("(Latest) "):tr("(Old) "))+" "+DateTime::formatLongDateTime( (*it)[i].first)); - ui->versions_CB->setItemData(i,QString::fromStdString((*it)[i].second.toStdString())); + ui->versions_CB->insertItem(i, ((i==0)?tr("(Latest) "):tr("(Old) "))+" "+DateTime::formatLongDateTime( post_versions[i].first)); + ui->versions_CB->setItemData(i,QString::fromStdString(post_versions[i].second.toStdString())); - std::cerr << " added new post version " << (*it)[i].first << " " << (*it)[i].second << std::endl; + std::cerr << " added new post version " << post_versions[i].first << " " << post_versions[i].second << std::endl; - if(mThreadId == (*it)[i].second) + if(mThreadId == post_versions[i].second) current_index = i ; } @@ -1947,7 +1945,6 @@ void GxsForumThreadWidget::insertMessage() ui->versions_CB->hide(); ui->time_label->show(); } -#endif ui->versions_CB->blockSignals(false) ; @@ -2238,7 +2235,7 @@ void GxsForumThreadWidget::markMsgAsReadUnread (bool read, bool children, bool f QModelIndex index = *selectedIndexes.begin(); - mThreadModel->setMsgReadStatus(index,read,children); + mThreadModel->setMsgReadStatus(mThreadProxyModel->mapToSource(index),read,children); } #ifdef TODO @@ -2723,13 +2720,21 @@ bool GxsForumThreadWidget::filterItem(QTreeWidgetItem *item, const QString &text void GxsForumThreadWidget::updateGroupName() { + ui->threadTreeWidget->selectionModel()->clear(); + ui->threadTreeWidget->selectionModel()->reset(); + mThreadId.clear(); ui->forumName->setText(QString::fromUtf8(mForumGroup.mMeta.mGroupName.c_str())); + ui->threadTreeWidget->sortByColumn(RsGxsForumModel::COLUMN_THREAD_DATE, Qt::DescendingOrder); + ui->threadTreeWidget->update(); } void GxsForumThreadWidget::updateGroupData() { mSubscribeFlags = 0; mSignFlags = 0; + mThreadId.clear(); mForumDescription.clear(); + ui->threadTreeWidget->selectionModel()->clear(); + ui->threadTreeWidget->selectionModel()->reset(); emit groupChanged(this); diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h index 426b56456..44ef20f75 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h @@ -237,8 +237,6 @@ private: RsGxsMessageId mNavigatePendingMsgId; QList mIgnoredMsgId; - QMap > > mPostVersions ; // holds older versions of posts - RsGxsForumModel *mThreadModel; QSortFilterProxyModel *mThreadProxyModel;