File indexing completed on 2024-05-05 05:13:10
0001 /* 0002 This file is part of Akregator. 0003 0004 SPDX-FileCopyrightText: 2007 Frank Osterfeld <frank.osterfeld@kdemail.net> 0005 SPDX-FileCopyrightText: 2009 Jonathan Marten <jjm@keelhaul.me.uk> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 0008 */ 0009 0010 #include "subscriptionlistdelegate.h" 0011 #include "subscriptionlistmodel.h" 0012 0013 #include "akregator_debug.h" 0014 0015 #include <QApplication> 0016 #include <QHeaderView> 0017 #include <QStyle> 0018 #include <QTreeView> 0019 0020 using namespace Akregator; 0021 0022 Akregator::SubscriptionListDelegate::SubscriptionListDelegate(QWidget *parent) 0023 : QStyledItemDelegate(parent) 0024 { 0025 // TODO reimplement 0026 // connect(KGlobalSettings::self(), &KGlobalSettings::appearanceChanged, this, &SubscriptionListDelegate::recalculateRowHeight); 0027 recalculateRowHeight(); 0028 } 0029 0030 Akregator::SubscriptionListDelegate::~SubscriptionListDelegate() = default; 0031 0032 QSize Akregator::SubscriptionListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0033 { 0034 QSize size = QStyledItemDelegate::sizeHint(option, index); 0035 size.setHeight(qMax(size.height(), (m_viewIconHeight + 2))); 0036 // +2 for row top/bottom margin 0037 return size; 0038 } 0039 0040 void Akregator::SubscriptionListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0041 { 0042 QStyleOptionViewItem newOption = option; 0043 if (index.data(SubscriptionListModel::HasUnreadRole).toBool()) { 0044 // feed has unread articles 0045 newOption.font.setBold(true); 0046 } 0047 0048 // fix [Bug 190052] numeric columns aligned to the left 0049 if (index.column() == SubscriptionListModel::UnreadCountColumn || index.column() == SubscriptionListModel::TotalCountColumn) { 0050 newOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; 0051 } else { 0052 newOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter; 0053 } 0054 0055 // No need to translate the painter here - the item is vertically centered 0056 // within its sizeHint rectangle. 0057 QStyledItemDelegate::paint(painter, newOption, index); 0058 } 0059 0060 void Akregator::SubscriptionListDelegate::recalculateRowHeight() 0061 { 0062 m_viewIconHeight = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize); 0063 qCDebug(AKREGATOR_LOG) << "icon height" << m_viewIconHeight; 0064 } 0065 0066 void Akregator::SubscriptionListDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const 0067 { 0068 QStyledItemDelegate::initStyleOption(option, index); 0069 0070 if (index.column() != 0) { 0071 // Append unread count to the title column only (it is always the first 0072 // one) 0073 return; 0074 } 0075 0076 auto view = static_cast<QTreeView *>(parent()); 0077 if (!view->header()->isSectionHidden(SubscriptionListModel::UnreadCountColumn)) { 0078 // Do not append unread count to the title if the unread count column 0079 // is visible 0080 return; 0081 } else { 0082 view->header()->resizeSection(SubscriptionListModel::UnreadCountColumn, QHeaderView::ResizeToContents); 0083 } 0084 0085 if (!view->header()->isSectionHidden(SubscriptionListModel::TotalCountColumn)) { 0086 view->header()->resizeSection(SubscriptionListModel::TotalCountColumn, QHeaderView::ResizeToContents); 0087 } 0088 0089 auto optionV4 = qstyleoption_cast<QStyleOptionViewItem *>(option); 0090 if (!optionV4) { 0091 // Should never happen, but play it safe 0092 return; 0093 } 0094 0095 QModelIndex unreadIndex = index.sibling(index.row(), SubscriptionListModel::UnreadCountColumn); 0096 int unread = unreadIndex.data().toInt(); 0097 if (unread > 0) { 0098 optionV4->text += QStringLiteral(" (%1)").arg(unread); 0099 } 0100 } 0101 0102 #include "moc_subscriptionlistdelegate.cpp"