00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 from Koo.Common import Common
00030 from Koo.Common import Icons
00031 from Koo.Common.Settings import *
00032 from Koo import Rpc
00033 
00034 from FormView import FormView, FormContainer, FormTabWidget
00035 from Koo.View.AbstractParser import *
00036 from Koo.Fields.FieldWidgetFactory import *
00037 from Koo.Fields.AbstractFieldWidget import *
00038 from PyQt4.QtCore import *
00039 from PyQt4.QtGui import *
00040 
00041 
00042 class FormParser(AbstractParser):
00043 
00044         def create(self, viewId, parent, viewModel, node, fields, filter=None):
00045                 self.viewModel = viewModel
00046                 self.filter = filter
00047                 self.widgetList = []
00048                 self.context = parent.context
00049                 
00050                 self.view = FormView( parent )
00051                 self.view.id = viewId
00052                 
00053                 self.unnamed = 0
00054                 
00055                 container, onWriteFunction = self.parse( node, fields )
00056                 self.view.setWidget( container )
00057                 self.view.setOnWriteFunction( onWriteFunction )
00058                 return self.view
00059 
00060         def parse(self, root_node, fields, notebook=None, container=None):
00061                 attrs = Common.nodeAttributes(root_node)
00062                 onWriteFunction = attrs.get('on_write', '')
00063 
00064                 if container == None :
00065                         parent = self.view
00066                         if notebook:
00067                                 parent = notebook
00068                         
00069                         
00070                         
00071                         container = FormContainer( parent, int(attrs.get('col',4)) )
00072                 
00073                 if not self.view.title:
00074                         self.view.title = attrs.get('string', 'Unknown')
00075 
00076                 for node in root_node.childNodes:
00077                         if not node.nodeType==node.ELEMENT_NODE:
00078                                 continue
00079                         attrs = Common.nodeAttributes(node)
00080                         if node.localName=='image':
00081                                 icon = QLabel(container)
00082                                 icon.setPixmap( Icons.kdePixmap(attrs['name']) ) 
00083                                 container.addWidget(icon, attrs)
00084 
00085                         elif node.localName=='separator':
00086                                 caption = attrs.get( 'string', '' )
00087 
00088                                 separator = QWidget( container )
00089                                 separator.setSizePolicy( QSizePolicy.Expanding, QSizePolicy.Fixed )
00090                                 label = QLabel( separator )
00091                                 label.setText( caption )
00092                                 font = label.font()
00093                                 font.setBold( True )
00094                                 label.setFont( font )
00095                                 line = QFrame( separator )
00096                                 line.setFrameShape( QFrame.HLine )
00097                                 line.setFrameShadow( QFrame.Plain )
00098                                 layout = QVBoxLayout( separator )
00099                                 layout.setAlignment( Qt.AlignTop )
00100                                 layout.setContentsMargins( 0, 0, 0, 0 )
00101                                 layout.setSpacing( 0 )
00102                                 layout.addWidget( label )
00103                                 layout.addWidget( line )
00104 
00105                                 self.view.addStateWidget( separator, attrs.get('attrs'), attrs.get('states') )
00106                                 container.addWidget( separator, attrs )
00107 
00108                         elif node.localName=='label':
00109                                 text = attrs.get('string', '')
00110                                 if not text:
00111                                         for node in node.childNodes:
00112                                                 if node.nodeType == node.TEXT_NODE:
00113                                                         text += node.data
00114                                                 else:
00115                                                         text += node.toxml()
00116                                 label = QLabel( text, container )
00117                                 label.setWordWrap( True )
00118                                 label.setSizePolicy( QSizePolicy.Preferred, QSizePolicy.Fixed )
00119                                 container.addWidget(label, attrs)
00120 
00121                         elif node.localName=='newline':
00122                                 container.newRow()
00123 
00124                         elif node.localName=='button':
00125                                 button = FieldWidgetFactory.create( 'button', container, self.view, attrs )
00126                                 if not self.isWidgetVisible( attrs ):
00127                                         continue
00128                                 name = attrs.get('name')
00129                                 if not name:
00130                                         name = 'unnamed_%d' % self.unnamed
00131                                         self.unnamed += 1
00132                                 self.view.widgets[name] = button
00133                                 self.view.addStateWidget( button, attrs.get('attrs'), attrs.get('states') )
00134                                 container.addWidget(button, attrs)
00135 
00136                         elif node.localName=='notebook':
00137                                 if not self.isWidgetVisible( attrs ):
00138                                         continue
00139                                 tab = FormTabWidget( container )
00140                                 if attrs and 'tabpos' in attrs:
00141                                         pos = { 
00142                                                 'up': QTabWidget.North,
00143                                                 'down':QTabWidget.South,
00144                                                 'left':QTabWidget.West,
00145                                                 'right':QTabWidget.East
00146                                         } [attrs['tabpos']]
00147                                 else:
00148                                         pos = {
00149                                                 'left': QTabWidget.West,
00150                                                 'top': QTabWidget.North,
00151                                                 'right': QTabWidget.East,
00152                                                 'bottom': QTabWidget.South
00153                                         } [Settings.value('koo.tabs_position')]
00154                                         
00155                                 tab.setTabPosition( pos )
00156 
00157                                 attrs['colspan'] = attrs.get('colspan', 3)
00158                                 
00159                                 container.addWidget(tab, attrs)
00160                                 
00161                                 
00162                                 
00163                                 
00164                                 
00165                                 _ , onWriteFunction = self.parse(node, fields, tab, container)
00166 
00167                         elif node.localName=='page':
00168                                 if not self.isWidgetVisible( attrs ):
00169                                         continue
00170                                 widget, onWriteFunction = self.parse(node, fields, notebook )
00171                                 
00172                                 
00173                                 widget.isTab = True
00174                                 self.view.addStateWidget( widget, attrs.get('attrs'), attrs.get('states') )
00175 
00176                                 notebook.addTab( widget, Common.normalizeLabel( attrs.get('string', '') ) )
00177 
00178                         elif node.localName =='hpaned':
00179                                 widget = QSplitter( Qt.Horizontal, container )
00180 
00181                                 container.addWidget(widget, attrs)
00182                                 self.parse( node, fields, widget, container)
00183 
00184                         elif node.localName =='vpaned':
00185                                 widget = QSplitter( Qt.Vertical, container )
00186 
00187                                 container.addWidget(widget, attrs)
00188                                 self.parse( node, fields, widget, container)
00189 
00190                         elif node.localName == 'child1':
00191                                 widget, onWriteFunction = self.parse( node, fields )
00192                                 notebook.addWidget( widget )
00193 
00194                         elif node.localName == 'child2':
00195                                 widget, onWriteFunction = self.parse( node, fields )
00196                                 notebook.addWidget( widget )
00197 
00198                         elif node.localName =='action':
00199                                 name = str(attrs['name'])
00200                                 widget = FieldWidgetFactory.create( 'action', container, self.view, attrs )
00201                                 attrs['colspan'] = attrs.get('colspan', 3)
00202                                 self.view.widgets[name] = widget 
00203                                 container.addWidget(widget, attrs)
00204 
00205                         elif node.localName=='field':
00206                                 if not self.isWidgetVisible( attrs ):
00207                                         continue
00208                                 name = attrs['name']
00209                                 del attrs['name']
00210                                 type = attrs.get('widget', fields[name]['type'])
00211                                 fields[name].update(attrs)
00212                                 fields[name]['model'] = self.viewModel
00213 
00214                                 
00215                                 widget = FieldWidgetFactory.create( type, container, self.view, fields[name] )
00216                                 if not widget:
00217                                         continue
00218 
00219                                 fields[name]['name'] = name
00220                                 if self.filter:
00221                                         widget.node = node
00222                                         self.widgetList.append(widget)
00223 
00224 
00225                                 label = None
00226                                 if not int(attrs.get('nolabel', 0)):
00227                                         label = fields[name]['string']+' :'
00228                                         
00229                                 self.view.widgets[name] = widget
00230                                 
00231 
00232                                 if not 'help' in attrs:
00233                                         attrs['help'] = fields[name].get('help', False)
00234 
00235                                 container.addWidget(widget, attrs, label)
00236 
00237                         elif node.localName=='group':
00238                                 if not self.isWidgetVisible( attrs ):
00239                                         continue
00240                                 widget, onWriteFunction = self.parse( node, fields, notebook )
00241                                 if 'string' in attrs:
00242                                         group = QGroupBox( notebook )
00243                                         group.setTitle( attrs['string'] )
00244                                         layout = QHBoxLayout( group )
00245                                         layout.setAlignment( Qt.AlignTop )
00246                                         layout.setContentsMargins( 0, 0, 0, 0 )
00247                                         layout.setSpacing( 0 )
00248                                         layout.addWidget( widget )
00249                                         widget = group
00250                                 
00251                                 self.view.addStateWidget( widget, attrs.get('attrs'), attrs.get('states') )
00252                                 container.addWidget( widget, attrs )
00253 
00254                 return  container, onWriteFunction
00255 
00256         def isWidgetVisible(self, attributes):
00257                 value = attributes.get('invisible')
00258                 if not value:
00259                         return True
00260                 return not eval(value, {'context':self.context})
00261 
00262