~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/desktop/applets/kickoff/ui/itemdelegate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2007 Robert Knight <robertknight@gmail.com>
 
3
    Copyright 2007 Kevin Ottens <ervin@kde.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
// Own
 
22
#include "ui/itemdelegate.h"
 
23
 
 
24
// Qt
 
25
#include <QModelIndex>
 
26
#include <QPainter>
 
27
#include <QStyle>
 
28
#include <QStyleOptionViewItem>
 
29
 
 
30
// KDE
 
31
#include <KDebug>
 
32
#include <KGlobal>
 
33
#include <kcapacitybar.h>
 
34
 
 
35
// plasma
 
36
#include <Plasma/Plasma>
 
37
 
 
38
using namespace Kickoff;
 
39
 
 
40
ItemDelegate::ItemDelegate(QObject *parent)
 
41
        : Plasma::Delegate(parent)
 
42
{
 
43
}
 
44
 
 
45
void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
 
46
{
 
47
    Plasma::Delegate::paint(painter, option, index);
 
48
 
 
49
    qreal freeSpace = -1;
 
50
    qreal usedSpace = -1;
 
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;
 
54
    }
 
55
 
 
56
 
 
57
    // draw free space information (for drive icons)
 
58
    if (usedSpace >= 0) {
 
59
        painter->save();
 
60
 
 
61
        QRect emptyRect = rectAfterTitle(option, index);
 
62
 
 
63
        QSize barSize = QSize(qMin(emptyRect.width(), option.rect.width() / 3), emptyRect.height());
 
64
 
 
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);
 
70
            }
 
71
 
 
72
            QRect spaceRect = QStyle::alignedRect(option.direction,
 
73
                                                  Qt::AlignRight, barSize, emptyRect);
 
74
 
 
75
            if (!(option.state & (QStyle::State_Selected | QStyle::State_MouseOver | QStyle::State_HasFocus))) {
 
76
                painter->setOpacity(painter->opacity() / 2.5);
 
77
            } else {
 
78
            }
 
79
 
 
80
            KCapacityBar capacityBar(KCapacityBar::DrawTextInline);
 
81
            capacityBar.setValue((usedSpace / (freeSpace + usedSpace))*100);
 
82
            capacityBar.drawCapacityBar(painter, spaceRect);
 
83
 
 
84
            // -- Removed the free space text because it added too much 'visual noise' to the item
 
85
            //
 
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));
 
90
        }
 
91
 
 
92
        painter->restore();
 
93
    }
 
94
 
 
95
}
 
96
 
 
97
bool ItemDelegate::isVisible(const QModelIndex& index) const
 
98
{
 
99
    if (!index.isValid()) return false;
 
100
 
 
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()) {
 
106
                return true;
 
107
            }
 
108
        }
 
109
        return false;
 
110
    }
 
111
 
 
112
    return !index.data(UrlRole).isNull();
 
113
}
 
114