Beispiel #1
0
    def createConfigurationInterface(self, dlg):
        self.configPage = ConfigPage(None, self.applet)
        self.configPage.setData(self.cfg)
        dlg.addPage(self.configPage, i18n('General'), 'applications-utilities')

        self.connect(dlg, SIGNAL('applyClicked()'), self, SLOT('configAccepted()'))
        self.connect(dlg, SIGNAL('okClicked()'), self, SLOT('configAccepted()'))
Beispiel #2
0
class AnalogMeter(Applet):
    def __init__(self, parent, args = None):
        Applet.__init__(self, parent)
        self.cfg = {}
        self.halfSecondSource = False

    def init(self):
        # To find ui files from package dir
        UiHelper.applet = self.applet

        self.setAspectRatioMode(Plasma.Square)
        cg = self.config()
        self.cfg['header'] = U(cg.readEntry('header', '{value:1.1f} {unit}'))
        self.cfg['font'] = QFont()
        self.cfg['font'].fromString(
                U(cg.readEntry('font', 'Sans Serif,8,-1,5,50,0,0,0,0,0')))
        self.cfg['fontcolor'] = QColor(U(cg.readEntry('fontcolor', '#000000')))
        self.cfg['interval'] = I(cg.readEntry('interval', '60000'))
        self.cfg['min'] = F(cg.readEntry('min', '0.0'))
        self.cfg['max'] = F(cg.readEntry('max', '100.0'))
        self.cfg['autorange'] = cg.readEntry('autorange', 'true').toBool()
        try:
            self.cfg['source'] = eval(U(cg.readEntry('source', '')))
            check(isinstance(self.cfg['source'], dict))
        except:
            self.cfg['source'] = None

        self.smengine = self.applet.dataEngine('systemmonitor')
        self.allSystemmonitorSources = self.smengine.sources()
        self.activeSystemmonitorSources = self.parseSystemmonitorSources()
        self.createMeter()
        self.connect(self.smengine, SIGNAL('sourceAdded(const QString&)'), self.sourceAdded)
        self.connect(self.smengine, SIGNAL('sourceRemoved(const QString&)'), self.sourceRemoved)

    def sourceAdded(self, name):
        self.allSystemmonitorSources.append(name)
        if name in self.activeSystemmonitorSources:
            self.connectSystemMonitorSource(name)

    def connectSystemMonitorSource(self, name):
        if name in self.allSystemmonitorSources:
            # Hack for correctly update 'systemmonitor' dataengine
            self.halfSecondSource = True
            self.smengine.connectSource(name, self, 500)

    def sourceRemoved(self, name):
        self.allSystemmonitorSources.removeAll(name)

    def parseSystemmonitorSources(self):
        result = []
        if self.cfg['source'] and self.cfg['source']['dataengine'] == 'systemmonitor':
            result.append(self.cfg['source']['source'])
        return result

    def createMeter(self):
        layout = self.applet.layout()
        if layout is None:
            layout = QGraphicsLinearLayout(Qt.Vertical, self.applet)
            layout.setContentsMargins(0, 0, 0, 0)
            layout.setSpacing(0)
        while layout.count() > 0:
            item = layout.itemAt(0)
            layout.removeAt(0)
            del item
        if self.cfg['source'] == None:
            self.setConfigurationRequired(True, i18n('Source not configured'))
            return
        self.setConfigurationRequired(False, '')

        theme = Plasma.Theme.defaultTheme()
        self.meter = Plasma.Meter(self.applet)
        self.meter.setMeterType(Plasma.Meter.AnalogMeter)
        self.meter.setLabel(1, '')
        self.meter.setLabelColor(1, self.cfg['fontcolor'])
        self.meter.setLabelAlignment(1, Qt.AlignCenter)
        self.meter.setLabelFont(1, self.cfg['font'])
        if self.cfg['autorange']:
            self.meter.setMinimum(MAX)
            self.meter.setMaximum(MIN)
        else:
            self.meter.setMinimum(self.cfg['min'])
            self.meter.setMaximum(self.cfg['max'])
        layout.addItem(self.meter)
        c = self.cfg['source']
        self.valueName = QString(c['value'])
        self.minName = QString(c['min'])
        self.maxName = QString(c['max'])
        self.unitName = QString(c['unit'])
        if c['dataengine'] == 'systemmonitor':
            self.connectSystemMonitorSource(c['source'])
        else:
            self.dataEngine(c['dataengine']).connectSource(c['source'], self, self.cfg['interval'])

    @pyqtSignature("dataUpdated(const QString &, const Plasma::DataEngine::Data &)")
    def dataUpdated(self, sourceName, data):
        #print data
        if self.valueName in data:
            # Hack for correctly update 'systemmonitor' dataengine ---------------------
            if self.halfSecondSource:
                self.halfSecondSource = False
                c = self.cfg['source']
                #self.dataEngine(c['dataengine']).disconnectSource(c['source'], self)
                self.dataEngine(c['dataengine']).connectSource(c['source'], \
                                self, self.cfg['interval'])
            # --------------------------------------------------------------------------
            if self.cfg['autorange'] and F(data[self.minName]) != F(data[self.maxName]):
                if self.meter.minimum() != F(data[self.minName]):
                    self.meter.setMinimum(F(data[self.minName]))
                if self.meter.maximum() != F(data[self.maxName]):
                    self.meter.setMaximum(F(data[self.maxName]))
            elif self.cfg['autorange']:
                if self.meter.minimum() > F(data[self.valueName]):
                    self.meter.setMinimum(F(data[self.valueName]))
                if self.meter.maximum() < F(data[self.valueName]):
                    self.meter.setMaximum(F(data[self.valueName]))
                print self.meter.minimum(), self.meter.maximum()

            self.meter.setValue(F(data[self.valueName]))
            try:
                s = self.cfg['header'].format(value = F(data[self.valueName]),
                                             max = F(data[self.maxName]),
                                             min = F(data[self.minName]),
                                             unit = U(data[self.unitName]),
                                             name = self.cfg['source']['name'])
            except:
                s = i18n('error')
            self.meter.setLabel(1, s)

    @pyqtSignature("configAccepted()")
    def configAccepted(self):
        cg = self.config()
        self.cfg = self.configPage.data()
        cg.writeEntry('header', self.cfg['header'])
        cg.writeEntry('autorange', self.cfg['autorange'])
        cg.writeEntry('min', self.cfg['min'])
        cg.writeEntry('max', self.cfg['max'])
        cg.writeEntry('font', U(self.cfg['font']))
        cg.writeEntry('fontcolor', U(self.cfg['fontcolor']))
        cg.writeEntry('source', repr(self.cfg['source']))
        cg.writeEntry('interval', repr(self.cfg['interval']))
        self.activeSystemmonitorSources = self.parseSystemmonitorSources()
        self.createMeter()

    def createConfigurationInterface(self, dlg):
        self.configPage = ConfigPage(None, self.applet)
        self.configPage.setData(self.cfg)
        dlg.addPage(self.configPage, i18n('General'), 'applications-utilities')

        self.connect(dlg, SIGNAL('applyClicked()'), self, SLOT('configAccepted()'))
        self.connect(dlg, SIGNAL('okClicked()'), self, SLOT('configAccepted()'))

    def constraintsEvent(self, constraints):
        if constraints & Plasma.FormFactorConstraint:
            self.setBackgroundHints(Plasma.Applet.NoBackground)