11#!/usr/bin/env python
22
3- ##Copyright 2008-2011 Thomas Paviot (tpaviot@gmail.com)
3+ ##Copyright 2009-2014 Thomas Paviot (tpaviot@gmail.com)
44##
55##This file is part of pythonOCC.
66##
1717##You should have received a copy of the GNU Lesser General Public License
1818##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
1919
20+
2021import os
2122
2223import sys
23- from PyQt4 import QtCore , QtGui , QtOpenGL
24+ from PySide import QtCore , QtGui , QtOpenGL
2425import OCCViewer
2526
27+
2628class point (object ):
27- def __init__ (self ,obj = None ):
28- self .x = 0
29- self .y = 0
30- if obj is not None :
31- self .set (obj )
29+ def __init__ (self , obj = None ):
30+ self .x = 0
31+ self .y = 0
32+ if obj is not None :
33+ self .set (obj )
34+
35+ def set (self , obj ):
36+ self .x = obj .x ()
37+ self .y = obj .y ()
3238
33- def set (self ,obj ):
34- self .x = obj .x ()
35- self .y = obj .y ()
3639
3740class qtBaseViewer (QtOpenGL .QGLWidget ):
3841 ''' The base Qt Widget for an OCC viewer
3942 '''
40- def __init__ (self , parent = None ):
41- QtOpenGL .QGLWidget .__init__ (self ,parent )
43+ def __init__ (self , parent = None ):
44+ QtOpenGL .QGLWidget .__init__ (self , parent )
4245 self ._display = None
4346 self ._inited = False
44- self .setMouseTracking (True ) # enable Mouse Tracking
45- self .setFocusPolicy (QtCore .Qt .WheelFocus )# Strong focus
47+ self .setMouseTracking (True ) # enable Mouse Tracking
48+ self .setFocusPolicy (QtCore .Qt .WheelFocus ) # Strong focus
4649 # On X11, setting this attribute will disable all double buffering
4750 self .setAttribute (QtCore .Qt .WA_PaintOnScreen )
4851 # setting this flag implicitly disables double buffering for the widget
4952 self .setAttribute (QtCore .Qt .WA_NoSystemBackground )
5053
5154 def GetHandle (self ):
5255 return int (self .winId ())
53-
56+
5457 def resizeEvent (self , event ):
5558 if self ._inited :
5659 self ._display .OnResize ()
5760
5861 def paintEngine (self ):
5962 return None
6063
64+
6165class qtViewer3d (qtBaseViewer ):
6266 def __init__ (self , * kargs ):
6367 qtBaseViewer .__init__ (self , * kargs )
@@ -85,8 +89,7 @@ def set_shade_mode():
8589 self ._display .DisableAntiAliasing ()
8690 self ._display .SetModeShaded ()
8791
88- self ._key_map = {
89- ord ('W' ): self ._display .SetModeWireFrame ,
92+ self ._key_map = {ord ('W' ): self ._display .SetModeWireFrame ,
9093 ord ('S' ): set_shade_mode ,
9194 ord ('A' ): self ._display .EnableAntiAliasing ,
9295 ord ('B' ): self ._display .DisableAntiAliasing ,
@@ -96,68 +99,64 @@ def set_shade_mode():
9699 ord ('G' ): self ._display .SetSelectionMode
97100 }
98101
99- def keyPressEvent (self ,event ):
102+ def keyPressEvent (self , event ):
100103 code = event .key ()
101- if self ._key_map . has_key ( code ) :
104+ if code in self ._key_map :
102105 self ._key_map [code ]()
103106 else :
104- print 'key' ,code ,' not mapped to any function'
107+ print 'key' , code , ' not mapped to any function'
105108
106109 def Test (self ):
107110 if self ._inited :
108111 self ._display .Test ()
109-
112+
110113 def focusInEvent (self , event ):
111- #print 'focus in!!'
112114 if self ._inited :
113115 self ._display .Repaint ()
114116
115117 def focusOutEvent (self , event ):
116- #print 'focus out'
117118 if self ._inited :
118119 self ._display .Repaint ()
119- #print 'repainted'
120120
121121 def paintEvent (self , event ):
122122 if self ._inited :
123123 self ._display .Repaint ()
124124 if self ._drawbox :
125125 painter = QtGui .QPainter (self )
126- painter .setPen (QtGui .QPen (QtGui .QColor (0 ,0 , 0 ), 1 ))
126+ painter .setPen (QtGui .QPen (QtGui .QColor (0 , 0 , 0 ), 1 ))
127127 rect = QtCore .QRect (* self ._drawbox )
128128 painter .drawRect (rect )
129129 painter .end ()
130130
131131 def ZoomAll (self , evt ):
132132 self ._display .Zoom_FitAll ()
133133
134- def wheelEvent (self ,event ):
135- if event .delta ()> 0 :
134+ def wheelEvent (self , event ):
135+ if event .delta () > 0 :
136136 zoom_factor = 2
137137 else :
138138 zoom_factor = 0.5
139139 self ._display .Repaint ()
140140 self ._display .ZoomFactor (zoom_factor )
141-
142- def dragMoveEvent (self ,event ):
143- pass #print 'dragmove event'
141+
142+ def dragMoveEvent (self , event ):
143+ pass
144144
145145 def mousePressEvent (self , event ):
146- #print 'mouse press event'
147146 self .setFocus ()
148147 self .dragStartPos = point (event .pos ())
149- self ._display .StartRotation (self .dragStartPos .x ,self .dragStartPos .y )
148+ self ._display .StartRotation (self .dragStartPos .x , self .dragStartPos .y )
150149
151150 def mouseReleaseEvent (self , event ):
152151 pt = point (event .pos ())
153152 if event .button () == QtCore .Qt .LeftButton :
154153 pt = point (event .pos ())
155154 if self ._select_area :
156155 [Xmin , Ymin , dx , dy ] = self ._drawbox
157- selected_shapes = self ._display .SelectArea (Xmin ,Ymin ,Xmin + dx ,Ymin + dy )
156+ selected_shapes = self ._display .SelectArea (Xmin , Ymin , Xmin + dx , Ymin + dy )
158157 self ._select_area = False
159158 else :
160- self ._display .Select (pt .x ,pt .y )
159+ self ._display .Select (pt .x , pt .y )
161160 elif event .button () == QtCore .Qt .RightButton :
162161 if self ._zoom_area :
163162 [Xmin , Ymin , dx , dy ] = self ._drawbox
@@ -166,14 +165,14 @@ def mouseReleaseEvent(self, event):
166165
167166 def DrawBox (self , event ):
168167 tolerance = 2
169- pt = point ( event .pos () )
168+ pt = point (event .pos ())
170169 dx = pt .x - self .dragStartPos .x
171170 dy = pt .y - self .dragStartPos .y
172- if abs ( dx ) <= tolerance and abs ( dy ) <= tolerance :
171+ if abs (dx ) <= tolerance and abs (dy ) <= tolerance :
173172 return
174173 self .repaint ()
175- self ._drawbox = [self .dragStartPos .x , self .dragStartPos .y , dx , dy ]
176-
174+ self ._drawbox = [self .dragStartPos .x , self .dragStartPos .y , dx , dy ]
175+
177176 def mouseMoveEvent (self , evt ):
178177 pt = point (evt .pos ())
179178 buttons = int (evt .buttons ())
@@ -182,7 +181,7 @@ def mouseMoveEvent(self, evt):
182181 if (buttons == QtCore .Qt .LeftButton and not modifiers == QtCore .Qt .ShiftModifier ):
183182 dx = pt .x - self .dragStartPos .x
184183 dy = pt .y - self .dragStartPos .y
185- self ._display .Rotation (pt .x ,pt .y )
184+ self ._display .Rotation (pt .x , pt .y )
186185 self ._drawbox = False
187186 # DYNAMIC ZOOM
188187 elif (buttons == QtCore .Qt .RightButton and not modifiers == QtCore .Qt .ShiftModifier ):
@@ -197,47 +196,34 @@ def mouseMoveEvent(self, evt):
197196 dy = pt .y - self .dragStartPos .y
198197 self .dragStartPos .x = pt .x
199198 self .dragStartPos .y = pt .y
200- self ._display .Pan (dx ,- dy )
199+ self ._display .Pan (dx , - dy )
201200 self ._drawbox = False
202201 # DRAW BOX
203- elif (buttons == QtCore .Qt .RightButton and modifiers == QtCore .Qt .ShiftModifier ): # ZOOM WINDOW
202+ # ZOOM WINDOW
203+ elif (buttons == QtCore .Qt .RightButton and modifiers == QtCore .Qt .ShiftModifier ):
204204 self ._zoom_area = True
205205 self .DrawBox (evt )
206- elif (buttons == QtCore .Qt .LeftButton and modifiers == QtCore .Qt .ShiftModifier ): # SELECT AREA
206+ # SELECT AREA
207+ elif (buttons == QtCore .Qt .LeftButton and modifiers == QtCore .Qt .ShiftModifier ):
207208 self ._select_area = True
208209 self .DrawBox (evt )
209210 else :
210211 self ._drawbox = False
211- self ._display .MoveTo (pt .x ,pt .y )
212+ self ._display .MoveTo (pt .x , pt .y )
213+
212214
213215def Test3d ():
214- class MainWindow (QtGui .QMainWindow ):
215- def __init__ (self , * args ):
216- apply (QtGui .QMainWindow .__init__ ,(self ,)+ args )
217- self .canva = qtViewer3d (self )
218- self .setWindowTitle (self .tr ("qtDisplay3d sample" ))
219- self .resize (640 ,480 )
220- self .canva = qtViewer3d (self )
221- self .setCentralWidget (self .canva )
222- app = QtGui .QApplication (sys .argv )
223- win = MainWindow ()
224- win .show ()
225- win .canva .InitDriver ()
226- win .canva ._display .Test ()
227- app .exec_ ()
228-
229- def Test3d_bis ():
230216 class AppFrame (QtGui .QWidget ):
231217 def __init__ (self , parent = None ):
232- QtGui .QWidget .__init__ (self ,parent )
218+ QtGui .QWidget .__init__ (self , parent )
233219 self .setWindowTitle (self .tr ("qtDisplay3d sample" ))
234220 self .resize (640 , 480 )
235221 self .canva = qtViewer3d (self )
236222 mainLayout = QtGui .QHBoxLayout ()
237223 mainLayout .addWidget (self .canva )
238- mainLayout .setMargin ( 0 )
239- self .setLayout (mainLayout )
240-
224+ mainLayout .setContentsMargins ( 0 , 0 , 0 , 0 )
225+ self .setLayout (mainLayout )
226+
241227 def runTests (self ):
242228 self .canva ._display .Test ()
243229
@@ -248,5 +234,5 @@ def runTests(self):
248234 frame .runTests ()
249235 sys .exit (app .exec_ ())
250236
251- if __name__ == "__main__" :
252- Test3d ()
237+ if __name__ == "__main__" :
238+ Test3d_bis ()
0 commit comments