1
/***************************************************************************
2
* Copyright (C) 2008-2010 by Daniel Nicoletti *
3
* dantti85-pk@yahoo.com.br *
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. *
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. *
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>
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.
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.
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.
41
#include "KpkRequirements.h"
42
#include "ui_KpkRequirements.h"
45
#include "KpkSimulateModel.h"
51
#include <QStandardItemModel>
53
Q_DECLARE_METATYPE(Package::Info)
56
#define LATERAL_MARGIN 4
58
class KActionsViewDelegate : public QAbstractItemDelegate
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;
70
void setIconSize(int newSize);
72
void setShowHoverIndication(bool show);
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;
79
qreal contentsOpacity(const QModelIndex &index) const;
84
QList<QPersistentModelIndex> m_appearingItems;
85
int m_appearingIconSize;
86
qreal m_appearingOpacity;
88
QList<QPersistentModelIndex> m_disappearingItems;
89
int m_disappearingIconSize;
90
qreal m_disappearingOpacity;
92
bool m_showHoverIndication;
94
QMap<QPersistentModelIndex, QTimeLine*> m_timeLineMap;
95
QMap<QTimeLine*, QPersistentModelIndex> m_timeLineInverseMap;
98
KActionsViewDelegate::KActionsViewDelegate(QObject *parent) :
99
QAbstractItemDelegate(parent),
101
m_appearingIconSize(0),
102
m_appearingOpacity(0.0),
103
m_disappearingIconSize(0),
104
m_disappearingOpacity(0.0),
105
m_showHoverIndication(true)
109
KActionsViewDelegate::~KActionsViewDelegate()
113
QSize KActionsViewDelegate::sizeHint(const QStyleOptionViewItem &option,
114
const QModelIndex &index) const
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;
123
return QSize(option.rect.width(), option.fontMetrics.height() / 2 + qMax(iconSize, option.fontMetrics.height()));
126
void KActionsViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
130
if (m_appearingItems.contains(index)) {
131
painter->setOpacity(m_appearingOpacity);
132
} else if (m_disappearingItems.contains(index)) {
133
painter->setOpacity(m_disappearingOpacity);
136
QStyleOptionViewItemV4 opt = option;
137
if (!m_showHoverIndication) {
138
opt.state &= ~QStyle::State_MouseOver;
140
QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter);
142
bool isLTR = option.direction == Qt::LeftToRight;
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);
150
if (option.state & QStyle::State_Selected) {
151
painter->setPen(option.palette.highlightedText().color());
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()));
162
int KActionsViewDelegate::iconSize() const
167
void KActionsViewDelegate::setIconSize(int newSize)
169
m_iconSize = newSize;
172
void KActionsViewDelegate::setShowHoverIndication(bool show)
174
m_showHoverIndication = show;
177
void KActionsViewDelegate::addFadeAnimation(const QModelIndex &index, QTimeLine *timeLine)
179
m_timeLineMap.insert(index, timeLine);
180
m_timeLineInverseMap.insert(timeLine, index);
183
void KActionsViewDelegate::removeFadeAnimation(const QModelIndex &index)
185
QTimeLine *timeLine = m_timeLineMap.value(index, 0);
186
m_timeLineMap.remove(index);
187
m_timeLineInverseMap.remove(timeLine);
190
QModelIndex KActionsViewDelegate::indexForFadeAnimation(QTimeLine *timeLine) const
192
return m_timeLineInverseMap.value(timeLine, QModelIndex());
195
QTimeLine *KActionsViewDelegate::fadeAnimationForIndex(const QModelIndex &index) const
197
return m_timeLineMap.value(index, 0);
200
qreal KActionsViewDelegate::contentsOpacity(const QModelIndex &index) const
202
QTimeLine *timeLine = fadeAnimationForIndex(index);
204
return timeLine->currentValue();
210
class KpkRequirementsPrivate
213
QStandardItemModel *actionsModel;
214
bool hideAutoConfirm;
216
Ui::KpkRequirements ui;
219
KpkRequirements::KpkRequirements(KpkSimulateModel *model, QWidget *parent)
221
d(new KpkRequirementsPrivate)
223
d->ui.setupUi(mainWidget());
225
d->hideAutoConfirm = false;
227
setCaption(i18n("Additional changes"));
228
setButtons(KDialog::Ok | KDialog::Cancel);
229
setButtonText(KDialog::Ok, i18n("Continue"));
232
setMinimumSize(QSize(450,300));
233
setInitialSize(QSize(450,300));
234
KConfig config("KPackageKit");
235
KConfigGroup requirementsDialog(&config, "requirementsDialog");
236
restoreDialogSize(requirementsDialog);
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));
243
connect(d->ui.actionsView->selectionModel(),
244
SIGNAL(currentChanged(const QModelIndex &,
245
const QModelIndex &)),
247
SLOT(actionClicked(const QModelIndex &)));
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);
256
// Populate the actionsModel
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;
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);
276
d->actionsModel->appendRow(item);
277
d->hideAutoConfirm = true;
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);
288
d->actionsModel->appendRow(item);
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);
301
d->actionsModel->appendRow(item);
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);
312
d->actionsModel->appendRow(item);
315
if (d->actionsModel->rowCount()) {
316
d->ui.actionsView->setCurrentIndex(d->actionsModel->index(0, 0));
318
d->ui.packageView->resizeColumnToContents(0);
319
d->ui.packageView->resizeColumnToContents(1);
321
if (d->hideAutoConfirm) {
322
d->ui.confirmCB->setVisible(false);
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));
330
KpkRequirements::~KpkRequirements()
333
KConfig config("KPackageKit");
334
KConfigGroup requirementsDialog(&config, "requirementsDialog");
335
saveDialogSize(requirementsDialog);
337
if (!d->hideAutoConfirm) {
338
requirementsDialog.writeEntry("autoConfirm", d->ui.confirmCB->isChecked());
344
void KpkRequirements::show()
346
if (d->ui.confirmCB->isChecked()) {
353
void KpkRequirements::actionClicked(const QModelIndex &index)
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);
361
#include "KpkRequirements.moc"