File indexing completed on 2024-11-24 04:53:02

0001 /* Copyright (C) Roland Pallai <dap78@magex.hu>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "MsgItemDelegate.h"
0024 #include <QTreeView>
0025 #include "Imap/Model/ItemRoles.h"
0026 #include "Imap/Model/MsgListModel.h"
0027 #include "MsgListView.h"
0028 
0029 namespace Gui
0030 {
0031 
0032 MsgItemDelegate::MsgItemDelegate(QObject *parent, Imap::Mailbox::FavoriteTagsModel *m_favoriteTagsModel) :
0033     ColoredItemDelegate(parent), m_favoriteTagsModel(m_favoriteTagsModel)
0034 {
0035 }
0036 
0037 QColor MsgItemDelegate::itemColor(const QModelIndex &index) const
0038 {
0039     auto view = static_cast<MsgListView*>(parent());
0040 
0041     Q_ASSERT(index.isValid());
0042     QModelIndex sindex = index.sibling(index.row(), Imap::Mailbox::MsgListModel::SUBJECT);
0043     QVariant flags;
0044     if (!sindex.model()->hasChildren(sindex) || view->isExpanded(sindex)) {
0045         flags = index.data(Imap::Mailbox::RoleMessageFlags);
0046     } else {
0047         flags = index.data(Imap::Mailbox::RoleThreadAggregatedFlags);
0048     }
0049 
0050     return m_favoriteTagsModel->findBestColorForTags(flags.toStringList());
0051 }
0052 
0053 QFont MsgItemDelegate::itemFont(const QModelIndex &index) const
0054 {
0055     QFont font;
0056 
0057     Q_ASSERT(index.isValid());
0058 
0059     // We will need the data, but asking for Flags or IsMarkedXYZ doesn't cause a fetch
0060     index.data(Imap::Mailbox::RoleMessageSubject);
0061 
0062     // These items should definitely *not* be rendered in bold
0063     if (!index.data(Imap::Mailbox::RoleIsFetched).toBool())
0064         return font;
0065 
0066     if (index.data(Imap::Mailbox::RoleMessageIsMarkedDeleted).toBool())
0067         font.setStrikeOut(true);
0068 
0069     if (! index.data(Imap::Mailbox::RoleMessageIsMarkedRead).toBool()) {
0070         // If any message is marked as unread, show it in bold and be done with it
0071         font.setBold(true);
0072     } else if (index.model()->hasChildren(index.sibling(index.row(), 0)) &&
0073                index.data(Imap::Mailbox::RoleThreadRootWithUnreadMessages).toBool()) {
0074         // If this node is not marked as read, is a root of some thread and that thread
0075         // contains unread messages, display the thread's root underlined
0076         font.setUnderline(true);
0077     }
0078 
0079     if (index.column() == Imap::Mailbox::MsgListModel::SUBJECT && index.data(Imap::Mailbox::RoleMessageSubject).toString().isEmpty()) {
0080         font.setItalic(true);
0081     }
0082 
0083     return font;
0084 }
0085 
0086 void MsgItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0087 {
0088     QStyleOptionViewItem viewOption(option);
0089     auto foregroundColor = itemColor(index);
0090     if (foregroundColor.isValid())
0091         viewOption.palette.setColor(QPalette::Text, foregroundColor);
0092     viewOption.font = itemFont(index);
0093     ColoredItemDelegate::paintWithForeground(painter, viewOption, index, foregroundColor);
0094 }
0095 
0096 }