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>
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
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
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.
22
#include "ResourceDelegate.h"
25
#include <QApplication>
26
#include <QtGui/QAbstractItemView>
27
#include <QtGui/QPainter>
28
#include <QtGui/QPushButton>
29
#include <QtGui/QTreeView>
32
#include <KApplication>
33
#include <KIconLoader>
36
#include <KStandardDirs>
37
#include <Nepomuk/KRatingPainter>
41
#include <Transaction/TransactionModel.h>
42
#include <resources/AbstractResource.h>
43
#include <resources/ResourcesModel.h>
46
#include "ResourceExtender.h"
48
#define FAV_ICON_SIZE 24
49
#define EMBLEM_ICON_SIZE 8
50
#define UNIVERSAL_PADDING 4
51
#define MAIN_ICON_SIZE 32
53
ResourceDelegate::ResourceDelegate(QAbstractItemView *parent)
54
: KExtendableItemDelegate(parent),
56
m_showInfoButton(true)
59
QPushButton button, button2;
60
KIcon icon("edit-delete");
62
button.setText(i18n("Install"));
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);
71
m_emblem = KIcon("dialog-ok").pixmap(QSize(16, 16));
72
m_ratingPainter = new KRatingPainter;
75
void ResourceDelegate::paint(QPainter *painter,
76
const QStyleOptionViewItem &option,
77
const QModelIndex &index) const
79
if (!index.isValid()) {
82
bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight);
84
QStyleOptionViewItemV4 opt(option);
85
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
87
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
91
KExtendableItemDelegate::paint(painter, opt, index);
95
opt.rect.setLeft(option.rect.left() + UNIVERSAL_PADDING);
96
leftCount = opt.rect.left() + UNIVERSAL_PADDING;
98
opt.rect.setRight(option.rect.right() - UNIVERSAL_PADDING);
99
leftCount = opt.rect.width() - (UNIVERSAL_PADDING + MAIN_ICON_SIZE);
102
int left = opt.rect.left();
103
int top = opt.rect.top();
104
int width = opt.rect.width();
106
QRect rect = opt.rect;
109
rect.setLeft(left + width - (m_buttonSize.width() + UNIVERSAL_PADDING));
110
width -= m_buttonSize.width() + UNIVERSAL_PADDING;
112
rect.setLeft(left + UNIVERSAL_PADDING);
113
left += m_buttonSize.width() + UNIVERSAL_PADDING;
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
120
bool transactionActive = index.data(ResourcesModel::ActiveRole).toBool();
122
if (!transactionActive) {
123
int rating = index.data(ResourcesModel::RatingRole).toInt();
125
m_ratingPainter->paint(painter, rect, rating);
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);
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;
149
QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))?
150
option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text);
152
// Painting main column
153
QStyleOptionViewItem local_option_title(option);
154
QStyleOptionViewItem local_option_normal(option);
156
local_option_normal.font.setPointSize(local_option_normal.font.pointSize() - 1);
158
QPixmap pixmap(option.rect.size());
159
pixmap.fill(Qt::transparent);
161
p.translate(-option.rect.topLeft());
164
KIcon icon(index.data(ResourcesModel::IconRole).toString());
166
int iconSize = calcItemHeight(option) - 2 * UNIVERSAL_PADDING;
169
top + UNIVERSAL_PADDING,
175
if (index.data(ResourcesModel::InstalledRole).toBool()) {
176
p.drawPixmap(leftCount, top + rect.height() - m_emblem.height()/2, m_emblem);
182
leftCount += iconSize + UNIVERSAL_PADDING;
183
textWidth = width - (leftCount - left);
185
leftCount -= UNIVERSAL_PADDING;
186
textWidth = leftCount - left;
191
const int itemHeight = calcItemHeight(option);
193
p.setPen(foregroundColor);
195
int topTextHeight = QFontInfo(local_option_title.font).pixelSize();
196
p.setFont(local_option_title.font);
197
p.drawText(leftCount,
200
topTextHeight + UNIVERSAL_PADDING,
201
Qt::AlignVCenter | Qt::AlignLeft,
202
index.data(ResourcesModel::NameRole).toString());
204
// draw the bottom line
205
iconSize = topTextHeight + UNIVERSAL_PADDING;
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);
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());
222
painter->drawPixmap(option.rect.topLeft(), pixmap);
225
int ResourceDelegate::calcItemHeight(const QStyleOptionViewItem &option) const
227
// Painting main column
228
QStyleOptionViewItem local_option_title(option);
229
QStyleOptionViewItem local_option_normal(option);
231
local_option_normal.font.setPointSize(local_option_normal.font.pointSize() - 1);
233
int textHeight = QFontInfo(local_option_title.font).pixelSize() + QFontInfo(local_option_normal.font).pixelSize();
234
return textHeight + 3 * UNIVERSAL_PADDING;
237
bool ResourceDelegate::editorEvent(QEvent *event,
238
QAbstractItemModel *model,
239
const QStyleOptionViewItem &option,
240
const QModelIndex &index)
243
if (event->type() == QEvent::MouseButtonRelease) {
244
itemActivated(index);
247
return KExtendableItemDelegate::editorEvent(event, model, option, index);
250
QSize ResourceDelegate::sizeHint(const QStyleOptionViewItem &option,
251
const QModelIndex &index ) const
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);
258
ret.rheight() += calcItemHeight(option);
259
ret.rwidth() += width;
264
void ResourceDelegate::itemActivated(QModelIndex index)
266
if ((index == m_oldIndex && isExtended(index))) {
270
if (isExtended(m_oldIndex)) {
271
disconnect(m_extender, SIGNAL(infoButtonClicked(AbstractResource*)),
272
this, SIGNAL(infoButtonClicked(AbstractResource*)));
273
contractItem(m_oldIndex);
275
m_extender->deleteLater();
279
QVariant appVarient = static_cast<const QAbstractItemModel*>(index.model())->data(index, ResourcesModel::ApplicationRole);
280
AbstractResource *app = qobject_cast<AbstractResource*>(appVarient.value<QObject*>());
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*)));
288
extendItem(m_extender, index);
292
void ResourceDelegate::invalidate()
294
// If only contractAll was a Q_SLOT...
298
void ResourceDelegate::setShowInfoButton(bool show)
300
m_showInfoButton = show;