diff lightsim/openglsim.py @ 466:f69ba5ac17c5

qt version of lightsim now works
author drewp@bigasterisk.com
date Mon, 01 Sep 2008 05:36:24 +0000
parents 60b49f6c2027
children 61164aaadbaa
line wrap: on
line diff
--- a/lightsim/openglsim.py	Mon Sep 01 05:35:58 2008 +0000
+++ b/lightsim/openglsim.py	Mon Sep 01 05:36:24 2008 +0000
@@ -8,7 +8,8 @@
 import Image
 from louie import dispatcher
 
-from PyQt4 import QtCore, QtOpenGL
+from PyQt4.QtCore import QSize
+from PyQt4.QtOpenGL import QGLWidget
 from OpenGL.GL import *
 
 def xxxdrawWithAlpha(imgString, w, h, alpha):
@@ -21,15 +22,14 @@
     #glBlendColor(1, 1, 1, mag) # needs ARB_imaging
     glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString)
 
-class Surface(QtOpenGL.QGLWidget):
+class Surface(QGLWidget):
     """widget that adds multiple image files together with adjustable scales"""
-    def __init__(self, parent, filenames, width=512, height=270,
-                 imgRescaleTo=None):
+    def __init__(self, parent, filenames, imgRescaleTo=None):
         """
         imgRescaleTo can be a length of pixels to reduce all the input
         images into. Try 64 for a low res drawing.
         """
-        QtOpenGL.QGLWidget.__init__(self, parent)
+        QGLWidget.__init__(self, parent)
 
         self.levels = {} # filename : brightness
   
@@ -37,6 +37,7 @@
         for filename in filenames:
             print "open", filename
             im = Image.open(filename)
+            self.origImageSize = im.size[0], im.size[1]
             if imgRescaleTo:
                 im.thumbnail((imgRescaleTo, imgRescaleTo))
             im = im.transpose(Image.FLIP_TOP_BOTTOM)
@@ -44,8 +45,6 @@
             self.imageHeight = im.size[1]
             self.image[filename] = im.convert("RGBA").tostring()
   
-        #self.set_centerpoint(0, 0, 0)
-
     def initializeGL(self): 
         glDisable(GL_CULL_FACE)
         glShadeModel(GL_FLAT)
@@ -53,11 +52,8 @@
         import OpenGL
         print OpenGL.__version__
        
-#    def minimumSizeHint(self):
-#        return QtCore.QSize(512, 512)
-
-#    def sizeHint(self):
-#        return QtCore.QSize(512, 512)
+    def sizeHint(self):
+        return QSize(*self.origImageSize)
 
     def paintGL(self):
         """you set self.levels to dict and call tkRedraw"""
@@ -75,20 +71,23 @@
 
         # drawing to glAccum might be good
         layerTimes = []
+        layers = 0
         for filename, mag in self.levels.items():
             t = time.time()
-            self.drawWithAlpha(self.image[filename],
-                               self.imageWidth, self.imageHeight, mag)
+            layers += self.drawWithAlpha(self.image[filename],
+                                         self.imageWidth, self.imageHeight, mag)
             layerTimes.append(time.time() - t)
 
+        dispatcher.send("status", key="visibleLayers", value=str(layers))
+
         dispatcher.send("status", key="redraw",
                         value="%.1fms" % ((time.time() - start) * 1000))
         
 
     def drawWithAlpha(self, imgString, w, h, alpha):
-        """without opengl extensions"""
+        """without opengl extensions. Returns number of layers drawn"""
         if alpha == 0:
-            return
+            return 0
         t = time.time()
         ar = num.reshape(num.fromstring(imgString, dtype='uint8'),
                          (w * h, 4))
@@ -109,6 +108,7 @@
         glDrawPixels(w, h,
                      GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
         #print "  draw", time.time() - t
+        return 1
 
     def newLevels(self, event=None, levels=None):
         if levels != self.levels: