In a previous post I explained how to program a simple plasmoid in QML. Now it’s time to move forward and let the user configure some parameters.
In a plasmoid, a git part of the infrastructure needed for configuration is already provided by KDE, so we have to provide just the specific to our plasmoid: Basically a configuration widget were the user can enter her preferences and a xml-formatted configuration file. Everything else , like read/write from/to disk, display configuration widow, etc is done by KDE.
So, let’s start with an example: A plasmoid I wrote called wmip. For all of you interested, all the stuff is available here.. This plasmoid displays your current IP address, ISP name and physical location. In the very first version, I got as feedback that the fonts being black made it almost unreadable on dark themes, so it was clear that if I wmip shall be more than a nice loose of time, the font color has to be configurable. So I did, and added also configuration of the font size and the possibility of turning the background off. Finally I decided to give the user control of the refresh timer as well; that is, after how many minutes the script has to fetch the IP address and other data again.
So basically, these are my configuration parameters:
Font size
Font color
Background y/n
Refresh rate
I recomend having a look the my post [How to write a small plasmoid in QML for KDE desktop] (http://juanmanueldehoyos.com/how-to-write-a-small-plasmoid-fo/) just in case you are not familiar with QML.
First of all we need a configuration file in XML format according to what KDE expect.
We see here each parameter inside a section. The types are basic ones from javascript. The default value is the value contained by those parameter at the beginning. No surprises here.
Now that we have our configuration file, we save it as main.xml inside a directory called config.I’ll cover the directory structure later in this post.
Now we need a widget for the configuration. For this I used the design view of QtCreator to create a file I named config.ui and put it under a directory called ui, together with the script main.qml.
The file looks like this:
You are free to copy&paste it and open it with QtCreator. But the interesting point here is the link between the entries we configured previously in main.xml and the controls defined in config.ui that are suppose to read/write them. The key in in the name: In config.ui, the controls are named kcfg_. So the widget that controls the paramter *fontSize* is called kcfg_fontSize. We respect this naming convention and KDE puts the magic.
OK, we have a configuration file and the UI file to handle it. Now what?
Now we can make use of the configured values in our scrip main.qml.
We need a global object plasmoid whose properties become very handy to access the KDE infrastructure, in our case, the configuration system. In order to get the object, the following include is needed:
Just as a matter of order, I declared some variables to store the values:
An I declared a function to read all the parameter:
By invoking plasmoid.readConfig() we get the corresponding value. But when do we invoke configChanged()? Yes, you guessed right: When the configuration changes. For that we need to attach this function to the signal configChanged emitted by KDE each time the user updates the configuration.
And that’s all we need. Just to give the whole picture, that’s how main.qml looks like:
I promised to give a hint about the directory layout. In my case it looks like: