fixed update of QTreeView when something changes in Forum model using dataChanged() signal

This commit is contained in:
csoler 2018-11-24 22:06:34 +01:00
parent 6f83bb520e
commit 7384015850
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
4 changed files with 92 additions and 84 deletions

View File

@ -1,6 +1,7 @@
#include <QApplication>
#include <QFontMetrics>
#include <QModelIndex>
#include <QIcon>
#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<ForumModelPostEntry>& 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::vector<F
debug_dump();
emit layoutChanged();
emit dataChanged(createIndex(0,0,(void*)NULL), createIndex(0,COLUMN_THREAD_NB_COLUMNS-1,(void*)NULL));
}
void RsGxsForumModel::update_posts(const RsGxsGroupId& group_id)
@ -946,6 +932,8 @@ void RsGxsForumModel::setMsgReadStatus(const QModelIndex& i,bool read_status,boo
bool has_unread_below,has_read_below;
recursSetMsgReadStatus(entry,read_status,with_children) ;
recursUpdateReadStatus(0,has_unread_below,has_read_below);
emit dataChanged(createIndex(0,0,(void*)NULL), createIndex(0,COLUMN_THREAD_NB_COLUMNS-1,(void*)NULL));
}
void RsGxsForumModel::recursSetMsgReadStatus(ForumModelIndex i,bool read_status,bool with_children)

View File

@ -69,7 +69,8 @@ public:
COLUMN_THREAD_AUTHOR =0x04,
COLUMN_THREAD_CONTENT =0x05,
COLUMN_THREAD_MSGID =0x06,
COLUMN_THREAD_NB_COLUMNS =0x07,
COLUMN_THREAD_DATA =0x07,
COLUMN_THREAD_NB_COLUMNS =0x08,
};
enum Roles{ SortRole = Qt::UserRole+1,
@ -115,7 +116,7 @@ public:
QVariant authorRole (const ForumModelPostEntry& fmpe, int col) const;
QVariant sortRole (const ForumModelPostEntry& fmpe, int col) const;
QVariant fontRole (const ForumModelPostEntry& fmpe, int col) const;
QVariant textColorRole(const ForumModelPostEntry& fmpe, int col) const;
QVariant textColorRole (const ForumModelPostEntry& fmpe, int col) const;
/*!
* \brief debug_dump

View File

@ -139,6 +139,55 @@ public:
}
};
class ReadStatusItemDelegate: public QStyledItemDelegate
{
public:
ReadStatusItemDelegate() {}
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!index.isValid())
{
std::cerr << "(EE) attempt to draw an invalid index." << std::endl;
return ;
}
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
// disable default icon
opt.icon = QIcon();
// draw default item
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0);
const QRect r = option.rect;
QIcon icon ;
// get pixmap
unsigned int read_status = qvariant_cast<uint32_t>(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<QTreeWidgetItem*> rows;
//QList<QTreeWidgetItem*> rowsRead;
//QList<QTreeWidgetItem*> 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<QTreeWidgetItem*> 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)) ;

View File

@ -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();