Development/Tutorials/Plasma/JavaScript/ConfigDialog

Jump to: navigation, search

Once you have created your plasmoid, you will need to do the following to add a Config dialog:

  • Add a ui/config.ui file. You can use qtdesigner or edit the XML file if you are an advanced developer.
  • Add an event listener function to the "ConfigChanged" event.
  • Create an XML config file into your config directory. The default is config/main.xml. See [1] for more information.

The config.ui File

You'll need this tips before creating the configuration dialog's user interface:

  • qtdesigner is a good tool for editing this files without typing XML. Like a WYSIWYG editor.
  • You don't have to add an "accept", "apply" nor a "cancel" button, those will be added automatically.
  • Any configuration entry's name must have a "kcfg_" prefix.

Entries can be of type boolean, string, integer, etc.

The Event Listener Function

Typically, is correct to create a new javascript file called "code/config.js" and then is imported using the import command.

This JS file should have all the functions necessary to manage the configuration of your plasmoid and knows what to do when one of these events happen:

  • is the first time the app starts (so default configurations is used)
  • the users changes the configuration by clicking "Accept" or "Apply" button.

And it should have a function that is called when the the configurations is changed (by one of the two event stated). Let's call that function configChanged().


Finally, you have to tell to the plasmoid object that the configChanged() function is the one it have to call when one event need to change the configuration. For that, you can add this QML component into your main QML file:

1     Component.onCompleted: {
2         plasmoid.addEventListener('ConfigChanged', Config.changed)
3     }

The Component.onCompleted is a QML signal that is called as soon as the QML objects are loaded, and is used for "startups" scripts (see Component documentation).

See plasmoid object's callbacks for the addEventListener() function.

The XML Config Values File

This file will have the default values and its entry names for the configuration. You may like to see the [2] page for more information about these files.

Remember that for each "kcfg_" objects names at the ui/config.ui file should match the entry names of this config values files (without the prefix).

Example

At the ui/config.ui file I defined the following:

  • Just a QLineEdit object. Its objectName property is setted to kcfg_textconfig.

At the ui/main.qml I have the following:

 1 import QtQuick 1.1
 2 import org.kde.plasma.components 0.1 as PComp
 3 import '../code/Config.js' as Config
 4 
 5 Item {
 6 
 7     ...
 8 
 9     PComp.Label {
10             id: resp
11             text: i18n("")
12     }
13     Component.onCompleted: {
14         plasmoid.addEventListener('ConfigChanged', Config.configChanged)
15     }
16 }

At the code/Config.js I defined the following:

1 function configChanged(){
2     // Not necessary, main.xml is the default if no activeConfig is setted:
3     plasmoid.activeConfig = "main"; 
4     resp.text = "Config changed to...";
5     var text = plasmoid.readConfig("configtext");
6     resp.text = resp.text + text;
7 }

At the config/main.xml I wrote the following:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
 3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4       xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
 5       http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
 6   <kcfgfile name=""/>
 7 
 8   <group name="General">
 9     <entry name="configtext" type="String">
10       <label>This is a label.</label>
11       <default>hi world</default>
12     </entry>
13   </group>
14 
15 </kcfg>

Useful Links


This page was last modified on 25 February 2015, at 05:38. Content is available under Creative Commons License SA 4.0 unless otherwise noted.