This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[RESOLVED] Plasmoid in PyQt - how config works?

Tags: None
(comma "," separated)
ophys
Registered Member
Posts
8
Karma
0
hi! i'm writing my first plasmoid. it's an alternative shutdown plasmoid.
My problem is that i can't figure out how config works. i'm trying to using it copying from another plasmoid.

when i click on the "OK" button in the configuration interface of the plasmoid it should store the config, but it doesn't.
there's the code. i highlighted the probably wrong lines with "!!!!"
i'm sorry for the confused code, i'll rewrite it once end
...and sorry for my english

main.py
Code: Select all
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
from PyKDE4.plasmascript import Applet
from Appearance import Appearance
from PyKDE4.kdecore import *
from PyKDE4.kdeui import *
from PyKDE4.kio import *
from PyKDE4 import kdecore
from PyKDE4 import kdeui
from PyQt4 import QtCore, QtGui
import dbus
import os



class ShutDown(Plasma.PushButton):
  bus = dbus.SystemBus()
  man = bus.get_object('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')
  @pyqtSignature("send()")
  def send(self, fax=None):
          self.man.Stop(dbus_interface='org.freedesktop.ConsoleKit.Manager')       
         
  def __init__(self, applet, parent):
        Plasma.PushButton.__init__(self, applet)
        self.plasmoid=parent
        self.setText("TurnOff")
        self.connect(self, SIGNAL("clicked()"), self, SLOT("send()"))



   
       
class LogOut(Plasma.PushButton):
  bus = dbus.SystemBus()
  man = bus.get_object('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')
  @pyqtSignature("send()")
  def send(self, fax=None):
        os.system("qdbus org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout 0 0 0")

 
 
  def __init__(self, applet, parent):
        Plasma.PushButton.__init__(self, applet)
        self.plasmoid=parent
        self.setText("LogOut")
        self.connect(self, SIGNAL("clicked()"), self, SLOT("send()"))           

class Reboot(Plasma.PushButton):
  bus = dbus.SystemBus()
  man = bus.get_object('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')
  @pyqtSignature("send()")
  def send(self, fax=None):
        self.man.Restart(dbus_interface='org.freedesktop.ConsoleKit.Manager')
         
  def __init__(self, applet, parent):
        Plasma.PushButton.__init__(self, applet)
        self.plasmoid=parent
        self.setText("Reboot")
        self.connect(self, SIGNAL("clicked()"), self, SLOT("send()"))   

class Suspend(Plasma.PushButton):
  bus = dbus.SystemBus()
  man = bus.get_object('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')
  @pyqtSignature("send()")
  def send(self, fax=None):
          self.pow.Suspend(dbus_interface='org.freedesktop.UPower')   
         
  def __init__(self, applet, parent):
        Plasma.PushButton.__init__(self, applet)
        self.plasmoid=parent
        self.setText("Suspend")
        self.connect(self, SIGNAL("clicked()"), self, SLOT("send()"))
               

     
class TurnOffApplet(plasmascript.Applet):
 
  def __init__(self,parent,args=None):
        plasmascript.Applet.__init__(self,parent)
 
  def init(self):
        self.conf=self.config('shutup')
        self.setHasConfigurationInterface(True)
        self.setAspectRatioMode(Plasma.Square)

        self.theme = Plasma.Svg(self)
        self.theme.setImagePath("widgets/background")
        self.setBackgroundHints(Plasma.Applet.DefaultBackground)
 
        self.layout = QGraphicsLinearLayout(Qt.Horizontal, self.applet)
        self.applet.setLayout(self.layout)
        self.resize(450,125)
        self.layout.addItem(ShutDown(self.applet, self))
        self.layout.addItem(LogOut(self.applet, self))
        self.layout.addItem(Reboot(self.applet, self))
        self.layout.addItem(Suspend(self.applet, self))
 
   !!!!     self.ShutDown_check=bool(self.conf.readEntry("ShutDown", True).toBool())

 
  def createConfigurationInterface(self, parent):
        self.Config = Appearance(parent)

        page = parent.addPage(self.Config,"Appearence")
        page.setIcon(KIcon("preferences-desktop-color"))
       

  def showConfigurationInterface(self):
        dialog = KPageDialog()
        appearance = Appearance(dialog)
        dialog.setFaceType(KPageDialog.List)
        dialog.setButtons( KDialog.ButtonCode(KDialog.Ok |KDialog.Cancel) )
        dialog.resize (450,400)
       
    !!!!    self.ShutDown_check=bool(self.conf.readEntry("ShutDown", True).toBool())
     !!!!   appearance.ShutDown.setChecked(self.ShutDown_check)
       
       
        self.createConfigurationInterface(dialog)
        dialog.exec_()
       
        if dialog.result()==QDialog.Accepted:
           self.ShutDown_check=bool(appearance.ShutDown.isChecked())
           self.saveConfig()

  def saveConfig(self):
    !!!!    self.conf.writeEntry("ShutDown", self.ShutDown_check)

def CreateApplet(parent):
   return TurnOffApplet(parent)     



Appearance.py
Code: Select all
# -*- coding: utf-8 -*-
from PyQt4.QtGui import QWidget
from AppearanceUi import Appearance_Dialog

class Appearance(QWidget,Appearance_Dialog):

    def __init__(self,parent):
        QWidget.__init__(self)
        self.parent = parent
        self.setupUi(self)


AppearanceUi.py
Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyKDE4 import kdecore
from PyKDE4 import kdeui
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os

class Appearance_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(242, 217)
        self.ShutDown = QtGui.QCheckBox(Dialog)
        self.ShutDown.setGeometry(QtCore.QRect(20, 30, 201, 21))
        self.ShutDown.setObjectName("ShutDown")
        self.Reboot = QtGui.QCheckBox(Dialog)
        self.Reboot.setGeometry(QtCore.QRect(20, 110, 181, 21))
        self.Reboot.setChecked(True)
        self.Reboot.setObjectName("Reboot")
        self.Logout = QtGui.QCheckBox(Dialog)
        self.Logout.setGeometry(QtCore.QRect(20, 70, 171, 23))
        self.Logout.setChecked(True)
        self.Logout.setObjectName("Logout")
        self.Suspend = QtGui.QCheckBox(Dialog)
        self.Suspend.setGeometry(QtCore.QRect(20, 150, 171, 21))
        self.Suspend.setChecked(True)
        self.Suspend.setObjectName("Suspend")
        self.retranslateUi(Dialog)
       
    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(kdecore.i18n("Dialog"))
        self.ShutDown.setText(kdecore.i18n("Show ShutDown button"))
        self.Reboot.setText(kdecore.i18n("Show Reboot button"))
        self.Logout.setText(kdecore.i18n("Show Logout button"))
        self.Suspend.setText(kdecore.i18n("Show Suspend button"))

Last edited by ophys on Tue Jun 15, 2010 12:37 pm, edited 2 times in total.
User avatar
ivan
KDE Developer
Posts
918
Karma
14
OS
It doesn't write the config in a sense that you don't see it in the config file, or in a sense that the next time you call readEntry(...) you get the old value?

If it is the former, it just means that config hasn't synced yet. If you want to sync it manually, call conf.sync() (though, I'm not sure it is available via Python bindings)


Image
ophys
Registered Member
Posts
8
Karma
0
ivan wrote:It doesn't write the config in a sense that you don't see it in the config file, or in a sense that the next time you call readEntry(...) you get the old value?

in the config file i see always the same value
Code: Select all
[Containments][1][Applets][52][Configuration][shutup]
ShutDown=true

whatever i click on the checkbox or not the value is always true.
i don't know why..
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Just wondering, why are you creating the configuration dialog yourself?

http://purinchu.net/kdelibs-apidocs/pla ... 67d6781669


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
ophys
Registered Member
Posts
8
Karma
0
I don't know... I'm copying from another plasmoid :D
could it cause the problem?

when the plasmoid will be ready i'll edit it according to the standards
User avatar
ivan
KDE Developer
Posts
918
Karma
14
OS
You didn't really answer my question - are you checking the value only via the config file or readEntry() returns the old value?

p.s. Fixed typos in the title


Image
ophys
Registered Member
Posts
8
Karma
0
ivan wrote:You didn't really answer my question - are you checking the value only via the config file or readEntry() returns the old value?


I simply open the config file with kate, and as I said it's always the same
value. So also readEntry returns always the same value. i think the error is in the writing process of the entry.
User avatar
ivan
KDE Developer
Posts
918
Karma
14
OS
> so also readEntry returns always the same value.

This is not a conclusion you can make - KConfig caches values and doesn't automatically save them to the disk - that is why I asked the question in the first place.

You should put some debugging code before writeEntry - to see whether it is being invoked at all.


Image
ophys
Registered Member
Posts
8
Karma
0
i think you're right.Maybe the problem is this:
Code: Select all
 self.ShutDown_check=bool(appearance.ShutDown.isChecked())

it returns always true, whatever i click on the checkbox or not.
( I thought I didn't use writeEntry in the right way)

you can notice it
Code: Select all
     if dialog.result()==QDialog.Accepted:
           self.ShutDown_check=bool(appearance.ShutDown.isChecked())
           if self.ShutDown_check== True:
              os.system("konsole")
           else:
         os.system("firefox")

it always start konsole.

but why it's always true?
miss the code some Qt signal to work?



that the entire main.py.

Code: Select all
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
from PyKDE4.plasmascript import Applet
from Appearance import Appearance
from PyKDE4.kdecore import *
from PyKDE4.kdeui import *
from PyKDE4.kio import *
from PyKDE4 import kdecore
from PyKDE4 import kdeui
from PyQt4 import QtCore, QtGui
import dbus
import os



class ShutDown(Plasma.PushButton):
  bus = dbus.SystemBus()
  man = bus.get_object('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')
  @pyqtSignature("send()")
  def send(self, fax=None):
          self.man.Stop(dbus_interface='org.freedesktop.ConsoleKit.Manager')       
         
  def __init__(self, applet, parent):
        Plasma.PushButton.__init__(self, applet)
        self.plasmoid=parent
        self.setText("TurnOff")
        self.connect(self, SIGNAL("clicked()"), self, SLOT("send()"))



   
       
class LogOut(Plasma.PushButton):
  bus = dbus.SystemBus()
  man = bus.get_object('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')
  @pyqtSignature("send()")
  def send(self, fax=None):
        os.system("qdbus org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout 0 0 0")

 
 
  def __init__(self, applet, parent):
        Plasma.PushButton.__init__(self, applet)
        self.plasmoid=parent
        self.setText("LogOut")
        self.connect(self, SIGNAL("clicked()"), self, SLOT("send()"))           

class Reboot(Plasma.PushButton):
  bus = dbus.SystemBus()
  man = bus.get_object('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')
  @pyqtSignature("send()")
  def send(self, fax=None):
        self.man.Restart(dbus_interface='org.freedesktop.ConsoleKit.Manager')
         
  def __init__(self, applet, parent):
        Plasma.PushButton.__init__(self, applet)
        self.plasmoid=parent
        self.setText("Reboot")
        self.connect(self, SIGNAL("clicked()"), self, SLOT("send()"))   

class Suspend(Plasma.PushButton):
  bus = dbus.SystemBus()
  man = bus.get_object('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')
  @pyqtSignature("send()")
  def send(self, fax=None):
          self.pow.Suspend(dbus_interface='org.freedesktop.UPower')   
         
  def __init__(self, applet, parent):
        Plasma.PushButton.__init__(self, applet)
        self.plasmoid=parent
        self.setText("Suspend")
        self.connect(self, SIGNAL("clicked()"), self, SLOT("send()"))
               

     
class TurnOffApplet(plasmascript.Applet):
 
  def __init__(self,parent,args=None):
        plasmascript.Applet.__init__(self,parent)
 
  def init(self):
        self.conf=self.config('shutup')
        self.setHasConfigurationInterface(True)
        self.setAspectRatioMode(Plasma.Square)

        self.theme = Plasma.Svg(self)
        self.theme.setImagePath("widgets/background")
        self.setBackgroundHints(Plasma.Applet.DefaultBackground)
 
        self.layout = QGraphicsLinearLayout(Qt.Horizontal, self.applet)
        self.applet.setLayout(self.layout)
        self.resize(450,125)
        self.layout.addItem(ShutDown(self.applet, self))
        self.layout.addItem(LogOut(self.applet, self))
        self.layout.addItem(Reboot(self.applet, self))
        self.layout.addItem(Suspend(self.applet, self))
 
        self.ShutDown_check=bool(self.conf.readEntry("ShutDown", True).toBool())

 
  def createConfigurationInterface(self, parent):
        self.Config = Appearance(parent)

        page = parent.addPage(self.Config,"Appearence")
        page.setIcon(KIcon("preferences-desktop-color"))
       

  def showConfigurationInterface(self):
        dialog = KPageDialog()
        appearance = Appearance(dialog)
        dialog.setFaceType(KPageDialog.List)
        dialog.setButtons( KDialog.ButtonCode(KDialog.Ok |KDialog.Cancel) )
        dialog.resize (450,400)
        self.conf.sync()
        self.ShutDown_check=bool(self.conf.readEntry("ShutDown", True).toBool())
        appearance.ShutDown.setChecked(self.ShutDown_check)
       
       
        self.createConfigurationInterface(dialog)
        dialog.exec_()
       
        if dialog.result()==QDialog.Accepted:
           self.ShutDown_check=bool(appearance.ShutDown.isChecked())
           if self.ShutDown_check== True:
              os.system("konsole")
           else:
         os.system("firefox")
             
         
#          self.saveConfig()

  def saveConfig(self):
        self.conf.writeEntry("ShutDown", self.ShutDown_check)
        self.conf.sync()

def CreateApplet(parent):
   return TurnOffApplet(parent)     
ophys
Registered Member
Posts
8
Karma
0
resolved
I delete createConfigurationInterface() function and modified showConfigurationInterface
Code: Select all
  def showConfigurationInterface(self):
        dialog = KPageDialog()
        dialog.setFaceType(KPageDialog.List)
        dialog.setButtons( KDialog.ButtonCode(KDialog.Ok |KDialog.Cancel) )
        dialog.resize (450,400)
        appearance = Appearance(dialog)       
        appearance.ShutDown.setChecked(self.ShutDown_check)
        p = dialog.addPage(appearance, i18n("Appearance"))
        p.setIcon(KIcon("preferences-desktop-color"))
        dialog.exec_()
        if dialog.result()==QDialog.Accepted:
         self.ShutDown_check=bool(appearance.ShutDown.isChecked())
         if self.ShutDown_check== True:
            os.system("konsole")
         else:
            os.system("firefox")
         self.saveConfig()


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]