nodebox_gui.py
Go to the documentation of this file.
00001 #import roslib; roslib.load_manifest('rcommander_core')
00002 import PyQt4.QtGui as qtg
00003 import PyQt4.QtCore as qtc
00004 import PyQt4.QtOpenGL as qtl
00005 from nodebox import graphics
00006 
00007 import time
00008 import rospy
00009 import threading
00010 
00011 class AnimationRunner(threading.Thread):
00012 
00013     def __init__(self, f):
00014         threading.Thread.__init__(self)    
00015         self.f = f
00016         self.should_stop = False
00017 
00018     def run(self):
00019         time.sleep(2.)
00020         r = rospy.Rate(30)
00021         while not rospy.is_shutdown() and not self.should_stop:
00022             self.f()
00023             r.sleep()
00024 
00025 
00026 class NodeBoxGUI:
00027 
00028     def __init__(self, graphics_view):
00029 
00030         #print 'GUI IS SHUTDOWN??1', rospy.is_shutdown()
00031         #add scene to QGraphicsView
00032         scene = qtg.QGraphicsScene()
00033         graphics_view.setViewport(qtl.QGLWidget())
00034         #print 'GUI IS SHUTDOWN??11', rospy.is_shutdown()
00035         self.drawing_widget = NodeBoxGUIHelper(graphics_view.viewport(), scene)
00036         #print 'GUI IS SHUTDOWN??111', rospy.is_shutdown()
00037         graphics_view.setScene(scene)
00038         # TODO: graphics_view._scene = scene
00039         #print 'GUI IS SHUTDOWN??112', rospy.is_shutdown()
00040         scene.setItemIndexMethod(qtg.QGraphicsScene.NoIndex)
00041         #print 'GUI IS SHUTDOWN??113', rospy.is_shutdown()
00042         scene.addItem(self.drawing_widget)
00043         #print 'GUI IS SHUTDOWN??2', rospy.is_shutdown()
00044 
00045         #Add NB to scene
00046         self.namespace = {}
00047         self.canvas = graphics.Canvas()
00048         #print 'text scale', QPixmap(1, 1).logicalDpiX() / 72.0
00049         self.canvas._setTextScaleFactor(qtg.QPixmap(1, 1).logicalDpiX() / 72.0)
00050         self.context = graphics.Context(self.canvas, self.namespace)
00051 
00052         #Setup the scene
00053         self.setup()
00054         self.animationTimer = qtc.QTimer(self)
00055         self.connect(self.animationTimer, qtc.SIGNAL("timeout()"), self._draw)
00056         self.animationTimer.start(1000.0 / self.canvas.speed)
00057         #print 'GUI IS SHUTDOWN??3', rospy.is_shutdown()
00058 
00059         #self.animation_runner = AnimationRunner(self._draw)
00060         #self.animation_runner.start()
00061 
00062     def stop_drawing(self):
00063         self.animationTimer.stop()
00064 
00065     def properties(self):
00066         properties = self.namespace
00067 
00068         pos = self.drawing_widget.mousePosition
00069         properties["MOUSEX"], properties["MOUSEY"] = pos.x(), pos.y()
00070         properties["mousedoubleclick"]   = self.drawing_widget.mousedoubleclick
00071         self.drawing_widget.mousedoubleclick = False
00072         properties["mousedown"]   = self.drawing_widget.mousedown
00073         properties["rightdown"]   = self.drawing_widget.rightdown
00074         properties["keydown"]     = self.drawing_widget.keydown
00075         properties["key"]         = self.drawing_widget.key
00076         properties["keycode"]     = self.drawing_widget.keycode
00077         properties["scrollwheel"] = self.drawing_widget.scrollwheel
00078         properties["wheeldelta"]  = self.drawing_widget.wheeldelta
00079         return properties
00080 
00081     def _draw(self):
00082         self.canvas.clear()
00083         self.draw(self.properties())
00084         self.drawing_widget.set_canvas(self.canvas)
00085         self.context._resetContext()
00086 
00087     def draw(self, properties):
00088         pass
00089 
00090     def setup(self):
00091         pass
00092 
00093 class NodeBoxGUIHelper(qtg.QGraphicsWidget):
00094 
00095     def __init__(self, viewport, scene, parent=None):
00096         qtg.QGraphicsWidget.__init__(self, parent)
00097         self.setFlag(qtg.QGraphicsItem.ItemClipsToShape, True)
00098         self.setFocusPolicy(qtc.Qt.StrongFocus)
00099 
00100         self.mousedown = False
00101         self.rightdown = False
00102         self.mousePosition = qtc.QPointF(0, 0)
00103         self.mousedoubleclick = False
00104         self.keydown = False
00105         self.key = None
00106         self.keycode = None        
00107         self.scrollwheel = False
00108         self.wheeldelta = 0.0
00109 
00110         self._dirty = True
00111         self._canvas = None
00112         self._viewPort = viewport
00113         self._scene = scene
00114         self._rect = qtc.QRectF(0, 0, 1000, 1000)
00115         self._shape = qtg.QPainterPath()
00116         self._shape.addRect(self._rect)
00117 
00118     def mousePressEvent(self, event):
00119         self.mousePosition = event.scenePos()
00120 
00121         if event.button() == qtc.Qt.LeftButton:
00122             self.mousedown = True 
00123 
00124         if event.button() == qtc.Qt.RightButton:
00125             self.rightdown = True
00126 
00127         self.setFocus()
00128 
00129     def mouseDoubleClickEvent(self, event):
00130         if event.button() == qtc.Qt.LeftButton:
00131             self.mousedoubleclick = True
00132             self.setFocus()
00133 
00134     def mouseMoveEvent(self, event):
00135         self.mousePosition = event.scenePos()
00136 
00137     def mouseReleaseEvent(self, event): 
00138         self.mousePosition = event.scenePos()
00139         if event.button() == qtc.Qt.LeftButton: 
00140                 self.mousedown = False 
00141         if event.button() == qtc.Qt.RightButton: 
00142             self.rightdown = False
00143 
00144     def keyPressEvent(self, event): 
00145         self.keydown = True 
00146         self.key = event.text() 
00147         self.keycode = event.key() 
00148 
00149     def keyReleaseEvent(self, event): 
00150         self.keydown = False 
00151         self.key = event.text() 
00152         self.keycode = event.key() 
00153 
00154     def wheelEvent(self, event): 
00155         self.scrollwheel = True 
00156         self.wheeldelta = event.delta() / 120.
00157 
00158     ######################################################################
00159     # Magical drawing functions
00160     ######################################################################
00161 
00162     def get_canvas(self):
00163         return self._canvas
00164 
00165     def set_canvas(self, canvas):
00166         self._canvas = canvas
00167         if canvas is not None:
00168             x, y, width, height = self._rect.getRect()
00169             size = int(width), int(height)
00170             if size != self.get_canvas().size:
00171                 width, height = self.get_canvas().size
00172                 scene = self._scene
00173                 self._rect = rect = qtc.QRectF(0, 0, width, height)
00174                 scene.setSceneRect(rect)
00175                 self._shape = shape = qtg.QPainterPath()
00176                 shape.addRect(rect)
00177         #self._check_cache()
00178         self._dirty = True      #signal that we want to be redrawn to paint
00179         self._viewPort.update() #tell QT that we want to be redrawn
00180 
00181     ##########################################################################################
00182     # Must be implemented for mouse events
00183     ##########################################################################################
00184     def boundingRect(self):
00185         return self._rect
00186 
00187     def shape(self):
00188         return self._shape
00189 
00190     def paint(self, painter, item, widget):
00191         if self._dirty:
00192             if self.get_canvas() is None: 
00193                 return
00194             painter.save()
00195             self.get_canvas().draw(painter)
00196             painter.restore()
00197 
00198     #def _check_cache(self):
00199     #    cacheMode = self.cacheMode()
00200     #    DeviceCoordinateCache = QGraphicsItem.DeviceCoordinateCache
00201     #    NoCache = QGraphicsItem.NoCache
00202     #    if self.get_canvas() is not None:
00203     #        #if self.document.animationTimer is not None:
00204     #        #    cache = False
00205     #        #elif len(self.get_canvas()) > 400:
00206     #        if len(self.get_canvas()) > 400:
00207     #            x, y, width, height = self._rect.getRect()
00208     #            cache = True
00209     #            if width > 1000 or height > 1000: #or \
00210     #                #not (width * self.zoom <= 1200 and height * self.zoom <= 1200):
00211     #                cache = False
00212     #        else:
00213     #            cache = False
00214     #        if cache and cacheMode != DeviceCoordinateCache:
00215     #            self.setCacheMode(DeviceCoordinateCache)
00216     #        elif not cache and cacheMode != NoCache:
00217     #            self.setCacheMode(NoCache)
00218     #    elif cacheMode != NoCache:
00219     #        self.setCacheMode(QNoCache)
00220 


rcommander
Author(s): Hai Nguyen (haidai@gmail.com)
autogenerated on Thu Nov 28 2013 11:46:34