From b415df15cc1e01d00d1dd61f9d864875a1d33571 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Thu, 19 Apr 2007 13:27:14 +0000 Subject: [PATCH] add multiple data form support (XEP-0004) --- src/common/dataforms.py | 58 +++++++++++++++++++++++++++-------------- src/dataforms_widget.py | 29 ++++++++++++--------- 2 files changed, 55 insertions(+), 32 deletions(-) diff --git a/src/common/dataforms.py b/src/common/dataforms.py index 470ef6ae2..a5db7e6fc 100644 --- a/src/common/dataforms.py +++ b/src/common/dataforms.py @@ -63,12 +63,14 @@ def ExtendField(node): 'list-multi': ListMultiField, 'list-single': ListSingleField, 'text-multi': TextMultiField, - }[typ](extend=node) - return f + } + if typ not in f: + typ = 'text-single' + return f[typ](extend=node) def ExtendForm(node): ''' 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) else: return SimpleDataForm(extend=node) @@ -385,9 +387,25 @@ class SimpleDataForm(DataForm, DataRecord): DataRecord.__init__(self, fields=fields, extend=self, associated=self) 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 - 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 def items(): @@ -401,7 +419,7 @@ class MultipleDataForm(DataForm): DataRecord(extend=record) self.addChild(node=record) def fdel(self): - for record in self.getTags('record'): + for record in self.getTags('item'): self.delChild(record) return locals() @@ -409,18 +427,18 @@ class MultipleDataForm(DataForm): for record in self.getTags('item'): yield record - @nested_property - def recorded(): - ''' DataRecord that contains descriptions of fields in records.''' - def fget(self): - return self.getTag('recorded') - def fset(self, record): - try: - self.delChild('recorded') - except: - pass - - record.setName('recorded') - self.addChild(node=record) - return locals() +# @nested_property +# def reported(): +# ''' DataRecord that contains descriptions of fields in records.''' +# def fget(self): +# return self.getTag('reported') +# def fset(self, record): +# try: +# self.delChild('reported') +# except: +# pass +# +# record.setName('reported') +# self.addChild(node=record) +# return locals() diff --git a/src/dataforms_widget.py b/src/dataforms_widget.py index ddd44b889..19fb91c18 100644 --- a/src/dataforms_widget.py +++ b/src/dataforms_widget.py @@ -16,8 +16,8 @@ ## """ This module contains widget that can display data form (JEP-0004). Words single and multiple refers here to types of data forms: -single means these with one record of data (without element), -multiple - these which may contain more data (with element).""" +single means these with one record of data (without element), +multiple - these which may contain more data (with element).""" import gtk @@ -41,7 +41,7 @@ class DataFormWidget(gtk.Alignment, object): for name in ('instructions_label', 'instructions_hseparator', 'single_form_viewport', 'data_form_types_notebook', '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'): self.__dict__[name] = self.xml.get_widget(name) @@ -141,7 +141,7 @@ class DataFormWidget(gtk.Alignment, object): # creating model for form... 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, # we just do not display them. # TODO: boolean fields @@ -157,8 +157,7 @@ class DataFormWidget(gtk.Alignment, object): self.multiplemodel.append([field.value for field in item.iter_fields()]) # constructing columns... - for field, counter in zip(self._data_form.iter_fields(), itertools.count()): - print repr(field), repr(counter) + for field, counter in zip(self._data_form.reported.iter_fields(), itertools.count()): self.records_treeview.append_column( gtk.TreeViewColumn(field.label, gtk.CellRendererText(), text=counter)) @@ -172,8 +171,14 @@ class DataFormWidget(gtk.Alignment, object): self.clean_data_form = self.clean_multiple_data_form - # refresh list look - self.refresh_multiple_buttons() + readwrite = self._data_form.type != 'result' + 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): '''(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() model = self.records_treeview.get_model() count = selection.count_selected_rows() - if count==0: + if count == 0: self.remove_button.set_sensitive(False) self.edit_button.set_sensitive(False) self.up_button.set_sensitive(False) self.down_button.set_sensitive(False) - elif count==1: + elif count == 1: self.remove_button.set_sensitive(True) self.edit_button.set_sensitive(True) _, (path,) = selection.get_selected_rows() @@ -199,7 +204,7 @@ class DataFormWidget(gtk.Alignment, object): if model.iter_next(iter) is None: self.up_button.set_sensitive(True) self.down_button.set_sensitive(False) - elif path==(0,): + elif path == (0, ): self.up_button.set_sensitive(False) self.down_button.set_sensitive(True) else: @@ -211,7 +216,7 @@ class DataFormWidget(gtk.Alignment, object): self.up_button.set_sensitive(False) self.down_button.set_sensitive(False) - if len(model)==0: + if len(model) == 0: self.clear_button.set_sensitive(False) else: self.clear_button.set_sensitive(True)