Straightened out QApplication problems (made it global) dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Thu, 07 Jun 2012 10:52:15 -0700
branchdev
changeset 261 7b846a522bed
parent 260 27c47fc33468
child 262 ee064472dc1f
Straightened out QApplication problems (made it global)
bindings/python/dionysus/viewer/PyGLWidget.py
bindings/python/dionysus/viewer/__init__.py
bindings/python/dionysus/viewer/complex.py
bindings/python/dionysus/viewer/complex2d.py
bindings/python/dionysus/viewer/complex3d.py
bindings/python/dionysus/viewer/diagram.py
--- a/bindings/python/dionysus/viewer/PyGLWidget.py	Thu Jun 07 10:11:01 2012 -0700
+++ b/bindings/python/dionysus/viewer/PyGLWidget.py	Thu Jun 07 10:52:15 2012 -0700
@@ -77,7 +77,7 @@
 
     def initializeGL(self):
         # OpenGL state
-        glClearColor(0.0, 0.0, 0.0, 0.0)
+        glClearColor(1.0, 1.0, 1.0, 0.0)
         glEnable(GL_DEPTH_TEST)
         self.reset_view()
 
@@ -129,7 +129,7 @@
         glMatrixMode(GL_MODELVIEW)
         glLoadMatrixd(self.modelview_matrix_)
         self.updateGL()
-   
+
     def translate(self, _trans):
         # Translate the object by _trans
         # Update modelview_matrix_
@@ -208,7 +208,7 @@
 
     def wheelEvent(self, _event):
         # Use the mouse wheel to zoom in/out
-        
+
         d = - float(_event.delta()) / 200.0 * self.radius_
         self.translate([0.0, 0.0, d])
         self.updateGL()
@@ -224,7 +224,7 @@
         if ((newPoint2D.x() < 0) or (newPoint2D.x() > self.width()) or
             (newPoint2D.y() < 0) or (newPoint2D.y() > self.height())):
             return
-        
+
         # Left button: rotate around center_
         # Middle button: translate object
         # Left & middle button: zoom in/out
@@ -267,13 +267,13 @@
                              -2.0 * dy / h * up / n * z,
                              0.0] )
 
-    
+
         # rotate
         elif (_event.buttons() & QtCore.Qt.LeftButton):
             if (not self.isInRotation_):
                 self.isInRotation_ = True
                 self.rotationBeginEvent.emit()
-       
+
             axis = [0.0, 0.0, 0.0]
             angle = 0.0
 
--- a/bindings/python/dionysus/viewer/__init__.py	Thu Jun 07 10:11:01 2012 -0700
+++ b/bindings/python/dionysus/viewer/__init__.py	Thu Jun 07 10:52:15 2012 -0700
@@ -1,3 +1,17 @@
-from diagram    import *
-from complex    import *
-from complex3d  import *
+from    diagram     import show_diagram as _show_diagram
+from    complex2d   import show_complex_2D
+from    complex3d   import show_complex_3D
+
+from    PyQt4       import QtGui
+
+_app = QtGui.QApplication([])
+
+def show_complex(points, complex = None, values = None):
+    if len(points[0]) == 2:
+        show_complex_2D(points, complex, values, app = _app)
+    if len(points[0]) == 3:
+        show_complex_3D(points, complex, values, app = _app)
+    #_app.exec_()
+
+def show_diagram(dgm):
+    _show_diagram(dgm, _app)
--- a/bindings/python/dionysus/viewer/complex.py	Thu Jun 07 10:11:01 2012 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,75 +0,0 @@
-from    PyQt4       import QtGui, QtCore
-from    dionysus    import Simplex
-
-class ComplexViewer(QtGui.QGraphicsView):
-    def __init__(self, points, complex = None, values = None):
-        super(QtGui.QGraphicsView, self).__init__()
-
-        self.points = points
-        if complex:
-            self.complex = [s for s in complex]
-        else:
-            # Create vertex simplices if no complex provided
-            self.complex = [Simplex([i]) for i in xrange(len(self.points))]
-
-        if not values:
-            values = [0]*len(self.points)
-        self.maxval, self.minval = max(values), min(values)
-
-        self.setRenderHint(QtGui.QPainter.Antialiasing)
-        self.scene = QtGui.QGraphicsScene(self)
-        self.setScene(self.scene)
-
-        minx = min(p[0] for p in points)
-        miny = min(p[1] for p in points)
-        maxx = max(p[0] for p in points)
-        maxy = max(p[1] for p in points)
-
-        radius = min(maxx - minx, maxy - miny)/100
-        self.scene.setSceneRect(minx - 10*radius, miny - 10*radius, (maxx - minx) + 20*radius, (maxy - miny) + 20*radius)
-
-        self.complex.sort(lambda s1, s2: -cmp(s1.dimension(), s2.dimension()))
-        for s in self.complex:
-            vertices = [v for v in s.vertices]
-            if s.dimension() == 0:              # point
-                p = points[vertices[0]]
-                v = values[vertices[0]]
-                item = QtGui.QGraphicsEllipseItem(p[0] - radius/2,p[1] - radius/2,radius,radius)
-                color = self.colormap(v)
-                item.setBrush(QtGui.QBrush(color))
-                item.setPen(QtGui.QPen(color))
-            elif s.dimension() == 1:            # edge
-                p0 = points[vertices[0]]
-                p1 = points[vertices[1]]
-                item = QtGui.QGraphicsLineItem(p0[0], p0[1], p1[0], p1[1])
-            else:                               # higher-d simplex
-                pts = [QtCore.QPointF(points[v][0], points[v][1]) for v in vertices]
-                item = QtGui.QGraphicsPolygonItem(QtGui.QPolygonF(pts))
-                item.setBrush(QtCore.Qt.blue)
-
-            self.scene.addItem(item)
-
-        # Flip y-axis
-        self.scale(1,-1)
-
-        # Set the correct view
-        rect = self.scene.itemsBoundingRect()
-        self.fitInView(rect, QtCore.Qt.KeepAspectRatio)
-
-    def colormap(self, v):
-        if self.maxval <= self.minval:
-            t = 0
-        else:
-            t = (v - self.minval)/(self.maxval - self.minval)
-        c = QtGui.QColor()
-        c.setHsv(int(t*255), 255, 255)
-        return c
-
-# TODO: cycle
-def show_complex_2D(points, complex = None, values = None):
-    app = QtGui.QApplication([])
-    view = ComplexViewer(points, complex, values)
-    view.show()
-    view.raise_()
-    app.exec_()
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bindings/python/dionysus/viewer/complex2d.py	Thu Jun 07 10:52:15 2012 -0700
@@ -0,0 +1,74 @@
+from    PyQt4       import QtGui, QtCore
+from    dionysus    import Simplex
+
+class ComplexViewer(QtGui.QGraphicsView):
+    def __init__(self, points, complex = None, values = None):
+        super(QtGui.QGraphicsView, self).__init__()
+
+        self.points = points
+        if complex:
+            self.complex = [s for s in complex]
+        else:
+            # Create vertex simplices if no complex provided
+            self.complex = [Simplex([i]) for i in xrange(len(self.points))]
+
+        if not values:
+            values = [0]*len(self.points)
+        self.maxval, self.minval = max(values), min(values)
+
+        self.setRenderHint(QtGui.QPainter.Antialiasing)
+        self.scene = QtGui.QGraphicsScene(self)
+        self.setScene(self.scene)
+
+        minx = min(p[0] for p in points)
+        miny = min(p[1] for p in points)
+        maxx = max(p[0] for p in points)
+        maxy = max(p[1] for p in points)
+
+        radius = min(maxx - minx, maxy - miny)/100
+        self.scene.setSceneRect(minx - 10*radius, miny - 10*radius, (maxx - minx) + 20*radius, (maxy - miny) + 20*radius)
+
+        self.complex.sort(lambda s1, s2: -cmp(s1.dimension(), s2.dimension()))
+        for s in self.complex:
+            vertices = [v for v in s.vertices]
+            if s.dimension() == 0:              # point
+                p = points[vertices[0]]
+                v = values[vertices[0]]
+                item = QtGui.QGraphicsEllipseItem(p[0] - radius/2,p[1] - radius/2,radius,radius)
+                color = self.colormap(v)
+                item.setBrush(QtGui.QBrush(color))
+                item.setPen(QtGui.QPen(color))
+            elif s.dimension() == 1:            # edge
+                p0 = points[vertices[0]]
+                p1 = points[vertices[1]]
+                item = QtGui.QGraphicsLineItem(p0[0], p0[1], p1[0], p1[1])
+            else:                               # higher-d simplex
+                pts = [QtCore.QPointF(points[v][0], points[v][1]) for v in vertices]
+                item = QtGui.QGraphicsPolygonItem(QtGui.QPolygonF(pts))
+                item.setBrush(QtCore.Qt.blue)
+
+            self.scene.addItem(item)
+
+        # Flip y-axis
+        self.scale(1,-1)
+
+        # Set the correct view
+        rect = self.scene.itemsBoundingRect()
+        self.fitInView(rect, QtCore.Qt.KeepAspectRatio)
+
+    def colormap(self, v):
+        if self.maxval <= self.minval:
+            t = 0
+        else:
+            t = (v - self.minval)/(self.maxval - self.minval)
+        c = QtGui.QColor()
+        c.setHsv(int(t*255), 255, 255)
+        return c
+
+# TODO: cycle
+def show_complex_2D(points, complex = None, values = None, app = None):
+    #app = QtGui.QApplication([])
+    view = ComplexViewer(points, complex, values)
+    view.show()
+    view.raise_()
+    app.exec_()
--- a/bindings/python/dionysus/viewer/complex3d.py	Thu Jun 07 10:11:01 2012 -0700
+++ b/bindings/python/dionysus/viewer/complex3d.py	Thu Jun 07 10:52:15 2012 -0700
@@ -14,22 +14,29 @@
             # Create vertex simplices if no complex provided
             self.complex = [Simplex([i]) for i in xrange(len(self.points))]
 
+        self.values = values
         if not values:
-            values = [0]*len(self.points)
-        self.maxval, self.minval = max(values), min(values)
+            self.values = [0]*len(self.points)
+        self.maxval, self.minval = max(self.values), min(self.values)
 
     def paintGL(self):
         PyGLWidget.paintGL(self)
         self.complex.sort(lambda s1, s2: -cmp(s1.dimension(), s2.dimension()))
         for s in self.complex:
             vertices = [v for v in s.vertices]
-        #    if s.dimension() == 0:              # point
-        #        p = points[vertices[0]]
-        #        v = values[vertices[0]]
-        #        item = QtGui.QGraphicsEllipseItem(p[0] - radius/2,p[1] - radius/2,radius,radius)
-        #        color = self.colormap(v)
-        #        item.setBrush(QtGui.QBrush(color))
-        #        item.setPen(QtGui.QPen(color))
+            if s.dimension() == 0:              # point
+                p = self.points[vertices[0]]
+                v = self.values[vertices[0]]
+
+                glPointSize(3.0)
+                c = self.colormap(v)
+                cr = float(c.red())/255
+                cg = float(c.green())/255
+                cb = float(c.blue())/255
+                glColor3f(cr, cg, cb)
+                glBegin(GL_POINTS)
+                glVertex3f(p[0],p[1],p[2])
+                glEnd()
             if s.dimension() == 1:            # edge
                 p0 = self.points[vertices[0]]
                 p1 = self.points[vertices[1]]
@@ -44,7 +51,7 @@
                 p1 = self.points[vertices[1]]
                 p2 = self.points[vertices[2]]
 
-                glColor3f(1,0,0)
+                glColor3f(1,1,0)
                 glBegin(GL_TRIANGLES)
                 glVertex3f(p0[0],p0[1],p0[2])
                 glVertex3f(p1[0],p1[1],p1[2])
@@ -62,10 +69,9 @@
         return c
 
 # TODO: cycle
-def show_complex_3D(points, complex = None, values = None):
-    app = QtGui.QApplication([])
+def show_complex_3D(points, complex = None, values = None, app = None):
+    #app = QtGui.QApplication([])
     view = ComplexViewer3D(points, complex, values)
     view.show()
     view.raise_()
     app.exec_()
-
--- a/bindings/python/dionysus/viewer/diagram.py	Thu Jun 07 10:11:01 2012 -0700
+++ b/bindings/python/dionysus/viewer/diagram.py	Thu Jun 07 10:52:15 2012 -0700
@@ -56,10 +56,14 @@
 
     def draw_axes(self, minx, miny, maxx, maxy):
         # Draw axes and diagonal
-        self.scene.addItem(QtGui.QGraphicsLineItem(0,0, maxx, 0))
-        self.scene.addItem(QtGui.QGraphicsLineItem(minx,0, 0, 0))
-        self.scene.addItem(QtGui.QGraphicsLineItem(0,0, 0, maxy))
-        self.scene.addItem(QtGui.QGraphicsLineItem(0,miny, 0, 0))
+        if maxx > 0:
+            self.scene.addItem(QtGui.QGraphicsLineItem(0,0, maxx, 0))
+        if minx < 0:
+            self.scene.addItem(QtGui.QGraphicsLineItem(minx,0, 0, 0))
+        if maxy > 0:
+            self.scene.addItem(QtGui.QGraphicsLineItem(0,0, 0, maxy))
+        if miny < 0:
+            self.scene.addItem(QtGui.QGraphicsLineItem(0,miny, 0, 0))
         self.scene.addItem(QtGui.QGraphicsLineItem(0,0, min(maxx, maxy), min(maxx, maxy)))
         self.scene.addItem(QtGui.QGraphicsLineItem(max(minx,miny), max(minx,miny), 0,0))
 
@@ -76,8 +80,8 @@
             self.scene.addItem(line)
 
 
-def show_diagram(dgm):
-    app = QtGui.QApplication([])
+def show_diagram(dgm, app):
+    #app = QtGui.QApplication([])
     view = DiagramViewer(dgm)
     view.show()
     view.raise_()