~blue-shell/blue-shell/muon-firefox-apps

« back to all changes in this revision

Viewing changes to muon/PackageModel/PackageDelegate.cpp

  • Committer: Jonathan Thomas
  • Date: 2011-11-06 02:19:48 UTC
  • Revision ID: git-v1:498a98a05157c758a93f50092851c3ede98812f8
These classes are all now only used by MPM due to the new Updater GUI,
so move them out of libmuon and back to the muon folder.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright © 2010 Jonathan Thomas <echidnaman@kubuntu.org>             *
 
3
 *                                                                         *
 
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.                    *
 
11
 *                                                                         *
 
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.                          *
 
16
 *                                                                         *
 
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
 ***************************************************************************/
 
20
 
 
21
#include "PackageDelegate.h"
 
22
 
 
23
// Qt
 
24
#include <QApplication>
 
25
#include <QtGui/QPainter>
 
26
 
 
27
// KDE
 
28
#include <KIcon>
 
29
#include <KIconLoader>
 
30
 
 
31
// Own
 
32
#include "../libmuon/MuonStrings.h"
 
33
#include "PackageModel.h"
 
34
 
 
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)))
 
40
{
 
41
    m_spacing  = 4;
 
42
 
 
43
    m_iconSize = KIconLoader::SizeSmallMedium;
 
44
}
 
45
 
 
46
PackageDelegate::~PackageDelegate()
 
47
{
 
48
}
 
49
 
 
50
void PackageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 
51
{
 
52
    if (!index.isValid()) {
 
53
        return;
 
54
    }
 
55
 
 
56
    paintBackground(painter, option);
 
57
 
 
58
    switch (index.column()) {
 
59
    case 0:
 
60
        paintPackageName(painter, option, index);
 
61
        break;
 
62
    case 1: // Status
 
63
    case 2: // Action
 
64
        paintText(painter, option, index);
 
65
        break;
 
66
    default:
 
67
        break;
 
68
    }
 
69
}
 
70
 
 
71
void PackageDelegate::paintBackground(QPainter *painter, const QStyleOptionViewItem &option) const
 
72
{
 
73
    QStyleOptionViewItemV4 opt(option);
 
74
    QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
 
75
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
 
76
}
 
77
 
 
78
void PackageDelegate::paintPackageName(QPainter *painter, const QStyleOptionViewItem &option , const QModelIndex &index) const
 
79
{
 
80
    int left = option.rect.left();
 
81
    int top = option.rect.top();
 
82
    int width = option.rect.width();
 
83
 
 
84
    bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight);
 
85
 
 
86
    QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected)) ?
 
87
                             option.palette.color(QPalette::HighlightedText) : option.palette.color(QPalette::Text);
 
88
 
 
89
    // Pixmap that the text/icon goes in
 
90
    QPixmap pixmap(option.rect.size());
 
91
    pixmap.fill(Qt::transparent);
 
92
    QPainter p(&pixmap);
 
93
    p.translate(-option.rect.topLeft());
 
94
 
 
95
    m_icon.paint(&p,
 
96
                 leftToRight ? left + m_spacing : left + width - m_spacing - m_iconSize,
 
97
                 top + m_spacing,
 
98
                 m_iconSize,
 
99
                 m_iconSize,
 
100
                 Qt::AlignCenter,
 
101
                 QIcon::Normal);
 
102
 
 
103
    int state = index.data(PackageModel::StatusRole).toInt();
 
104
 
 
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(),
 
108
                     m_lockedEmblem);
 
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(),
 
112
                     m_supportedEmblem);
 
113
    }
 
114
 
 
115
    // Text
 
116
    QStyleOptionViewItem name_item(option);
 
117
    QStyleOptionViewItem description_item(option);
 
118
 
 
119
    description_item.font.setPointSize(name_item.font.pointSize() - 1);
 
120
 
 
121
    int textInner = 2 * m_spacing + m_iconSize;
 
122
    const int itemHeight = calcItemHeight(option);
 
123
 
 
124
    p.setPen(foregroundColor);
 
125
    p.drawText(left + (leftToRight ? textInner : 0),
 
126
               top + 1,
 
127
               width - textInner,
 
128
               itemHeight / 2,
 
129
               Qt::AlignBottom | Qt::AlignLeft,
 
130
               index.data(PackageModel::NameRole).toString());
 
131
 
 
132
    p.drawText(left + (leftToRight ? textInner : 0) + 10,
 
133
               top + itemHeight / 2,
 
134
               width - textInner,
 
135
               itemHeight / 2,
 
136
               Qt::AlignTop | Qt::AlignLeft,
 
137
               index.data(PackageModel::DescriptionRole).toString());
 
138
 
 
139
    QLinearGradient gradient;
 
140
 
 
141
    // Gradient part of the background - fading of the text at the end
 
142
    if (leftToRight) {
 
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);
 
147
    } else {
 
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);
 
152
    }
 
153
 
 
154
    QRect paintRect = option.rect;
 
155
    p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
 
156
    p.fillRect(paintRect, gradient);
 
157
 
 
158
    p.end();
 
159
 
 
160
    painter->drawPixmap(option.rect.topLeft(), pixmap);
 
161
}
 
162
 
 
163
void PackageDelegate::paintText(QPainter *painter, const QStyleOptionViewItem &option , const QModelIndex &index) const
 
164
{
 
165
    int state;
 
166
    QString text;
 
167
    QPen pen;
 
168
 
 
169
    switch (index.column()) {
 
170
    case 1:
 
171
        state = index.data(PackageModel::StatusRole).toInt();
 
172
 
 
173
        if (state & QApt::Package::NowBroken) {
 
174
            text = MuonStrings::global()->packageStateName(QApt::Package::NowBroken);
 
175
            pen.setColor(Qt::red);
 
176
            break;
 
177
        }
 
178
 
 
179
        if (state & QApt::Package::Installed) {
 
180
            text = MuonStrings::global()->packageStateName(QApt::Package::Installed);
 
181
            pen.setColor(Qt::darkGreen);
 
182
 
 
183
            if (state & QApt::Package::Upgradeable) {
 
184
                text = MuonStrings::global()->packageStateName(QApt::Package::Upgradeable);
 
185
                pen.setColor(Qt::darkYellow);
 
186
            }
 
187
        } else {
 
188
            text = MuonStrings::global()->packageStateName(QApt::Package::NotInstalled);
 
189
            pen.setColor(Qt::blue);
 
190
        }
 
191
        break;
 
192
    case 2:
 
193
        state = index.data(PackageModel::ActionRole).toInt();
 
194
 
 
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
 
199
            break;
 
200
        }
 
201
 
 
202
        if (state & QApt::Package::ToInstall) {
 
203
            text = MuonStrings::global()->packageStateName(QApt::Package::ToInstall);
 
204
            pen.setColor(Qt::darkGreen);
 
205
        }
 
206
 
 
207
        if (state & QApt::Package::ToUpgrade) {
 
208
            text = MuonStrings::global()->packageStateName(QApt::Package::ToUpgrade);
 
209
            pen.setColor(Qt::darkYellow);
 
210
            break;
 
211
        }
 
212
 
 
213
        if (state & QApt::Package::ToRemove) {
 
214
            text = MuonStrings::global()->packageStateName(QApt::Package::ToRemove);
 
215
            pen.setColor(Qt::red);
 
216
        }
 
217
 
 
218
        if (state & QApt::Package::ToPurge) {
 
219
            text = MuonStrings::global()->packageStateName(QApt::Package::ToPurge);
 
220
            pen.setColor(Qt::red);
 
221
            break;
 
222
        }
 
223
 
 
224
        if (state & QApt::Package::ToReInstall) {
 
225
            text = MuonStrings::global()->packageStateName(QApt::Package::ToReInstall);
 
226
            pen.setColor(Qt::darkGreen);
 
227
            break;
 
228
        }
 
229
 
 
230
        if (state & QApt::Package::ToDowngrade) {
 
231
            text = MuonStrings::global()->packageStateName(QApt::Package::ToDowngrade);
 
232
            pen.setColor(Qt::darkYellow);
 
233
            break;
 
234
        }
 
235
        break;
 
236
    }
 
237
 
 
238
    QFont font = option.font;
 
239
    QFontMetrics fontMetrics(font);
 
240
 
 
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();
 
244
 
 
245
    painter->setPen(pen);
 
246
    painter->drawText(x, y, fontMetrics.elidedText(text, option.textElideMode, width));
 
247
}
 
248
 
 
249
QSize PackageDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
 
250
{
 
251
    Q_UNUSED(index);
 
252
 
 
253
    QSize size;
 
254
    QFontMetrics metric = QFontMetrics(option.font);
 
255
 
 
256
    switch (index.column()) {
 
257
    case 0:
 
258
        size.setWidth(metric.width(index.data(PackageModel::DescriptionRole).toString()));
 
259
        break;
 
260
    case 1:
 
261
        size.setWidth(metric.width(index.data(PackageModel::StatusRole).toString()));
 
262
        break;
 
263
    case 2:
 
264
        size.setWidth(metric.width(index.data(PackageModel::ActionRole).toString()));
 
265
        break;
 
266
    default:
 
267
        break;
 
268
    }
 
269
    size.setHeight(option.fontMetrics.height() * 2 + m_spacing);
 
270
 
 
271
    return size;
 
272
}
 
273
 
 
274
int PackageDelegate::calcItemHeight(const QStyleOptionViewItem &option) const
 
275
{
 
276
    // Painting main column
 
277
    QStyleOptionViewItem name_item(option);
 
278
    QStyleOptionViewItem description_item(option);
 
279
 
 
280
    description_item.font.setPointSize(name_item.font.pointSize() - 1);
 
281
 
 
282
    int textHeight = QFontInfo(name_item.font).pixelSize() + QFontInfo(description_item.font).pixelSize();
 
283
    return qMax(textHeight, m_iconSize) + 2 * m_spacing;
 
284
}
 
285
 
 
286
#include "PackageDelegate.moc"