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