def set_widget(self, widget, pos, colspan=1, rowspan=1): """<DOC> Adds a widget to the form. Arguments: widget -- The widget to add. pos -- The position to add the widget, which can be an index or a (column, row) tuple. Keyword arguments: colspan -- The number of columns that the widget should span (default=1). rowspan -- The number of rows that the widget should span (default=1). </DOC>""" index = self.cell_index(pos) if index >= len(self.widgets): raise form_error(u"Widget position (%s, %s) is outside of the form" % pos) if type(colspan) != int or colspan < 1 or colspan > len(self.cols): raise form_error(u"Column span %s is invalid (i.e. too large, too small, or not a number)" % colspan) if type(rowspan) != int or rowspan < 1 or rowspan > len(self.rows): raise form_error(u"Row span %s is invalid (i.e. too large, too small, or not a number)" % rowspan) self.widgets[index] = widget self.span[index] = colspan, rowspan widget.set_rect(self.get_rect(index))
def set_widget(self, widget, pos, colspan=1, rowspan=1): """<DOC> Adds a widget to the form. Arguments: widget -- The widget to add. pos -- The position to add the widget, which can be an index or a (column, row) tuple. Keyword arguments: colspan -- The number of columns that the widget should span (default=1). rowspan -- The number of rows that the widget should span (default=1). </DOC>""" index = self.cell_index(pos) if index >= len(self.widgets): raise form_error( \ u'Widget position (%s, %s) is outside of the form' % pos) if type(colspan) != int or colspan < 1 or colspan > len(self.cols): raise form_error( \ u'Column span %s is invalid (i.e. too large, too small, or not a number)' \ % colspan) if type(rowspan) != int or rowspan < 1 or rowspan > len(self.rows): raise form_error( \ u'Row span %s is invalid (i.e. too large, too small, or not a number)' \ % rowspan) self.widgets[index] = widget self.span[index] = colspan, rowspan widget.set_rect(self.get_rect(index))
def get_rect(self, index): """ Returns the boundary area for a given cell Arguments: index -- a cell index Returns: A (left, top, width, height) tuple """ col = index % len(self.cols) row = index / len(self.cols) colspan, rowspan = self.span[index] effective_width = self.width - self.margins[1] - self.margins[3] effective_height = self.height - self.margins[0] - self.margins[2] x1 = effective_width * sum(self.cols[:col]) + self.spacing y1 = effective_height * sum(self.rows[:row]) + self.spacing x2 = effective_width * sum(self.cols[: col + colspan]) - self.spacing y2 = effective_height * sum(self.rows[: row + rowspan]) - self.spacing w = x2 - x1 h = y2 - y1 if w <= 0 or h <= 0: raise form_error("There is not enough space to show some form widgets. Please modify the form geometry!") return x1 + self.margins[3], y1 + self.margins[0], w, h
def get_rect(self, index): """ Returns the boundary area for a given cell Arguments: index -- a cell index Returns: A (left, top, width, height) tuple """ col = index % len(self.cols) row = index / len(self.cols) colspan, rowspan = self.span[index] effective_width = self.width - self.margins[1] - self.margins[3] effective_height = self.height - self.margins[0] - self.margins[2] x1 = effective_width * sum(self.cols[:col]) + self.spacing y1 = effective_height * sum(self.rows[:row]) + self.spacing x2 = effective_width * sum(self.cols[:col + colspan]) - self.spacing y2 = effective_height * sum(self.rows[:row + rowspan]) - self.spacing w = x2 - x1 h = y2 - y1 if w <= 0 or h <= 0: raise form_error( \ 'There is not enough space to show some form widgets. Please modify the form geometry!') return x1 + self.margins[3], y1 + self.margins[0], w, h
def set_value(self, val): """<DOC> Sets the rating scale value. Arguments: val -- The value. </DOC>""" if val != None and (val >= len(self.nodes) or val < 0): raise form_error(u'Trying to select a non-existing node (%s). Did you specify an incorrect default value?' \ % val) self.value = val self.set_var(val)
def cell_index(self, pos): """<DOC> Converts a position to a cell index. A cell index corresponds to the # number of the cell in the form, from left-to-right, top-to-bottom. Arguments: pos -- A position, which can be an index (int) or a column, row tuple. Returns: A cell index </DOC>""" if type(pos) == int: return pos if type(pos) in (tuple, list) and len(pos) == 2: return pos[1] * len(self.cols) + pos[0] raise form_error(u'%s is an invalid position in the form' % pos)
def cell_index(self, pos): """<DOC> Converts a position to a cell index. A cell index corresponds to the # number of the cell in the form, from left-to-right, top-to-bottom. Arguments: pos -- A position, which can be an index (int) or a column, row tuple. Returns: A cell index </DOC>""" if type(pos) == int: return pos if type(pos) in (tuple, list) and len(pos) == 2: return pos[1] * len(self.cols) + pos[0] raise form_error(u"%s is an invalid position in the form" % pos)
def __init__(self, form): """<DOC> Constructor. Arguments: form -- The parent form. </DOC>""" self.type = u'widget' self.form = form self.rect = None self.focus = False self.var = None # Check if the form parameter is valid if not isinstance(form, _form): raise form_error( \ u'The first parameter passed to the constructor of a form widget should be a form, not "%s"' \ % form)
def set_var(self, val, var=None): """<DOC> Sets an experimental variable. Arguments: val -- A value. Keyword arguments: var -- A variable name, or None to use widget default. (default=None) </DOC>""" if var == None: var = self.var if var == None: return # Set the response variable l_val = [] # When this function is called via the constructor, the checkbox is not # yet part of the form. Therefore, we need to add it explicitly to the # widget list. widget_list = self.form.widgets[:] if self not in self.form.widgets: widget_list += [self] for widget in widget_list: if widget != None and widget.type == u"checkbox" and widget.var == self.var: if widget.group != self.group and self.group != None: raise form_error( _( u"All checkbox widgets without a group or within the same group should have the same variable." ) ) if widget.checked or widget.checked == u"yes": l_val.append(self.form.experiment.unistr(widget.text)) val = u";".join(l_val) if val == u"": val = u"no" self.form.experiment.set(var, val)
def set_var(self, val, var=None): """<DOC> Sets an experimental variable. Arguments: val -- A value. Keyword arguments: var -- A variable name, or None to use widget default. (default=None) </DOC>""" if var == None: var = self.var if var == None: return # Set the response variable l_val = [] # When this function is called via the constructor, the checkbox is not # yet part of the form. Therefore, we need to add it explicitly to the # widget list. widget_list = self.form.widgets[:] if self not in self.form.widgets: widget_list += [self] for widget in widget_list: if widget != None and widget.type == u'checkbox' and \ widget.group == self.group: if widget.var != self.var: raise form_error(_( \ u'All checkbox widgets without a group or within the same group should have the same variable.')) if widget.checked or widget.checked == u'yes': l_val.append(self.form.experiment.unistr(widget.text)) val = ';'.join(l_val) if val == u'': val = u'no' self.form.experiment.set(var, val)