From 730a3be2e4c86ccf9fa744b2cf7941bfcf704cbe Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 8 Jun 2020 20:56:59 +0200 Subject: [PATCH] added filtering for posts --- .../gui/gxschannels/GxsChannelPostsModel.cpp | 80 +++++++++++++------ .../gui/gxschannels/GxsChannelPostsModel.h | 10 ++- .../GxsChannelPostsWidgetWithModel.cpp | 13 +-- .../GxsChannelPostsWidgetWithModel.h | 2 +- 4 files changed, 70 insertions(+), 35 deletions(-) diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.cpp index 87cd08ca8..8800513eb 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.cpp @@ -45,15 +45,17 @@ const QString RsGxsChannelPostsModel::FilterString("filtered"); RsGxsChannelPostsModel::RsGxsChannelPostsModel(QObject *parent) : QAbstractItemModel(parent), mTreeMode(TREE_MODE_PLAIN), mColumns(6) { - initEmptyHierarchy(mPosts); + initEmptyHierarchy(); } -void RsGxsChannelPostsModel::initEmptyHierarchy(std::vector& posts) +void RsGxsChannelPostsModel::initEmptyHierarchy() { preMods(); - posts.resize(1); // adds a sentinel item - posts[0].mMeta.mMsgName = "Root sentinel post" ; + mPosts.resize(1); // adds a sentinel item + mPosts[0].mMeta.mMsgName = "Root sentinel post" ; + mFilteredPosts.resize(1); + mFilteredPosts[0] = 1; postMods(); } @@ -68,7 +70,7 @@ void RsGxsChannelPostsModel::postMods() { endResetModel(); - emit dataChanged(createIndex(0,0,(void*)NULL), createIndex(mPosts.size(),mColumns-1,(void*)NULL)); + emit dataChanged(createIndex(0,0,(void*)NULL), createIndex(mFilteredPosts.size(),mColumns-1,(void*)NULL)); } void RsGxsChannelPostsModel::getFilesList(std::list& files) @@ -87,17 +89,43 @@ void RsGxsChannelPostsModel::getFilesList(std::list& files) files.push_back(it.second); } -void RsGxsChannelPostsModel::setTreeMode(TreeMode mode) +void RsGxsChannelPostsModel::setFilter(const QStringList& strings, uint32_t& count) { - if(mode == mTreeMode) - return; - preMods(); - // We're not removing/adding rows here. We're simply asking for re-draw. + beginRemoveRows(QModelIndex(),0,rowCount()-1); + endRemoveRows(); - mTreeMode = mode; - postMods(); + if(strings.empty()) + { + mFilteredPosts.clear(); + for(int i=0;i 0) return 0; - if(mPosts.empty()) // security. Should never happen. + if(mFilteredPosts.empty()) // security. Should never happen. return 0; if(!parent.isValid()) - return (mPosts.size()-1 + mColumns-1)/mColumns; // mPosts always has an item at 0, so size()>=1, and mColumn>=1 + return (mFilteredPosts.size()-1 + mColumns-1)/mColumns; // mFilteredPosts always has an item at 0, so size()>=1, and mColumn>=1 RsErr() << __PRETTY_FUNCTION__ << " rowCount cannot figure out the porper number of rows." << std::endl; return 0; @@ -128,10 +156,10 @@ bool RsGxsChannelPostsModel::getPostData(const QModelIndex& i,RsGxsChannelPost& quintptr ref = i.internalId(); uint32_t entry = 0; - if(!convertRefPointerToTabEntry(ref,entry) || entry >= mPosts.size()) + if(!convertRefPointerToTabEntry(ref,entry) || entry >= mFilteredPosts.size()) return false ; - fmpe = mPosts[entry]; + fmpe = mPosts[mFilteredPosts[entry]]; return true; @@ -247,7 +275,7 @@ quintptr RsGxsChannelPostsModel::getParentRow(quintptr ref,int& row) const { ChannelPostsModelIndex ref_entry; - if(!convertRefPointerToTabEntry(ref,ref_entry) || ref_entry >= mPosts.size()) + if(!convertRefPointerToTabEntry(ref,ref_entry) || ref_entry >= mFilteredPosts.size()) return 0 ; if(ref_entry == 0) @@ -302,7 +330,7 @@ QVariant RsGxsChannelPostsModel::data(const QModelIndex &index, int role) const return QVariant() ; } - if(!convertRefPointerToTabEntry(ref,entry) || entry >= mPosts.size()) + if(!convertRefPointerToTabEntry(ref,entry) || entry >= mFilteredPosts.size()) { #ifdef DEBUG_CHANNEL_MODEL std::cerr << "Bad pointer: " << (void*)ref << std::endl; @@ -310,7 +338,7 @@ QVariant RsGxsChannelPostsModel::data(const QModelIndex &index, int role) const return QVariant() ; } - const RsGxsChannelPost& fmpe(mPosts[entry]); + const RsGxsChannelPost& fmpe(mPosts[mFilteredPosts[entry]]); switch(role) { @@ -367,7 +395,7 @@ void RsGxsChannelPostsModel::clear() preMods(); mPosts.clear(); - initEmptyHierarchy(mPosts); + initEmptyHierarchy(); postMods(); emit channelLoaded(); @@ -385,6 +413,10 @@ void RsGxsChannelPostsModel::setPosts(const RsGxsChannelGroup& group, std::vecto createPostsArray(posts); + mFilteredPosts.clear(); + for(int i=0;i= mPosts.size()) + if(!convertRefPointerToTabEntry(ref,entry) || entry >= mFilteredPosts.size()) return ; #warning TODO @@ -603,11 +635,11 @@ QModelIndex RsGxsChannelPostsModel::getIndexOfMessage(const RsGxsMessageId& mid) RsGxsMessageId postId = mid; - for(uint32_t i=1;i &msgs_array, std::vector &posts, std::map > > &mPostVersions); void createPostsArray(std::vector &posts); void setPosts(const RsGxsChannelGroup& group, std::vector &posts); - void initEmptyHierarchy(std::vector& posts); + void initEmptyHierarchy(); + + std::vector mFilteredPosts; // stores the list of displayes indices due to filtering. + std::vector mPosts ; // store the list of posts updated from rsForums. - std::vector mPosts ; // store the list of posts updated from rsForums. //std::map > > mPostVersions; // stores versions of posts QColor mTextColorRead ; diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp index 82bc63234..bfae93391 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp @@ -209,6 +209,7 @@ void ChannelPostFilesDelegate::paint(QPainter * painter, const QStyleOptionViewI GxsChannelFilesStatusWidget w(file); w.setFixedWidth(option.rect.width()); + w.setFixedHeight(option.rect.height()); QPixmap pixmap(w.size()); pixmap.fill(QRgb(0x00ffffff)); // choose a fully transparent background @@ -283,7 +284,8 @@ GxsChannelPostsWidgetWithModel::GxsChannelPostsWidgetWithModel(const RsGxsGroupI connect(ui->filterLineEdit, SIGNAL(textChanged(QString)), ui->feedWidget, SLOT(setFilterText(QString))); connect(ui->filterLineEdit, SIGNAL(textChanged(QString)), ui->fileWidget, SLOT(setFilterText(QString))); #endif - connect(ui->filterLineEdit, SIGNAL(filterChanged(int)), this, SLOT(filterChanged(int))); + connect(ui->filterLineEdit, SIGNAL(filterChanged(int)), this, SLOT(filterChanged())); + connect(ui->filterLineEdit, SIGNAL(textChanged(QString)), this, SLOT(filterChanged())); /* Initialize view button */ //setViewMode(VIEW_MODE_FEEDS); see processSettings @@ -743,12 +745,11 @@ void GxsChannelPostsWidgetWithModel::setViewMode(int viewMode) #endif } -void GxsChannelPostsWidgetWithModel::filterChanged(int filter) +void GxsChannelPostsWidgetWithModel::filterChanged() { -#ifdef TODO - ui->feedWidget->setFilterType(filter); - ui->fileWidget->setFilterType(filter); -#endif + QStringList ql = ui->filterLineEdit->text().split(' ',QString::SkipEmptyParts); + uint32_t count; + mChannelPostsModel->setFilter(ql,count); } #ifdef TODO diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h index c679a732d..a4e70d75e 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h @@ -132,7 +132,7 @@ private slots: void createMsg(); void toggleAutoDownload(); void subscribeGroup(bool subscribe); - void filterChanged(int filter); + void filterChanged(); void setViewMode(int viewMode); void settingsChanged(); void handlePostsTreeSizeChange(QSize s);