2
* Copyright (C) 2010 Petri Damsten <damu@iki.fi>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Library General Public License version 2 as
6
* published by the Free Software Foundation
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details
13
* You should have received a copy of the GNU Library General Public
14
* License along with this program; if not, write to the
15
* Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
#include <Plasma/SignalPlotter>
21
#include <Plasma/Meter>
22
#include <Plasma/Theme>
23
#include <Plasma/Frame>
24
#include <KColorUtils>
25
#include <KGlobalSettings>
26
#include <QGraphicsLinearLayout>
31
Plotter::Plotter(QGraphicsItem* parent, Qt::WindowFlags wFlags)
32
: QGraphicsWidget(parent, wFlags)
41
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
43
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
50
void Plotter::setAnalog(bool analog)
52
if (analog && m_layout->count() < 2) {
53
m_meter = new Plasma::Meter(this);
54
m_meter->setMeterType(Plasma::Meter::AnalogMeter);
55
m_meter->setLabelAlignment(1, Qt::AlignCenter);
56
m_layout->insertItem(0, m_meter);
57
m_meter->setMinimum(m_min);
58
m_meter->setMaximum(m_max);
59
m_meter->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
61
} else if (m_layout->count() > 1) {
62
m_layout->removeAt(0);
68
void Plotter::setMinMax(double min, double max)
71
m_meter->setMinimum(min);
72
m_meter->setMaximum(max);
74
m_plotter->setUseAutoRange(false);
75
m_plotter->setVerticalRange(min, max);
80
const QString& Plotter::title()
85
void Plotter::setTitle(const QString& title)
87
m_plotter->setTitle(title);
89
m_meter->setLabel(0, title);
94
void Plotter::setUnit(const QString& unit)
96
m_plotter->setUnit(unit);
100
void Plotter::setScale(qreal scale)
102
m_plotter->scale(scale);
105
void Plotter::setStackPlots(bool stack)
107
m_plotter->setStackPlots(stack);
110
void Plotter::setPlotCount(int count)
112
for (int i = 0; i < m_plotCount; ++i) {
113
m_plotter->removePlot(0);
116
Plasma::Theme* theme = Plasma::Theme::defaultTheme();
117
QColor text = theme->color(Plasma::Theme::TextColor);
118
QColor bg = theme->color(Plasma::Theme::BackgroundColor);
119
for (int i = 0; i < m_plotCount; ++i) {
120
QColor color = KColorUtils::tint(text, bg, 0.4 + ((double)i / 2.5));
121
m_plotter->addPlot(color);
125
void Plotter::setCustomPlots(const QList<QColor>& colors)
127
for (int i = 0; i < m_plotCount; ++i) {
128
m_plotter->removePlot(0);
130
m_plotCount = colors.count();
131
foreach (const QColor& color, colors) {
132
m_plotter->addPlot(color);
136
void Plotter::createWidgets()
138
m_layout = new QGraphicsLinearLayout(Qt::Horizontal);
139
m_layout->setContentsMargins(0, 0, 0, 0);
140
m_layout->setSpacing(5);
143
m_plotter = new Plasma::SignalPlotter(this);
144
m_plotter->setThinFrame(false);
145
m_plotter->setShowLabels(false);
146
m_plotter->setShowTopBar(true);
147
m_plotter->setShowVerticalLines(false);
148
m_plotter->setShowHorizontalLines(false);
149
m_plotter->setUseAutoRange(true);
150
m_plotter->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
151
m_layout->addItem(m_plotter);
155
void Plotter::themeChanged()
157
Plasma::Theme* theme = Plasma::Theme::defaultTheme();
159
m_meter->setLabelColor(0, theme->color(Plasma::Theme::TextColor));
160
m_meter->setLabelColor(0, theme->color(Plasma::Theme::TextColor));
161
m_meter->setLabelColor(1, QColor("#000"));
163
m_plotter->setFontColor(theme->color(Plasma::Theme::TextColor));
164
m_plotter->setSvgBackground("widgets/plot-background");
165
QColor linesColor = theme->color(Plasma::Theme::TextColor);
166
linesColor.setAlphaF(0.4);
167
m_plotter->setHorizontalLinesColor(linesColor);
168
m_plotter->setVerticalLinesColor(linesColor);
169
setPlotCount(m_plotCount);
173
void Plotter::addSample(const QList<double>& values)
175
m_plotter->addSample(values);
177
foreach (double value, values) {
178
double v = value / m_plotter->scaledBy();
179
list << QString("%1 %2").arg(v, 0, 'f', (v > 1000.0) ? 0 : 1).arg(m_unit);
181
setOverlayText(list.join(" / "));
183
m_meter->setValue(values[0]);
187
void Plotter::setOverlayText(const QString& text)
189
if (!m_overlayFrame) {
190
QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(Qt::Vertical, m_plotter);
191
m_plotter->setLayout(layout);
192
m_overlayFrame = new Plasma::Frame(m_plotter);
193
m_overlayFrame->setZValue(10);
194
m_overlayFrame->resize(m_overlayFrame->size().height() * 2.5,
195
m_overlayFrame->size().height());
196
layout->addStretch();
197
QGraphicsLinearLayout* layout2 = new QGraphicsLinearLayout(Qt::Horizontal, layout);
198
layout2->addStretch();
199
layout2->addItem(m_overlayFrame);
200
layout2->addStretch();
201
layout->addItem(layout2);
204
m_overlayFrame->setText(text);
206
if (m_showAnalogValue) {
207
m_meter->setLabel(1, text);
209
m_meter->setLabel(1, QString());
214
void Plotter::resizeEvent(QGraphicsSceneResizeEvent* event)
217
qreal h = size().height();
218
qreal fontHeight = h / (7.0 * 1.5); // Seven rows
219
Plasma::Theme* theme = Plasma::Theme::defaultTheme();
220
QFont font = theme->font(Plasma::Theme::DefaultFont);
221
QFont smallest = KGlobalSettings::smallestReadableFont();
223
QFontMetrics metrics(font);
225
for (int i = 0; i < m_plotCount; ++i) {
226
list << QString("888.0 %2").arg(m_unit);
228
QString valueText = list.join(" / ");
230
font.setPointSizeF(smallest.pointSizeF());
232
metrics = QFontMetrics(font);
233
if (metrics.height() > fontHeight) {
236
font.setPointSizeF(font.pointSizeF() + 0.5);
239
m_plotter->setFont(font);
240
m_plotter->setShowTopBar(metrics.height() < h / 6);
241
m_plotter->setShowLabels(show);
242
m_plotter->setShowHorizontalLines(show);
243
if (m_overlayFrame) {
244
m_overlayFrame->setVisible(metrics.height() < h / 3 &&
245
metrics.width(valueText) < size().width() * 0.8);
246
m_overlayFrame->setFont(font);
250
m_meter->setLabelFont(0, font);
251
m_meter->setLabelFont(1, font);
252
// Make analog meter square
253
m_meter->setMinimumSize(h, 8);
254
m_showAnalogValue = (m_meter->size().width() * 0.7 > metrics.width(valueText));
255
if (m_meter->size().width() * 0.9 > metrics.width(m_title)) {
256
m_meter->setLabel(0, m_title);
258
m_meter->setLabel(0, QString());
260
m_meter->setLabel(1, QString());
266
#include "plotter.moc"