Factored out more functionality into ViewerItem
authorDmitriy Morozov <dmitriy@mrzv.org>
Sat, 05 Mar 2011 15:27:02 -0800
changeset 3 6f1239040a06
parent 2 56a5778c903c
child 4 d52927c0bf92
Factored out more functionality into ViewerItem
ViewerItem.py
points.py
--- a/ViewerItem.py	Sat Mar 05 15:09:50 2011 -0800
+++ b/ViewerItem.py	Sat Mar 05 15:27:02 2011 -0800
@@ -1,9 +1,13 @@
-from    PyQt4       import QtGui, QtCore 
+from    PyQt4       import QtGui, QtCore
 
 class ViewerItem(QtGui.QListWidgetItem):
     def __init__(self, name, parent = None):
         super(ViewerItem, self).__init__(name, parent, QtGui.QListWidgetItem.UserType)
 
+        self.color = QtGui.QColor(255, 0, 255)
+        self.visible = True
+        self.setChecked()
+
     def toggleVisible(self):
         self.visible = not self.visible
         self.setChecked()
@@ -13,6 +17,20 @@
             self.setCheckState(QtCore.Qt.Checked)
         else:
             self.setCheckState(QtCore.Qt.Unchecked)
-    
+        
+    def createMenu(self):
+        menu = QtGui.QMenu()
+        colorAction = menu.addAction("Color")
+        colorAction.command = lambda: self.colorCommand()
+        return menu
+
     def contextMenu(self, position):
-        pass
+        menu = self.createMenu()
+        action = menu.exec_(position)
+        if action: action.command()
+
+    def colorCommand(self):
+        color = QtGui.QColorDialog.getColor(self.color)
+        if color.isValid():
+            self.color = color
+
--- a/points.py	Sat Mar 05 15:09:50 2011 -0800
+++ b/points.py	Sat Mar 05 15:27:02 2011 -0800
@@ -45,28 +45,19 @@
                 if line.startswith('#'): continue
                 self.points.append(Point(map(float, line.split())))
         self.create_display_list()
-        self.color = QtGui.QColor(255, 0, 255)
-        self.visible = True
-        self.setChecked()
         self.center, self.min, self.max = centerMinMax(self.points)
 
     def create_display_list(self):
         self.display_list = glGenLists(1)
         glNewList(self.display_list, GL_COMPILE)
+        self.draw_points()
+        glEndList()
+
+    def draw_points(self):
         glBegin(GL_POINTS)
         for p in self.points:
             glVertex3f(p.x, p.y, p.z)
         glEnd()
-        glEndList()
-
-    def contextMenu(self, position):
-        menu = QtGui.QMenu()
-        colorAction = menu.addAction("Color")
-        action = menu.exec_(position)
-        if action == colorAction:
-            color = QtGui.QColorDialog.getColor(self.color)
-            if color.isValid():
-                self.color = color
 
     def draw(self):
         if not self.visible: return