1 | /* |
2 | * kdfitemdelegate.cpp |
3 | * |
4 | * Copyright (c) 2009 Dario Andres Rodriguez <andresbajotierra@gmail.com> |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
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. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "kdfitemdelegate.h" |
22 | |
23 | #include <QtGui/QStyleOptionViewItem> |
24 | #include <QtGui/QPainter> |
25 | |
26 | #include <klocale.h> |
27 | #include <kcapacitybar.h> |
28 | |
29 | #include <QPalette> |
30 | #include <QBrush> |
31 | |
32 | #include "kdfwidget.h" |
33 | #include "kdfutil.h" |
34 | |
35 | void KDFItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
36 | const QModelIndex &index) const |
37 | { |
38 | if ( index.column() == KDFWidget::UsageBarCol ) |
39 | { |
40 | |
41 | int progress = index.data( Qt::UserRole ).toInt(); |
42 | |
43 | if( progress!=-1 ) |
44 | { |
45 | KCapacityBar bar; |
46 | bar.setBarHeight( option.rect.height() -2 ); |
47 | bar.setValue( progress ); |
48 | bar.setText( QString::number( progress ) + QLatin1Char( '%' ) ); |
49 | //Draw red bar on >=Full_Percent |
50 | if ( progress >= Full_Percent ) |
51 | { |
52 | QPalette p ( bar.palette() ); |
53 | p.setBrush( QPalette::Highlight, QBrush( Qt::red ) ); |
54 | bar.setPalette( p ); |
55 | } |
56 | |
57 | if (option.state & QStyle::State_Selected || option.state & QStyle::State_MouseOver) |
58 | QStyledItemDelegate::paint( painter, option, index ); |
59 | |
60 | QRect rect(option.rect); |
61 | bar.drawCapacityBar( painter, rect.adjusted( 0, 0, -2, -1 ) ); |
62 | } |
63 | else |
64 | { |
65 | QStyledItemDelegate::paint( painter, option, index ); |
66 | } |
67 | |
68 | } |
69 | else |
70 | { |
71 | |
72 | QStyledItemDelegate::paint( painter, option, index ); |
73 | } |
74 | } |
75 | |
76 | |