2
Copyright 2007 Robert Knight <robertknight@gmail.com>
3
Copyright 2007 Kevin Ottens <ervin@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library 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 GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
22
#include "ui/itemdelegate.h"
25
#include <QModelIndex>
28
#include <QStyleOptionViewItem>
33
#include <kcapacitybar.h>
36
#include <Plasma/Plasma>
38
using namespace Kickoff;
40
ItemDelegate::ItemDelegate(QObject *parent)
41
: Plasma::Delegate(parent)
45
void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
47
Plasma::Delegate::paint(painter, option, index);
51
if (!index.data(DiskFreeSpaceRole).isNull()) {
52
freeSpace = index.data(DiskFreeSpaceRole).value<int>() / 1024.0 / 1024.0;
53
usedSpace = index.data(DiskUsedSpaceRole).value<int>() / 1024.0 / 1024.0;
57
// draw free space information (for drive icons)
61
QRect emptyRect = rectAfterTitle(option, index);
63
QSize barSize = QSize(qMin(emptyRect.width(), option.rect.width() / 3), emptyRect.height());
65
if (barSize.width() > 0) {
66
// if the item view is gradually resized smaller or larger, make the bar fade out/in
67
// as enough space for it becomes available
68
if (barSize.width() < 20.0) {
69
painter->setOpacity(barSize.width() / 20.0);
72
QRect spaceRect = QStyle::alignedRect(option.direction,
73
Qt::AlignRight, barSize, emptyRect);
75
if (!(option.state & (QStyle::State_Selected | QStyle::State_MouseOver | QStyle::State_HasFocus))) {
76
painter->setOpacity(painter->opacity() / 2.5);
80
KCapacityBar capacityBar(KCapacityBar::DrawTextInline);
81
capacityBar.setValue((usedSpace / (freeSpace + usedSpace))*100);
82
capacityBar.drawCapacityBar(painter, spaceRect);
84
// -- Removed the free space text because it added too much 'visual noise' to the item
86
// some precision is lost here, but it is acceptible given that the disk-free bar
87
// is only shown as a guide
88
// QString freeSpaceString = KGlobal::locale()->formatByteSize(freeSpace*1024*1024*1024);
89
// painter->drawText(spaceRect,Qt::AlignCenter,i18n("%1 free",freeSpaceString));
97
bool ItemDelegate::isVisible(const QModelIndex& index) const
99
if (!index.isValid()) return false;
101
if (index.model()->hasChildren(index)) {
102
const int childCount = index.model()->rowCount(index);
103
for (int i = 0; i < childCount; ++i) {
104
QModelIndex child = index.model()->index(i, 0, index);
105
if (!child.data(UrlRole).isNull()) {
112
return !index.data(UrlRole).isNull();