add more spacing
[personal-kdebase.git] / workspace / plasma / applets / system-monitor / net.cpp
blob3333a0ee2672000c67dc19e93cce592738da292c
1 /*
2 * Copyright (C) 2008 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.
19 #include "net.h"
20 #include <Plasma/SignalPlotter>
21 #include <Plasma/Theme>
22 #include <KConfigDialog>
23 #include <QTimer>
24 #include <QGraphicsLinearLayout>
26 SM::Net::Net(QObject *parent, const QVariantList &args)
27 : SM::Applet(parent, args)
29 setHasConfigurationInterface(true);
30 resize(234 + 20 + 23, 135 + 20 + 25);
31 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
32 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
35 SM::Net::~Net()
39 void SM::Net::init()
41 KConfigGroup cg = config();
42 setEngine(dataEngine("systemmonitor"));
43 setInterval(cg.readEntry("interval", 2) * 1000);
44 setTitle(i18n("Network"));
45 if (engine()->sources().count() == 0) {
46 connect(engine(), SIGNAL(sourceAdded(QString)), this, SLOT(initLater(const QString)));
47 } else {
48 parseSources();
52 void SM::Net::parseSources()
54 QRegExp rx("network/interfaces/(\\w+)/transmitter/data");
56 foreach (const QString& s, engine()->sources()) {
57 if (rx.indexIn(s) != -1) {
58 //kDebug() << rx.cap(1);
59 if (rx.cap(1) != "lo") {
60 m_interfaces << s;
64 KConfigGroup cg = config();
65 setItems(cg.readEntry("interfaces", m_interfaces));
66 connectToEngine();
69 void SM::Net::initLater(const QString &name)
71 // How we know all (network) sources are ready???
72 if (name == "ps") {
73 QTimer::singleShot(0, this, SLOT(parseSources()));
77 bool SM::Net::addMeter(const QString& source)
79 QStringList l = source.split('/');
80 if (l.count() < 3) {
81 return false;
83 QString interface = l[2];
84 Plasma::Theme* theme = Plasma::Theme::defaultTheme();
85 Plasma::SignalPlotter *plotter = new Plasma::SignalPlotter(this);
86 QColor color = theme->color(Plasma::Theme::TextColor);
87 plotter->addPlot(QColor(((color.red() - 128) * 0.65) + 128,
88 ((color.green() - 128) * 0.65) + 128, 0, color.alpha()));
89 plotter->addPlot(QColor(((color.red() - 128) * 0.90) + 128, 0, 0, color.alpha()));
90 plotter->setUseAutoRange(true);
91 plotter->setThinFrame(false);
92 plotter->setShowLabels(false);
93 plotter->setShowTopBar(false);
94 plotter->setShowVerticalLines(false);
95 plotter->setShowHorizontalLines(false);
96 plotter->setStackPlots(true);
97 plotter->setFontColor(theme->color(Plasma::Theme::HighlightColor));
98 QFont font = theme->font(Plasma::Theme::DefaultFont);
99 font.setPointSize(8);
100 plotter->setFont(font);
101 plotter->setHorizontalLinesColor(theme->color(Plasma::Theme::HighlightColor));
102 plotter->setVerticalLinesColor(theme->color(Plasma::Theme::HighlightColor));
103 plotter->setHorizontalLinesCount(4);
104 //plotter->setSvgBackground("widgets/plot-background");
105 plotter->setTitle(interface);
106 plotter->setUnit("KiB/s");
107 appendPlotter(interface, plotter);
108 mainLayout()->addItem(plotter);
109 connectSource("network/interfaces/" + interface + "/receiver/data");
110 setPreferredItemHeight(80);
111 return true;
114 void SM::Net::themeChanged()
116 Plasma::Theme* theme = Plasma::Theme::defaultTheme();
117 QColor color = theme->color(Plasma::Theme::TextColor);
118 foreach (Plasma::SignalPlotter *plotter, plotters().values()) {
119 plotter->removePlot(1);
120 plotter->removePlot(0);
121 plotter->addPlot(QColor(((color.red() - 128) * 0.65) + 128,
122 ((color.green() - 128) * 0.65) + 128, 0, color.alpha()));
123 plotter->addPlot(QColor(((color.red() - 128) * 0.90) + 128, 0, 0, color.alpha()));
124 plotter->setFontColor(theme->color(Plasma::Theme::HighlightColor));
125 plotter->setHorizontalLinesColor(theme->color(Plasma::Theme::HighlightColor));
126 plotter->setVerticalLinesColor(theme->color(Plasma::Theme::HighlightColor));
130 void SM::Net::dataUpdated(const QString& source,
131 const Plasma::DataEngine::Data &data)
133 QString interface = source.split('/')[2];
135 m_data[interface] << data["value"].toDouble();
136 if (m_data[interface].count() > 1) {
137 Plasma::SignalPlotter *plotter = plotters()[interface];
138 if (plotter) {
139 plotter->addSample(m_data[interface]);
140 //plotter->addSample(QList<double>() << 50 << 25);
142 m_data[interface].clear();
146 void SM::Net::createConfigurationInterface(KConfigDialog *parent)
148 QWidget *widget = new QWidget();
149 ui.setupUi(widget);
150 m_model.clear();
151 m_model.setHorizontalHeaderLabels(QStringList() << i18n("Network interface"));
152 QStandardItem *parentItem = m_model.invisibleRootItem();
155 foreach (const QString& interface, m_interfaces) {
156 QString ifname = interface.split('/')[2];
157 QStandardItem *item1 = new QStandardItem(ifname);
158 item1->setEditable(false);
159 item1->setCheckable(true);
160 item1->setData(interface);
161 if (items().contains(interface)) {
162 item1->setCheckState(Qt::Checked);
164 parentItem->appendRow(QList<QStandardItem *>() << item1);
166 ui.treeView->setModel(&m_model);
167 ui.treeView->resizeColumnToContents(0);
168 ui.intervalSpinBox->setValue(interval() / 1000);
170 parent->addPage(widget, i18n("Interfaces"), "network-workgroup");
171 connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
172 connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
175 void SM::Net::configAccepted()
177 KConfigGroup cg = config();
178 QStandardItem *parentItem = m_model.invisibleRootItem();
180 clearItems();
181 for (int i = 0; i < parentItem->rowCount(); ++i) {
182 QStandardItem *item = parentItem->child(i, 0);
183 if (item) {
184 if (item->checkState() == Qt::Checked) {
185 appendItem(item->data().toString());
189 cg.writeEntry("interfaces", items());
191 uint interval = ui.intervalSpinBox->value();
192 cg.writeEntry("interval", interval);
193 interval *= 1000;
194 setInterval(interval);
195 emit configNeedsSaving();
196 connectToEngine();
199 void SM::Net::setDetail(Detail detail)
201 foreach (const QString& key, plotters().keys()) {
202 plotters().value(key)->setShowLabels(detail == SM::Applet::High);
203 //plotters().value(key)->setShowTopBar(detail == SM::Applet::High);
204 //plotters().value(key)->setShowVerticalLines(detail == SM::Applet::High);
205 plotters().value(key)->setShowHorizontalLines(detail == SM::Applet::High);
209 #include "net.moc"