~neon/katomic/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************
 *
 * Copyright 2006-2009 Dmitry Suzdalev <dimsuz@gmail.com>
 *
 * This file is part of the KDE project "KAtomic"
 *
 * KAtomic is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * KAtomic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KAtomic; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 ********************************************************************/
#include "levelsetdelegate.h"

#include <QPainter>
#include <QApplication>

#include <KLocalizedString>
#include <QFontDatabase>

#include "commondefs.h"

LevelSetDelegate::LevelSetDelegate(QObject* parent)
    : QStyledItemDelegate(parent), m_lineHeight(-1)
{
}

void LevelSetDelegate::paint(QPainter* p, const QStyleOptionViewItem& opt, const QModelIndex& index) const
{
    p->save();
    QStyleOptionViewItem option(opt);
    initStyleOption(&option, index);

    //
    // Draw item with no textwith a decoration and selection (if it's selected)
    //

    // we want to paint text by ourselves, so set it to null, paint selection and then paint text
    QString text = index.data(Qt::DisplayRole).toString();
    option.text = QString();
    option.decorationSize = QSize(48, 48);

    QStyle* style = QApplication::style();
    style->drawControl(QStyle::CE_ItemViewItem, &option, p, nullptr);

    if (option.state & QStyle::State_Selected)
        p->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText));
    else
        p->setPen(option.palette.color(QPalette::Normal, QPalette::Text));

    //
    // Draw text
    //
    int marginH = style->pixelMetric( QStyle::PM_FocusFrameHMargin );
    int marginV = style->pixelMetric( QStyle::PM_FocusFrameVMargin );
    int innerSpacing = 9;

    int textStartX = option.decorationSize.width() + innerSpacing;
    QRect r = opt.rect.adjusted(textStartX, marginV*2, 0, 0);

    QFontMetrics fm(opt.font);

    int flags = Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine;
    QFont font = p->font();
    font.setBold(true);
    p->setFont(font);
    p->drawText(r, flags, text);

    //
    // Draw Author name
    //
    QString authorName = index.data(KAtomic::LevelSetAuthorRole).toString();
    if (!authorName.isEmpty())
    {
        if (option.state & QStyle::State_Selected)
            p->setPen(option.palette.color(QPalette::Disabled, QPalette::HighlightedText));
        else
            p->setPen(option.palette.color(QPalette::Disabled, QPalette::Text));

        r = r.adjusted(innerSpacing, fm.lineSpacing(), -marginH*2, 0);
        flags = Qt::AlignLeft | Qt::AlignTop;

        p->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));

        QString text = i18n("by %1", authorName);
        QString authorEmail = index.data(KAtomic::LevelSetAuthorEmailRole).toString();
        if (!authorEmail.isEmpty())
            text.append(QStringLiteral(" <%1>").arg(authorEmail));

        int numLevels = index.data(KAtomic::LevelSetLevelCountRole).toUInt();
        text.append(i18np(", contains 1 level", ", contains %1 levels", numLevels));

        p->drawText(r, flags, text);
    }

    //
    // Draw description
    //
    QString descr = index.data(KAtomic::LevelSetDescriptionRole).toString();
    if (!descr.isEmpty())
    {
        if (option.state & QStyle::State_Selected)
            p->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText));
        else
            p->setPen(option.palette.color(QPalette::Normal, QPalette::Text));

        r = opt.rect.adjusted(textStartX, fm.lineSpacing()*2, -marginH*2, -marginV*2);
        flags = Qt::AlignLeft | Qt::AlignBottom | Qt::TextSingleLine;
        p->setFont(opt.font);
        QString elided = fm.elidedText(descr, Qt::ElideMiddle, r.width(), flags);
        p->drawText(r, flags, descr);
    }

    p->restore();
}

QSize LevelSetDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    Q_UNUSED(index)

    if (m_lineHeight == -1)
    {
        QFontMetrics fm(option.font);
        m_lineHeight = fm.lineSpacing();
    }

    return QSize(option.rect.width(), qMax(m_lineHeight*3, 64));
}