Advertisement
Guest User

Untitled

a guest
Feb 7th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.76 KB | None | 0 0
  1. """
  2. This file is part of OpenSesame.
  3.  
  4. OpenSesame is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. OpenSesame is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with OpenSesame.  If not, see <http://www.gnu.org/licenses/>.
  16. """
  17.  
  18. from libopensesame import item, exceptions
  19. from libqtopensesame import qtplugin
  20. import openexp.canvas
  21. import os.path
  22. import imp
  23. from PyQt4 import QtGui, QtCore
  24.  
  25. class eyelink_drift_correct(item.item):
  26.  
  27.     """
  28.     This class (the class with the same name as the module)
  29.     handles the basic functionality of the item. It does
  30.     not deal with GUI stuff.
  31.     """
  32.  
  33.     def __init__(self, name, experiment, string = None):
  34.  
  35.         """
  36.         Constructor
  37.         """
  38.  
  39.         # The item_typeshould match the name of the module
  40.         self.item_type = "eyelink_drift_correct"
  41.  
  42.         self._mode_manual = "Manual (spacebar triggered)"
  43.         self._mode_auto = "Automatic (fixation triggered)"
  44.         self.mode = self._mode_manual
  45.  
  46.         self.xpos = 0
  47.         self.ypos = 0
  48.  
  49.         # Provide a short accurate description of the items functionality
  50.         self.description = "Drift correction plugin for the Eyelink series of eye trackers (SR-Research)"
  51.  
  52.         # The parent handles the rest of the contruction
  53.         item.item.__init__(self, name, experiment, string)
  54.  
  55.     def prepare(self):
  56.  
  57.         """
  58.         Prepare the item. In this case this means drawing a fixation
  59.         dot to an offline canvas.
  60.         """
  61.  
  62.         # Pass the word on to the parent
  63.         item.item.prepare(self)
  64.  
  65.         # Create an eyelink instance if it doesn't exist yet. Libeyelink is
  66.         # dynamically loaded
  67.         if not hasattr(self.experiment, "eyelink"):
  68.             raise exceptions.runtime_error("Please connect to the eyelink using the the eyelink_calibrate plugin before using any other eyelink plugins")
  69.  
  70.         # Report success
  71.         return True
  72.  
  73.     def run(self):
  74.  
  75.         """
  76.         Run the item. In this case this means putting the offline canvas
  77.         to the display and waiting for the specified duration.
  78.         """
  79.  
  80.         self.set_item_onset()
  81.        
  82.         try:
  83.             x = int(self.get("xpos", _eval=True))
  84.             y = int(self.get("ypos", _eval=True))
  85.         except:
  86.             raise exceptions.runtime_error("Please use numeric values for the coordinates in eyelink_drift_correct item '%s'" % self.name)
  87.  
  88.         if not self.has("coordinates") or self.get("coordinates") == "relative":
  89.             x += self.get("width") / 2
  90.             y += self.get("height") / 2
  91.  
  92.         # Draw a fixation cross
  93.         c = openexp.canvas.canvas(self.experiment, self.get("background"), self.get("foreground"))
  94.         c.set_penwidth(3)
  95.         c.line(x - 5, y, x + 5, y)
  96.         c.line(x, y - 5, x, y + 5)
  97.         c.show()
  98.         # Do drift correction
  99.         while not self.experiment.eyelink.drift_correction( (x, y), self.get("mode") == self._mode_auto):
  100.             # if esc was pressed, ask for confirmation to abort:
  101.             if self.experiment.eyelink_esc_pressed:
  102.                 self.experiment.eyelink.confirm_abort_experiment() # raises an exception if confirmed
  103.                
  104.                 # if not confirmed, set esc pressed back to false
  105.                 self.experiment.eyelink_esc_pressed = False
  106.             self.experiment.eyelink.calibrate()
  107.             c.show()
  108.  
  109.         # Report success
  110.         return True
  111.  
  112. class qteyelink_drift_correct(eyelink_drift_correct, qtplugin.qtplugin):
  113.  
  114.     """
  115.     This class (the class named qt[name of module] handles
  116.     the GUI part of the plugin. For more information about
  117.     GUI programming using PyQt4, see:
  118.     <http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/classes.html>
  119.     """
  120.  
  121.     def __init__(self, name, experiment, string = None):
  122.  
  123.         """
  124.         Constructor
  125.         """
  126.  
  127.         # Pass the word on to the parents
  128.         eyelink_drift_correct.__init__(self, name, experiment, string)
  129.         qtplugin.qtplugin.__init__(self, __file__)
  130.  
  131.     def init_edit_widget(self):
  132.  
  133.         """
  134.         This function creates the controls for the edit
  135.         widget.
  136.         """
  137.  
  138.         # Lock the widget until we're doing creating it
  139.         self.lock = True
  140.  
  141.         # Pass the word on to the parent
  142.         qtplugin.qtplugin.init_edit_widget(self, False)
  143.         self.add_combobox_control("mode", "Mode", [self._mode_manual, self._mode_auto], tooltip = "Indicates if drift correction should be manual or automatic")
  144.  
  145.         if self.has("coordinates") and self.get("coordinates") == "absolute":
  146.             self.add_line_edit_control("xpos", "X coordinate", self.get("width") / 2)
  147.             self.add_line_edit_control("ypos", "Y coordinate", self.get("height") / 2)
  148.         else:
  149.             self.add_line_edit_control("xpos", "X coordinate", 0)
  150.             self.add_line_edit_control("ypos", "Y coordinate", 0)
  151.  
  152.         # Add a stretch to the edit_vbox, so that the controls do not
  153.         # stretch to the bottom of the window.
  154.         self.edit_vbox.addStretch()
  155.  
  156.         # Unlock
  157.         self.lock = True
  158.  
  159.     def apply_edit_changes(self):
  160.  
  161.         """
  162.         Set the variables based on the controls
  163.         """
  164.  
  165.         # Abort if the parent reports failure of if the controls are locked
  166.         if not qtplugin.qtplugin.apply_edit_changes(self, False) or self.lock:
  167.             return False
  168.  
  169.         # Refresh the main window, so that changes become visible everywhere
  170.         self.experiment.main_window.refresh(self.name)
  171.  
  172.         # Report success
  173.         return True
  174.  
  175.     def edit_widget(self):
  176.  
  177.         """
  178.         Set the controls based on the variables
  179.         """
  180.  
  181.         # Lock the controls, otherwise a recursive loop might aris
  182.         # in which updating the controls causes the variables to be
  183.         # updated, which causes the controls to be updated, etc...
  184.         self.lock = True
  185.  
  186.         # Let the parent handle everything
  187.         qtplugin.qtplugin.edit_widget(self)
  188.  
  189.         # Unlock
  190.         self.lock = False
  191.  
  192.         # Return the _edit_widget
  193.         return self._edit_widget
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement