add more spacing
[personal-kdebase.git] / workspace / plasma / applets / kickoff / ui / itemdelegate.cpp
blob494b8e49613f759c4fdead2781c5d1fe501e1cd6
1 /*
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.
21 // Own
22 #include "ui/itemdelegate.h"
24 // Qt
25 #include <QApplication>
26 #include <QFontMetrics>
27 #include <QIcon>
28 #include <QModelIndex>
29 #include <QPainter>
30 #include <QStyle>
31 #include <QStyleOptionViewItem>
32 #include <QStyleOptionProgressBar>
34 // KDE
35 #include <KColorUtils>
36 #include <KDebug>
37 #include <KGlobal>
38 #include <KGlobalSettings>
39 #include <kcapacitybar.h>
41 // plasma
42 #include <Plasma/Plasma>
44 using namespace Kickoff;
46 ItemDelegate::ItemDelegate(QObject *parent)
47 : Plasma::Delegate(parent)
51 void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
53 Plasma::Delegate::paint(painter, option, index);
55 qreal freeSpace = -1;
56 qreal usedSpace = -1;
57 if (!index.data(DiskFreeSpaceRole).isNull()) {
58 freeSpace = index.data(DiskFreeSpaceRole).value<int>() / 1024.0 / 1024.0;
59 usedSpace = index.data(DiskUsedSpaceRole).value<int>() / 1024.0 / 1024.0;
63 // draw free space information (for drive icons)
64 if (usedSpace >= 0) {
65 painter->save();
67 QRect emptyRect = rectAfterTitle(option, index);
69 QSize barSize = QSize(qMin(emptyRect.width(), option.rect.width() / 3), emptyRect.height());
71 if (barSize.width() > 0) {
72 // if the item view is gradually resized smaller or larger, make the bar fade out/in
73 // as enough space for it becomes available
74 if (barSize.width() < 20.0) {
75 painter->setOpacity(barSize.width() / 20.0);
78 QRect spaceRect = QStyle::alignedRect(option.direction,
79 Qt::AlignRight, barSize, emptyRect);
81 if (!(option.state & (QStyle::State_Selected | QStyle::State_MouseOver | QStyle::State_HasFocus))) {
82 painter->setOpacity(painter->opacity() / 2.5);
83 } else {
86 KCapacityBar capacityBar(KCapacityBar::DrawTextInline);
87 capacityBar.setValue((usedSpace / (freeSpace + usedSpace))*100);
88 capacityBar.drawCapacityBar(painter, spaceRect);
90 // -- Removed the free space text because it added too much 'visual noise' to the item
92 // some precision is lost here, but it is acceptible given that the disk-free bar
93 // is only shown as a guide
94 // QString freeSpaceString = KGlobal::locale()->formatByteSize(freeSpace*1024*1024*1024);
95 // painter->drawText(spaceRect,Qt::AlignCenter,i18n("%1 free",freeSpaceString));
98 painter->restore();
103 bool ItemDelegate::isVisible(const QModelIndex& index) const
105 Q_ASSERT(index.isValid());
107 if (index.model()->hasChildren(index)) {
108 int childCount = index.model()->rowCount(index);
109 for (int i = 0; i < childCount; ++i) {
110 QModelIndex child = index.model()->index(i, 0, index);
111 if (!child.data(UrlRole).isNull()) {
112 return true;
115 return false;
118 return !index.data(UrlRole).isNull();