File: chartWidget.cpp

package info (click to toggle)
kde-workspace 4%3A4.8.4-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 89,920 kB
  • sloc: cpp: 373,584; ansic: 35,020; xml: 7,435; perl: 1,550; sh: 1,329; ruby: 1,135; python: 646; asm: 566; makefile: 38
file content (154 lines) | stat: -rw-r--r-- 4,634 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***************************************************************************
 *   KT list view item task implementation.                                *
 *   --------------------------------------------------------------------  *
 *   Copyright (C) 1999, Gary Meyer <gary@meyer.net>                       *
 *   --------------------------------------------------------------------  *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "chartWidget.h"

#include <QVBoxLayout>
#include <QLinearGradient>
#include <QLabel>
#include <QPainter>
#include <QPen>
#include <QColor>

#include <klocale.h>
#include <kdebug.h>
#include <kglobal.h>

Chart::Chart(QWidget* parent) :
	QWidget(parent) {
	
	setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
	
	memoryInfos = NULL;
	freeMemoryLabel = NULL;
}


void Chart::setMemoryInfos(t_memsize* memoryInfos) {
	this->memoryInfos = memoryInfos;
}

void Chart::setFreeMemoryLabel(QLabel* freeMemoryLabel) {
	this->freeMemoryLabel = freeMemoryLabel;
}


/* Graphical Memory Display */
bool Chart::drawChart(t_memsize total, const QList<t_memsize>& used, const QList<QColor>& colors, const QList<QString>& texts) {
	
	QPainter paint(this);

	QPen pen(QColor(0, 0, 0));
	paint.setPen(pen);

	if (total == NO_MEMORY_INFO) {
		paint.fillRect(1, 1, width() - 2, height() - 2, QBrush(QColor(128, 128, 128)));
		paint.setPen(pen);
		paint.drawRect(rect());
		freeMemoryLabel->setText(i18n("Not available."));

		return false;
	}

	int startline = height()-2;
	
	int percent, localheight;
	t_memsize last_used = 0;

	for (int count = used.size()-1; count >=0; --count) {
		last_used = used.at(count);
		QColor color = colors.at(count);
		QString text = texts.at(count);

		percent = (((qint64)last_used) * 100) / total;
		
		if (count)
			localheight = ((height()-2) * percent) / 100;
		else
			localheight = startline;

		//kDebug() << "Count : " <<  count << " Percent : " << percent << "%" << " Localheight:" << localheight << endl;
		
		if (localheight>0) {
			QLinearGradient gradient(QPointF(1, startline), QPointF(width()-2, -localheight));

			QColor endProgressColor(0xFF, 0xFF, 0xFF, 100);
			gradient.setColorAt(0, color);
			gradient.setColorAt(1, endProgressColor);
			paint.fillRect(1, startline, width()-2, -localheight, gradient);

			//paint.fillRect(1, startline, width()-2, -localheight, color);

			if (localheight >= SPACING) {
				paint.drawText(0, startline-localheight, width(), localheight, Qt::AlignCenter | Qt::TextWordWrap, QString("%1 %2%").arg(text).arg(percent));
			}
		}

		startline -= localheight;

	}

	// draw surrounding box
	QRect r = rect();
	qDrawShadePanel(&paint, r.x(), r.y(), r.width(), r.height(), palette(), true, 1);

	freeMemoryLabel->setText(i18n("%1 free", formattedUnit(last_used)));

	
	return true;
}


QString Chart::formattedUnit(t_memsize value) {
	const KLocale* locale = KGlobal::locale();
	const double multiplier = (locale->binaryUnitDialect() == KLocale::MetricBinaryDialect) ? 1000 : 1024;
	if (value > (multiplier * multiplier))
		if (value > (multiplier * multiplier * multiplier))
			return locale->formatByteSize(value, 2, KLocale::DefaultBinaryDialect, KLocale::UnitGigaByte);
		else
			return locale->formatByteSize(value, 2, KLocale::DefaultBinaryDialect, KLocale::UnitMegaByte);
	else
		return locale->formatByteSize(value, 2, KLocale::DefaultBinaryDialect, KLocale::UnitKiloByte);
}

ChartWidget::ChartWidget(const QString& title, const QString& hint, Chart* chartImplementation, QWidget* parent) :
	QWidget(parent) {

	QVBoxLayout* mainLayout = new QVBoxLayout(this);
	
	titleLabel = new QLabel("<strong>" + title + "</strong>", this);
	titleLabel->setAlignment(Qt::AlignHCenter);
	titleLabel->setToolTip(hint);
	mainLayout->addWidget(titleLabel);
	
	chart = chartImplementation;
	chart->setToolTip(hint);
	mainLayout->addWidget(chart);
	
	freeMemoryLabel = new QLabel("", this);
	freeMemoryLabel->setAlignment(Qt::AlignHCenter);
	freeMemoryLabel->setToolTip(hint);
	mainLayout->addWidget(freeMemoryLabel);

	chart->setFreeMemoryLabel(freeMemoryLabel);
}

void ChartWidget::setMemoryInfos(t_memsize* memoryInfos) {
	chart->setMemoryInfos(memoryInfos);
}

void ChartWidget::refresh() {
	//The update() method will launch paintEvent() automatically
	chart->update();
	
}