Logo Search packages:      
Sourcecode: zim version File versions  Download package

def zim::gui::widgets::Dialog::add_fields (   self,
  fields,
  table = None,
  trigger_response = True 
) [inherited]

Add a number of fields to the dialog, convenience method to
construct simple forms. The argument 'fields' should be a list of
field definitions; each definition is a tupple of:

      * The field name
      * The field type
      * The label to put in front of the input field
      * The initial value of the field

The following field types are supported: 'bool', 'int', 'list',
'string', 'password', 'page', 'namespace', 'dir', 'file' and 'image'.

If 'table' is specified the fields are added to that table, otherwise
a new table is constructed and added to the dialog. Returns the table
to allow building a form in multiple calls.

If 'trigger_response' is True pressing <Enter> in the last Entry widget
will call response_ok(). Set to False if more forms will follow in the
same dialog.

Definition at line 539 of file widgets.py.

00539                                                                      :
            '''Add a number of fields to the dialog, convenience method to
            construct simple forms. The argument 'fields' should be a list of
            field definitions; each definition is a tupple of:

                  * The field name
                  * The field type
                  * The label to put in front of the input field
                  * The initial value of the field

            The following field types are supported: 'bool', 'int', 'list',
            'string', 'password', 'page', 'namespace', 'dir', 'file' and 'image'.

            If 'table' is specified the fields are added to that table, otherwise
            a new table is constructed and added to the dialog. Returns the table
            to allow building a form in multiple calls.

            If 'trigger_response' is True pressing <Enter> in the last Entry widget
            will call response_ok(). Set to False if more forms will follow in the
            same dialog.
            '''
            if table is None:
                  table = gtk.Table()
                  table.set_border_width(5)
                  table.set_row_spacings(5)
                  table.set_col_spacings(12)
                  self.vbox.add(table)
            i = table.get_property('n-rows')

            for field in fields:
                  name, type, label, value = field
                  if type == 'bool':
                        button = gtk.CheckButton(label=label)
                        button.set_active(value or False)
                        self.inputs[name] = button
                        table.attach(button, 0,2, i,i+1)
                  elif type == 'int':
                        label = gtk.Label(label+':')
                        label.set_alignment(0.0, 0.5)
                        table.attach(label, 0,1, i,i+1, xoptions=gtk.FILL)
                        button = gtk.SpinButton()
                        v, min, max = value
                        button.set_value(v)
                        button.set_range(min, max)
                        self.inputs[name] = button
                        table.attach(button, 1,2, i,i+1)
                  elif type == 'list':
                        label = gtk.Label(label+':')
                        label.set_alignment(0.0, 0.5)
                        table.attach(label, 0,1, i,i+1, xoptions=gtk.FILL)
                        value, options = value
                        combobox = gtk.combo_box_new_text()
                        for option in options:
                              combobox.append_text(str(option))
                        try:
                              active = list(options).index(value)
                                    # list() needed for python 2.5 compat
                              combobox.set_active(active)
                        except ValueError:
                              pass
                        self.inputs[name] = combobox
                        table.attach(combobox, 1,2, i,i+1)
                  elif type in ('string', 'password', 'page', 'namespace', 'dir', 'file', 'image'):
                        label = gtk.Label(label+': ')
                        label.set_alignment(0.0, 0.5)
                        table.attach(label, 0,1, i,i+1, xoptions=gtk.FILL)
                        if type in ('page', 'namespace'):
                              if type == 'page':
                                    entry = PageEntry(self.ui.notebook, path_context=self.path_context)
                              else:
                                    entry = NamespaceEntry(self.ui.notebook, path_context=self.path_context)
                              if value:
                                    if isinstance(value, basestring):
                                          entry.set_text(value)
                                    else:
                                          assert isinstance(value, Path)
                                          entry.set_path(value)
                              self.inputs[name] = entry
                              table.attach(entry, 1,2, i,i+1)
                        else:
                              entry = gtk.Entry()
                              entry.zim_type = type
                              if not value is None:
                                    entry.set_text(str(value))
                              self.inputs[name] = entry
                              table.attach(entry, 1,2, i,i+1)

                              if type in ('dir', 'file', 'image'):
                                    # FIXME use inline icon for newer versions of Gtk
                                    browse = gtk.Button('_Browse')
                                    browse.connect('clicked', self._select_file, (type, entry))
                                    table.attach(browse, 2,3, i,i+1, xoptions=gtk.FILL)
                              elif type == 'password':
                                    entry.set_visibility(False)
                  else:
                        assert False, 'BUG: unknown field type: %s' % type
                  i += 1

            def focus_next(o, next):
                  next.grab_focus()

            for i in range(len(fields)-1):
                  name = fields[i][0]
                  next = fields[i+1][0]
                  try:
                        self.inputs[name].connect('activate', focus_next, self.inputs[next])
                  except Exception:
                        pass

            if trigger_response:
                  last = fields[-1][0]
                  self.inputs[last].connect('activate', lambda o: self.response_ok())

            return table

      def _select_file(self, button, data):


Generated by  Doxygen 1.6.0   Back to index