add more spacing
[personal-kdebase.git] / apps / kinfocenter / memory / chartWidget.cpp
blobf5098ecdd496f2b3ac58d42c88598e524aff5323
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 ***************************************************************************/
12 #include "chartWidget.h"
14 #include <QVBoxLayout>
15 #include <QLinearGradient>
16 #include <QLabel>
17 #include <QPainter>
18 #include <QPen>
19 #include <QColor>
21 #include <klocale.h>
22 #include <kdebug.h>
23 #include <kglobal.h>
25 Chart::Chart(QWidget* parent) :
26 QWidget(parent) {
28 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
30 memoryInfos = NULL;
31 freeMemoryLabel = NULL;
35 void Chart::setMemoryInfos(t_memsize* memoryInfos) {
36 this->memoryInfos = memoryInfos;
39 void Chart::setFreeMemoryLabel(QLabel* freeMemoryLabel) {
40 this->freeMemoryLabel = freeMemoryLabel;
44 /* Graphical Memory Display */
45 bool Chart::drawChart(t_memsize total, const QList<t_memsize>& used, const QList<QColor>& colors, const QList<QString>& texts) {
47 QPainter paint(this);
49 QPen pen(QColor(0, 0, 0));
50 paint.setPen(pen);
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."));
58 return false;
61 int startline = height()-2;
63 int percent, localheight;
64 t_memsize last_used = 0;
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);
71 percent = (((qint64)last_used) * 100) / total;
73 if (count)
74 localheight = ((height()-2) * percent) / 100;
75 else
76 localheight = startline;
78 //kDebug() << "Count : " << count << " Percent : " << percent << "%" << " Localheight:" << localheight << endl;
80 if (localheight>0) {
81 QLinearGradient gradient(QPointF(1, startline), QPointF(width()-2, -localheight));
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);
88 //paint.fillRect(1, startline, width()-2, -localheight, color);
90 if (localheight >= SPACING) {
91 paint.drawText(0, startline-localheight, width(), localheight, Qt::AlignCenter | Qt::TextWordWrap, QString("%1 %2%").arg(text).arg(percent));
95 startline -= localheight;
99 // draw surrounding box
100 QRect r = rect();
101 qDrawShadePanel(&paint, r.x(), r.y(), r.width(), r.height(), palette(), true, 1);
103 freeMemoryLabel->setText(i18n("%1 free", formattedUnit(last_used)));
106 return true;
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));
120 ChartWidget::ChartWidget(const QString& title, const QString& hint, Chart* chartImplementation, QWidget* parent) :
121 QWidget(parent) {
123 QVBoxLayout* mainLayout = new QVBoxLayout(this);
125 titleLabel = new QLabel("<strong>" + title + "</strong>", this);
126 titleLabel->setAlignment(Qt::AlignHCenter);
127 titleLabel->setToolTip(hint);
128 mainLayout->addWidget(titleLabel);
130 chart = chartImplementation;
131 chart->setToolTip(hint);
132 mainLayout->addWidget(chart);
134 freeMemoryLabel = new QLabel("", this);
135 freeMemoryLabel->setAlignment(Qt::AlignHCenter);
136 freeMemoryLabel->setToolTip(hint);
137 mainLayout->addWidget(freeMemoryLabel);
139 chart->setFreeMemoryLabel(freeMemoryLabel);
142 void ChartWidget::setMemoryInfos(t_memsize* memoryInfos) {
143 chart->setMemoryInfos(memoryInfos);
146 void ChartWidget::refresh() {
147 //The update() method will launch paintEvent() automatically
148 chart->update();