From 73840158504dd6714fc683565ee0775f54421879 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 24 Nov 2018 22:06:34 +0100 Subject: [PATCH] fixed update of QTreeView when something changes in Forum model using dataChanged() signal --- .../src/gui/gxsforums/GxsForumModel.cpp | 64 +++++------ .../src/gui/gxsforums/GxsForumModel.h | 5 +- .../gui/gxsforums/GxsForumThreadWidget.cpp | 105 +++++++++++------- .../src/gui/gxsforums/GxsForumThreadWidget.h | 2 +- 4 files changed, 92 insertions(+), 84 deletions(-) diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp index ce28e9191..da5c1c281 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "util/qtthreadsutils.h" #include "util/DateTime.h" @@ -10,8 +11,6 @@ //#define DEBUG_FORUMMODEL -#define COLUMN_THREAD_DATA 0 // column for storing the userdata like parentid - Q_DECLARE_METATYPE(RsMsgMetaData); std::ostream& operator<<(std::ostream& o, const QModelIndex& i);// defined elsewhere @@ -25,30 +24,6 @@ RsGxsForumModel::RsGxsForumModel(QObject *parent) mFilterColumn=0; mUseChildTS=false; mFlatView=false; - -// // adds some fake posts to debug -// -// int N=5 ; -// mPosts.resize(N+1); -// -// for(int i=1;i<=N;++i) -// { -// mPosts[0].mChildren.push_back(ForumModelIndex(i)); -// mPosts[i].mParent = ForumModelIndex(0); -// mPosts[i].prow = i-1; -// -// RsMsgMetaData meta; -// meta.mMsgName = std::string("message ") + QString::number(i).toStdString() ; -// } -// -// // add one child to last post -// mPosts.resize(N+2); -// mPosts[N].mChildren.push_back(ForumModelIndex(N+1)); -// mPosts[N+1].mParent = ForumModelIndex(N); -// mPosts[N+1].prow = 0; -// -// RsMsgMetaData meta; -// meta.mMsgName = std::string("message ") + QString::number(N+1).toStdString() ; } void RsGxsForumModel::initEmptyHierarchy(std::vector& posts) @@ -232,19 +207,27 @@ int RsGxsForumModel::getChildrenCount(void *ref) const QVariant RsGxsForumModel::headerData(int section, Qt::Orientation orientation, int role) const { - if(role != Qt::DisplayRole) - return QVariant(); + if(role == Qt::DisplayRole) + switch(section) + { + case COLUMN_THREAD_TITLE: return tr("Title"); + case COLUMN_THREAD_DATE: return tr("Date"); + case COLUMN_THREAD_AUTHOR: return tr("Author"); + case COLUMN_THREAD_DISTRIBUTION: return tr("Distribution"); + default: + return QVariant(); + } - switch(section) - { - case COLUMN_THREAD_TITLE: return tr("Title"); - case COLUMN_THREAD_DATE: return tr("Date"); - case COLUMN_THREAD_AUTHOR: return tr("Author"); - case COLUMN_THREAD_DISTRIBUTION: return QString("[icon missing]") ; - case COLUMN_THREAD_READ: return QString("[icon missing]") ; - default: - return QString("[unused]"); - } + if(role == Qt::DecorationRole) + switch(section) + { + case COLUMN_THREAD_DISTRIBUTION: return QIcon(":/icons/flag_green.png"); + case COLUMN_THREAD_READ: return QIcon(":/images/message-state-read.png"); + default: + return QVariant(); + } + + return QVariant(); } QVariant RsGxsForumModel::data(const QModelIndex &index, int role) const @@ -453,6 +436,8 @@ QVariant RsGxsForumModel::decorationRole(const ForumModelPostEntry& fmpe,int col { if(col == COLUMN_THREAD_DISTRIBUTION) return QVariant(fmpe.mReputationWarningLevel); + else if(col == COLUMN_THREAD_READ) + return QVariant(fmpe.mMsgStatus); else return QVariant(); } @@ -486,6 +471,7 @@ void RsGxsForumModel::setPosts(const RsGxsForumGroup& group, const std::vectordrawControl(QStyle::CE_ItemViewItem, &opt, painter, 0); + + const QRect r = option.rect; + + QIcon icon ; + + // get pixmap + unsigned int read_status = qvariant_cast(index.data(Qt::DecorationRole)); + + bool unread = IS_MSG_UNREAD(read_status); + bool missing = index.sibling(index.row(),RsGxsForumModel::COLUMN_THREAD_DATA).data(ROLE_THREAD_MISSING).toBool(); + + // set icon + if (missing) + icon = QIcon(); + else + { + if (unread) + icon = QIcon(":/images/message-state-unread.png"); + else + icon = QIcon(":/images/message-state-read.png"); + } + + QPixmap pix = icon.pixmap(r.size()); + + // draw pixmap at center of item + const QPoint p = QPoint((r.width() - pix.width())/2, (r.height() - pix.height())/2); + painter->drawPixmap(r.topLeft() + p, pix); + } +}; + class AuthorItemDelegate: public QStyledItemDelegate { public: @@ -281,6 +330,7 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget ui->threadTreeWidget->setModel(mThreadModel); ui->threadTreeWidget->setItemDelegateForColumn(RsGxsForumModel::COLUMN_THREAD_DISTRIBUTION,new DistributionItemDelegate()) ; ui->threadTreeWidget->setItemDelegateForColumn(RsGxsForumModel::COLUMN_THREAD_AUTHOR,new AuthorItemDelegate()) ; + ui->threadTreeWidget->setItemDelegateForColumn(RsGxsForumModel::COLUMN_THREAD_READ,new ReadStatusItemDelegate()) ; connect(ui->versions_CB, SIGNAL(currentIndexChanged(int)), this, SLOT(changedVersion())); connect(ui->threadTreeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(threadListCustomPopupMenu(QPoint))); @@ -295,7 +345,7 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget ui->newthreadButton->setText(tr("New thread")); connect(ui->threadTreeWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(changedThread(QModelIndex))); - connect(ui->threadTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(clickedThread(QTreeWidgetItem*,int))); + connect(ui->threadTreeWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(clickedThread(QModelIndex))); connect(ui->viewBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changedViewBox())); connect(ui->expandButton, SIGNAL(clicked()), this, SLOT(togglethreadview())); @@ -671,32 +721,8 @@ void GxsForumThreadWidget::threadListCustomPopupMenu(QPoint /*point*/) QAction *showinpeopleAct = new QAction(QIcon(":/images/info16.png"), tr("Show author in people tab"), &contextMnu); connect(showinpeopleAct, SIGNAL(triggered()), this, SLOT(showInPeopleTab())); - if (IS_GROUP_SUBSCRIBED(mSubscribeFlags)) { - //QList rows; - //QList rowsRead; - //QList rowsUnread; - //int nCount = getSelectedMsgCount(&rows, &rowsRead, &rowsUnread); - - //if (rowsUnread.isEmpty()) { - // markMsgAsRead->setDisabled(true); - //} - //if (rowsRead.isEmpty()) { - // markMsgAsUnread->setDisabled(true); - //} - - //bool hasUnreadChildren = false; - //bool hasReadChildren = false; - - //int rowCount = rows.count(); - - //for (int i = 0; i < rowCount; ++i) { - // if (hasUnreadChildren || rows[i]->data(RsGxsForumModel::COLUMN_THREAD_DATA, ROLE_THREAD_UNREADCHILDREN).toBool()) { - // hasUnreadChildren = true; - // } - // if (hasReadChildren || rows[i]->data(RsGxsForumModel::COLUMN_THREAD_DATA, ROLE_THREAD_READCHILDREN).toBool()) { - // hasReadChildren = true; - // } - //} + if (IS_GROUP_SUBSCRIBED(mSubscribeFlags)) + { markMsgAsReadChildren->setEnabled(current_post.mPostFlags & ForumModelPostEntry::FLAG_POST_HAS_UNREAD_CHILDREN); markMsgAsUnreadChildren->setEnabled(current_post.mPostFlags & ForumModelPostEntry::FLAG_POST_HAS_READ_CHILDREN); @@ -874,27 +900,19 @@ void GxsForumThreadWidget::changedThread(QModelIndex index) insertMessage(); } -void GxsForumThreadWidget::clickedThread(QTreeWidgetItem *item, int column) +void GxsForumThreadWidget::clickedThread(QModelIndex index) { - if (item == NULL) { + if(!index.isValid()) return; - } - if (mFillThread) { + if (groupId().isNull() || !IS_GROUP_SUBSCRIBED(mSubscribeFlags)) return; - } - if (groupId().isNull() || !IS_GROUP_SUBSCRIBED(mSubscribeFlags)) { - return; - } - - if (column == RsGxsForumModel::COLUMN_THREAD_READ) { - QList rows; - rows.append(item); -#ifdef TODO - uint32_t status = item->data(RsGxsForumModel::COLUMN_THREAD_DATA, ROLE_THREAD_STATUS).toUInt(); - setMsgReadStatus(rows, IS_MSG_UNREAD(status)); -#endif + if (index.column() == RsGxsForumModel::COLUMN_THREAD_READ) + { + ForumModelPostEntry fmpe; + mThreadModel->getPostData(index,fmpe); + mThreadModel->setMsgReadStatus(index, IS_MSG_UNREAD(fmpe.mMsgStatus),false); } } @@ -2685,6 +2703,7 @@ void GxsForumThreadWidget::updateGroupData() mForumGroup = group; insertGroupData(); + mSubscribeFlags = group.mMeta.mSubscribeFlags; ui->threadTreeWidget->setColumnHidden(RsGxsForumModel::COLUMN_THREAD_DISTRIBUTION, !IS_GROUP_PGP_KNOWN_AUTHED(mForumGroup.mMeta.mSignFlags) && !(IS_GROUP_PGP_AUTHED(mForumGroup.mMeta.mSignFlags))); ui->subscribeToolButton->setHidden(IS_GROUP_SUBSCRIBED(mSubscribeFlags)) ; diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h index b32296d65..416bd0aaf 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h @@ -99,7 +99,7 @@ private slots: void changedThread(QModelIndex index); void changedVersion(); - void clickedThread (QTreeWidgetItem *item, int column); + void clickedThread (QModelIndex index); void reply_with_private_message(); void replytoforummessage();