pythonqtkde-plasmaplasmoid

Saving config of a Python plasmoid


I managed to write a plasmoid using Python with a configuration dialog where one can select a file. I also managed to read out the selected value.

But how can I make the selection persistent? I'm pretty sure that there's some KDE/Qt predefined function or way to do it, but I didn't find documentation about this.

Here's my code (which can be started with plasma-windowed):

metadata.desktop:

[Desktop Entry]
Encoding=UTF-8
Name=Config Test
Type=Service
ServiceTypes=Plasma/Applet
X-Plasma-API=python
X-Plasma-MainScript=code/main.py

contents/code/main.py:

# -*- coding: utf-8 -*-

from PyQt4 import QtCore
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript

class configTest(plasmascript.Applet):

        def __init__(self, parent, args = None):
                plasmascript.Applet.__init__(self, parent)

        def init(self):
                self.setAspectRatioMode(Plasma.IgnoreAspectRatio)

        def paintInterface(self, painter, option, rect):
                painter.save()
                painter.setPen(QtCore.Qt.black)
                painter.drawText(rect, QtCore.Qt.AlignLeft, str(self.config('main').readEntry('testEntry')))
                painter.restore()

def CreateApplet(parent):
        return configTest(parent) 

contents/ui/config.ui:

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
 <class>Config</class> 
  <widget class="QWidget" name="verticalLayoutWidget"> 
   <layout class="QVBoxLayout" name="verticalLayout"> 
    <item> 
     <widget class="KUrlRequester" name="kcfg_testEntry"/> 
    </item> 
   </layout> 
  </widget> 
 <resources/> 
 <connections/> 
</ui>

contents/config/main.xml:

<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
                          http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
  <kcfgfile name="configTestrc"/>
  <include>kglobalsettings.h</include>
  <group name="main">
    <entry name="testEntry" type="Url"></entry>
  </group>
</kcfg>

Thanks in advance for all help!


Solution

  • Finally, I can answer this question myself. The above code does actually work and the configuration is stored automagically – as soon as the package has been installed (via plasmapkg) and started by the plasma desktop tools.

    This is not the case when the plasmoid is started via plasma-windowed.