Skip navigation.
KDE Developer's Journals

SuperKaramba and Plasma, Part 2

dipesh's picture

After providing last time a screenshot and a screencast (2.4MB, mpeg4) of SuperKaramba in action running the Aero AIO theme, here we go with a more concrete sample that connects SuperKaramba and Plasma together.

This Python sample script does use the Plasma TimeEngine to display the current local time - such kind of clock-examples will never end I guess Smiling

# Import the needed modules.
import karamba, PlasmaApplet
 
# Fetch the TimeEngine Plasma::DataEngine object.
engine = PlasmaApplet.dataEngine("time")

# We like to update it each second.
engine.setProperty("reportSeconds", True)
 
# Connect with the TimeEngine's "Local" source.
engine.connectSource("Local")
 
# This is the richedit widget we use to display something.
text = None
 
# This method just returns some text to display.
def getText():
    return "%s\n%s" % (PlasmaApplet.name(),engine.query("Local"))
 
# This method got called by SuperKaramba on initialization.
def initWidget(widget):
    global text
    karamba.resizeWidget(widget, 400, 400)
    text = karamba.createText(widget, 10, 10, 380, 380, getText())
    karamba.redrawWidget(widget)
 
# This method got called by SuperKaramba on update.
def widgetUpdated(widget):
    global text
    karamba.changeText(widget, text, getText())
    karamba.redrawWidget(widget)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aseigo's picture

some improvements?

some thoughts:

why not connect the Local source from the time engine to the text edit directly? and then you don't need to do the DataEngine::query (which means hitting the data twice in a row), the getText() method or widgetUpdated for that matter.

if you use a Plasma::LineEdit or Plasma::Label in there, you get this all for free to boot. and if you use a layout (e.g. VBoxLayout) you get nice management of the widget geometry.

you'll also save on the redrawWidget calls as those also happen for free. should cut your example down to .. (*counts*) .. 6 lines of code, 7 if you include the import line. that's less than half of the 15 lines in the example =)

so .. it would look something like:

engine = PlasmaApplet.dataEngine("time")
engine.setProperty("reportSeconds", True)
text = PlasmaLabel(this)
engine.connectSource("Local", text)
layout = PlasmaVBoxLayout(this)
layout.addItem(text)

dipesh's picture

re: some improvements?

This would need either to turn all the methods Plasma::Widget, Plasma::Label, etc. are providing into Q_PROPERTY/Q_SLOTS/Q_INVOKABLE or to write adaptors for all of them. My personal preference would be the first since it would save code duplications.

[edit:]
or shouldn't scripts be able to manipulate such widgets?

dipesh's picture

Done with commit r693790,

Done with commit r693790, following works now too (5 lines of code btw);

import PlasmaApplet
engine = PlasmaApplet.dataEngine("time")
engine.setProperty("reportSeconds", True)
widget = PlasmaApplet.widget("LineEdit")
engine.connectSource("Local", widget)
suslik's picture

Wow! You had me atimport

Wow! You had me at

import PlasmaApplet
engine = PlasmaApplet.dataEngine("time")

I wonder if it's possible to establish signal-slot relationship between time engine changing the second/minute and widgetNotify().

That way I could just use the callbacks to update the clock, instead of waking up all the time and checking the time.

dipesh's picture

probably something like

probably something like this;

import karamba, PlasmaApplet
[...]
def sourceUpdated(source,data):
    karamba.changeText(karamba, text, data["Time"])
    karamba.redrawWidget(karamba)
engine.connect("sourceUpdated(QString,QVariantMap)", sourceUpdated)

can do the job already. But I'll look more detailed at it within next days, maybe just a compatible updated() functionality for SK Eye-wink

dipesh's picture

Done with commit r694880,

Done with commit r694880, see the second sample at the tutorial Smiling

aseigo's picture

yes

Plasma::Applet::dataEngine(const QString&) really makes it easy to access data, that's for sure. it even takes care of ref'ing/deref'ing the engine behind the scenes so the engine gets memory managed properly even thought shared.

as for signal/slot between the engine and the display ... that's the whole point of the updated slot in all the Plasma::Widgets. see my comment above.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.