File indexing completed on 2024-05-12 05:09:46
0001 /*************************************************************************** 0002 Copyright (C) 2008-2020 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 // a good portion of this was borrowed from akonadi/collectionstatisticsdelegate.cpp, which is 0026 // Copyright (c) 2008 Thomas McGuire <thomas.mcguire@gmx.net> 0027 // used under the terms of the GNU LGPL v2 0028 0029 #include "countdelegate.h" 0030 #include "../models/models.h" 0031 0032 #include <QTreeView> 0033 #include <QPainter> 0034 0035 #include <KColorScheme> 0036 0037 using Tellico::GUI::CountDelegate; 0038 0039 CountDelegate::CountDelegate(QTreeView* parent_) : QStyledItemDelegate(parent_), m_showCount(false) { 0040 } 0041 0042 CountDelegate::~CountDelegate() { 0043 } 0044 0045 QTreeView* CountDelegate::parent() const { 0046 return static_cast<QTreeView*>(QObject::parent()); 0047 } 0048 0049 void CountDelegate::initStyleOption(QStyleOptionViewItem* option_, 0050 const QModelIndex& index_) const { 0051 if(m_showCount) { 0052 QStyleOptionViewItem* noTextOption = qstyleoption_cast<QStyleOptionViewItem*>(option_); 0053 QStyledItemDelegate::initStyleOption(noTextOption, index_); 0054 noTextOption->text.clear(); 0055 } else { 0056 QStyledItemDelegate::initStyleOption(option_, index_); 0057 } 0058 } 0059 0060 void CountDelegate::paint(QPainter* painter_, 0061 const QStyleOptionViewItem& option_, 0062 const QModelIndex& index_) const { 0063 Q_ASSERT(index_.isValid()); 0064 0065 // only paint for first column and no parent 0066 m_showCount = index_.column() == 0 && !index_.parent().isValid(); 0067 0068 // First, paint the basic, but without the text. We remove the text 0069 // in initStyleOption(), which gets called by QStyledItemDelegate::paint(). 0070 QStyledItemDelegate::paint(painter_, option_, index_); 0071 0072 if(!m_showCount) { 0073 return; 0074 } 0075 m_showCount = false; 0076 0077 // Now, we retrieve the correct style option by calling initStyleOption from 0078 // the superclass. 0079 QStyleOptionViewItem option(option_); 0080 QStyledItemDelegate::initStyleOption(&option, index_); 0081 QString text = option.text; 0082 0083 QVariant countValue = index_.data(RowCountRole); 0084 QString countString = QStringLiteral(" (%1)").arg(countValue.toInt()); 0085 0086 // Now calculate the rectangle for the text 0087 QStyle* s = parent()->style(); 0088 const QWidget* widget = option.widget; 0089 const QRect itemRect = s->subElementRect(QStyle::SE_ItemViewItemText, &option, widget); 0090 0091 // Squeeze the folder text if it is to big and calculate the rectangles 0092 // where the text and the count will be drawn to 0093 QFontMetrics fm(painter_->fontMetrics()); 0094 #if (QT_VERSION < QT_VERSION_CHECK(5, 11, 0)) 0095 const int countWidth = fm.width(countString); 0096 int textWidth = fm.width(text); 0097 #else 0098 const int countWidth = fm.horizontalAdvance(countString); 0099 int textWidth = fm.horizontalAdvance(text); 0100 #endif 0101 if(textWidth + countWidth > itemRect.width()) { 0102 text = fm.elidedText(text, Qt::ElideRight, itemRect.width() - countWidth); 0103 #if (QT_VERSION < QT_VERSION_CHECK(5, 11, 0)) 0104 textWidth = fm.width(text); 0105 #else 0106 textWidth = fm.horizontalAdvance(text); 0107 #endif 0108 } 0109 0110 const int top = itemRect.top() + (itemRect.height() - fm.height()) / 2; 0111 QRect textRect = itemRect; 0112 textRect.setRight(textRect.left() + textWidth); 0113 textRect.setTop(top); 0114 textRect.setHeight(fm.height()); 0115 QRect countRect = itemRect; 0116 countRect.setLeft(textRect.right()); 0117 countRect.setTop(top); 0118 countRect.setHeight(fm.height()); 0119 0120 QColor textColor = index_.data(Qt::ForegroundRole).value<QColor>(); 0121 if(!textColor.isValid()) { 0122 if(option.state & QStyle::State_Selected) { 0123 textColor = option.palette.highlightedText().color(); 0124 } else { 0125 textColor = option.palette.text().color(); 0126 } 0127 } 0128 KColorScheme::ColorSet colorSet = (option.state & QStyle::State_Selected) ? 0129 KColorScheme::Selection : KColorScheme::View; 0130 KColorScheme cs(QPalette::Active, colorSet); 0131 QColor countColor = cs.foreground(KColorScheme::LinkText).color(); 0132 0133 painter_->save(); 0134 painter_->setPen(textColor); 0135 painter_->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text); 0136 painter_->setPen(countColor); 0137 painter_->drawText(countRect, Qt::AlignLeft | Qt::AlignVCenter, countString); 0138 painter_->restore(); 0139 }