1
/***************************************************************************
2
* Copyright © 2010 Jonathan Thomas <echidnaman@kubuntu.org> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License as *
6
* published by the Free Software Foundation; either version 2 of *
7
* the License or (at your option) version 3 or any later version *
8
* accepted by the membership of KDE e.V. (or its successor approved *
9
* by the membership of KDE e.V.), which shall act as a proxy *
10
* defined in Section 14 of version 3 of the license. *
12
* This program 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. *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
19
***************************************************************************/
21
#include "PackageDelegate.h"
24
#include <QApplication>
25
#include <QtGui/QPainter>
29
#include <KIconLoader>
32
#include "../libmuon/MuonStrings.h"
33
#include "PackageModel.h"
35
PackageDelegate::PackageDelegate(QObject *parent)
36
: QAbstractItemDelegate(parent)
37
, m_icon(KIcon("muon"))
38
, m_supportedEmblem(KIcon("ubuntu-logo").pixmap(QSize(12,12)))
39
, m_lockedEmblem(KIcon("object-locked").pixmap(QSize(12,12)))
43
m_iconSize = KIconLoader::SizeSmallMedium;
46
PackageDelegate::~PackageDelegate()
50
void PackageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
52
if (!index.isValid()) {
56
paintBackground(painter, option);
58
switch (index.column()) {
60
paintPackageName(painter, option, index);
64
paintText(painter, option, index);
71
void PackageDelegate::paintBackground(QPainter *painter, const QStyleOptionViewItem &option) const
73
QStyleOptionViewItemV4 opt(option);
74
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
75
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
78
void PackageDelegate::paintPackageName(QPainter *painter, const QStyleOptionViewItem &option , const QModelIndex &index) const
80
int left = option.rect.left();
81
int top = option.rect.top();
82
int width = option.rect.width();
84
bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight);
86
QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected)) ?
87
option.palette.color(QPalette::HighlightedText) : option.palette.color(QPalette::Text);
89
// Pixmap that the text/icon goes in
90
QPixmap pixmap(option.rect.size());
91
pixmap.fill(Qt::transparent);
93
p.translate(-option.rect.topLeft());
96
leftToRight ? left + m_spacing : left + width - m_spacing - m_iconSize,
103
int state = index.data(PackageModel::StatusRole).toInt();
105
if (state & QApt::Package::IsPinned) {
106
p.drawPixmap(left + m_iconSize - m_lockedEmblem.width()/2,
107
top + option.rect.height() - 1.5*m_lockedEmblem.height(),
109
} else if (index.data(PackageModel::SupportRole).toBool()) {
110
p.drawPixmap(left + m_iconSize - m_lockedEmblem.width()/2,
111
top + option.rect.height() - 1.5*m_lockedEmblem.height(),
116
QStyleOptionViewItem name_item(option);
117
QStyleOptionViewItem description_item(option);
119
description_item.font.setPointSize(name_item.font.pointSize() - 1);
121
int textInner = 2 * m_spacing + m_iconSize;
122
const int itemHeight = calcItemHeight(option);
124
p.setPen(foregroundColor);
125
p.drawText(left + (leftToRight ? textInner : 0),
129
Qt::AlignBottom | Qt::AlignLeft,
130
index.data(PackageModel::NameRole).toString());
132
p.drawText(left + (leftToRight ? textInner : 0) + 10,
133
top + itemHeight / 2,
136
Qt::AlignTop | Qt::AlignLeft,
137
index.data(PackageModel::DescriptionRole).toString());
139
QLinearGradient gradient;
141
// Gradient part of the background - fading of the text at the end
143
gradient = QLinearGradient(left + width - m_spacing - 16 /*fade length*/, 0,
144
left + width - m_spacing, 0);
145
gradient.setColorAt(0, Qt::white);
146
gradient.setColorAt(1, Qt::transparent);
148
gradient = QLinearGradient(left + m_spacing, 0,
149
left + m_spacing + 16, 0);
150
gradient.setColorAt(0, Qt::transparent);
151
gradient.setColorAt(1, Qt::white);
154
QRect paintRect = option.rect;
155
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
156
p.fillRect(paintRect, gradient);
160
painter->drawPixmap(option.rect.topLeft(), pixmap);
163
void PackageDelegate::paintText(QPainter *painter, const QStyleOptionViewItem &option , const QModelIndex &index) const
169
switch (index.column()) {
171
state = index.data(PackageModel::StatusRole).toInt();
173
if (state & QApt::Package::NowBroken) {
174
text = MuonStrings::global()->packageStateName(QApt::Package::NowBroken);
175
pen.setColor(Qt::red);
179
if (state & QApt::Package::Installed) {
180
text = MuonStrings::global()->packageStateName(QApt::Package::Installed);
181
pen.setColor(Qt::darkGreen);
183
if (state & QApt::Package::Upgradeable) {
184
text = MuonStrings::global()->packageStateName(QApt::Package::Upgradeable);
185
pen.setColor(Qt::darkYellow);
188
text = MuonStrings::global()->packageStateName(QApt::Package::NotInstalled);
189
pen.setColor(Qt::blue);
193
state = index.data(PackageModel::ActionRole).toInt();
195
if (state & QApt::Package::ToKeep) {
196
text = MuonStrings::global()->packageStateName(QApt::Package::ToKeep);
197
pen.setColor(Qt::blue);
198
// No other "To" flag will be set if we are keeping
202
if (state & QApt::Package::ToInstall) {
203
text = MuonStrings::global()->packageStateName(QApt::Package::ToInstall);
204
pen.setColor(Qt::darkGreen);
207
if (state & QApt::Package::ToUpgrade) {
208
text = MuonStrings::global()->packageStateName(QApt::Package::ToUpgrade);
209
pen.setColor(Qt::darkYellow);
213
if (state & QApt::Package::ToRemove) {
214
text = MuonStrings::global()->packageStateName(QApt::Package::ToRemove);
215
pen.setColor(Qt::red);
218
if (state & QApt::Package::ToPurge) {
219
text = MuonStrings::global()->packageStateName(QApt::Package::ToPurge);
220
pen.setColor(Qt::red);
224
if (state & QApt::Package::ToReInstall) {
225
text = MuonStrings::global()->packageStateName(QApt::Package::ToReInstall);
226
pen.setColor(Qt::darkGreen);
230
if (state & QApt::Package::ToDowngrade) {
231
text = MuonStrings::global()->packageStateName(QApt::Package::ToDowngrade);
232
pen.setColor(Qt::darkYellow);
238
QFont font = option.font;
239
QFontMetrics fontMetrics(font);
241
int x = option.rect.x() + m_spacing;
242
int y = option.rect.y() + calcItemHeight(option) / 4 + fontMetrics.height() -1;
243
int width = option.rect.width();
245
painter->setPen(pen);
246
painter->drawText(x, y, fontMetrics.elidedText(text, option.textElideMode, width));
249
QSize PackageDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
254
QFontMetrics metric = QFontMetrics(option.font);
256
switch (index.column()) {
258
size.setWidth(metric.width(index.data(PackageModel::DescriptionRole).toString()));
261
size.setWidth(metric.width(index.data(PackageModel::StatusRole).toString()));
264
size.setWidth(metric.width(index.data(PackageModel::ActionRole).toString()));
269
size.setHeight(option.fontMetrics.height() * 2 + m_spacing);
274
int PackageDelegate::calcItemHeight(const QStyleOptionViewItem &option) const
276
// Painting main column
277
QStyleOptionViewItem name_item(option);
278
QStyleOptionViewItem description_item(option);
280
description_item.font.setPointSize(name_item.font.pointSize() - 1);
282
int textHeight = QFontInfo(name_item.font).pixelSize() + QFontInfo(description_item.font).pixelSize();
283
return qMax(textHeight, m_iconSize) + 2 * m_spacing;
286
#include "PackageDelegate.moc"