add multiple data form support (XEP-0004)

This commit is contained in:
Yann Leboulanger 2007-04-19 13:27:14 +00:00
parent 1c0314b656
commit b415df15cc
2 changed files with 55 additions and 32 deletions

View File

@ -63,12 +63,14 @@ def ExtendField(node):
'list-multi': ListMultiField, 'list-multi': ListMultiField,
'list-single': ListSingleField, 'list-single': ListSingleField,
'text-multi': TextMultiField, 'text-multi': TextMultiField,
}[typ](extend=node) }
return f if typ not in f:
typ = 'text-single'
return f[typ](extend=node)
def ExtendForm(node): def ExtendForm(node):
''' Helper function to extend a node to form of appropriate type. ''' ''' Helper function to extend a node to form of appropriate type. '''
if node.getTag('recorded') is not None: if node.getTag('reported') is not None:
return MultipleDataForm(extend=node) return MultipleDataForm(extend=node)
else: else:
return SimpleDataForm(extend=node) return SimpleDataForm(extend=node)
@ -385,9 +387,25 @@ class SimpleDataForm(DataForm, DataRecord):
DataRecord.__init__(self, fields=fields, extend=self, associated=self) DataRecord.__init__(self, fields=fields, extend=self, associated=self)
class MultipleDataForm(DataForm): class MultipleDataForm(DataForm):
def __init__(self): def __init__(self, type=None, title=None, instructions=None, items=None, extend=None):
DataForm.__init__(self, type=type, title=title, instructions=instructions, extend=extend)
# all records, recorded into DataRecords # all records, recorded into DataRecords
pass if extend is None:
# we have to build this object from scratch
xmpp.Node.__init__(self)
if items is not None: self.items = items
else:
# we already have xmpp.Node inside - try to convert all
# fields into DataField objects
if items is None:
self.items = list(self.iterTags('item'))
else:
for item in self.getTags('item'):
self.delChild(item)
self.items = items
reported_tag = self.getTag('reported')
self.reported = DataRecord(extend = reported_tag)
@nested_property @nested_property
def items(): def items():
@ -401,7 +419,7 @@ class MultipleDataForm(DataForm):
DataRecord(extend=record) DataRecord(extend=record)
self.addChild(node=record) self.addChild(node=record)
def fdel(self): def fdel(self):
for record in self.getTags('record'): for record in self.getTags('item'):
self.delChild(record) self.delChild(record)
return locals() return locals()
@ -409,18 +427,18 @@ class MultipleDataForm(DataForm):
for record in self.getTags('item'): for record in self.getTags('item'):
yield record yield record
@nested_property # @nested_property
def recorded(): # def reported():
''' DataRecord that contains descriptions of fields in records.''' # ''' DataRecord that contains descriptions of fields in records.'''
def fget(self): # def fget(self):
return self.getTag('recorded') # return self.getTag('reported')
def fset(self, record): # def fset(self, record):
try: # try:
self.delChild('recorded') # self.delChild('reported')
except: # except:
pass # pass
#
record.setName('recorded') # record.setName('reported')
self.addChild(node=record) # self.addChild(node=record)
return locals() # return locals()

View File

@ -16,8 +16,8 @@
## ##
""" This module contains widget that can display data form (JEP-0004). """ This module contains widget that can display data form (JEP-0004).
Words single and multiple refers here to types of data forms: Words single and multiple refers here to types of data forms:
single means these with one record of data (without <recorded/> element), single means these with one record of data (without <reported/> element),
multiple - these which may contain more data (with <recorded/> element).""" multiple - these which may contain more data (with <reported/> element)."""
import gtk import gtk
@ -41,7 +41,7 @@ class DataFormWidget(gtk.Alignment, object):
for name in ('instructions_label', 'instructions_hseparator', for name in ('instructions_label', 'instructions_hseparator',
'single_form_viewport', 'data_form_types_notebook', 'single_form_viewport', 'data_form_types_notebook',
'single_form_scrolledwindow', 'multiple_form_hbox', 'single_form_scrolledwindow', 'multiple_form_hbox',
'records_treeview', 'add_button', 'remove_button', 'records_treeview', 'buttons_vbox', 'add_button', 'remove_button',
'edit_button', 'up_button', 'down_button', 'clear_button'): 'edit_button', 'up_button', 'down_button', 'clear_button'):
self.__dict__[name] = self.xml.get_widget(name) self.__dict__[name] = self.xml.get_widget(name)
@ -141,7 +141,7 @@ class DataFormWidget(gtk.Alignment, object):
# creating model for form... # creating model for form...
fieldtypes = [] fieldtypes = []
for field in self._data_form.recorded.iter_fields(): for field in self._data_form.reported.iter_fields():
# note: we store also text-private and hidden fields, # note: we store also text-private and hidden fields,
# we just do not display them. # we just do not display them.
# TODO: boolean fields # TODO: boolean fields
@ -157,8 +157,7 @@ class DataFormWidget(gtk.Alignment, object):
self.multiplemodel.append([field.value for field in item.iter_fields()]) self.multiplemodel.append([field.value for field in item.iter_fields()])
# constructing columns... # constructing columns...
for field, counter in zip(self._data_form.iter_fields(), itertools.count()): for field, counter in zip(self._data_form.reported.iter_fields(), itertools.count()):
print repr(field), repr(counter)
self.records_treeview.append_column( self.records_treeview.append_column(
gtk.TreeViewColumn(field.label, gtk.CellRendererText(), gtk.TreeViewColumn(field.label, gtk.CellRendererText(),
text=counter)) text=counter))
@ -172,8 +171,14 @@ class DataFormWidget(gtk.Alignment, object):
self.clean_data_form = self.clean_multiple_data_form self.clean_data_form = self.clean_multiple_data_form
# refresh list look readwrite = self._data_form.type != 'result'
self.refresh_multiple_buttons() if not readwrite:
self.buttons_vbox.set_no_show_all(True)
self.buttons_vbox.hide()
else:
self.buttons_vbox.set_no_show_all(False)
# refresh list look
self.refresh_multiple_buttons()
def clean_multiple_data_form(self): def clean_multiple_data_form(self):
'''(Called as clean_data_form, read the docs of clean_data_form()). '''(Called as clean_data_form, read the docs of clean_data_form()).
@ -186,12 +191,12 @@ class DataFormWidget(gtk.Alignment, object):
selection = self.records_treeview.get_selection() selection = self.records_treeview.get_selection()
model = self.records_treeview.get_model() model = self.records_treeview.get_model()
count = selection.count_selected_rows() count = selection.count_selected_rows()
if count==0: if count == 0:
self.remove_button.set_sensitive(False) self.remove_button.set_sensitive(False)
self.edit_button.set_sensitive(False) self.edit_button.set_sensitive(False)
self.up_button.set_sensitive(False) self.up_button.set_sensitive(False)
self.down_button.set_sensitive(False) self.down_button.set_sensitive(False)
elif count==1: elif count == 1:
self.remove_button.set_sensitive(True) self.remove_button.set_sensitive(True)
self.edit_button.set_sensitive(True) self.edit_button.set_sensitive(True)
_, (path,) = selection.get_selected_rows() _, (path,) = selection.get_selected_rows()
@ -199,7 +204,7 @@ class DataFormWidget(gtk.Alignment, object):
if model.iter_next(iter) is None: if model.iter_next(iter) is None:
self.up_button.set_sensitive(True) self.up_button.set_sensitive(True)
self.down_button.set_sensitive(False) self.down_button.set_sensitive(False)
elif path==(0,): elif path == (0, ):
self.up_button.set_sensitive(False) self.up_button.set_sensitive(False)
self.down_button.set_sensitive(True) self.down_button.set_sensitive(True)
else: else:
@ -211,7 +216,7 @@ class DataFormWidget(gtk.Alignment, object):
self.up_button.set_sensitive(False) self.up_button.set_sensitive(False)
self.down_button.set_sensitive(False) self.down_button.set_sensitive(False)
if len(model)==0: if len(model) == 0:
self.clear_button.set_sensitive(False) self.clear_button.set_sensitive(False)
else: else:
self.clear_button.set_sensitive(True) self.clear_button.set_sensitive(True)