File: networkplotter.cpp

package info (click to toggle)
wicd-kde 0.3.0-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,000 kB
  • sloc: cpp: 2,963; sh: 3; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 4,149 bytes parent folder | download | duplicates (2)
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
/****************************************************************************
 *  Copyright (c) 2011 Anthony Vital <anthony.vital@gmail.com>              *
 *                                                                          *
 *  This file is part of Wicd Client KDE.                                   *
 *                                                                          *
 *  Wicd Client KDE 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 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  Wicd Client KDE is distributed in the hope that it will be useful,      *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with Wicd Client KDE.  If not, see <http://www.gnu.org/licenses/>.*
 ****************************************************************************/

#include "networkplotter.h"

#include <QGraphicsLinearLayout>

#include <KGlobalSettings>

#include <Plasma/DataEngineManager>

NetworkPlotter::NetworkPlotter(QGraphicsItem *parent = 0)
    : Plasma::SignalPlotter( parent )
{
    setThinFrame(false);
    setShowLabels(false);
    setShowTopBar(false);
    setShowVerticalLines(false);
    setShowHorizontalLines(false);
    setUseAutoRange(true);
    
    addPlot(QColor("#0099ff"));
    addPlot(QColor("#91ff00"));
    
    QGraphicsLinearLayout* vLayout = new QGraphicsLinearLayout(Qt::Vertical, this);
    setLayout(vLayout);
    m_overlayFrame = new Plasma::Frame(this);
    m_overlayFrame->setFont(KGlobalSettings::smallestReadableFont());
    vLayout->addStretch();
    QGraphicsLinearLayout* hLayout = new QGraphicsLinearLayout(Qt::Horizontal, vLayout);
    hLayout->addStretch();
    hLayout->addItem(m_overlayFrame);
    hLayout->addStretch();
    vLayout->addItem(hLayout);
    
    setMinimumHeight(60);
    
    Plasma::DataEngineManager::self()->loadEngine("systemmonitor");
    m_data = QList<double>() << -1 << -1;
    m_interval = 1000;
}

NetworkPlotter::~NetworkPlotter()
{
    Plasma::DataEngineManager::self()->unloadEngine("systemmonitor");
}

void NetworkPlotter::setInterface(const QString interface)
{
    if (interface.isEmpty() || (!interface.isEmpty() && interface == m_interface)) {
        return;
    }

    Plasma::DataEngine *e = Plasma::DataEngineManager::self()->engine("systemmonitor");
    if (!e->isValid())
        return;
    
    e->disconnectSource("network/interfaces/"+m_interface+"/receiver/data", this);
    e->disconnectSource("network/interfaces/"+m_interface+"/transmitter/data", this);

    e->connectSource("network/interfaces/"+interface+"/receiver/data", this, m_interval);
    e->connectSource("network/interfaces/"+interface+"/transmitter/data", this, m_interval);
    
    m_interface = interface;
}

void NetworkPlotter::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
{
    QStringList split = source.split('/');
    if (split.length() < 4) {
        return;
    }
    //are we getting received or transmitted data?
    int index = (split[3] == "receiver") ? 0 : 1;
    //fill m_data accordingly
    m_data[index] = qMax(0.0, data["value"].toDouble());
    //update plotter only when the 2 values are filled
    if (!m_data.contains(-1)) {
        addSample(m_data);
        QStringList list;
        list << QString("%1 %2").arg(m_data[0], 0, 'f', (m_data[0] > 1000.0) ? 0 : 1).arg(i18n("KiB/s"));
        list << QString("%1 %2").arg(m_data[1], 0, 'f', (m_data[1] > 1000.0) ? 0 : 1).arg(i18n("KiB/s"));
        m_overlayFrame->setText(list.join(" / "));
        m_data = QList<double>() << -1 << -1;
    }
}