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.View.AbstractParser import *
00030 
00031 import time
00032 
00033 from Koo.Model import KooModel
00034 
00035 from TreeView import *
00036 from PyQt4.uic import *
00037 from PyQt4.QtCore import *
00038 from PyQt4.QtGui import *
00039 
00040 from Koo.Fields.FieldWidgetFactory import *
00041 from Koo.Fields.FieldDelegateFactory import *
00042 from Koo.Common import Common
00043 from Koo.Common.Numeric import *
00044 from Koo.Common.Calendar import *
00045 from Koo.Common.ViewSettings import *
00046 
00047 
00048 class TreeParser(AbstractParser):
00049         def create(self, viewId, parent, model, rootNode, fields):
00050                 
00051                 screen = parent
00052 
00053                 attrs = Common.nodeAttributes(rootNode)
00054                 
00055                 view = TreeView( parent, attrs.get('type','tree') )
00056                 view.id = viewId
00057                 if 'gridwidth' in attrs:
00058                         view.setGridWidth( int(attrs['gridwidth']) )
00059                 if 'gridheight' in attrs:
00060                         view.setGridWidth( int(attrs['gridheight']) )
00061 
00062                 view.setOnWriteFunction( attrs.get('on_write', '') )
00063 
00064                 if not view.title:
00065                         view.title = attrs.get('string', 'Unknown' )
00066 
00067                 colors = []
00068                 for color_spec in attrs.get('colors', '').split(';'):
00069                         if color_spec:
00070                                 colour, test = color_spec.split(':')
00071                                 colors.append( ( colour, str(test) ) )
00072 
00073                 header = []
00074                 columns = []
00075                 for node in rootNode.childNodes:
00076                         node_attrs = Common.nodeAttributes(node)
00077                         if node.localName == 'field':
00078                                 fname = node_attrs['name']
00079                                 twidth = {
00080                                         'integer': 60,
00081                                         'float': 80,
00082                                         'date': 70,
00083                                         'datetime': 130,
00084                                         'selection': 130,
00085                                         'char': 140,
00086                                         'one2many': 50,
00087                                 }
00088 
00089 
00090                                 if 'readonly' in node_attrs:
00091                                         fields[fname]['readonly'] = Common.stringToBool(node_attrs['readonly'])
00092                                 if 'required' in node_attrs:
00093                                         fields[fname]['required'] = Common.stringToBool(node_attrs['required'])
00094 
00095                                 if 'sum' in node_attrs and fields[fname]['type'] in ('integer', 'float', 'float_time'):
00096                                         bold = bool(int(node_attrs.get('sum_bold', 0)))
00097                                         label = node_attrs['sum']
00098                                         digits = fields.get('digits', (16,2))
00099                                         view.addAggregate( fname, label, bold, digits ) 
00100 
00101                                 node_attrs.update(fields[fname])
00102 
00103                                 visible = not eval(fields[fname].get('invisible', 'False'), {'context': screen.context})
00104 
00105                                 if 'width' in fields[fname]:
00106                                         width = int(fields[fname]['width'])
00107                                 else:
00108                                         width = twidth.get(fields[fname]['type'], 200)
00109                                 header.append( { 'name': fname, 'type': fields[fname]['type'], 'string': fields[fname].get('string', '') })
00110                                 columns.append({ 
00111                                         'width': width , 
00112                                         'type': fields[fname]['type'], 
00113                                         'attributes':node_attrs,
00114                                         'visible': visible
00115                                 })
00116 
00117                 view.finishAggregates()
00118 
00119                 model = KooModel.KooModel( view )
00120                 model.setMode( KooModel.KooModel.ListMode )
00121                 model.setRecordGroup( screen.group )
00122                 model.setFields( fields )
00123                 model.setFieldsOrder( [x['name'] for x in header] )
00124                 model.setColors( colors )
00125                 model.setReadOnly( not attrs.get('editable', False) )
00126                 view.setReadOnly( not attrs.get('editable', False) )
00127 
00128                 if attrs.get('editable', False) == 'top':
00129                         view.setAddOnTop( True )
00130 
00131                 if view.isReadOnly():
00132                         model.setShowBackgroundColor( False )
00133                 else:
00134                         model.setShowBackgroundColor( True )
00135 
00136                 
00137                 
00138                 
00139                 
00140                 
00141                 
00142                 domain = screen.group.domain()
00143                 screen.group.setDomainForEmptyGroup()
00144 
00145                 view.setModel( model )
00146 
00147                 for column in range( len(columns)):
00148                         current = columns[column]
00149                         if view._widgetType in ('tree','table'):
00150                                 view.widget.setColumnWidth( column, current['width'] )
00151                         if not current['visible']:
00152                                 view.widget.hideColumn( column )
00153 
00154                         delegate = FieldDelegateFactory.create( current['type'], view.widget, current['attributes'] )
00155                         view.widget.setItemDelegateForColumn( column, delegate )
00156 
00157                 view.setViewSettings( ViewSettings.load( view.id ) )
00158 
00159                 screen.group.setDomain( domain )
00160 
00161                 return view
00162 
00163