Tag Archives: declarative

Declarative Knowledge Base

BlaBla

As I described in the latest entry, with the KDE Plasma Workspace 4.6 there will be a new feature that will be a key one for the future evolution of the Plasma platform: the ability to write plasmoids with just QML and Javascript.

So, let’s try to see if it’s possible rewrite the average widget in with the declarative scriptengine…

We have a dataengine (using a neat library called Attica) that can query the various functionalities of websites that offer an API compatible with the Open Collaboration Services, such as OpenDesktop.org. One of the features offered by this API is the Knowledge Base: users subscribed to the site can ask any question (in topic with the website) and other users can answer to them. Plasma offers a desktop widget that can query and visualize those question/answers. It’s written in c++ and is about 400-500 lines of code.

How much QML code would take to write a very minimal Declarative Plasmoid that can access the knowledge base entries trough the Plasma dataengine? Here it is

import Qt 4.7
import org.kde.plasma.core 0.1 as PlasmaCore

ListView {
    clip: true
    width: 200
    height: 300

    PlasmaCore.DataSource {
        id: source
        engine: "ocs"
        source: "KnowledgeBaseList\provider:https://api.opendesktop.org/v1/\query:opendesktop\sortMode:new\page:0\pageSize:10"
        interval: 120000
    }

    model: PlasmaCore.DataModel {
        dataSource: source
        key: "KnowledgeBase-[\d]*"
    }

    delegate: Text {
        text: Name
    }
}

24 lines 😉

What is important to look here is the DataSource{} definition that defines to what engine we’re connecting and to what source. And the DataModel{} that hooks up what has been fetched by the DataSource to a suitable model for the use by the ListView.

Let’s expand from this to something that can be compared to the C++ version in terms of functionality. You can download an early example of the code from here. (all is available from the playground svn repo). It’s still early to include it in the 4.6 release for various reasons, but the path is definitely clear 🙂

Here you can see a brief screencast of the two plasmoids, the C++ and the declarative one working side by side.

OGG version

Here is a video tutorial that explains the steps taken to write this plasmoid.

It is also a little demo of the Plasmate IDE that while it’s still at early development stages, it’s pretty impressive already: all you have to do is write the code and test it in the preview side panel. zero worries about creating the package structure, desktop files or installing :p

OGG version