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

« back to all changes in this revision

Viewing changes to kinfocenter/Modules/memory/chartWidget.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
 *   KT list view item task implementation.                                *
 
3
 *   --------------------------------------------------------------------  *
 
4
 *   Copyright (C) 1999, Gary Meyer <gary@meyer.net>                       *
 
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
 
 
12
#include "chartWidget.h"
 
13
 
 
14
#include <QVBoxLayout>
 
15
#include <QLinearGradient>
 
16
#include <QLabel>
 
17
#include <QPainter>
 
18
#include <QPen>
 
19
#include <QColor>
 
20
 
 
21
#include <klocale.h>
 
22
#include <kdebug.h>
 
23
#include <kglobal.h>
 
24
 
 
25
Chart::Chart(QWidget* parent) :
 
26
        QWidget(parent) {
 
27
        
 
28
        setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
 
29
        
 
30
        memoryInfos = NULL;
 
31
        freeMemoryLabel = NULL;
 
32
}
 
33
 
 
34
 
 
35
void Chart::setMemoryInfos(t_memsize* memoryInfos) {
 
36
        this->memoryInfos = memoryInfos;
 
37
}
 
38
 
 
39
void Chart::setFreeMemoryLabel(QLabel* freeMemoryLabel) {
 
40
        this->freeMemoryLabel = freeMemoryLabel;
 
41
}
 
42
 
 
43
 
 
44
/* Graphical Memory Display */
 
45
bool Chart::drawChart(t_memsize total, const QList<t_memsize>& used, const QList<QColor>& colors, const QList<QString>& texts) {
 
46
        
 
47
        QPainter paint(this);
 
48
 
 
49
        QPen pen(QColor(0, 0, 0));
 
50
        paint.setPen(pen);
 
51
 
 
52
        if (total == NO_MEMORY_INFO) {
 
53
                paint.fillRect(1, 1, width() - 2, height() - 2, QBrush(QColor(128, 128, 128)));
 
54
                paint.setPen(pen);
 
55
                paint.drawRect(rect());
 
56
                freeMemoryLabel->setText(i18n("Not available."));
 
57
 
 
58
                return false;
 
59
        }
 
60
 
 
61
        int startline = height()-2;
 
62
        
 
63
        int percent, localheight;
 
64
        t_memsize last_used = 0;
 
65
 
 
66
        for (int count = used.size()-1; count >=0; --count) {
 
67
                last_used = used.at(count);
 
68
                QColor color = colors.at(count);
 
69
                QString text = texts.at(count);
 
70
 
 
71
                percent = (((qint64)last_used) * 100) / total;
 
72
                
 
73
                if (count)
 
74
                        localheight = ((height()-2) * percent) / 100;
 
75
                else
 
76
                        localheight = startline;
 
77
 
 
78
                //kDebug() << "Count : " <<  count << " Percent : " << percent << "%" << " Localheight:" << localheight << endl;
 
79
                
 
80
                if (localheight>0) {
 
81
                        QLinearGradient gradient(QPointF(1, startline), QPointF(width()-2, -localheight));
 
82
 
 
83
                        QColor endProgressColor(0xFF, 0xFF, 0xFF, 100);
 
84
                        gradient.setColorAt(0, color);
 
85
                        gradient.setColorAt(1, endProgressColor);
 
86
                        paint.fillRect(1, startline, width()-2, -localheight, gradient);
 
87
 
 
88
                        //paint.fillRect(1, startline, width()-2, -localheight, color);
 
89
 
 
90
                        if (localheight >= SPACING) {
 
91
                                paint.drawText(0, startline-localheight, width(), localheight, Qt::AlignCenter | Qt::TextWordWrap, QString("%1 %2%").arg(text).arg(percent));
 
92
                        }
 
93
                }
 
94
 
 
95
                startline -= localheight;
 
96
 
 
97
        }
 
98
 
 
99
        // draw surrounding box
 
100
        QRect r = rect();
 
101
        qDrawShadePanel(&paint, r.x(), r.y(), r.width(), r.height(), palette(), true, 1);
 
102
 
 
103
        freeMemoryLabel->setText(i18n("%1 free", formattedUnit(last_used)));
 
104
 
 
105
        
 
106
        return true;
 
107
}
 
108
 
 
109
 
 
110
QString Chart::formattedUnit(t_memsize value) {
 
111
        if (value > (1024 * 1024))
 
112
                if (value > (1024 * 1024 * 1024))
 
113
                        return i18n("%1 GiB", KGlobal::locale()->formatNumber(value / (1024 * 1024 * 1024.0), 2));
 
114
                else
 
115
                        return i18n("%1 MiB", KGlobal::locale()->formatNumber(value / (1024 * 1024.0), 2));
 
116
        else
 
117
                return i18n("%1 KiB", KGlobal::locale()->formatNumber(value / 1024.0, 2));
 
118
}
 
119
 
 
120
ChartWidget::ChartWidget(const QString& title, const QString& hint, Chart* chartImplementation, QWidget* parent) :
 
121
        QWidget(parent) {
 
122
 
 
123
        QVBoxLayout* mainLayout = new QVBoxLayout(this);
 
124
        
 
125
        titleLabel = new QLabel("<strong>" + title + "</strong>", this);
 
126
        titleLabel->setAlignment(Qt::AlignHCenter);
 
127
        titleLabel->setToolTip(hint);
 
128
        mainLayout->addWidget(titleLabel);
 
129
        
 
130
        chart = chartImplementation;
 
131
        chart->setToolTip(hint);
 
132
        mainLayout->addWidget(chart);
 
133
        
 
134
        freeMemoryLabel = new QLabel("", this);
 
135
        freeMemoryLabel->setAlignment(Qt::AlignHCenter);
 
136
        freeMemoryLabel->setToolTip(hint);
 
137
        mainLayout->addWidget(freeMemoryLabel);
 
138
 
 
139
        chart->setFreeMemoryLabel(freeMemoryLabel);
 
140
}
 
141
 
 
142
void ChartWidget::setMemoryInfos(t_memsize* memoryInfos) {
 
143
        chart->setMemoryInfos(memoryInfos);
 
144
}
 
145
 
 
146
void ChartWidget::refresh() {
 
147
        //The update() method will launch paintEvent() automatically
 
148
        chart->update();
 
149
        
 
150
}
 
151
 
 
152