Development/Tutorials/Plasma4/Python/Writing DataEngines: Difference between revisions
(Building blocks for the tutorial) |
(Second block done) |
||
Line 63: | Line 63: | ||
return self.updateSourceEvent(name) | return self.updateSourceEvent(name) | ||
</code> | </code> | ||
The <tt>sources()</tt> function is called from applets to request what data sources a DataEngine has (for example, if your applet needs to connect to an online service, the DataEngine could offer a list of suitable services). In this case we first create a list with the "Local" name (which refers to the local time on your computer) and then we extend the list using <tt>KSystemTimeZones.zones().keys()</tt>. This adds the time zones known to KDE to the sources. As a final step, we return this list. | |||
<tt>sourceRequestEvent</tt> is a function that is called when the applet accesses the DataEngine and requests a specific source (<tt>name</tt>). This function is usually used to make preparations (for example, setting up a connection) before the data can be received. In this case, we simply return <tt>updateSourceEvent</tt> with the source name. |
Revision as of 09:05, 24 January 2009
Abstract
As you have seen in the using DataEngines tutorial, Plasma applets can make use of DataEngines to visualize data of many different kinds. In fact, the already available DataEngines offer many options for your applets. But what if you have a specific need, not covered by those?
The problem is easily solved by writing your own Plasma DataEngine, and this tutorial will show you how to create one.
Prerequisites
As with applets, DataEngines need the same directory structure (see the Getting Started tutorial), so the first step is to create the appropriate directories. The difference with respect to applets lies in the metadata.desktop file:
[Desktop Entry]
Name=Python Date and Time
Comment=Python Time data for Plasmoids
Type=Service
Icon=preferences-system-time
ServiceTypes=Plasma/DataEngine
X-Plasma-API=python
X-KDE-PluginInfo-Author=Simon Edwards
[email protected]
X-KDE-PluginInfo-Name=plasma-dataengine-pytime
X-KDE-PluginInfo-Version=1.0
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=Python Date and Time
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=LGPL
X-KDE-PluginInfo-EnabledByDefault=true
Take a look at the ServiceType line. When using applets, it was "Plasma/Applet", but since now we're dealing with DataEngines, its value is "Plasma/DataEngine".
Main script
For this tutorial we'll use the plasma-dataengine-pytime code, which is available on KDE's SVN. This DataEngine is an equivalent to the C++ time DataEngine we connected to in the using DataEngines tutorial. Its function is to display time and date with respect to the current timezone. You can view the main.py file online. As the file is rather long, we'll take a look at it in sections.
- Copyright stuff
from PyQt4.QtCore import *
from PyKDE4.kdecore import *
from PyKDE4 import plasmascript
class PyTimeEngine(plasmascript.DataEngine):
def __init__(self,parent,args=None):
plasmascript.DataEngine.__init__(self,parent)
def init(self):
self.setMinimumPollingInterval(333)
We start with the copyright information, followed by importing the required modules (PyQt4.QtCore and plasmascript). Since we're dealing with time and timezones, we also import kdecore to make use of what KDE offers.
In __init__ we just initialize the class. In init() instead, we set the minimum polling interval, that is the minimum time (in milliseconds) that needs to pass between one data request (from an applet, for example) and another.
def sources(self):
sources = ["Local"]
sources.extend(KSystemTimeZones.zones().keys())
return sources
def sourceRequestEvent(self, name):
return self.updateSourceEvent(name)
The sources() function is called from applets to request what data sources a DataEngine has (for example, if your applet needs to connect to an online service, the DataEngine could offer a list of suitable services). In this case we first create a list with the "Local" name (which refers to the local time on your computer) and then we extend the list using KSystemTimeZones.zones().keys(). This adds the time zones known to KDE to the sources. As a final step, we return this list.
sourceRequestEvent is a function that is called when the applet accesses the DataEngine and requests a specific source (name). This function is usually used to make preparations (for example, setting up a connection) before the data can be received. In this case, we simply return updateSourceEvent with the source name.