1 /*
2 This file is part of the KDE project "KAtomic"
3
4 SPDX-FileCopyrightText: 2006-2009 Dmitry Suzdalev <dimsuz@gmail.com>
5
6 SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "levelsetdelegate.h"
10
11 #include "commondefs.h"
12
13 #include <KLocalizedString>
14 #include <QFontDatabase>
15
16 #include <QPainter>
17 #include <QApplication>
18
LevelSetDelegate(QObject * parent)19 LevelSetDelegate::LevelSetDelegate(QObject* parent)
20 : QStyledItemDelegate(parent), m_lineHeight(-1)
21 {
22 }
23
paint(QPainter * p,const QStyleOptionViewItem & opt,const QModelIndex & index) const24 void LevelSetDelegate::paint(QPainter* p, const QStyleOptionViewItem& opt, const QModelIndex& index) const
25 {
26 p->save();
27 QStyleOptionViewItem option(opt);
28 initStyleOption(&option, index);
29
30 //
31 // Draw item with no textwith a decoration and selection (if it's selected)
32 //
33
34 // we want to paint text by ourselves, so set it to null, paint selection and then paint text
35 QString text = index.data(Qt::DisplayRole).toString();
36 option.text = QString();
37 option.decorationSize = QSize(48, 48);
38
39 QStyle* style = QApplication::style();
40 style->drawControl(QStyle::CE_ItemViewItem, &option, p, nullptr);
41
42 if (option.state & QStyle::State_Selected)
43 p->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText));
44 else
45 p->setPen(option.palette.color(QPalette::Normal, QPalette::Text));
46
47 //
48 // Draw text
49 //
50 int marginH = style->pixelMetric( QStyle::PM_FocusFrameHMargin );
51 int marginV = style->pixelMetric( QStyle::PM_FocusFrameVMargin );
52 int innerSpacing = 9;
53
54 int textStartX = option.decorationSize.width() + innerSpacing;
55 QRect r = opt.rect.adjusted(textStartX, marginV*2, 0, 0);
56
57 QFontMetrics fm(opt.font);
58
59 int flags = Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine;
60 QFont font = p->font();
61 font.setBold(true);
62 p->setFont(font);
63 p->drawText(r, flags, text);
64
65 //
66 // Draw Author name
67 //
68 QString authorName = index.data(KAtomic::LevelSetAuthorRole).toString();
69 if (!authorName.isEmpty())
70 {
71 if (option.state & QStyle::State_Selected)
72 p->setPen(option.palette.color(QPalette::Disabled, QPalette::HighlightedText));
73 else
74 p->setPen(option.palette.color(QPalette::Disabled, QPalette::Text));
75
76 r = r.adjusted(innerSpacing, fm.lineSpacing(), -marginH*2, 0);
77 flags = Qt::AlignLeft | Qt::AlignTop;
78
79 p->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
80
81 QString text = i18n("by %1", authorName);
82 QString authorEmail = index.data(KAtomic::LevelSetAuthorEmailRole).toString();
83 if (!authorEmail.isEmpty())
84 text.append(QStringLiteral(" <%1>").arg(authorEmail));
85
86 int numLevels = index.data(KAtomic::LevelSetLevelCountRole).toUInt();
87 text.append(i18np(", contains 1 level", ", contains %1 levels", numLevels));
88
89 p->drawText(r, flags, text);
90 }
91
92 //
93 // Draw description
94 //
95 QString descr = index.data(KAtomic::LevelSetDescriptionRole).toString();
96 if (!descr.isEmpty())
97 {
98 if (option.state & QStyle::State_Selected)
99 p->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText));
100 else
101 p->setPen(option.palette.color(QPalette::Normal, QPalette::Text));
102
103 r = opt.rect.adjusted(textStartX, fm.lineSpacing()*2, -marginH*2, -marginV*2);
104 flags = Qt::AlignLeft | Qt::AlignBottom | Qt::TextSingleLine;
105 p->setFont(opt.font);
106 QString elided = fm.elidedText(descr, Qt::ElideMiddle, r.width(), flags);
107 p->drawText(r, flags, descr);
108 }
109
110 p->restore();
111 }
112
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const113 QSize LevelSetDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
114 {
115 Q_UNUSED(index)
116
117 if (m_lineHeight == -1)
118 {
119 QFontMetrics fm(option.font);
120 m_lineHeight = fm.lineSpacing();
121 }
122
123 return QSize(option.rect.width(), qMax(m_lineHeight*3, 64));
124 }
125