From b7c8c16e2931c6841f2f4a0d08f92450ce021bc9 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 8 Feb 2019 15:41:20 +0100 Subject: [PATCH] inserted the model into the widget --- retroshare-gui/src/gui/MessagesDialog.cpp | 135 +++++++++++++++--- retroshare-gui/src/gui/MessagesDialog.h | 7 +- retroshare-gui/src/gui/MessagesDialog.ui | 14 +- .../src/gui/gxs/GxsIdTreeWidgetItem.h | 81 +++++++++++ retroshare-gui/src/gui/msgs/MessageModel.h | 10 ++ retroshare-gui/src/gui/msgs/MessageWidget.cpp | 2 +- 6 files changed, 214 insertions(+), 35 deletions(-) diff --git a/retroshare-gui/src/gui/MessagesDialog.cpp b/retroshare-gui/src/gui/MessagesDialog.cpp index 0f9419f07..74f370540 100644 --- a/retroshare-gui/src/gui/MessagesDialog.cpp +++ b/retroshare-gui/src/gui/MessagesDialog.cpp @@ -40,6 +40,7 @@ #include "msgs/MessageUserNotify.h" #include "msgs/MessageWidget.h" #include "msgs/TagsMenu.h" +#include "msgs/MessageModel.h" #include "settings/rsharesettings.h" #include "util/DateTime.h" @@ -99,20 +100,24 @@ MessagesDialog::LockUpdate::LockUpdate (MessagesDialog *pDialog, bool bUpdate) { +#ifdef TODO m_pDialog = pDialog; m_bUpdate = bUpdate; ++m_pDialog->lockUpdate; +#endif } MessagesDialog::LockUpdate::~LockUpdate () { +#ifdef TODO if(--m_pDialog->lockUpdate < 0) m_pDialog->lockUpdate = 0; if (m_bUpdate && m_pDialog->lockUpdate == 0) { m_pDialog->insertMessages(); } +#endif } void MessagesDialog::LockUpdate::setUpdate(bool bUpdate) @@ -120,6 +125,25 @@ void MessagesDialog::LockUpdate::setUpdate(bool bUpdate) m_bUpdate = bUpdate; } +class MessageSortFilterProxyModel: public QSortFilterProxyModel +{ +public: + MessageSortFilterProxyModel(const QHeaderView *header,QObject *parent = NULL): QSortFilterProxyModel(parent),m_header(header) {} + + bool lessThan(const QModelIndex& left, const QModelIndex& right) const override + { + return left.data(RsMessageModel::SortRole) < right.data(RsMessageModel::SortRole) ; + } + + bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override + { + return sourceModel()->index(source_row,0,source_parent).data(RsMessageModel::FilterRole).toString() == RsMessageModel::FilterString ; + } + +private: + const QHeaderView *m_header ; +}; + /** Constructor */ MessagesDialog::MessagesDialog(QWidget *parent) : MainPage(parent) @@ -165,6 +189,22 @@ MessagesDialog::MessagesDialog(QWidget *parent) mCurrMsgId = ""; + mMessageModel = new RsMessageModel(this); + mMessageProxyModel = new MessageSortFilterProxyModel(ui.messageTreeWidget->header(),this); + mMessageProxyModel->setSourceModel(mMessageModel); + mMessageProxyModel->setSortRole(RsMessageModel::SortRole); + ui.messageTreeWidget->setModel(mMessageProxyModel); + + mMessageProxyModel->setFilterRole(RsMessageModel::FilterRole); + mMessageProxyModel->setFilterRegExp(QRegExp(QString(RsMessageModel::FilterString))) ; + + ui.messageTreeWidget->setSortingEnabled(true); + + //ui.messageTreeWidget->setItemDelegateForColumn(RsMessageModel::COLUMN_THREAD_DISTRIBUTION,new DistributionItemDelegate()) ; + ui.messageTreeWidget->setItemDelegateForColumn(RsMessageModel::COLUMN_THREAD_AUTHOR,new GxsIdTreeItemDelegate()) ; + //ui.messageTreeWidget->setItemDelegateForColumn(RsGxsForumModel::COLUMN_THREAD_READ,new ReadStatusItemDelegate()) ; + +#ifdef TO_REMOVE // Set the QStandardItemModel ui.messageTreeWidget->setColumnCount(COLUMN_COUNT); QTreeWidgetItem *headerItem = ui.messageTreeWidget->headerItem(); @@ -190,6 +230,7 @@ MessagesDialog::MessagesDialog(QWidget *parent) headerItem->setToolTip(COLUMN_DATE, tr("Click to sort by date")); headerItem->setToolTip(COLUMN_TAGS, tr("Click to sort by tags")); headerItem->setToolTip(COLUMN_STAR, tr("Click to sort by star")); +#endif mMessageCompareRole = new RSTreeWidgetItemCompareRole; mMessageCompareRole->setRole(COLUMN_SUBJECT, ROLE_SORT); @@ -287,9 +328,11 @@ MessagesDialog::MessagesDialog(QWidget *parent) // create timer for navigation timer = new RsProtectedTimer(this); +#ifdef TODO timer->setInterval(300); timer->setSingleShot(true); connect(timer, SIGNAL(timeout()), this, SLOT(updateCurrentMessage())); +#endif ui.messageTreeWidget->installEventFilter(this); @@ -380,6 +423,7 @@ void MessagesDialog::processSettings(bool load) bool MessagesDialog::eventFilter(QObject *obj, QEvent *event) { +#ifdef TODO if (obj == ui.messageTreeWidget) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(event); @@ -390,12 +434,14 @@ bool MessagesDialog::eventFilter(QObject *obj, QEvent *event) } } } +#endif // pass the event on to the parent class return MainPage::eventFilter(obj, event); } void MessagesDialog::changeEvent(QEvent *e) { +#ifdef TODO QWidget::changeEvent(e); switch (e->type()) { case QEvent::StyleChange: @@ -405,6 +451,7 @@ void MessagesDialog::changeEvent(QEvent *e) // remove compiler warnings break; } +#endif } void MessagesDialog::fillQuickView() @@ -486,17 +533,32 @@ void MessagesDialog::fillQuickView() // else // MainPage::keyPressEvent(e) ; //} +int MessagesDialog::getSelectedMessages(QList& mid) +{ + //To check if the selection has more than one row. + + mid.clear(); + QModelIndexList qmil = ui.messageTreeWidget->selectionModel()->selectedRows(); + + foreach(const QModelIndex& m, qmil) + mid.push_back(m.sibling(m.row(),COLUMN_DATA).data(ROLE_MSGID).toString()) ; + + return mid.size(); +} int MessagesDialog::getSelectedMsgCount (QList *items, QList *itemsRead, QList *itemsUnread, QList *itemsStar) { +#ifdef TODO if (items) items->clear(); if (itemsRead) itemsRead->clear(); if (itemsUnread) itemsUnread->clear(); if (itemsStar) itemsStar->clear(); //To check if the selection has more than one row. - QList selectedItems = ui.messageTreeWidget->selectedItems(); - foreach (QTreeWidgetItem *item, selectedItems) + QList selectedMessages; + getSelectedMessages(selectedMessages); + + foreach (const QString&, selectedMessages) { if (items || itemsRead || itemsUnread || itemsStar) { if (items) items->append(item); @@ -516,6 +578,8 @@ int MessagesDialog::getSelectedMsgCount (QList *items, QListclear(); +// ui.messageTreeWidget->clear(); ui.quickViewWidget->setCurrentItem(NULL); listMode = LIST_BOX; - insertMessages(); - insertMsgTxtAndFiles(ui.messageTreeWidget->currentItem()); +// insertMessages(); +// insertMsgTxtAndFiles(ui.messageTreeWidget->currentItem()); + switch(box_row) + { + case 0: mMessageModel->setCurrentBox(RsMessageModel::BOX_INBOX ); break; + case 1: mMessageModel->setCurrentBox(RsMessageModel::BOX_OUTBOX); break; + case 2: mMessageModel->setCurrentBox(RsMessageModel::BOX_DRAFTS); break; + case 3: mMessageModel->setCurrentBox(RsMessageModel::BOX_SENT ); break; + case 4: mMessageModel->setCurrentBox(RsMessageModel::BOX_TRASH ); break; + default: + mMessageModel->setCurrentBox(RsMessageModel::BOX_NONE); break; + } inChange = false; } void MessagesDialog::changeQuickView(int newrow) { +#warning Missing code here! +#ifdef TODO Q_UNUSED(newrow); if (inChange) { @@ -799,16 +875,20 @@ void MessagesDialog::changeQuickView(int newrow) insertMsgTxtAndFiles(ui.messageTreeWidget->currentItem()); inChange = false; +#endif } void MessagesDialog::messagesTagsChanged() { +#ifdef TODO if (lockUpdate) { return; } fillQuickView(); insertMessages(); +#endif +#warning Missing code here! } static void InitIconAndFont(QTreeWidgetItem *item) @@ -877,6 +957,7 @@ static void InitIconAndFont(QTreeWidgetItem *item) item->setData(COLUMN_DATA, ROLE_UNREAD, isNew); } +#ifdef TO_REMOVE void MessagesDialog::insertMessages() { if (lockUpdate) { @@ -1336,10 +1417,12 @@ void MessagesDialog::insertMessages() updateMessageSummaryList(); } +#endif // current row in messageTreeWidget has changed void MessagesDialog::currentItemChanged(QTreeWidgetItem *item) { +#ifdef TODO timer->stop(); if (item) { @@ -1350,6 +1433,7 @@ void MessagesDialog::currentItemChanged(QTreeWidgetItem *item) } updateInterface(); +#endif } // click in messageTreeWidget @@ -1377,9 +1461,10 @@ void MessagesDialog::clicked(QTreeWidgetItem *item, int column) return; } } - +#ifdef TODO timer->stop(); timerIndex = ui.messageTreeWidget->indexOfTopLevelItem(item); +#endif // show current message directly updateCurrentMessage(); @@ -1421,8 +1506,10 @@ void MessagesDialog::doubleClicked(QTreeWidgetItem *item, int column) // show current message directly void MessagesDialog::updateCurrentMessage() { +#ifdef TODO timer->stop(); insertMsgTxtAndFiles(ui.messageTreeWidget->topLevelItem(timerIndex)); +#endif } void MessagesDialog::setMsgAsReadUnread(const QList &items, bool read) @@ -1573,10 +1660,11 @@ void MessagesDialog::insertMsgTxtAndFiles(QTreeWidgetItem *item, bool bSetToRead bool MessagesDialog::getCurrentMsg(std::string &cid, std::string &mid) { - QTreeWidgetItem *item = ui.messageTreeWidget->currentItem(); + QModelIndex indx = ui.messageTreeWidget->currentIndex(); +#ifdef TODO /* get its Ids */ - if (!item) + if (!indx.isValid()) { //If no message is selected. assume first message is selected. if (ui.messageTreeWidget->topLevelItemCount() == 0) @@ -1588,8 +1676,13 @@ bool MessagesDialog::getCurrentMsg(std::string &cid, std::string &mid) if (!item) { return false; } - cid = item->data(COLUMN_DATA, ROLE_SRCID).toString().toStdString(); - mid = item->data(COLUMN_DATA, ROLE_MSGID).toString().toStdString(); +#endif + if(!indx.isValid()) + return false ; + + cid = indx.sibling(indx.row(),COLUMN_DATA).data(ROLE_SRCID).toString().toStdString(); + mid = indx.sibling(indx.row(),COLUMN_DATA).data(ROLE_MSGID).toString().toStdString(); + return true; } @@ -1597,7 +1690,8 @@ void MessagesDialog::removemessage() { LockUpdate Lock (this, true); - QList selectedItems = ui.messageTreeWidget->selectedItems(); + QList selectedMessages ; + getSelectedMessages(selectedMessages); bool doDelete = false; int listrow = ui.listWidget->currentRow(); @@ -1609,16 +1703,11 @@ void MessagesDialog::removemessage() } } - foreach (QTreeWidgetItem *item, selectedItems) { - QString mid = item->data(COLUMN_DATA, ROLE_MSGID).toString(); - - // close tab showing this message -// closeTab(mid.toStdString()); - + foreach (const QString& m, selectedMessages) { if (doDelete) { - rsMail->MessageDelete(mid.toStdString()); + rsMail->MessageDelete(m.toStdString()); } else { - rsMail->MessageToTrash(mid.toStdString(), true); + rsMail->MessageToTrash(m.toStdString(), true); } } @@ -1658,7 +1747,9 @@ void MessagesDialog::buttonStyle() void MessagesDialog::filterChanged(const QString& text) { +#ifdef TODO ui.messageTreeWidget->filterItems(ui.filterLineEdit->currentFilter(), text); +#endif } void MessagesDialog::filterColumnChanged(int column) @@ -1669,9 +1760,11 @@ void MessagesDialog::filterColumnChanged(int column) if (column == COLUMN_CONTENT) { // need content ... refill - insertMessages(); + //insertMessages(); } +#ifdef TODO ui.messageTreeWidget->filterItems(column, ui.filterLineEdit->text()); +#endif // save index Settings->setValueToGroup("MessageDialog", "filterColumn", column); diff --git a/retroshare-gui/src/gui/MessagesDialog.h b/retroshare-gui/src/gui/MessagesDialog.h index 5801a6320..8b3a96dcd 100644 --- a/retroshare-gui/src/gui/MessagesDialog.h +++ b/retroshare-gui/src/gui/MessagesDialog.h @@ -30,6 +30,8 @@ class RSTreeWidgetItemCompareRole; class MessageWidget; +class QTreeWidgetItem; +class RsMessageModel; class MessagesDialog : public MainPage { @@ -59,9 +61,10 @@ public: protected: bool eventFilter(QObject *obj, QEvent *ev); void changeEvent(QEvent *e); + int getSelectedMessages(QList& mid); public slots: - void insertMessages(); + //void insertMessages(); void messagesTagsChanged(); private slots: @@ -153,6 +156,8 @@ private: RSTreeWidgetItemCompareRole *mMessageCompareRole; MessageWidget *msgWidget; + RsMessageModel *mMessageModel; + QSortFilterProxyModel *mMessageProxyModel; /* Color definitions (for standard see qss.default) */ QColor mTextColorInbox; diff --git a/retroshare-gui/src/gui/MessagesDialog.ui b/retroshare-gui/src/gui/MessagesDialog.ui index 1203a1eb2..1ea2a904b 100644 --- a/retroshare-gui/src/gui/MessagesDialog.ui +++ b/retroshare-gui/src/gui/MessagesDialog.ui @@ -6,7 +6,7 @@ 0 0 - 775 + 842 485 @@ -726,7 +726,7 @@ Qt::Vertical - + Qt::CustomContextMenu @@ -745,11 +745,6 @@ false - - - 1 - - @@ -843,11 +838,6 @@ QLineEdit
gui/common/LineEditClear.h
- - RSTreeWidget - QTreeWidget -
gui/common/RSTreeWidget.h
-
RSTabWidget QTabWidget diff --git a/retroshare-gui/src/gui/gxs/GxsIdTreeWidgetItem.h b/retroshare-gui/src/gui/gxs/GxsIdTreeWidgetItem.h index edb8515e4..c8b83dbe0 100644 --- a/retroshare-gui/src/gui/gxs/GxsIdTreeWidgetItem.h +++ b/retroshare-gui/src/gui/gxs/GxsIdTreeWidgetItem.h @@ -21,6 +21,8 @@ #ifndef _GXS_ID_TREEWIDGETITEM_H #define _GXS_ID_TREEWIDGETITEM_H +#include +#include #include #include "gui/common/RSTreeWidgetItem.h" @@ -69,4 +71,83 @@ private: RsGxsImage mAvatar; }; +// This class is responsible of rendering authors of type RsGxsId in tree views. Used in forums, messages, etc. + +class GxsIdTreeItemDelegate: public QStyledItemDelegate +{ +public: + GxsIdTreeItemDelegate() {} + + 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(1.2*(pix.width() + fm.width(str)),std::max(1.1*pix.height(),1.4*fm.height())); + } + + virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex& index) const override + { + 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; + + 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)) + icon = GxsIdDetails::getLoadingIcon(id); + else + icon = *icons.begin(); + + QPixmap pix = icon.pixmap(r.size()); + const QPoint p = QPoint(r.height()/2.0, (r.height() - pix.height())/2); + + // draw pixmap at center of item + painter->drawPixmap(r.topLeft() + p, pix); + painter->drawText(r.topLeft() + QPoint(r.height()+ f/2.0 + f/2.0,f*1.0), str); + } +}; + + #endif diff --git a/retroshare-gui/src/gui/msgs/MessageModel.h b/retroshare-gui/src/gui/msgs/MessageModel.h index 5cf13f370..89b0894f8 100644 --- a/retroshare-gui/src/gui/msgs/MessageModel.h +++ b/retroshare-gui/src/gui/msgs/MessageModel.h @@ -71,6 +71,15 @@ public: explicit RsMessageModel(QObject *parent = NULL); ~RsMessageModel(){} + enum BoxName { + BOX_NONE = 0x00, + BOX_INBOX = 0x01, + BOX_OUTBOX = 0x02, + BOX_DRAFTS = 0x03, + BOX_SENT = 0x04, + BOX_TRASH = 0x05 + }; + enum Columns { COLUMN_THREAD_STAR =0x00, COLUMN_THREAD_ATTACHMENT =0x01, @@ -96,6 +105,7 @@ public: // This method will asynchroneously update the data + void setCurrentBox(BoxName bn) {} void updateMessages(); const RsMessageId& currentMessageId() const; diff --git a/retroshare-gui/src/gui/msgs/MessageWidget.cpp b/retroshare-gui/src/gui/msgs/MessageWidget.cpp index 02d74b5da..5294c1d26 100644 --- a/retroshare-gui/src/gui/msgs/MessageWidget.cpp +++ b/retroshare-gui/src/gui/msgs/MessageWidget.cpp @@ -108,7 +108,7 @@ MessageWidget *MessageWidget::openMsg(const std::string &msgId, bool window) msgWidget->isWindow = window; msgWidget->fill(msgId); - if (parent) { + if (parent) { parent->addWidget(msgWidget); }