Contents

Building

Next, you need to setup your plasmoid's development environment. For this example, our plasmoid will be called 'powerchart'. Make a directory to put everything in. Plasma expects a certain set of files and directories to exist when loading a plasmoid package:

/ - The root of the package

metadata.desktop - Metadata about the plasmoid

contents/ - The directory plasma looks in for all your resources

ui/ - 'ui' type resources (not covered here)

code/ - 'code' type resources

main.py - The plugin's code. You can change this in metadata.desktop.

To run it, try

plasmoidviewer [root of the package]

metadata.desktop

metadata.desktop contains the plasmoid's metadata such as its name, author, and scripting engine.

[Desktop Entry]
#Encoding of this file
Encoding=UTF-8
#The name to be shown in the 'Add Widgets' dialog
Name=Battery Graph
#Tells KDE this is provides the 'Plasma applet'
ServiceTypes=Plasma/Applet
#Tells KDE this is a service of sorts
Type=Service
#The icon to show in the 'Add Widgets' dialog
Icon=battery
#The language your plugin is written in
X-Plasma-API=python
#The main entrypoint for your plasmoid
X-Plasma-MainScript=code/main.pyX-KDE-PluginInfo-Author=John DoeX-KDE-PluginInfo-Email=j.doe@example.com
#The internal name of the plasmoid. Your plasmoid gets installed into ~/.kde/share/apps/plasma/<Name>/
X-KDE-PluginInfo-Name=powerchartX-KDE-PluginInfo-Version=pre0.1X-KDE-PluginInfo-Website=http://plasma.kde.org/X-KDE-PluginInfo-Category=ExamplesX-KDE-PluginInfo-Depends=X-KDE-PluginInfo-License=GPL
#Tells KDE if this plasmoid should be available by default, or if the user needs to jump through hoops to enable it.
X-KDE-PluginInfo-EnabledByDefault=true

Once you have that, you can start hacking away.

The Code

The first thing you do in a python file of course is import your dependencies:

The last import contains the bridge between your python code and the underlying C++ API. If you were writing this in C++, you'd inherit the Plasma::Applet class. It isn't as straightforward in Python though. There is some python wrapper code (in /usr/lib/python2.5/site-packages/PyKDE4/plasmascript.py) that needs to work some magic so your python script can access protected Plasma::Applet members.

The init() method is where you should put the majority of your initialization code such as creating widgets and loading data engines.

self.applet is the actual C++ Applet object your PowerChart class represents. When creating widgets and similar objects, you need to pass in self.applet instead of self when the good old fashioned C++ API reference says you need a QGraphicsWidget or Applet. If not, your applet will fail to load and you'll be puzzled why.

The code above is fairly self-explainatory as well. A basic layout is created, a plotter widget is added to it, a plot is added to the plotter, and some administrivia is performed. The next step is to connect to our dataengine.

As mentioned earlier, the plasmascript module contains some magic bridge code that lets you access protected members while python thinks you're outside the Applet class. With the odd exception of self.applet, everything else works like normal.

Above, we requested the soliddevice engine, used a solid predicate to search for battery devices, and told the engine we're interested in updates from that battery. When updates come along, your applet's dataUpdated method is called.

A problem with mixing Python with Qt's signals is the lack of type safety. To get around this, you need to manually declare the dataUpdated slot's signature with the pyqtSignature decorator. Another problem is the use of QStrings. It makes things take a little more typing but it doesn't add a whole lot more effort.

In our dataUpdated method, we find the data we want, pry it out of the Qt code, and put it in our plotter's chart. Other data can be found by poking around with plasmaengineexplorer.

Finishing up

Our applet class is all written. The last thing we need to do is tell plasma how to get our class. It expects our main code file to contain a CreateApplet method that returns our plasmascript.Applet class.

Two simple lines accomplish this:

def CreateApplet(parent):
return PowerChart(parent)

Packaging

Plasma packages are zip files that adhere to the expected directory structure explained at the start of this tutorial (link). You can call your file anything you want, but a good naming scheme is <Name>-<Version>.plasmoid eg powerchart-pre0.1.plasmoid. Remember while creating it, that the root of the package structure explained earlier means the root of the zip file.