Exemplo n.º 1
0
    def prepare(self):
        """Prepares the item."""

        item.item.prepare(self)

        if self.var.skip_item == u'no':

            # Get values for internal variables (to allow the use of OpenSesame variables)
            self._cols = self.var.cols
            self._rows = self.var.rows
            self._correct_button = self.var.correct_button
            self._start_coordinates = self.var.start_coordinates
            self._mouse_buttons_allowed = self.var.mouse_buttons_allowed

            # Prepare the form
            try:
                cols = [float(i) for i in unicode(self._cols).split(';')]
                rows = [float(i) for i in unicode(self._rows).split(';')]
                margins = [
                    float(i) for i in unicode(self.var.margins).split(';')
                ]
            except:
                raise osexception(
                    _(u'cols, rows, and margins should be numeric values separated by a semi-colon'
                      ))

            # Modification of original form:
            # if cols and rows only have one value, then treat this as the number of cols/rows
            if len(cols) == 1:
                cols = int(cols[0])
            if len(rows) == 1:
                rows = int(rows[0])

            # Initialize form
            self._form = MT_form(self.experiment, cols=cols, rows=rows, \
             margins=margins, spacing=self.var.spacing, theme=self.var._theme, item=self)

            # Prepare the widgets
            self.focus_widget = None
            for arglist, orig_kwdict in self._widgets:
                kwdict = orig_kwdict.copy()
                # Evaluate all values
                arglist = [self.syntax.eval_text(arg) for arg in arglist]
                for key, val in kwdict.items():
                    kwdict[key] = self.syntax.eval_text(val, var=self.var)
                # Translate paths into full file names
                if u'path' in kwdict:
                    kwdict[u'path'] = self.experiment.pool[kwdict[u'path']]
                # Process focus keyword
                focus = False
                if u'focus' in kwdict:
                    if kwdict[u'focus'] == u'yes':
                        focus = True
                    del kwdict[u'focus']
                # Parse arguments
                _type = arglist[4]
                try:
                    col = int(arglist[0])
                    row = int(arglist[1])
                    colspan = int(arglist[2])
                    rowspan = int(arglist[3])
                except:
                    raise osexception(
                        _(u'In a form widget col, row, colspan, and rowspan should be integer'
                          ))
                # Create the widget and add it to the form
                try:
                    _w = getattr(widgets, _type)(self._form, **kwdict)
                except Exception as e:
                    raise osexception(u'Failed to create widget "%s": %s' %
                                      (_type, e))
                self._form.set_widget(_w, (col, row),
                                      colspan=colspan,
                                      rowspan=rowspan)
                # Add as focus widget
                if focus:
                    if self.focus_widget is not None:
                        raise osexception(
                            _(u'You can only specify one focus widget'))
                    self.focus_widget = _w

            # Create list with allowed mouse buttons as integers
            if self.var.click_required == u'yes':
                # Convert to string first (in case that only one integer is provided)
                self._mouse_buttons_allowed = str(self._mouse_buttons_allowed)
                self._mouse_buttons_allowed = self.clean_input(
                    self._mouse_buttons_allowed)
                self._mouse_buttons_allowed = self._mouse_buttons_allowed.split(
                )
                self._mouse_buttons_allowed = [
                    int(i) for i in self._mouse_buttons_allowed
                ]

            # Prepare start_coordinates
            if self.var.reset_mouse == u'yes':

                self._start_unit = self.var.start_unit

                # Clean input for start_coordinates
                self._start_coordinates = self.clean_input(
                    self._start_coordinates)

                # Create start_coordinate tuple
                if '.' in self._start_coordinates:
                    self._start_coordinates = self._start_coordinates.split()
                    self._start_coordinates = tuple(
                        [float(i) for i in self._start_coordinates])
                else:
                    self._start_coordinates = self._start_coordinates.split()
                    self._start_coordinates = tuple(
                        [int(i) for i in self._start_coordinates])

            # Prepare initiation time warning
            if self.var.check_initiation_time == u'yes':
                self._max_initiation_time = int(self.var.max_initiation_time)

                try:
                    cmd, arglist, kwdict = self.syntax.parse_cmd(
                        self.var.warning_message)

                    # Evaluate all values
                    arglist = [self.syntax.eval_text(arg) for arg in arglist]
                    for key, val in kwdict.items():
                        kwdict[key] = self.syntax.eval_text(val, var=self.var)
                    # Translate paths into full file names
                    if u'path' in kwdict:
                        kwdict[u'path'] = self.experiment.pool[kwdict[u'path']]
                    # Create the widget
                    _type = arglist[4]
                    _w = getattr(widgets, _type)(self._form, **kwdict)
                    self._warning_widget = [
                        _w, (int(arglist[0]), int(arglist[1])),
                        int(arglist[2]),
                        int(arglist[3])
                    ]

                except:
                    raise osexception(
                        u'Failed to create widget for warning message.\
					Please check again the syntax and test it, e.g.,\
					by directly inserting the widget in the OpenSesame script first.')