1 | /******************************************************************* |
2 | * |
3 | * Copyright 2006-2009 Dmitry Suzdalev <dimsuz@gmail.com> |
4 | * |
5 | * This file is part of the KDE project "KAtomic" |
6 | * |
7 | * KAtomic is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2, or (at your option) |
10 | * any later version. |
11 | * |
12 | * KAtomic is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with KAtomic; see the file COPYING. If not, write to |
19 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
20 | * Boston, MA 02110-1301, USA. |
21 | * |
22 | ********************************************************************/ |
23 | #include "levelsetdelegate.h" |
24 | |
25 | #include <QPainter> |
26 | #include <QApplication> |
27 | |
28 | #include <KGlobalSettings> |
29 | #include <KLocale> |
30 | |
31 | #include "commondefs.h" |
32 | |
33 | LevelSetDelegate::LevelSetDelegate(QObject* parent) |
34 | : QStyledItemDelegate(parent), m_lineHeight(-1) |
35 | { |
36 | } |
37 | |
38 | void LevelSetDelegate::paint(QPainter* p, const QStyleOptionViewItem& opt, const QModelIndex& index) const |
39 | { |
40 | p->save(); |
41 | QStyleOptionViewItemV4 option(opt); |
42 | initStyleOption(&option, index); |
43 | |
44 | // |
45 | // Draw item with no textwith a decoration and selection (if it's selected) |
46 | // |
47 | |
48 | // we want to paint text by ourselves, so set it to null, paint selection and then paint text |
49 | QString text = index.data(Qt::DisplayRole).toString(); |
50 | option.text = QString(); |
51 | option.decorationSize = QSize(48, 48); |
52 | |
53 | QStyle* style = QApplication::style(); |
54 | style->drawControl(QStyle::CE_ItemViewItem, &option, p, 0); |
55 | |
56 | if (option.state & QStyle::State_Selected) |
57 | p->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText)); |
58 | else |
59 | p->setPen(option.palette.color(QPalette::Normal, QPalette::Text)); |
60 | |
61 | // |
62 | // Draw text |
63 | // |
64 | int marginH = style->pixelMetric( QStyle::PM_FocusFrameHMargin ); |
65 | int marginV = style->pixelMetric( QStyle::PM_FocusFrameVMargin ); |
66 | int innerSpacing = 9; |
67 | |
68 | int textStartX = option.decorationSize.width() + innerSpacing; |
69 | QRect r = opt.rect.adjusted(textStartX, marginV*2, |
70 | 0, 0); |
71 | |
72 | QFontMetrics fm(opt.font); |
73 | |
74 | int flags = Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine; |
75 | QFont font = p->font(); |
76 | font.setBold(true); |
77 | p->setFont(font); |
78 | p->drawText(r, flags, text); |
79 | |
80 | // |
81 | // Draw Author name |
82 | // |
83 | QString authorName = index.data(KAtomic::LevelSetAuthorRole).toString(); |
84 | if (!authorName.isEmpty()) |
85 | { |
86 | if (option.state & QStyle::State_Selected) |
87 | p->setPen(option.palette.color(QPalette::Disabled, QPalette::HighlightedText)); |
88 | else |
89 | p->setPen(option.palette.color(QPalette::Disabled, QPalette::Text)); |
90 | |
91 | r = r.adjusted(innerSpacing, fm.lineSpacing(), -marginH*2, 0); |
92 | flags = Qt::AlignLeft | Qt::AlignTop; |
93 | |
94 | p->setFont(KGlobalSettings::smallestReadableFont()); |
95 | |
96 | QString text = i18n("by %1" , authorName); |
97 | QString authorEmail = index.data(KAtomic::LevelSetAuthorEmailRole).toString(); |
98 | if (!authorEmail.isEmpty()) |
99 | text.append(QString::fromLatin1(" <%1>" ).arg(authorEmail)); |
100 | |
101 | int numLevels = index.data(KAtomic::LevelSetLevelCountRole).toUInt(); |
102 | text.append(i18np(", contains 1 level" , ", contains %1 levels" , numLevels)); |
103 | |
104 | p->drawText(r, flags, text); |
105 | } |
106 | |
107 | // |
108 | // Draw description |
109 | // |
110 | QString descr = index.data(KAtomic::LevelSetDescriptionRole).toString(); |
111 | if (!descr.isEmpty()) |
112 | { |
113 | if (option.state & QStyle::State_Selected) |
114 | p->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText)); |
115 | else |
116 | p->setPen(option.palette.color(QPalette::Normal, QPalette::Text)); |
117 | |
118 | r = opt.rect.adjusted(textStartX, fm.lineSpacing()*2, -marginH*2, -marginV*2); |
119 | flags = Qt::AlignLeft | Qt::AlignBottom | Qt::TextSingleLine; |
120 | p->setFont(opt.font); |
121 | QString elided = fm.elidedText(descr, Qt::ElideMiddle, r.width(), flags); |
122 | p->drawText(r, flags, descr); |
123 | } |
124 | |
125 | p->restore(); |
126 | } |
127 | |
128 | QSize LevelSetDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const |
129 | { |
130 | Q_UNUSED(index) |
131 | |
132 | if (m_lineHeight == -1) |
133 | { |
134 | QFontMetrics fm(option.font); |
135 | m_lineHeight = fm.lineSpacing(); |
136 | } |
137 | |
138 | return QSize(option.rect.width(), qMax(m_lineHeight*3, 64)); |
139 | } |
140 | |