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

5 thoughts on “Declarative Knowledge Base

  1. thorGT

    YMMD and ZOMFG 😉

    Seriously, now I’m finally happy. Is it allowed to start porting to qml or is this all preview tech that hasn’t been even released? (I’m a bit off Plasma development now, study and new computer building, you know 😉 I’ll come back as soon as i can, I promise – if you remember, I asked (bugged!) you about item views in Plasma on IRC some time ago) )

    And now a more technical questoin – I see a lot of Plasma* stuff in there – is this what is going to be swapped for QtComponents in the (not so distant) future?

  2. Marco Martin

    yes, you can start playing around those stuff right now 🙂
    the QML support will be there starting from 4.6.

    the Plasma* stuff has been categorized in several components.
    all that is called PlasmaWidget.something could be replaced by things from QtComponents eventually

    PlasmaCore.* is out of scope from QtComponents (some of it is not even graphical, like DataSource)

  3. thorGT

    OK, so I better come to plasma-devel and start from there. Guess massive conversion is closing on us 🙂 Would be nice to get rid of custom code (hate it, really hate it)

  4. JR

    How much can be ported to use QML? Does it only concern geometrical shapes moving around, like animations? Or can KWin (perhaps KWin2) start using it, for effects if not for drawing windows?

  5. Marco Martin

    It concerns drawing graphics for a gui in the context of a window.
    a window manager, being multi window for its very nature makes this less useful, but whether this could be actually of help or not is something only KWin developers can answer 😉

Comments are closed.