11#!/usr/bin/env python
22
3- ##Copyright 2009-2014 Thomas Paviot (tpaviot@gmail.com)
3+ ##Copyright 2009-2019 Thomas Paviot (tpaviot@gmail.com)
44##
55##This file is part of pythonOCC.
66##
2727from OCC .Display .backend import get_qt_modules
2828
2929QtCore , QtGui , QtWidgets , QtOpenGL = get_qt_modules ()
30+
3031# check if signal available, not available
3132# on PySide
3233HAVE_PYQT_SIGNAL = hasattr (QtCore , 'pyqtSignal' )
3839class qtBaseViewer (QtOpenGL .QGLWidget ):
3940 ''' The base Qt Widget for an OCC viewer
4041 '''
41-
4242 def __init__ (self , parent = None ):
43- QtOpenGL . QGLWidget . __init__ ( self , parent )
43+ super ( qtBaseViewer , self ). __init__ ( parent )
4444 self ._display = None
4545 self ._inited = False
4646
4747 # enable Mouse Tracking
4848 self .setMouseTracking (True )
49+
4950 # Strong focus
5051 self .setFocusPolicy (QtCore .Qt .WheelFocus )
5152
5253 # required for overpainting the widget
5354 self .setAttribute (QtCore .Qt .WA_PaintOnScreen )
5455 self .setAttribute (QtCore .Qt .WA_NoSystemBackground )
56+
5557 self .setAutoFillBackground (False )
5658
5759 def GetHandle (self ):
5860 ''' returns an the identifier of the GUI widget.
5961 It must be an integer
6062 '''
6163 win_id = self .winId () # this returns either an int or voitptr
62-
6364 if "%s" % type (win_id ) == "<type 'PyCObject'>" : # PySide
6465 ### with PySide, self.winId() does not return an integer
6566 if sys .platform == "win32" :
@@ -104,7 +105,7 @@ def __init__(self, *kargs):
104105 self ._rightisdown = False
105106 self ._selection = None
106107 self ._drawtext = True
107- self ._qApp = QtWidgets .QApplication .instance ()
108+ self ._qApp = QtWidgets .QApplication .instance ()
108109 self ._key_map = {}
109110 self ._current_cursor = "arrow"
110111 self ._available_cursors = {}
@@ -119,19 +120,19 @@ def qApp(self, value):
119120 self ._qApp = value
120121
121122 def InitDriver (self ):
122- self ._display = OCCViewer .Viewer3d (self .GetHandle ())
123+ self ._display = OCCViewer .Viewer3d (window_handle = self .GetHandle (), parent = self )
123124 self ._display .Create ()
124125 # background gradient
125- self ._display .set_bg_gradient_color (206 , 215 , 222 , 128 , 128 , 128 )
126- # background gradient
127- self ._display .display_trihedron ()
128126 self ._display .SetModeShaded ()
129- self ._display .DisableAntiAliasing ()
130127 self ._inited = True
131128 # dict mapping keys to functions
132- self ._SetupKeyMap ()
133- #
134- self ._display .thisown = False
129+ self ._key_map = {ord ('W' ): self ._display .SetModeWireFrame ,
130+ ord ('S' ): self ._display .SetModeShaded ,
131+ ord ('A' ): self ._display .EnableAntiAliasing ,
132+ ord ('B' ): self ._display .DisableAntiAliasing ,
133+ ord ('H' ): self ._display .SetModeHLR ,
134+ ord ('F' ): self ._display .FitAll ,
135+ ord ('G' ): self ._display .SetSelectionMode }
135136 self .createCursors ()
136137
137138 def createCursors (self ):
@@ -153,25 +154,14 @@ def createCursors(self):
153154
154155 self ._current_cursor = "arrow"
155156
156- def _SetupKeyMap (self ):
157- self ._key_map = {ord ('W' ): self ._display .SetModeWireFrame ,
158- ord ('S' ): self ._display .SetModeShaded ,
159- ord ('A' ): self ._display .EnableAntiAliasing ,
160- ord ('B' ): self ._display .DisableAntiAliasing ,
161- ord ('H' ): self ._display .SetModeHLR ,
162- ord ('F' ): self ._display .FitAll ,
163- ord ('G' ): self ._display .SetSelectionMode }
164-
165157 def keyPressEvent (self , event ):
166158 code = event .key ()
167159 if code in self ._key_map :
168160 self ._key_map [code ]()
161+ elif code in range (256 ):
162+ log .info ('key: "%s"(code %i) not mapped to any function' % (chr (code ), code ))
169163 else :
170- log .info ("key: %s \n not mapped to any function" , code )
171-
172- def Test (self ):
173- if self ._inited :
174- self ._display .Test ()
164+ log .info ('key: code %i not mapped to any function' % code )
175165
176166 def focusInEvent (self , event ):
177167 if self ._inited :
@@ -182,20 +172,14 @@ def focusOutEvent(self, event):
182172 self ._display .Repaint ()
183173
184174 def paintEvent (self , event ):
185- if self ._inited :
186- self ._display .Context .UpdateCurrentViewer ()
187- # important to allow overpainting of the OCC OpenGL context in Qt
188- self .swapBuffers ()
189-
190175 if self ._drawbox :
176+ self ._display .Repaint ()
177+ self ._display .Repaint ()
191178 painter = QtGui .QPainter (self )
192- painter .setPen (QtGui .QPen (QtGui .QColor (0 , 0 , 0 ), 1 ))
179+ painter .setPen (QtGui .QPen (QtGui .QColor (0 , 0 , 0 ), 2 ))
193180 rect = QtCore .QRect (* self ._drawbox )
194181 painter .drawRect (rect )
195182
196- def ZoomAll (self , evt ):
197- self ._display .FitAll ()
198-
199183 def wheelEvent (self , event ):
200184 try : # PyQt4/PySide
201185 delta = event .delta ()
@@ -205,12 +189,8 @@ def wheelEvent(self, event):
205189 zoom_factor = 2.
206190 else :
207191 zoom_factor = 0.5
208- self ._display .Repaint ()
209192 self ._display .ZoomFactor (zoom_factor )
210193
211- def dragMoveEvent (self , event ):
212- pass
213-
214194 @property
215195 def cursor (self ):
216196 return self ._current_cursor
@@ -271,7 +251,7 @@ def DrawBox(self, event):
271251 if abs (dx ) <= tolerance and abs (dy ) <= tolerance :
272252 return
273253 self ._drawbox = [self .dragStartPosX , self .dragStartPosY , dx , dy ]
274- self . update ()
254+
275255
276256 def mouseMoveEvent (self , evt ):
277257 pt = evt .pos ()
@@ -310,11 +290,13 @@ def mouseMoveEvent(self, evt):
310290 self ._zoom_area = True
311291 self .cursor = "zoom-area"
312292 self .DrawBox (evt )
293+ self .update ()
313294 # SELECT AREA
314295 elif (buttons == QtCore .Qt .LeftButton and
315296 modifiers == QtCore .Qt .ShiftModifier ):
316297 self ._select_area = True
317298 self .DrawBox (evt )
299+ self .update ()
318300 else :
319301 self ._drawbox = False
320302 self ._display .MoveTo (pt .x (), pt .y ())
0 commit comments