From c2686d1a9477da81cf074be5f4766aab7d786c7f Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 24 Nov 2018 16:11:40 +0100 Subject: [PATCH] fixed display of author in new ForumModel --- .../src/gui/gxsforums/GxsForumModel.cpp | 41 +++++++++--- .../src/gui/gxsforums/GxsForumModel.h | 6 +- .../gui/gxsforums/GxsForumThreadWidget.cpp | 67 +++++++++++++++---- 3 files changed, 89 insertions(+), 25 deletions(-) diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp index c5d300816..dc098a09f 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp @@ -5,6 +5,7 @@ #include "util/qtthreadsutils.h" #include "util/DateTime.h" #include "GxsForumModel.h" +#include "retroshare/rsgxsflags.h" #include "retroshare/rsgxsforums.h" //#define DEBUG_FORUMMODEL @@ -14,11 +15,9 @@ #define COLUMN_THREAD_DATE 2 #define COLUMN_THREAD_DISTRIBUTION 3 #define COLUMN_THREAD_AUTHOR 4 -#define COLUMN_THREAD_SIGNED 5 -#define COLUMN_THREAD_CONTENT 6 -#define COLUMN_THREAD_COUNT 7 -#define COLUMN_THREAD_MSGID 8 -#define COLUMN_THREAD_NB_COLUMNS 9 +#define COLUMN_THREAD_CONTENT 5 +#define COLUMN_THREAD_MSGID 6 +#define COLUMN_THREAD_NB_COLUMNS 7 #define COLUMN_THREAD_DATA 0 // column for storing the userdata like parentid @@ -252,6 +251,7 @@ QVariant RsGxsForumModel::data(const QModelIndex &index, int role) const switch(role) { case Qt::SizeHintRole: return sizeHintRole(index.column()) ; + case Qt::FontRole: case Qt::TextAlignmentRole: case Qt::TextColorRole: case Qt::WhatsThisRole: @@ -285,6 +285,14 @@ QVariant RsGxsForumModel::data(const QModelIndex &index, int role) const const ForumModelPostEntry& fmpe(mPosts[entry]); + if(role == Qt::FontRole) + { + QFont font ; + + font.setBold(IS_MSG_UNREAD(fmpe.mMsgStatus)); + + return QVariant(font); + } #ifdef DEBUG_FORUMMODEL std::cerr << " [ok]" << std::endl; #endif @@ -294,6 +302,7 @@ QVariant RsGxsForumModel::data(const QModelIndex &index, int role) const case Qt::DisplayRole: return displayRole (fmpe,index.column()) ; case Qt::DecorationRole: return decorationRole(fmpe,index.column()) ; case Qt::ToolTipRole: return toolTipRole (fmpe,index.column()) ; + case Qt::UserRole: return userRole (fmpe,index.column()) ; case ThreadPinnedRole: return pinnedRole (fmpe,index.column()) ; case MissingRole: return missingRole (fmpe,index.column()) ; @@ -308,7 +317,7 @@ QVariant RsGxsForumModel::statusRole(const ForumModelPostEntry& fmpe,int column) if(column != COLUMN_THREAD_DATA) return QVariant(); - return QVariant(fmpe.mStatus); + return QVariant(fmpe.mMsgStatus); } QVariant RsGxsForumModel::missingRole(const ForumModelPostEntry& fmpe,int column) const @@ -390,7 +399,7 @@ QVariant RsGxsForumModel::displayRole(const ForumModelPostEntry& fmpe,int col) c else return QVariant(QString::fromUtf8(fmpe.mTitle.c_str())); - //case COLUMN_THREAD_READ_STATUS:return QVariant(fmpe.mMsgStatus); + case COLUMN_THREAD_READ:return QVariant(); case COLUMN_THREAD_DATE: { QDateTime qtime; qtime.setTime_t(fmpe.mPublishTs); @@ -398,8 +407,9 @@ QVariant RsGxsForumModel::displayRole(const ForumModelPostEntry& fmpe,int col) c return QVariant(DateTime::formatDateTime(qtime)); } - case COLUMN_THREAD_AUTHOR: return QVariant(QString::fromStdString(fmpe.mAuthorId.toStdString())); - case COLUMN_THREAD_MSGID: return QVariant(QString::fromStdString(fmpe.mMsgId.toStdString())); + case COLUMN_THREAD_DISTRIBUTION: + case COLUMN_THREAD_AUTHOR: return QVariant(); + case COLUMN_THREAD_MSGID: return QVariant(); #ifdef TODO if (filterColumn == COLUMN_THREAD_CONTENT) { // need content for filter @@ -416,6 +426,17 @@ QVariant RsGxsForumModel::displayRole(const ForumModelPostEntry& fmpe,int col) c return QVariant("[ERROR]"); } +QVariant RsGxsForumModel::userRole(const ForumModelPostEntry& fmpe,int col) const +{ + switch(col) + { + case COLUMN_THREAD_AUTHOR: return QVariant(QString::fromStdString(fmpe.mAuthorId.toStdString())); + case COLUMN_THREAD_MSGID: return QVariant(QString::fromStdString(fmpe.mMsgId.toStdString())); + default: + return QVariant(); + } +} + QVariant RsGxsForumModel::decorationRole(const ForumModelPostEntry& fmpe,int col) const { if(col == COLUMN_THREAD_DISTRIBUTION) @@ -534,7 +555,7 @@ void RsGxsForumModel::convertMsgToPostEntry(const RsGxsForumGroup& mForumGroup,c fentry.mMsgId = msg.mMeta.mMsgId; fentry.mPublishTs = msg.mMeta.mPublishTs; fentry.mPostFlags = 0; - fentry.mStatus = msg.mMeta.mMsgStatus; + fentry.mMsgStatus = msg.mMeta.mMsgStatus; if(mForumGroup.mPinnedPosts.ids.find(msg.mMeta.mMsgId) != mForumGroup.mPinnedPosts.ids.end()) fentry.mPostFlags |= ForumModelPostEntry::FLAG_POST_IS_PINNED; diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumModel.h b/retroshare-gui/src/gui/gxsforums/GxsForumModel.h index a0726b3fe..aef4602d8 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumModel.h +++ b/retroshare-gui/src/gui/gxsforums/GxsForumModel.h @@ -27,7 +27,7 @@ typedef uint32_t ForumModelIndex; struct ForumModelPostEntry { - ForumModelPostEntry() : mPublishTs(0),mPostFlags(0),mReputationWarningLevel(0),mStatus(0),prow(0) {} + ForumModelPostEntry() : mPublishTs(0),mPostFlags(0),mReputationWarningLevel(0),mMsgStatus(0),prow(0) {} enum { // flags for display of posts FLAG_POST_IS_PINNED = 0x0001, @@ -41,7 +41,7 @@ struct ForumModelPostEntry uint32_t mPublishTs; uint32_t mPostFlags; int mReputationWarningLevel; - int mStatus; + int mMsgStatus; std::vector mChildren; ForumModelIndex mParent; @@ -82,11 +82,13 @@ public: QVariant displayRole (const ForumModelPostEntry& fmpe, int col) const; QVariant decorationRole(const ForumModelPostEntry& fmpe, int col) const; QVariant toolTipRole (const ForumModelPostEntry& fmpe, int col) const; + QVariant userRole (const ForumModelPostEntry& fmpe, int col) const; QVariant pinnedRole (const ForumModelPostEntry& fmpe, int col) const; QVariant missingRole (const ForumModelPostEntry& fmpe, int col) const; QVariant statusRole (const ForumModelPostEntry& fmpe, int col) const; QVariant authorRole (const ForumModelPostEntry& fmpe, int col) const; QVariant sortRole (const ForumModelPostEntry& fmpe, int col) const; + QVariant fontRole (const ForumModelPostEntry& fmpe, int col) const; /*! * \brief debug_dump diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp index 778804102..55353fca2 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp @@ -80,16 +80,16 @@ #define VIEW_FLAT 2 /* Thread constants */ + +// We need consts for that!! Defined in multiple places. #define COLUMN_THREAD_TITLE 0 #define COLUMN_THREAD_READ 1 #define COLUMN_THREAD_DATE 2 #define COLUMN_THREAD_DISTRIBUTION 3 #define COLUMN_THREAD_AUTHOR 4 -#define COLUMN_THREAD_SIGNED 5 -#define COLUMN_THREAD_CONTENT 6 -#define COLUMN_THREAD_COUNT 7 -#define COLUMN_THREAD_MSGID 8 -#define COLUMN_THREAD_NB_COLUMNS 9 +#define COLUMN_THREAD_CONTENT 5 +#define COLUMN_THREAD_MSGID 6 +#define COLUMN_THREAD_NB_COLUMNS 7 #define COLUMN_THREAD_DATA 0 // column for storing the userdata like parentid @@ -154,7 +154,36 @@ class AuthorItemDelegate: public QStyledItemDelegate public: AuthorItemDelegate() {} - virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex& index) const + QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override + { + QStyleOptionViewItemV4 opt = option; + initStyleOption(&opt, index); + + // disable default icon + opt.icon = QIcon(); + const QRect r = option.rect; + + RsGxsId id(index.data(Qt::UserRole).toString().toStdString()); + QString str; + QList icons; + QString comment; + + QFontMetricsF fm(option.font); + float f = fm.height(); + + QIcon icon ; + + if(!GxsIdDetails::MakeIdDesc(id, true, str, icons, comment,GxsIdDetails::ICON_TYPE_AVATAR)) + icon = GxsIdDetails::getLoadingIcon(id); + else + icon = *icons.begin(); + + QPixmap pix = icon.pixmap(r.size()); + + return QSize(pix.width() + fm.width(str),fm.height()); + } + + virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex& index) const override { if(!index.isValid()) { @@ -172,11 +201,14 @@ public: const QRect r = option.rect; - RsGxsId id(index.data(Qt::DisplayRole).toString().toStdString()); + RsGxsId id(index.data(Qt::UserRole).toString().toStdString()); QString str; QList icons; QString comment; + QFontMetricsF fm(painter->font()); + float f = fm.height(); + QIcon icon ; if(!GxsIdDetails::MakeIdDesc(id, true, str, icons, comment,GxsIdDetails::ICON_TYPE_AVATAR)) @@ -189,6 +221,7 @@ public: // 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); + painter->drawText(r.topLeft() + p + QPoint(pix.width()+f/2.0,f*0.8), str); } }; @@ -565,10 +598,18 @@ void GxsForumThreadWidget::threadListCustomPopupMenu(QPoint /*point*/) if (mFillThread) { return; } -#ifdef TODO QMenu contextMnu(this); - QList selectedItems = ui->threadTreeWidget->selectedItems(); + QModelIndexList selectedIndexes = ui->threadTreeWidget->selectionModel()->selectedIndexes(); + if(selectedIndexes.size() != 1) + return; + + QModelIndex index = *selectedIndexes.begin(); + + RsGxsMessageId mid(mThreadModel->data(index.sibling(index.row(),COLUMN_THREAD_MSGID),Qt::UserRole).toString().toStdString()); + + std::cerr << "Clicked on msg " << mid << std::endl; +#ifdef TODO QAction *editAct = new QAction(QIcon(IMAGE_MESSAGEEDIT), tr("Edit"), &contextMnu); connect(editAct, SIGNAL(triggered()), this, SLOT(editforummessage())); @@ -829,7 +870,7 @@ void GxsForumThreadWidget::changedThread(QModelIndex index) return; } - mThreadId = mOrigThreadId = RsGxsMessageId(mThreadModel->data(index.sibling(index.row(),COLUMN_THREAD_MSGID),Qt::DisplayRole).toString().toStdString()); + mThreadId = mOrigThreadId = RsGxsMessageId(mThreadModel->data(index.sibling(index.row(),COLUMN_THREAD_MSGID),Qt::UserRole).toString().toStdString()); std::cerr << "Switched to new thread ID " << mThreadId << std::endl; @@ -867,7 +908,7 @@ void GxsForumThreadWidget::calculateIconsAndFonts(QTreeWidgetItem *item, bool &h bool isNew = IS_MSG_NEW(status); bool unread = IS_MSG_UNREAD(status); bool missing = item->data(COLUMN_THREAD_DATA, ROLE_THREAD_MISSING).toBool(); - RsGxsMessageId msgId(item->data(COLUMN_THREAD_MSGID,Qt::DisplayRole).toString().toStdString()); + RsGxsMessageId msgId(item->data(COLUMN_THREAD_MSGID,Qt::UserRole).toString().toStdString()); // set icon if (missing) { @@ -899,7 +940,7 @@ void GxsForumThreadWidget::calculateIconsAndFonts(QTreeWidgetItem *item, bool &h bool is_pinned = mForumGroup.mPinnedPosts.ids.find(msgId) != mForumGroup.mPinnedPosts.ids.end(); // set font - for (int i = 0; i < COLUMN_THREAD_COUNT; ++i) { + for (int i = 0; i < COLUMN_THREAD_NB_COLUMNS; ++i) { QFont qf = item->font(i); if (!IS_GROUP_SUBSCRIBED(mSubscribeFlags)) { @@ -1452,7 +1493,7 @@ QTreeWidgetItem *GxsForumThreadWidget::convertMsgToThreadWidget(const RsGxsForum item->setText(COLUMN_THREAD_CONTENT, doc.toPlainText().replace(QString("\n"), QString(" "))); } - item->setData(COLUMN_THREAD_MSGID,Qt::DisplayRole, QString::fromStdString(msg.mMeta.mMsgId.toStdString())); + item->setData(COLUMN_THREAD_MSGID,Qt::UserRole, QString::fromStdString(msg.mMeta.mMsgId.toStdString())); //#TODO #if 0 if (IS_GROUP_SUBSCRIBED(subscribeFlags) && !(msginfo.mMsgFlags & RS_DISTRIB_MISSING_MSG)) {