aboutsummaryrefslogtreecommitdiffstats
path: root/plasma/applet/contents/code/main.py
blob: 744226bcb926865c45fdaccdc17c0b25d55a4df4 (plain)
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
# -*- coding: utf-8 -*-
# Net_monitor plasma interface
#
# Copyright, (C) Eugeni Dodonov <eugeni@mandriva.com>, 2011
#

from PyQt4.QtCore import Qt, QString, pyqtSignature
from PyQt4.QtGui import QGraphicsLinearLayout
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript

# localization
import gettext
try:
    gettext.install("net_monitor")
except IOError:
    _ = str

class NetMonitor(plasmascript.Applet):
    def __init__(self,parent,args=None):
        plasmascript.Applet.__init__(self,parent)

    def init(self):
        self.setHasConfigurationInterface(False)
        self.setAspectRatioMode(Plasma.Square)

        self.theme = Plasma.Svg(self)
        self.theme.setImagePath("widgets/background")
        self.setBackgroundHints(Plasma.Applet.DefaultBackground)

        self.layout = QGraphicsLinearLayout(Qt.Horizontal, self.applet)

        self.widgets = {}
        for source in self.connectToEngine():
            label = Plasma.Label(self.applet)
            label.setText("%s:" % source)
            self.layout.addItem(label)
            self.widgets[str(source)] = label
        self.applet.setLayout(self.layout)

    def connectToEngine(self):
        self.engine = self.dataEngine("net_monitor_data")
        self.sources = self.engine.sources()
        for source in self.sources:
            self.engine.connectSource(source, self, 1000)
        return self.sources

    @pyqtSignature("dataUpdated(const QString &, const Plasma::DataEngine::Data &)")
    def dataUpdated(self, sourceName, data):
        """Got something from data source"""
        iface = str(sourceName)
        if iface not in self.widgets:
            print "Error: data for %s not available yet" % iface
            return
        widget = self.widgets[iface]
        widget.setText("%s\nIn: %d\nOut: %d" % (sourceName, data[QString("data_in")], data[QString("data_out")]))

    def paintInterface(self, painter, option, rect):
        painter.save()
        painter.restore()
 
def CreateApplet(parent):
    return NetMonitor(parent)