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

« back to all changes in this revision

Viewing changes to plasma/generic/applets/system-monitor/plotter.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
 *   Copyright (C) 2010 Petri Damsten <damu@iki.fi>
 
3
 *
 
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
 
7
 *
 
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
 
12
 *
 
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.
 
17
 */
 
18
 
 
19
#include "plotter.h"
 
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>
 
27
#include <QWidget>
 
28
 
 
29
namespace SM {
 
30
 
 
31
Plotter::Plotter(QGraphicsItem* parent, Qt::WindowFlags wFlags)
 
32
    : QGraphicsWidget(parent, wFlags)
 
33
    , m_layout(0)
 
34
    , m_plotter(0)
 
35
    , m_meter(0)
 
36
    , m_plotCount(1)
 
37
    , m_min(0.0)
 
38
    , m_max(0.0)
 
39
    , m_overlayFrame(0)
 
40
{
 
41
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
42
    createWidgets();
 
43
    connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
 
44
}
 
45
 
 
46
Plotter::~Plotter()
 
47
{
 
48
}
 
49
 
 
50
void Plotter::setAnalog(bool analog)
 
51
{
 
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);
 
60
        themeChanged();
 
61
    } else if (m_layout->count() > 1) {
 
62
        m_layout->removeAt(0);
 
63
        delete m_meter;
 
64
        m_meter = 0;
 
65
    }
 
66
}
 
67
 
 
68
void Plotter::setMinMax(double min, double max)
 
69
{
 
70
    if (m_meter) {
 
71
        m_meter->setMinimum(min);
 
72
        m_meter->setMaximum(max);
 
73
    }
 
74
    m_plotter->setUseAutoRange(false);
 
75
    m_plotter->setVerticalRange(min, max);
 
76
    m_min = min;
 
77
    m_max = max;
 
78
}
 
79
 
 
80
const QString& Plotter::title()
 
81
{
 
82
    return m_title;
 
83
}
 
84
 
 
85
void Plotter::setTitle(const QString& title)
 
86
{
 
87
    m_plotter->setTitle(title);
 
88
    if (m_meter) {
 
89
        m_meter->setLabel(0, title);
 
90
    }
 
91
    m_title = title;
 
92
}
 
93
 
 
94
void Plotter::setUnit(const QString& unit)
 
95
{
 
96
    m_plotter->setUnit(unit);
 
97
    m_unit = unit;
 
98
}
 
99
 
 
100
void Plotter::setScale(qreal scale)
 
101
{
 
102
    m_plotter->scale(scale);
 
103
}
 
104
 
 
105
void Plotter::setStackPlots(bool stack)
 
106
{
 
107
    m_plotter->setStackPlots(stack);
 
108
}
 
109
 
 
110
void Plotter::setPlotCount(int count)
 
111
{
 
112
    for (int i = 0; i < m_plotCount; ++i) {
 
113
        m_plotter->removePlot(0);
 
114
    }
 
115
    m_plotCount = count;
 
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);
 
122
    }
 
123
}
 
124
 
 
125
void Plotter::setCustomPlots(const QList<QColor>& colors)
 
126
{
 
127
    for (int i = 0; i < m_plotCount; ++i) {
 
128
        m_plotter->removePlot(0);
 
129
    }
 
130
    m_plotCount = colors.count();
 
131
    foreach (const QColor& color, colors) {
 
132
        m_plotter->addPlot(color);
 
133
    }
 
134
}
 
135
 
 
136
void Plotter::createWidgets()
 
137
{
 
138
    m_layout = new QGraphicsLinearLayout(Qt::Horizontal);
 
139
    m_layout->setContentsMargins(0, 0, 0, 0);
 
140
    m_layout->setSpacing(5);
 
141
    setLayout(m_layout);
 
142
 
 
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);
 
152
    themeChanged();
 
153
}
 
154
 
 
155
void Plotter::themeChanged()
 
156
{
 
157
    Plasma::Theme* theme = Plasma::Theme::defaultTheme();
 
158
    if (m_meter) {
 
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"));
 
162
    }
 
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);
 
170
    resizeEvent(0);
 
171
}
 
172
 
 
173
void Plotter::addSample(const QList<double>& values)
 
174
{
 
175
    m_plotter->addSample(values);
 
176
    QStringList list;
 
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);
 
180
    }
 
181
    setOverlayText(list.join(" / "));
 
182
    if (m_meter) {
 
183
        m_meter->setValue(values[0]);
 
184
    }
 
185
}
 
186
 
 
187
void Plotter::setOverlayText(const QString& text)
 
188
{
 
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);
 
202
        resizeEvent(0);
 
203
    }
 
204
    m_overlayFrame->setText(text);
 
205
    if (m_meter) {
 
206
        if (m_showAnalogValue) {
 
207
            m_meter->setLabel(1, text);
 
208
        } else {
 
209
            m_meter->setLabel(1, QString());
 
210
        }
 
211
    }
 
212
}
 
213
 
 
214
void Plotter::resizeEvent(QGraphicsSceneResizeEvent* event)
 
215
{
 
216
    Q_UNUSED(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();
 
222
    bool show = false;
 
223
    QFontMetrics metrics(font);
 
224
    QStringList list;
 
225
    for (int i = 0; i < m_plotCount; ++i) {
 
226
        list << QString("888.0 %2").arg(m_unit);
 
227
    }
 
228
    QString valueText = list.join(" / ");
 
229
 
 
230
    font.setPointSizeF(smallest.pointSizeF());
 
231
    forever {
 
232
        metrics = QFontMetrics(font);
 
233
        if (metrics.height() > fontHeight) {
 
234
            break;
 
235
        }
 
236
        font.setPointSizeF(font.pointSizeF() + 0.5);
 
237
        show = true;
 
238
    }
 
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);
 
247
    }
 
248
 
 
249
    if (m_meter) {
 
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);
 
257
        } else {
 
258
            m_meter->setLabel(0, QString());
 
259
        }
 
260
        m_meter->setLabel(1, QString());
 
261
    }
 
262
}
 
263
 
 
264
} // namespace
 
265
 
 
266
#include "plotter.moc"