Ejemplo n.º 1
0
	def prepare(self):

		"""Prepare for the run phase"""

		item.item.prepare(self)

		# 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.margins).split(';')]
		except:
			raise osexception( \
				_('cols, rows, and margins should be numeric values separated by a semi-colon'))
		self._form = widgets.form(self.experiment, cols=cols, rows=rows, \
			margins=margins, spacing=self.spacing, theme=self.theme, item=self)

		# Prepare the widgets
		for _w in self._widgets:

			# Evaluate all keyword arguments
			w = {}
			for var, val in _w.iteritems():
				w[var] = self.eval_text(val)

			_type = w['type']
			col = w['col']
			row = w['row']
			colspan = w['colspan']
			rowspan = w['rowspan']
			del w['type']
			del w['col']
			del w['row']
			del w['colspan']
			del w['rowspan']

			# Translate paths into full file names
			if 'path' in w:
				w['path'] = self.experiment.get_file(w['path'])

			# Process focus keyword
			if 'focus' in w:
				focus = True
				del w['focus']
			else:
				focus = False

			# Create the widget and add it to the form
			try:
				_w = eval('widgets.%s(self._form, **w)' % _type)
			except Exception as e:
				raise osexception( \
					'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:
				self.focus_widget = _w
		return True
Ejemplo n.º 2
0
	def prepare(self):

		"""Prepare for the run phase"""

		item.item.prepare(self)

		# 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.margins).split(';')]
		except:
			raise exceptions.runtime_error( \
				_('cols, rows, and margins should be numeric values separated by a semi-colon'))
		self._form = widgets.form(self.experiment, cols=cols, rows=rows, \
			margins=margins, spacing=self.spacing, theme=self.theme, item=self)

		# Prepare the widgets
		for _w in self._widgets:

			# Evaluate all keyword arguments
			w = {}
			for var, val in _w.iteritems():
				w[var] = self.eval_text(val)

			_type = w['type']
			col = w['col']
			row = w['row']
			colspan = w['colspan']
			rowspan = w['rowspan']
			del w['type']
			del w['col']
			del w['row']
			del w['colspan']
			del w['rowspan']

			# Translate paths into full file names
			if 'path' in w:
				w['path'] = self.experiment.get_file(w['path'])

			# Process focus keyword
			if 'focus' in w:
				focus = True
				del w['focus']
			else:
				focus = False

			# Create the widget and add it to the form
			try:
				_w = eval('widgets.%s(self._form, **w)' % _type)
			except Exception as e:
				raise exceptions.runtime_error( \
					'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:
				self.focus_widget = _w
		return True
Ejemplo n.º 3
0
	def run(self):

		"""Run the item"""

		# Parse the option list
		option_list = self.var.options.split(u'\n') # split by return
		# Filter out empty options
		option_list = list(filter(lambda option: option != u'', option_list))
		# option_list.pop(len(option_list)-1) # remove last (empty) option
		if len(option_list) == 0:
			raise osexception(
				u'You must specify at least one response option in form_multiple_choice item "%s"' \
				% self.name)

		# Determine whether a button is shown and determine the number of rows
		rows = len(option_list) + 2
		if self.var.advance_immediately == u'no' \
			or self.var.allow_multiple == u'yes':
			show_button = True
			click_accepts = False
			rows += 1
		else:
			show_button = False
			click_accepts = True

		# Determine the group for the checkboxes
		if self.var.allow_multiple == u'no':
			group = u'response_group'
		else:
			group = None

		# The variable in which the response is stored
		var = self.var.form_var

		# Build the form
		try:
			margins = [float(i) for i in str(self.var.margins).split(u';')]
		except:
			raise osexception(
				_(u'margins should be numeric values separated by a semi-colon'))
		if self.var.timeout == u'infinite':
			timeout = None
		else:
			timeout = self.var.timeout
		form = widgets.form(self.experiment, cols=1, rows=rows,
			spacing=self.var.spacing, margins=margins, theme=self.var._theme,
			item=self, timeout=timeout, clicks=self.var.form_clicks==u'yes')
		form.set_widget(widgets.label(form, self.var.form_title), (0,0))
		form.set_widget(widgets.label(form, self.var.question), (0,1))
		i = 2
		for option in option_list:
			form.set_widget(widgets.checkbox(form, option,
				group=group, click_accepts=click_accepts, var=var), (0,i))
			i += 1
		if show_button:
			form.set_widget(widgets.button(form, self.var.button_text), (0,i))

		# Go!
		form._exec()
Ejemplo n.º 4
0
    def run(self):
        """Run the item"""

        # Parse the option list
        option_list = self.get(u'options').split(u'\n')  # split by return
        # Filter out empty options
        option_list = filter(lambda option: option != u'', option_list)
        # option_list.pop(len(option_list)-1) # remove last (empty) option
        if len(option_list) == 0:
            raise osexception( \
             u'You must specify at least one response option in form_multiple_choice item "%s"' \
             % self.name)

        # Determine whether a button is shown and determine the number of rows
        rows = len(option_list) + 2
        if self.get(u'advance_immediately') == u'no' or \
         self.get(u'allow_multiple') == u'yes':
            show_button = True
            click_accepts = False
            rows += 1
        else:
            show_button = False
            click_accepts = True

        # Determine the group for the checkboxes
        if self.get(u'allow_multiple') == u'no':
            group = u'response_group'
        else:
            group = None

        # The variable in which the response is stored
        var = self.get(u'form_var')

        # Build the form
        try:
            margins = [float(i) for i in unicode(self.margins).split(u';')]
        except:
            raise osexception( \
             _(u'margins should be numeric values separated by a semi-colon'))
        form = widgets.form(self.experiment,
                            cols=1,
                            rows=rows,
                            spacing=self.get(u'spacing'),
                            margins=margins,
                            theme=self.get(u'_theme'),
                            item=self)
        form.set_widget(widgets.label(form, self.get(u'form_title')), (0, 0))
        form.set_widget(widgets.label(form, self.get(u'question')), (0, 1))
        i = 2
        for option in option_list:
            form.set_widget(widgets.checkbox(form, option, group=group, \
             click_accepts=click_accepts, var=var), (0,i))
            i += 1
        if show_button:
            form.set_widget(widgets.button(form, self.get(u'button_text')), \
             (0,i))

        # Go!
        form._exec()
Ejemplo n.º 5
0
    def prepare(self):
        """Prepares the item."""

        item.item.prepare(self)

        # 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.margins).split(';')]
        except:
            raise osexception( \
             _(u'cols, rows, and margins should be numeric values separated by a semi-colon'))
        self._form = widgets.form(self.experiment, cols=cols, rows=rows, \
         margins=margins, spacing=self.spacing, theme=self._theme, item=self)

        # Prepare the widgets
        for _w in self._widgets:
            # Evaluate all keyword arguments
            w = {}
            for var, val in _w.iteritems():
                w[var] = self.eval_text(val)

            _type = w[u'type']
            col = w[u'col']
            row = w[u'row']
            colspan = w[u'colspan']
            rowspan = w[u'rowspan']
            del w[u'type']
            del w[u'col']
            del w[u'row']
            del w[u'colspan']
            del w[u'rowspan']

            # Translate paths into full file names
            if u'path' in w:
                w[u'path'] = self.experiment.get_file(w[u'path'])

            # Process focus keyword
            focus = False
            if u'focus' in w:
                if w[u'focus'] == u'yes':
                    focus = True
                del w[u'focus']

            # Create the widget and add it to the form
            try:
                _w = getattr(widgets, _type)(self._form, **w)
            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:
                self.focus_widget = _w
Ejemplo n.º 6
0
	def run(self):

		"""Run the item"""

		# Parse the option list
		option_list = self.get(u'options').split(u'\n') # split by return
		option_list.pop(len(option_list)-1) # remove last (empty) option
		if len(option_list) == 0:
			raise osexception( \
				u'You must specify at least one response option in form_multiple_choice item "%s"' \
				% self.name)

		# Determine whether a button is shown and determine the number of rows
		rows = len(option_list) + 2
		if self.get(u'advance_immediately') == u'no' or \
			self.get(u'allow_multiple') == u'yes':
			show_button = True
			click_accepts = False
			rows += 1
		else:
			show_button = False
			click_accepts = True

		# Determine the group for the checkboxes
		if self.get(u'allow_multiple') == u'no':
			group = u'response_group'
		else:
			group = None

		# The variable in which the response is stored
		var = self.get(u'form_var')

		# Build the form
		try:
			margins = [float(i) for i in unicode(self.margins).split(u';')]
		except:
			raise osexception( \
				_(u'margins should be numeric values separated by a semi-colon'))
		form = widgets.form(self.experiment, cols=1, rows=rows,
			spacing=self.get(u'spacing'), margins=margins,
			theme=self.get(u'_theme'), item=self)
		form.set_widget(widgets.label(form, self.get(u'form_title')), (0,0))
		form.set_widget(widgets.label(form, self.get(u'question')), (0,1))
		i = 2
		for option in option_list:
			form.set_widget(widgets.checkbox(form, option, group=group, \
				click_accepts=click_accepts, var=var), (0,i))
			i += 1
		if show_button:
			form.set_widget(widgets.button(form, self.get(u'button_text')), \
				(0,i))

		# Go!
		form._exec()
Ejemplo n.º 7
0
	def run(self):

		"""The run phase of the plug-in goes here."""
		form = widgets.form(self.experiment, cols=[1,5,1], rows=self.rows, margins=[32,32,32,32])
		label_title = widgets.label(form, text=self._question)
		form.set_widget(label_title, (0,0), colspan=3)
		for i in range(len(self.responses)):
			resp = self.responses[i]
			button = widgets.button(form, text=resp)
			form.set_widget(button, (1, i+1))
		button_clicked = form._exec()
		self.experiment.set(self._variable, button_clicked)		
Ejemplo n.º 8
0
    def _prepare_form(self):

        self._form = widgets.form(self.experiment,
                                  cols=(1),
                                  rows=(1, 5),
                                  item=self,
                                  clicks=self.var.form_clicks == u'yes')
        label = widgets.label(self._form,
                              text='Enter OMM participant identifier')
        self._text_input = widgets.text_input(
            self._form, return_accepts=True, var=self.var.participant_variable)
        self._form.set_widget(label, (0, 0))
        self._form.set_widget(self._text_input, (0, 1))
        self.run = self._run_form
Ejemplo n.º 9
0
	def run(self):
		
		"""Run the item"""
		
		# Parse the option list
		option_list = unicode(self.get('options')).split('\n') # split by return
		option_list.pop(len(option_list)-1) # remove last (empty) option
		if len(option_list) == 0:
			raise exceptions.runtime_error( \
			'You must specify at least one response option in form_multiple_choice item "%s"' \
			% self.name)
			
		# Determine whether a button is shown and determine the number of rows
		rows = len(option_list) + 2
		if self.get('advance_immediately') == 'no' or \
			self.get('allow_multiple') == 'yes':
			show_button = True
			click_accepts = False
			rows += 1
		else:
			show_button = False
			click_accepts = True
			
		# Determine the group for the checkboxes
		if self.get('allow_multiple') == 'no':
			group = 'response_group'
		else:
			group = None			
			
		# The variable in which the response is stored
		var = self.get('form_var')
		
		# Build the form
		form = widgets.form(self.experiment, cols=1, rows=rows)		
		form.set_widget(widgets.label(form, self.get('form_title')), (0,0))
		form.set_widget(widgets.label(form, self.get('question')), (0,1))
		i = 2
		for option in option_list:
			form.set_widget(widgets.checkbox(form, option, group=group, \
				click_accepts=click_accepts, var=var), (0,i))
			i += 1
		if show_button:
			form.set_widget(widgets.button(form, self.get('button_text')), \
				(0,i))

		# Go!
		form._exec()
		return True
Ejemplo n.º 10
0
    def run(self):
        """Run the item"""

        # Parse the option list
        option_list = unicode(self.get('options')).split(
            '\n')  # split by return
        option_list.pop(len(option_list) - 1)  # remove last (empty) option
        if len(option_list) == 0:
            raise exceptions.runtime_error( \
            'You must specify at least one response option in form_multiple_choice item "%s"' \
            % self.name)

        # Determine whether a button is shown and determine the number of rows
        rows = len(option_list) + 2
        if self.get('advance_immediately') == 'no' or \
         self.get('allow_multiple') == 'yes':
            show_button = True
            click_accepts = False
            rows += 1
        else:
            show_button = False
            click_accepts = True

        # Determine the group for the checkboxes
        if self.get('allow_multiple') == 'no':
            group = 'response_group'
        else:
            group = None

        # The variable in which the response is stored
        var = self.get('form_var')

        # Build the form
        form = widgets.form(self.experiment, cols=1, rows=rows)
        form.set_widget(widgets.label(form, self.get('form_title')), (0, 0))
        form.set_widget(widgets.label(form, self.get('question')), (0, 1))
        i = 2
        for option in option_list:
            form.set_widget(widgets.checkbox(form, option, group=group, \
             click_accepts=click_accepts, var=var), (0,i))
            i += 1
        if show_button:
            form.set_widget(widgets.button(form, self.get('button_text')), \
             (0,i))

        # Go!
        form._exec()
        return True
Ejemplo n.º 11
0
def Form(*args, **kwargs):
    """
	desc: |
		A factory function that creates a new `Form` object. For a
		description of possible keywords, see:

		- %link:manual/forms/widgets%

	returns:
		desc:	A `Form` object.
		type:	canvas

	example: |
		form = Form()
		label = Label(text='label')
		button = Button(text='Ok')
		form.set_widget(label, (0,0))
		form.set_widget(button, (0,1))
		form._exec()
	"""

    from libopensesame.widgets import form
    return form(experiment, **kwargs)
Ejemplo n.º 12
0
	def prepare(self):

		"""Prepares the item."""

		item.item.prepare(self)

		# Prepare the form
		try:
			cols = [float(i) for i in str(self.var.cols).split(u';')]
			rows = [float(i) for i in str(self.var.rows).split(u';')]
			margins = [float(i) for i in str(self.var.margins).split(u';')]
		except:
			raise osexception(
				_(u'cols, rows, and margins should be numeric values separated by a semi-colon'))
		if self.var.timeout == u'infinite':
			timeout = None
		else:
			timeout = self.var.timeout
		self._form = widgets.form(self.experiment, cols=cols, rows=rows,
			margins=margins, spacing=self.var.spacing, theme=self.var._theme,
			item=self, timeout=timeout, clicks=self.var.form_clicks==u'yes')

		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
Ejemplo n.º 13
0
    def prepare(self):
        """Prepares the item."""

        item.item.prepare(self)

        # Prepare the form
        try:
            cols = [float(i) for i in str(self.var.cols).split(u';')]
            rows = [float(i) for i in str(self.var.rows).split(u';')]
            margins = [float(i) for i in str(self.var.margins).split(u';')]
        except:
            raise osexception(
                _(u'cols, rows, and margins should be numeric values separated by a semi-colon'
                  ))
        if self.var.timeout == u'infinite':
            timeout = None
        else:
            timeout = self.var.timeout
        self._form = widgets.form(self.experiment,
                                  cols=cols,
                                  rows=rows,
                                  margins=margins,
                                  spacing=self.var.spacing,
                                  theme=self.var._theme,
                                  item=self,
                                  timeout=timeout,
                                  clicks=self.var.form_clicks == u'yes')

        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
Ejemplo n.º 14
0
	def run(self):
		# Check if we should run this at all
		# TODO: For some reason, this works fine, as
		# [data_loaded]
		# [data_loaded] = 1
		# , etc, but not
		# [data_loaded] = true, or '= True', or '== true', or '== True'.
		print self._runif
		print self.compile_cond(self._runif)
		if eval(self.compile_cond(self._runif)):
			# Run the itemf
			# User input
			form = widgets.form(self.experiment, cols=[1,5,1], rows=self.rows, margins=[32,32,32,32])
			label_title = widgets.label(form, text=self._question)
			form.set_widget(label_title, (0,0), colspan=3)
			for i in range(len(self.responses)):
				resp = self.responses[i]
				button = widgets.button(form, text=resp)
				form.set_widget(button, (1, i+1))
			button_clicked = form._exec()
			# Check input
			if button_clicked == self._yes:
				# Send data
				data = self.experiment.log_list
				self.c.clear()
				self.c.text('Connecting to server...')
				self.c.show()
				
				# Try to open the connection
				#address = 'http://cogsci.nl/etravers/androidsql.php'
				address = self._address
				try:
					page = urllib.urlopen(address)#, data='position=OpeningConnection')
					page.close()
					# Connection works. Send data.
					debug.msg('Connection to server  at %s succesful...' % address)
					t = 0
					for trial in data:
						progress = int(float(t)/len(data)*680)
						#print t, len(data), progress
						self.c.clear()
						self.c.text('Sending...')
						self.c.rect(300, 600, 680, 72, False, 'grey')
						self.c.rect(303, 601, progress, 70, True, 'red')
						self.c.show()
						encode_data = urllib.urlencode(trial)
						debug.msg(encode_data)
						for i in range(5):
							# Try to send data 5 times.
							try:
								page = urllib.urlopen(address, encode_data)
								page.close()
								print 'Sent!', t
							except IOError as e:
									print str(e)
							else:
								break
						t += 1
					result = 'Data sent!'
					self.experiment.set('data_sent', 1)
					# Check for, and delete, sent datafile.
					# Find the saved data
					sdcard_folders = ['/sdcard/', '/mnt/sdcard/']
					for path in sdcard_folders:
						if os.path.isdir(path):
							print path
							break
					if os.path.exists(os.path.join(path, 'datafile.txt')):
						data_path = os.path.join(path, 'datafile.txt')
						os.remove(data_path)
					elif os.path.exists('datafile.txt'):
						data_path = 'datafile.txt'
						os.remove(data_path)
					# Otherwise, there's nothing to delete.
				except IOError as e:
					result = 'Unable to connect.\nPlease try again when connected to the internet.'
					print str(e)
					self.experiment.set('data_sent', 0)
				debug.msg(result)
				self.c.clear()
				self.c.text(result)
				self.c.text('Tap to continue.', y = 500)
				self.c.show()
				self.m.get_click()
			else:
				# Don't send data
				self.experiment.set('data_sent', 0)