File indexing completed on 2024-04-21 04:02:00

0001 /*
0002     This file is part of the KDE project "KAtomic"
0003 
0004     SPDX-FileCopyrightText: 2006-2009 Dmitry Suzdalev <dimsuz@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "levelsetdelegate.h"
0010 
0011 #include "commondefs.h"
0012 
0013 #include <KLocalizedString>
0014 #include <QFontDatabase>
0015 
0016 #include <QPainter>
0017 #include <QApplication>
0018 
0019 LevelSetDelegate::LevelSetDelegate(QObject* parent)
0020     : QStyledItemDelegate(parent), m_lineHeight(-1)
0021 {
0022 }
0023 
0024 void LevelSetDelegate::paint(QPainter* p, const QStyleOptionViewItem& opt, const QModelIndex& index) const
0025 {
0026     p->save();
0027     QStyleOptionViewItem option(opt);
0028     initStyleOption(&option, index);
0029 
0030     //
0031     // Draw item with no textwith a decoration and selection (if it's selected)
0032     //
0033 
0034     // we want to paint text by ourselves, so set it to null, paint selection and then paint text
0035     QString text = index.data(Qt::DisplayRole).toString();
0036     option.text = QString();
0037     option.decorationSize = QSize(48, 48);
0038 
0039     QStyle* style = QApplication::style();
0040     style->drawControl(QStyle::CE_ItemViewItem, &option, p, nullptr);
0041 
0042     if (option.state & QStyle::State_Selected)
0043         p->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText));
0044     else
0045         p->setPen(option.palette.color(QPalette::Normal, QPalette::Text));
0046 
0047     //
0048     // Draw text
0049     //
0050     int marginH = style->pixelMetric( QStyle::PM_FocusFrameHMargin );
0051     int marginV = style->pixelMetric( QStyle::PM_FocusFrameVMargin );
0052     int innerSpacing = 9;
0053 
0054     int textStartX = option.decorationSize.width() + innerSpacing;
0055     QRect r = opt.rect.adjusted(textStartX, marginV*2, 0, 0);
0056 
0057     QFontMetrics fm(opt.font);
0058 
0059     int flags = Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine;
0060     QFont font = p->font();
0061     font.setBold(true);
0062     p->setFont(font);
0063     p->drawText(r, flags, text);
0064 
0065     //
0066     // Draw Author name
0067     //
0068     QString authorName = index.data(KAtomic::LevelSetAuthorRole).toString();
0069     if (!authorName.isEmpty())
0070     {
0071         if (option.state & QStyle::State_Selected)
0072             p->setPen(option.palette.color(QPalette::Disabled, QPalette::HighlightedText));
0073         else
0074             p->setPen(option.palette.color(QPalette::Disabled, QPalette::Text));
0075 
0076         r = r.adjusted(innerSpacing, fm.lineSpacing(), -marginH*2, 0);
0077         flags = Qt::AlignLeft | Qt::AlignTop;
0078 
0079         p->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
0080 
0081         QString text = i18n("by %1", authorName);
0082         QString authorEmail = index.data(KAtomic::LevelSetAuthorEmailRole).toString();
0083         if (!authorEmail.isEmpty())
0084             text.append(QStringLiteral(" <%1>").arg(authorEmail));
0085 
0086         int numLevels = index.data(KAtomic::LevelSetLevelCountRole).toUInt();
0087         text.append(i18np(", contains 1 level", ", contains %1 levels", numLevels));
0088 
0089         p->drawText(r, flags, text);
0090     }
0091 
0092     //
0093     // Draw description
0094     //
0095     QString descr = index.data(KAtomic::LevelSetDescriptionRole).toString();
0096     if (!descr.isEmpty())
0097     {
0098         if (option.state & QStyle::State_Selected)
0099             p->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText));
0100         else
0101             p->setPen(option.palette.color(QPalette::Normal, QPalette::Text));
0102 
0103         r = opt.rect.adjusted(textStartX, fm.lineSpacing()*2, -marginH*2, -marginV*2);
0104         flags = Qt::AlignLeft | Qt::AlignBottom | Qt::TextSingleLine;
0105         p->setFont(opt.font);
0106         QString elided = fm.elidedText(descr, Qt::ElideMiddle, r.width(), flags);
0107         p->drawText(r, flags, descr);
0108     }
0109 
0110     p->restore();
0111 }
0112 
0113 QSize LevelSetDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
0114 {
0115     Q_UNUSED(index)
0116 
0117     if (m_lineHeight == -1)
0118     {
0119         QFontMetrics fm(option.font);
0120         m_lineHeight = fm.lineSpacing();
0121     }
0122 
0123     return QSize(option.rect.width(), qMax(m_lineHeight*3, 64));
0124 }
0125 
0126 #include "moc_levelsetdelegate.cpp"