After my last blog I worked on YaST (what a surprise) and didn't have any time to post anything until now.
Meanwhile I read some chapters from the "Thinking in Java" book. There are some chapters about OOP in general and it brings me to idea, how it would be great in YaST. As a side effect it also means get away from ycp ;-). Thanks to UI independence I can use Python with the YaST UI.
Here's some code example:
widget_library.py:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import yui
class MainDialog:
def __init__(self):
self.factory = yui.YUI.widgetFactory()
self.dialog = self.factory.createMainDialog()
def __del__(self):
self.dialog.destroy()
class Popup:
def __init__(self):
self.factory = yui.YUI.widgetFactory()
self.dialog = self.factory.createPopupDialog()
def __del__(self):
self.dialog.destroy()
class TableDialog:
def __init__(self, d):
self.f = d.factory
vbox1 =self.f.createVBox( d.dialog )
self.f.createVStretch(vbox1)
hbox1 =self.f.createHBox( vbox1 )
self.f.createHStretch(hbox1)
self.f.createHSpacing(hbox1,1)
vbox2 =self.f.createVBox( hbox1 )
self.Table( vbox2 )
vspace =self.f.createVSpacing(vbox1,2)
self.f.createHSpacing(hbox1,1)
self.f.createHStretch(hbox1)
self.f.createVStretch(vbox1)
etype=-1
while etype!=5:
event = d.dialog.waitForEvent()
etype = event.eventType()
self.HandleEvent( event )
print etype
def Table(self,parent):
vbox3 =self.f.createVBox( parent )
self.customWidget(vbox3)
theader = yui.YTableHeader()
theader.addColumn("Targets")
self.table =self.f.createTable( vbox3, theader )
hbox2 =self.f.createHBox( vbox3 )
self.pbAdd =self.f.createPushButton( hbox2, "&Add" )
self.pbEdit =self.f.createPushButton( hbox2, "&Edit" )
self.pbDel =self.f.createPushButton( hbox2, "&Delete")
self.enableDisableButtons()
def customWidget(self,parent):
pass
def HandleEvent(self,event):
if (event.widget()==self.pbAdd):
self.handleAdd(event)
elif (event.widget()==self.pbEdit):
self.handleEdit(event)
elif (event.widget()==self.pbDel):
self.handleDel(event)
self.enableDisableButtons()
def enableDisableButtons(self):
self.pbEdit.setEnabled(self.table.itemsCount()>0)
self.pbDel.setEnabled(self.table.itemsCount()>0)
main.py:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import yui
from widget_library import *
class TargetDialog(TableDialog):
def __init__(self,d):
TableDialog.__init__(self,d)
def customWidget(self,parent):
hbox =self.f.createHBox( parent )
self.f.createInputField(hbox, "Target")
self.f.createInputField(hbox, "Identifier")
def handleAdd(self, event):
print "add target item"
def handleEdit(self,event):
print "edit target item ..."
def handleDel(self,event):
print "delete target item ..."
class MyTable(TableDialog):
def __init__(self,d):
TableDialog.__init__(self,d)
def handleAdd(self,event):
print "adding ..."
TargetDialog(Popup())
def handleEdit(self,event):
print "edit item ..."
def handleDel(self,event):
print "delete item ..."
if __name__== "__main__":
td = TargetDialog(MainDialog())
I know that code is not nice and not very useful, but here's some nice OOP examples:
inheritance, composition, etc ...
Easily put into constructor, what kind of dialog I want:
TableDialog(MainDialog()) or TargetDialog(Popup())
Inherit some class and override methods you want (see MyTable.handle*())
This is much better than generate file from template (as we do now).
I like this way and I'll keep working on it during my ITO.
Bye,
Michal
1 comment:
Nice! In case people want to try it out, they need to zypper in yast2-python-bindings python-yui
Also, I got AttributeError: TableDialog instance has no attribute 'handleAdd' so I replaced the very last line by td = TargetDialog(MainDialog())
Post a Comment