~ubuntu-branches/ubuntu/vivid/muon/vivid-proposed

« back to all changes in this revision

Viewing changes to installer/ResourceView/ResourceDelegate.cpp

  • Committer: Package Import Robot
  • Author(s): Scarlett Clark
  • Date: 2015-03-24 07:36:31 UTC
  • mto: This revision was merged to the branch mainline in revision 86.
  • Revision ID: package-import@ubuntu.com-20150324073631-7nmay5episnfkdlt
Tags: upstream-5.2.2
ImportĀ upstreamĀ versionĀ 5.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
3
 
 *   Copyright (C) 2008 Daniel Nicoletti <dantti85-pk@yahoo.com.br>
4
 
 *   Copyright (C) 2010-2012 Jonathan Thomas <echidnaman@kubuntu.org>
5
 
 *
6
 
 *   This program is free software; you can redistribute it and/or modify
7
 
 *   it under the terms of the GNU Library/Lesser General Public License
8
 
 *   version 2, or (at your option) any later version, as published by the
9
 
 *   Free Software Foundation
10
 
 *
11
 
 *   This program is distributed in the hope that it will be useful,
12
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *   GNU General Public License for more details
15
 
 *
16
 
 *   You should have received a copy of the GNU Library/Lesser General Public
17
 
 *   License along with this program; if not, write to the
18
 
 *   Free Software Foundation, Inc.,
19
 
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 
 */
21
 
 
22
 
#include "ResourceDelegate.h"
23
 
 
24
 
// Qt includes
25
 
#include <QApplication>
26
 
#include <QAbstractItemView>
27
 
#include <QPainter>
28
 
#include <QtWidgets/QPushButton>
29
 
#include <QtWidgets/QTreeView>
30
 
 
31
 
// KDE includes
32
 
#include <KApplication>
33
 
#include <KIconLoader>
34
 
#include <KGlobal>
35
 
#include <KLocalizedString>
36
 
#include <KStandardDirs>
37
 
#include <Nepomuk/KRatingPainter>
38
 
#include <KDebug>
39
 
 
40
 
// Libmuon includes
41
 
#include <Transaction/TransactionModel.h>
42
 
#include <resources/AbstractResource.h>
43
 
#include <resources/ResourcesModel.h>
44
 
 
45
 
// Own includes
46
 
#include "ResourceExtender.h"
47
 
 
48
 
#define FAV_ICON_SIZE 24
49
 
#define EMBLEM_ICON_SIZE 8
50
 
#define UNIVERSAL_PADDING 4
51
 
#define MAIN_ICON_SIZE 32
52
 
 
53
 
ResourceDelegate::ResourceDelegate(QAbstractItemView *parent)
54
 
  : KExtendableItemDelegate(parent),
55
 
    m_extender(0),
56
 
    m_showInfoButton(true)
57
 
{
58
 
    // To get sizing.
59
 
    QPushButton button, button2;
60
 
    QIcon icon(QIcon::fromTheme("edit-delete"));
61
 
 
62
 
    button.setText(i18n("Install"));
63
 
    button.setIcon(icon);
64
 
    button2.setText(i18n("Remove"));
65
 
    button2.setIcon(icon);
66
 
    m_buttonSize = button.sizeHint();
67
 
    int width = qMax(button.sizeHint().width(), button2.sizeHint().width());
68
 
    width = qMax(width, button2.sizeHint().width());
69
 
    m_buttonSize.setWidth(width);
70
 
 
71
 
    m_emblem = QIcon::fromTheme("dialog-ok").pixmap(QSize(16, 16));
72
 
    m_ratingPainter = new KRatingPainter;
73
 
}
74
 
 
75
 
void ResourceDelegate::paint(QPainter *painter,
76
 
                        const QStyleOptionViewItem &option,
77
 
                        const QModelIndex &index) const
78
 
{
79
 
    if (!index.isValid()) {
80
 
        return;
81
 
    }
82
 
    bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight);
83
 
 
84
 
    QStyleOptionViewItemV4 opt(option);
85
 
    QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
86
 
    painter->save();
87
 
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
88
 
    painter->restore();
89
 
 
90
 
    // paint the extender
91
 
    KExtendableItemDelegate::paint(painter, opt, index);
92
 
 
93
 
    int leftCount;
94
 
    if (leftToRight) {
95
 
        opt.rect.setLeft(option.rect.left() + UNIVERSAL_PADDING);
96
 
        leftCount = opt.rect.left() + UNIVERSAL_PADDING;
97
 
    } else {
98
 
        opt.rect.setRight(option.rect.right() - UNIVERSAL_PADDING);
99
 
        leftCount = opt.rect.width() - (UNIVERSAL_PADDING + MAIN_ICON_SIZE);
100
 
    }
101
 
 
102
 
    int left = opt.rect.left();
103
 
    int top = opt.rect.top();
104
 
    int width = opt.rect.width();
105
 
 
106
 
    QRect rect = opt.rect;
107
 
 
108
 
    if (leftToRight) {
109
 
        rect.setLeft(left + width - (m_buttonSize.width() + UNIVERSAL_PADDING));
110
 
        width -= m_buttonSize.width() + UNIVERSAL_PADDING;
111
 
    } else {
112
 
        rect.setLeft(left + UNIVERSAL_PADDING);
113
 
        left += m_buttonSize.width() + UNIVERSAL_PADDING;
114
 
    }
115
 
    // Calculate the top of the ratings widget which is the item height - the widget height size divided by 2
116
 
    // this give us a little value which is the top and bottom margin
117
 
    rect.setTop(rect.top() + ((calcItemHeight(option) - m_buttonSize.height()) / 2));
118
 
    rect.setSize(m_buttonSize); // the width and height sizes of the button
119
 
 
120
 
    bool transactionActive = index.data(ResourcesModel::ActiveRole).toBool();
121
 
 
122
 
    if (!transactionActive) {
123
 
        int rating = index.data(ResourcesModel::RatingRole).toInt();
124
 
        if (rating != -1) {
125
 
            m_ratingPainter->paint(painter, rect, rating);
126
 
        }
127
 
    } else {
128
 
        TransactionModel *transModel = TransactionModel::global();
129
 
        AbstractResource* res = qobject_cast<AbstractResource*>( index.data(ResourcesModel::ApplicationRole).value<QObject*>());
130
 
        Transaction *trans = transModel->transactionFromResource(res);
131
 
        QModelIndex transIndex = transModel->indexOf(trans);
132
 
        QStyleOptionProgressBar progressBarOption;
133
 
        progressBarOption.rect = rect;
134
 
        progressBarOption.minimum = 0;
135
 
        progressBarOption.maximum = 100;
136
 
        progressBarOption.progress = transIndex.data(TransactionModel::ProgressRole).toInt();
137
 
        progressBarOption.text = transIndex.data(TransactionModel::StatusTextRole).toString();
138
 
        progressBarOption.textVisible = true;
139
 
        KApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
140
 
    }
141
 
 
142
 
 
143
 
    // selects the mode to paint the icon based on the info field
144
 
    QIcon::Mode iconMode = QIcon::Normal;
145
 
    if (option.state & QStyle::State_MouseOver) {
146
 
        iconMode = QIcon::Active;
147
 
    }
148
 
 
149
 
    QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))?
150
 
    option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text);
151
 
 
152
 
    // Painting main column
153
 
    QStyleOptionViewItem local_option_title(option);
154
 
    QStyleOptionViewItem local_option_normal(option);
155
 
 
156
 
    local_option_normal.font.setPointSize(local_option_normal.font.pointSize() - 1);
157
 
 
158
 
    QPixmap pixmap(option.rect.size());
159
 
    pixmap.fill(Qt::transparent);
160
 
    QPainter p(&pixmap);
161
 
    p.translate(-option.rect.topLeft());
162
 
 
163
 
    // Main icon
164
 
    QIcon icon = QIcon::fromTheme(index.data(ResourcesModel::IconRole).toString());
165
 
 
166
 
    int iconSize = calcItemHeight(option) - 2 * UNIVERSAL_PADDING;
167
 
    icon.paint(&p,
168
 
               leftCount,
169
 
               top + UNIVERSAL_PADDING,
170
 
               iconSize,
171
 
               iconSize,
172
 
               Qt::AlignCenter,
173
 
               iconMode);
174
 
 
175
 
    if (index.data(ResourcesModel::InstalledRole).toBool()) {
176
 
        p.drawPixmap(leftCount, top + rect.height() - m_emblem.height()/2, m_emblem);
177
 
    }
178
 
 
179
 
    int textWidth;
180
 
    if (leftToRight) {
181
 
        // add the main icon
182
 
        leftCount += iconSize + UNIVERSAL_PADDING;
183
 
        textWidth = width - (leftCount - left);
184
 
    } else {
185
 
        leftCount -= UNIVERSAL_PADDING;
186
 
        textWidth = leftCount - left;
187
 
        leftCount = left;
188
 
    }
189
 
 
190
 
    // Text
191
 
    const int itemHeight = calcItemHeight(option);
192
 
 
193
 
    p.setPen(foregroundColor);
194
 
    // draw the top line
195
 
    int topTextHeight = QFontInfo(local_option_title.font).pixelSize();
196
 
    p.setFont(local_option_title.font);
197
 
    p.drawText(leftCount,
198
 
               top,
199
 
               textWidth,
200
 
               topTextHeight + UNIVERSAL_PADDING,
201
 
               Qt::AlignVCenter | Qt::AlignLeft,
202
 
               index.data(ResourcesModel::NameRole).toString());
203
 
 
204
 
    // draw the bottom line
205
 
    iconSize = topTextHeight + UNIVERSAL_PADDING;
206
 
 
207
 
    // store the original opacity
208
 
    qreal opa = p.opacity();
209
 
    if (!(option.state & QStyle::State_MouseOver) && !(option.state & QStyle::State_Selected)) {
210
 
        p.setOpacity(opa / 2.5);
211
 
    }
212
 
 
213
 
    p.setFont(local_option_normal.font);
214
 
    p.drawText(leftToRight ? leftCount + 0.5*iconSize: left - UNIVERSAL_PADDING,
215
 
               top + itemHeight / 2,
216
 
               textWidth - iconSize,
217
 
               QFontInfo(local_option_normal.font).pixelSize() + UNIVERSAL_PADDING,
218
 
               Qt::AlignTop | Qt::AlignLeft,
219
 
               index.data(ResourcesModel::CommentRole).toString());
220
 
    p.setOpacity(opa);
221
 
 
222
 
    painter->drawPixmap(option.rect.topLeft(), pixmap);
223
 
}
224
 
 
225
 
int ResourceDelegate::calcItemHeight(const QStyleOptionViewItem &option) const
226
 
{
227
 
    // Painting main column
228
 
    QStyleOptionViewItem local_option_title(option);
229
 
    QStyleOptionViewItem local_option_normal(option);
230
 
 
231
 
    local_option_normal.font.setPointSize(local_option_normal.font.pointSize() - 1);
232
 
 
233
 
    int textHeight = QFontInfo(local_option_title.font).pixelSize() + QFontInfo(local_option_normal.font).pixelSize();
234
 
    return textHeight + 3 * UNIVERSAL_PADDING;
235
 
}
236
 
 
237
 
bool ResourceDelegate::editorEvent(QEvent *event,
238
 
                                    QAbstractItemModel *model,
239
 
                                    const QStyleOptionViewItem &option,
240
 
                                    const QModelIndex &index)
241
 
{
242
 
    Q_UNUSED(option)
243
 
    if (event->type() == QEvent::MouseButtonRelease) {
244
 
       itemActivated(index);
245
 
    }
246
 
 
247
 
    return KExtendableItemDelegate::editorEvent(event, model, option, index);
248
 
}
249
 
 
250
 
QSize ResourceDelegate::sizeHint(const QStyleOptionViewItem &option,
251
 
        const QModelIndex &index ) const
252
 
{
253
 
    int width = (index.column() == 0) ? index.data(Qt::SizeHintRole).toSize().width() : FAV_ICON_SIZE + 2 * UNIVERSAL_PADDING;
254
 
    QSize ret(KExtendableItemDelegate::sizeHint(option, index));
255
 
    // remove the default size of the index
256
 
    ret -= QStyledItemDelegate::sizeHint(option, index);
257
 
 
258
 
    ret.rheight() += calcItemHeight(option);
259
 
    ret.rwidth()  += width;
260
 
 
261
 
    return ret;
262
 
}
263
 
 
264
 
void ResourceDelegate::itemActivated(QModelIndex index)
265
 
{
266
 
    if ((index == m_oldIndex && isExtended(index))) {
267
 
        return;
268
 
    }
269
 
 
270
 
    if (isExtended(m_oldIndex)) {
271
 
        disconnect(m_extender, SIGNAL(infoButtonClicked(AbstractResource*)),
272
 
                   this, SIGNAL(infoButtonClicked(AbstractResource*)));
273
 
        contractItem(m_oldIndex);
274
 
 
275
 
        m_extender->deleteLater();
276
 
        m_extender = 0;
277
 
    }
278
 
 
279
 
    QVariant appVarient = static_cast<const QAbstractItemModel*>(index.model())->data(index, ResourcesModel::ApplicationRole);
280
 
    AbstractResource *app = qobject_cast<AbstractResource*>(appVarient.value<QObject*>());
281
 
 
282
 
    QTreeView *view = static_cast<QTreeView*>(parent());
283
 
    m_extender = new ResourceExtender(view, app);
284
 
    m_extender->setShowInfoButton(m_showInfoButton);
285
 
    connect(m_extender, SIGNAL(infoButtonClicked(AbstractResource*)),
286
 
            this, SIGNAL(infoButtonClicked(AbstractResource*)));
287
 
 
288
 
    extendItem(m_extender, index);
289
 
    m_oldIndex = index;
290
 
}
291
 
 
292
 
void ResourceDelegate::invalidate()
293
 
{
294
 
    // If only contractAll was a Q_SLOT...
295
 
    contractAll();
296
 
}
297
 
 
298
 
void ResourceDelegate::setShowInfoButton(bool show)
299
 
{
300
 
    m_showInfoButton = show;
301
 
}