~dantti/apper/master

« back to all changes in this revision

Viewing changes to libshared/KpkRequirements.cpp

  • Committer: Daniel Nicoletti
  • Date: 2011-07-27 21:02:31 UTC
  • Revision ID: git-v1:2fb51b7787e2e9d70d2085cc8181a12f4277b047
The idea of libshared, was for it to be static, as
this doesn't seem too simple as putting STATIC word
I'm renaming it to libapper so we don't have such a geniric
lib installed (as pointed by Kevin Kofler

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2008-2010 by Daniel Nicoletti                           *
3
 
 *   dantti85-pk@yahoo.com.br                                              *
4
 
 *                                                                         *
5
 
 *   This program is free software; you can redistribute it and/or modify  *
6
 
 *   it under the terms of the GNU General Public License as published by  *
7
 
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 
 *   (at your option) any later version.                                   *
9
 
 *                                                                         *
10
 
 *   This program is distributed in the hope that it will be useful,       *
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 
 *   GNU General Public License for more details.                          *
14
 
 *                                                                         *
15
 
 *   You should have received a copy of the GNU General Public License     *
16
 
 *   along with this program; see the file COPYING. If not, write to       *
17
 
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
18
 
 *   Boston, MA 02110-1301, USA.                                           *
19
 
 ***************************************************************************/
20
 
// The delegate was written by
21
 
// Copyright (C) 2007 Kevin Ottens <ervin@kde.org>
22
 
// Copyright (C) 2008 Rafael Fernández López <ereslibre@kde.org>
23
 
// Under LGPL
24
 
/*
25
 
    This library is free software; you can redistribute it and/or
26
 
    modify it under the terms of the GNU Library General Public
27
 
    License version 2 as published by the Free Software Foundation.
28
 
 
29
 
    This library is distributed in the hope that it will be useful,
30
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32
 
    Library General Public License for more details.
33
 
 
34
 
    You should have received a copy of the GNU Library General Public License
35
 
    along with this library; see the file COPYING.LIB.  If not, write to
36
 
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
37
 
    Boston, MA 02110-1301, USA.
38
 
 
39
 
*/
40
 
 
41
 
#include "KpkRequirements.h"
42
 
#include "ui_KpkRequirements.h"
43
 
 
44
 
#include "KpkIcons.h"
45
 
#include "KpkSimulateModel.h"
46
 
 
47
 
#include <KDebug>
48
 
 
49
 
#include <QPalette>
50
 
#include <QTimeLine>
51
 
#include <QStandardItemModel>
52
 
 
53
 
Q_DECLARE_METATYPE(Package::Info)
54
 
 
55
 
#include <QPainter>
56
 
#define LATERAL_MARGIN 4
57
 
 
58
 
class KActionsViewDelegate : public QAbstractItemDelegate
59
 
{
60
 
public:
61
 
    KActionsViewDelegate(QObject *parent = 0);
62
 
    virtual ~KActionsViewDelegate();
63
 
    virtual QSize sizeHint(const QStyleOptionViewItem &option,
64
 
                           const QModelIndex &index) const;
65
 
    virtual void paint(QPainter *painter,
66
 
                       const QStyleOptionViewItem &option,
67
 
                       const QModelIndex &index) const;
68
 
 
69
 
    int iconSize() const;
70
 
    void setIconSize(int newSize);
71
 
 
72
 
    void setShowHoverIndication(bool show);
73
 
 
74
 
    void addFadeAnimation(const QModelIndex &index, QTimeLine *timeLine);
75
 
    void removeFadeAnimation(const QModelIndex &index);
76
 
    QModelIndex indexForFadeAnimation(QTimeLine *timeLine) const;
77
 
    QTimeLine *fadeAnimationForIndex(const QModelIndex &index) const;
78
 
 
79
 
    qreal contentsOpacity(const QModelIndex &index) const;
80
 
 
81
 
private:
82
 
    int m_iconSize;
83
 
 
84
 
    QList<QPersistentModelIndex> m_appearingItems;
85
 
    int m_appearingIconSize;
86
 
    qreal m_appearingOpacity;
87
 
 
88
 
    QList<QPersistentModelIndex> m_disappearingItems;
89
 
    int m_disappearingIconSize;
90
 
    qreal m_disappearingOpacity;
91
 
 
92
 
    bool m_showHoverIndication;
93
 
 
94
 
    QMap<QPersistentModelIndex, QTimeLine*> m_timeLineMap;
95
 
    QMap<QTimeLine*, QPersistentModelIndex> m_timeLineInverseMap;
96
 
};
97
 
 
98
 
KActionsViewDelegate::KActionsViewDelegate(QObject *parent) :
99
 
    QAbstractItemDelegate(parent),
100
 
    m_iconSize(48),
101
 
    m_appearingIconSize(0),
102
 
    m_appearingOpacity(0.0),
103
 
    m_disappearingIconSize(0),
104
 
    m_disappearingOpacity(0.0),
105
 
    m_showHoverIndication(true)
106
 
{
107
 
}
108
 
 
109
 
KActionsViewDelegate::~KActionsViewDelegate()
110
 
{
111
 
}
112
 
 
113
 
QSize KActionsViewDelegate::sizeHint(const QStyleOptionViewItem &option,
114
 
                                        const QModelIndex &index) const
115
 
{
116
 
    int iconSize = m_iconSize;
117
 
    if (m_appearingItems.contains(index)) {
118
 
        iconSize = m_appearingIconSize;
119
 
    } else if (m_disappearingItems.contains(index)) {
120
 
        iconSize = m_disappearingIconSize;
121
 
    }
122
 
 
123
 
    return QSize(option.rect.width(), option.fontMetrics.height() / 2 + qMax(iconSize, option.fontMetrics.height()));
124
 
}
125
 
 
126
 
void KActionsViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
127
 
{
128
 
    painter->save();
129
 
 
130
 
    if (m_appearingItems.contains(index)) {
131
 
        painter->setOpacity(m_appearingOpacity);
132
 
    } else if (m_disappearingItems.contains(index)) {
133
 
        painter->setOpacity(m_disappearingOpacity);
134
 
    }
135
 
 
136
 
    QStyleOptionViewItemV4 opt = option;
137
 
    if (!m_showHoverIndication) {
138
 
        opt.state &= ~QStyle::State_MouseOver;
139
 
    }
140
 
    QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter);
141
 
 
142
 
    bool isLTR = option.direction == Qt::LeftToRight;
143
 
 
144
 
    QIcon icon = index.model()->data(index, Qt::DecorationRole).value<QIcon>();
145
 
    QPixmap pm = icon.pixmap(m_iconSize, m_iconSize);
146
 
    QPoint point(isLTR ? option.rect.left() + LATERAL_MARGIN
147
 
                       : option.rect.right() - LATERAL_MARGIN - m_iconSize, option.rect.top() + (option.rect.height() - m_iconSize) / 2);
148
 
    painter->drawPixmap(point, pm);
149
 
 
150
 
    if (option.state & QStyle::State_Selected) {
151
 
        painter->setPen(option.palette.highlightedText().color());
152
 
    }
153
 
 
154
 
    QRect rectText;
155
 
    rectText = QRect(isLTR ? m_iconSize + LATERAL_MARGIN * 2 + option.rect.left()
156
 
                           : 0, option.rect.top(), option.rect.width() - m_iconSize - LATERAL_MARGIN * 2, option.rect.height());
157
 
    painter->drawText(rectText, Qt::AlignLeft | Qt::AlignVCenter, option.fontMetrics.elidedText(index.model()->data(index).toString(), Qt::ElideRight, rectText.width()));
158
 
 
159
 
    painter->restore();
160
 
}
161
 
 
162
 
int KActionsViewDelegate::iconSize() const
163
 
{
164
 
    return m_iconSize;
165
 
}
166
 
 
167
 
void KActionsViewDelegate::setIconSize(int newSize)
168
 
{
169
 
    m_iconSize = newSize;
170
 
}
171
 
 
172
 
void KActionsViewDelegate::setShowHoverIndication(bool show)
173
 
{
174
 
    m_showHoverIndication = show;
175
 
}
176
 
 
177
 
void KActionsViewDelegate::addFadeAnimation(const QModelIndex &index, QTimeLine *timeLine)
178
 
{
179
 
    m_timeLineMap.insert(index, timeLine);
180
 
    m_timeLineInverseMap.insert(timeLine, index);
181
 
}
182
 
 
183
 
void KActionsViewDelegate::removeFadeAnimation(const QModelIndex &index)
184
 
{
185
 
    QTimeLine *timeLine = m_timeLineMap.value(index, 0);
186
 
    m_timeLineMap.remove(index);
187
 
    m_timeLineInverseMap.remove(timeLine);
188
 
}
189
 
 
190
 
QModelIndex KActionsViewDelegate::indexForFadeAnimation(QTimeLine *timeLine) const
191
 
{
192
 
    return m_timeLineInverseMap.value(timeLine, QModelIndex());
193
 
}
194
 
 
195
 
QTimeLine *KActionsViewDelegate::fadeAnimationForIndex(const QModelIndex &index) const
196
 
{
197
 
    return m_timeLineMap.value(index, 0);
198
 
}
199
 
 
200
 
qreal KActionsViewDelegate::contentsOpacity(const QModelIndex &index) const
201
 
{
202
 
    QTimeLine *timeLine = fadeAnimationForIndex(index);
203
 
    if (timeLine) {
204
 
        return timeLine->currentValue();
205
 
    }
206
 
    return 0;
207
 
}
208
 
 
209
 
 
210
 
class KpkRequirementsPrivate
211
 
{
212
 
public:
213
 
    QStandardItemModel *actionsModel;
214
 
    bool hideAutoConfirm;
215
 
 
216
 
    Ui::KpkRequirements ui;
217
 
};
218
 
 
219
 
KpkRequirements::KpkRequirements(KpkSimulateModel *model, QWidget *parent)
220
 
 : KDialog(parent),
221
 
   d(new KpkRequirementsPrivate)
222
 
{
223
 
    d->ui.setupUi(mainWidget());
224
 
 
225
 
    d->hideAutoConfirm = false;
226
 
 
227
 
    setCaption(i18n("Additional changes"));
228
 
    setButtons(KDialog::Ok | KDialog::Cancel);
229
 
    setButtonText(KDialog::Ok, i18n("Continue"));
230
 
    setModal(true);
231
 
    // restore size
232
 
    setMinimumSize(QSize(450,300));
233
 
    setInitialSize(QSize(450,300));
234
 
    KConfig config("KPackageKit");
235
 
    KConfigGroup requirementsDialog(&config, "requirementsDialog");
236
 
    restoreDialogSize(requirementsDialog);
237
 
 
238
 
    d->ui.packageView->setModel(model);
239
 
    d->actionsModel = new QStandardItemModel(this);
240
 
    d->ui.actionsView->setModel(d->actionsModel);
241
 
    d->ui.actionsView->setItemDelegate(new KActionsViewDelegate(d->ui.actionsView));
242
 
 
243
 
    connect(d->ui.actionsView->selectionModel(),
244
 
                SIGNAL(currentChanged(const QModelIndex &,
245
 
                                      const QModelIndex &)),
246
 
            this,
247
 
                SLOT(actionClicked(const QModelIndex &)));
248
 
 
249
 
    // Sets a transparent background
250
 
    QWidget *actionsViewport = d->ui.actionsView->viewport();
251
 
    QPalette palette = actionsViewport->palette();
252
 
    palette.setColor(actionsViewport->backgroundRole(), Qt::transparent);
253
 
    palette.setColor(actionsViewport->foregroundRole(), palette.color(QPalette::WindowText));
254
 
    actionsViewport->setPalette(palette);
255
 
 
256
 
    // Populate the actionsModel
257
 
    QStandardItem *item;
258
 
    if (int c = model->countInfo(Package::InfoRemoving)) {
259
 
        item = new QStandardItem;
260
 
        item->setText(i18np("1 package to remove", "%1 packages to remove", c));
261
 
        item->setIcon(KpkIcons::actionIcon(Transaction::RoleRemovePackages));
262
 
        item->setData(QVariant::fromValue(Package::InfoRemoving));
263
 
        d->actionsModel->appendRow(item);
264
 
        model->setCurrentInfo(Package::InfoRemoving);
265
 
        d->hideAutoConfirm = true;
266
 
    }
267
 
 
268
 
    if (int c = model->countInfo(Package::InfoDowngrading)) {
269
 
        item = new QStandardItem;
270
 
        item->setText(i18np("1 package to downgrade", "%1 packages to downgrade", c));
271
 
        item->setIcon(KpkIcons::actionIcon(Transaction::RoleRollback));
272
 
        item->setData(QVariant::fromValue(Package::InfoDowngrading));
273
 
        if (model->currentInfo() == Package::UnknownInfo) {
274
 
            model->setCurrentInfo(Package::InfoDowngrading);
275
 
        }
276
 
        d->actionsModel->appendRow(item);
277
 
        d->hideAutoConfirm = true;
278
 
    }
279
 
 
280
 
    if (int c = model->countInfo(Package::InfoReinstalling)) {
281
 
        item = new QStandardItem;
282
 
        item->setText(i18np("1 package to reinstall", "%1 packages to reinstall", c));
283
 
        item->setIcon(KpkIcons::actionIcon(Transaction::RoleRemovePackages));
284
 
        item->setData(QVariant::fromValue(Package::InfoReinstalling));
285
 
        if (model->currentInfo() == Package::UnknownInfo) {
286
 
            model->setCurrentInfo(Package::InfoReinstalling);
287
 
        }
288
 
        d->actionsModel->appendRow(item);
289
 
    }
290
 
 
291
 
    if (int c = model->countInfo(Package::InfoInstalling)) {
292
 
        item = new QStandardItem;
293
 
//         item->setIconSize(QSize(48, 48));
294
 
        item->setText(i18np("1 package to install", "%1 packages to install", c));
295
 
        item->setIcon(KpkIcons::actionIcon(Transaction::RoleInstallPackages).pixmap(48.48));
296
 
        kDebug() << KIconLoader::global()->iconPath(KpkIcons::actionIconName(Transaction::RoleInstallPackages), KIconLoader::NoGroup, true);
297
 
        item->setData(QVariant::fromValue(Package::InfoInstalling));
298
 
        if (model->currentInfo() == Package::UnknownInfo) {
299
 
            model->setCurrentInfo(Package::InfoInstalling);
300
 
        }
301
 
        d->actionsModel->appendRow(item);
302
 
    }
303
 
 
304
 
    if (int c = model->countInfo(Package::InfoUpdating)) {
305
 
        item = new QStandardItem;
306
 
        item->setText(i18np("1 package to update", "%1 packages to update", c));
307
 
        item->setIcon(KpkIcons::actionIcon(Transaction::RoleUpdatePackages));
308
 
        item->setData(QVariant::fromValue(Package::InfoUpdating));
309
 
        if (model->currentInfo() == Package::UnknownInfo) {
310
 
            model->setCurrentInfo(Package::InfoUpdating);
311
 
        }
312
 
        d->actionsModel->appendRow(item);
313
 
    }
314
 
 
315
 
    if (d->actionsModel->rowCount()) {
316
 
        d->ui.actionsView->setCurrentIndex(d->actionsModel->index(0, 0));
317
 
    }
318
 
    d->ui.packageView->resizeColumnToContents(0);
319
 
    d->ui.packageView->resizeColumnToContents(1);
320
 
 
321
 
    if (d->hideAutoConfirm) {
322
 
        d->ui.confirmCB->setVisible(false);
323
 
    } else {
324
 
        // if the confirmCB is visible means that we can skip this
325
 
        // dialog, but only if the user previusly set so
326
 
        d->ui.confirmCB->setChecked(requirementsDialog.readEntry("autoConfirm", false));
327
 
    }
328
 
}
329
 
 
330
 
KpkRequirements::~KpkRequirements()
331
 
{
332
 
    // save size
333
 
    KConfig config("KPackageKit");
334
 
    KConfigGroup requirementsDialog(&config, "requirementsDialog");
335
 
    saveDialogSize(requirementsDialog);
336
 
 
337
 
    if (!d->hideAutoConfirm) {
338
 
        requirementsDialog.writeEntry("autoConfirm", d->ui.confirmCB->isChecked());
339
 
    }
340
 
    config.sync();
341
 
    delete d;
342
 
}
343
 
 
344
 
void KpkRequirements::show()
345
 
{
346
 
    if (d->ui.confirmCB->isChecked()) {
347
 
        emit accepted();
348
 
    } else{
349
 
        KDialog::show();
350
 
    }
351
 
}
352
 
 
353
 
void KpkRequirements::actionClicked(const QModelIndex &index)
354
 
{
355
 
    Package::Info state = index.data(Qt::UserRole+1).value<Package::Info>();
356
 
    static_cast<KpkSimulateModel*>(d->ui.packageView->model())->setCurrentInfo(state);
357
 
    d->ui.packageView->resizeColumnToContents(0);
358
 
    d->ui.packageView->resizeColumnToContents(1);
359
 
}
360
 
 
361
 
#include "KpkRequirements.moc"