Skip to content

Commit fa5d251

Browse files
jf---tpaviot
authored andcommitted
Adding PyQt5 support
1 parent 46c3191 commit fa5d251

11 files changed

Lines changed: 545 additions & 251 deletions

.travis.build.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ echo "Timestamp" && date
2222
cd ../test
2323
/home/travis/miniconda/bin/python run_tests.py
2424
/home/travis/miniconda/bin/python core_webgl_unittest.py
25-
xvfb-run -s "-screen 0 1024x768x16" /home/travis/miniconda/bin/python core_display_unittest.py
25+
xvfb-run -s "-screen 0 1024x768x16" /home/travis/miniconda/bin/python core_display_pyqt4_unittest.py
26+
xvfb-run -s "-screen 0 1024x768x16" /home/travis/miniconda/bin/python core_display_pyqt5_unittest.py
27+
xvfb-run -s "-screen 0 1024x768x16" /home/travis/miniconda/bin/python core_display_pyside_unittest.py
28+
xvfb-run -s "-screen 0 1024x768x16" /home/travis/miniconda/bin/python core_display_wx_unittest.py

examples/core_visualization_overpaint_viewer.py

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
##This file is part of pythonOCC.
1+
# This file is part of pythonOCC.
22
##
3-
##pythonOCC is free software: you can redistribute it and/or modify
4-
##it under the terms of the GNU Lesser General Public License as published by
5-
##the Free Software Foundation, either version 3 of the License, or
6-
##(at your option) any later version.
3+
# pythonOCC is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU Lesser General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
77
##
8-
##pythonOCC is distributed in the hope that it will be useful,
9-
##but WITHOUT ANY WARRANTY; without even the implied warranty of
10-
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11-
##GNU Lesser General Public License for more details.
8+
# pythonOCC is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU Lesser General Public License for more details.
1212
##
13-
##You should have received a copy of the GNU Lesser General Public License
14-
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
13+
# You should have received a copy of the GNU Lesser General Public License
14+
# along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
1515

1616
"""
1717
@@ -38,19 +38,11 @@
3838
import random
3939
import sys
4040

41-
from OCC.Display.qtDisplay import qtViewer3d, get_qt_modules
42-
# pyqt4 only
43-
from PyQt4 import Qt
41+
from OCC.Display.backend import get_backend, get_qt_modules
4442

45-
QtCore, QtGui, QtOpenGL = get_qt_modules()
46-
47-
try:
48-
from OpenGL.GL import (glViewport, glMatrixMode, glOrtho, glLoadIdentity,
49-
GL_PROJECTION, GL_MODELVIEW)
50-
except ImportError:
51-
msg = "for this example, the OpenGL module is required" \
52-
"why not run \"pip install PyOpenGL\"\?"
53-
sys.exit(status=1)
43+
backend = get_backend()
44+
QtCore, QtGui, QtWidgets, QtOpenGL = get_qt_modules()
45+
from OCC.Display.qtDisplay import qtViewer3d
5446

5547
# --------------------------------------------------------------------------
5648
# these are names of actions that invoke the OpenGL viewport to be redrawn
@@ -84,9 +76,10 @@ def __init__(self, position, radius, velocity):
8476
self.updateBrush()
8577

8678
def updateBrush(self):
87-
gradient = QtGui.QRadialGradient(QtCore.QPointF(self.radius, self.radius),
88-
self.radius,
89-
QtCore.QPointF(self.radius * 0.5, self.radius * 0.5))
79+
gradient = QtGui.QRadialGradient(
80+
QtCore.QPointF(self.radius, self.radius),
81+
self.radius,
82+
QtCore.QPointF(self.radius * 0.5, self.radius * 0.5))
9083
gradient.setColorAt(0, QtGui.QColor(255, 255, 255, 0))
9184
gradient.setColorAt(0.25, self.innerColor)
9285
gradient.setColorAt(1, self.outerColor)
@@ -197,10 +190,10 @@ def __init__(self, parent=None):
197190
# ---------------------------------------------------------------------
198191

199192
# QPoint stored on mouse press
200-
self._point_on_mouse_press = Qt.QPoint()
193+
self._point_on_mouse_press = QtCore.QPoint()
201194

202195
# QPoint stored on mouse move
203-
self._point_on_mouse_move = Qt.QPoint()
196+
self._point_on_mouse_move = QtCore.QPoint()
204197

205198
# stores the delta between self._point_on_mouse_press and self._point_on_mouse_move
206199
self._delta_event_pos = None
@@ -218,10 +211,10 @@ def point_on_mouse_press(self):
218211

219212
@point_on_mouse_press.setter
220213
def point_on_mouse_press(self, event):
221-
if isinstance(event, Qt.QMouseEvent):
222-
self._point_on_mouse_press = Qt.QPoint(event.pos())
223-
elif isinstance(event, Qt.QPoint):
224-
self._point_on_mouse_press = Qt.QPoint(event)
214+
if isinstance(event, QtGui.QMouseEvent):
215+
self._point_on_mouse_press = QtCore.QPoint(event.pos())
216+
elif isinstance(event, QtCore.QPoint):
217+
self._point_on_mouse_press = QtCore.QPoint(event)
225218

226219
@property
227220
def point_on_mouse_move(self):
@@ -231,10 +224,10 @@ def point_on_mouse_move(self):
231224

232225
@point_on_mouse_move.setter
233226
def point_on_mouse_move(self, event):
234-
if isinstance(event, (Qt.QMouseEvent, Qt.QWheelEvent)):
235-
self._point_on_mouse_move = Qt.QPoint(event.pos())
236-
elif isinstance(event, Qt.QPoint):
237-
self._point_on_mouse_move = Qt.QPoint(event)
227+
if isinstance(event, (QtGui.QMouseEvent, QtGui.QWheelEvent)):
228+
self._point_on_mouse_move = QtCore.QPoint(event.pos())
229+
elif isinstance(event, QtCore.QPoint):
230+
self._point_on_mouse_move = QtCore.QPoint(event)
238231

239232
@property
240233
def delta_mouse_event_pos(self):
@@ -345,7 +338,13 @@ def mouseMoveEvent(self, event):
345338
self.update()
346339

347340
def wheelEvent(self, event):
348-
if event.delta() > 0:
341+
342+
if self._have_pyqt5:
343+
delta = event.angleDelta().y()
344+
else:
345+
delta = event.delta()
346+
347+
if delta > 0:
349348
self.zoom_factor = 1.3
350349
else:
351350
self.zoom_factor = 0.7
@@ -371,8 +370,10 @@ def on_zoom_area(self):
371370
pass
372371
else:
373372
if not self.is_right_mouse_button_surpressed:
374-
coords = [self.point_on_mouse_press[0], self.point_on_mouse_press[1],
375-
self.point_on_mouse_move[0], self.point_on_mouse_move[1]]
373+
coords = [self.point_on_mouse_press[0],
374+
self.point_on_mouse_press[1],
375+
self.point_on_mouse_move[0],
376+
self.point_on_mouse_move[1]]
376377
self._display.ZoomArea(*coords)
377378

378379
def on_zoom_factor(self):
@@ -395,8 +396,10 @@ def on_dyn_zoom(self):
395396
through the shift + right mouse button
396397
397398
"""
398-
self._display.DynamicZoom(self.point_on_mouse_press[0], self.point_on_mouse_press[1],
399-
self.point_on_mouse_move[0], self.point_on_mouse_move[1]
399+
self._display.DynamicZoom(self.point_on_mouse_press[0],
400+
self.point_on_mouse_press[1],
401+
self.point_on_mouse_move[0],
402+
self.point_on_mouse_move[1]
400403
)
401404
self.point_on_mouse_press = self._point_on_mouse_move
402405

@@ -467,8 +470,9 @@ def _dispatch_camera_command_actions(self):
467470
action = getattr(self, self.current_action)
468471
action()
469472

470-
except Exception, e:
471-
print("tried invoking camera command action {0}, raising exception: {1}".format(self.current_action, e))
473+
except Exception:
474+
print("could not invoke camera command action {0}".format(
475+
self.current_action))
472476

473477
finally:
474478
self.current_action = None
@@ -555,11 +559,14 @@ def createBubbles(self, number):
555559
the viewport
556560
"""
557561
for _ in range(number):
558-
position = QtCore.QPointF(self.width() * (0.1 + 0.8 * random.random()),
559-
self.height() * (0.1 + 0.8 * random.random()))
560-
radius = min(self.width(), self.height()) * (0.0125 + 0.0875 * random.random())
561-
velocity = QtCore.QPointF(self.width() * 0.0125 * (-0.5 + random.random()),
562-
self.height() * 0.0125 * (-0.5 + random.random()))
562+
position = QtCore.QPointF(
563+
self.width() * (0.1 + 0.8 * random.random()),
564+
self.height() * (0.1 + 0.8 * random.random()))
565+
radius = min(self.width(), self.height()) * (
566+
0.0125 + 0.0875 * random.random())
567+
velocity = QtCore.QPointF(
568+
self.width() * 0.0125 * (-0.5 + random.random()),
569+
self.height() * 0.0125 * (-0.5 + random.random()))
563570

564571
self.bubbles.append(Bubble(position, radius, velocity))
565572

@@ -586,7 +593,8 @@ def drawBox(self, painter):
586593
pass
587594

588595
else:
589-
rect = QtCore.QRect(self.point_on_mouse_press[0], self.point_on_mouse_press[1], -dx, -dy)
596+
rect = QtCore.QRect(self.point_on_mouse_press[0],
597+
self.point_on_mouse_press[1], -dx, -dy)
590598
painter.drawRect(rect)
591599

592600
def drawBubbles(self, event, painter):
@@ -596,7 +604,7 @@ def drawBubbles(self, event, painter):
596604
for bubble in self.bubbles:
597605
bubble_rect = bubble.rect()
598606
if bubble_rect.intersects(QtCore.QRectF(event.rect())):
599-
pt = Qt.QPointF(self._point_on_mouse_move)
607+
pt = QtCore.QPointF(self._point_on_mouse_move)
600608
over_mouse = bubble_rect.contains(pt)
601609
bubble.drawBubble(painter, over_mouse)
602610

@@ -610,44 +618,50 @@ def drawInstructions(self, painter):
610618

611619
rect = metrics.boundingRect(0, 0, self.width() - 2 * border,
612620
int(self.height() * 0.125),
613-
QtCore.Qt.AlignCenter | QtCore.Qt.TextWordWrap, self.text)
621+
QtCore.Qt.AlignCenter | QtCore.Qt.TextWordWrap,
622+
self.text)
614623

615624
painter.setRenderHint(QtGui.QPainter.TextAntialiasing)
616625

617-
painter.fillRect(QtCore.QRect(0, 0, self.width(), rect.height() + 2 * border),
618-
QtGui.QColor(0, 0, 0, transparency))
626+
painter.fillRect(
627+
QtCore.QRect(0, 0, self.width(), rect.height() + 2 * border),
628+
QtGui.QColor(0, 0, 0, transparency))
619629

620630
painter.setPen(QtCore.Qt.white)
621631

622-
painter.fillRect(QtCore.QRect(0, 0, self.width(), rect.height() + 2 * border),
623-
QtGui.QColor(0, 0, 0, transparency))
632+
painter.fillRect(
633+
QtCore.QRect(0, 0, self.width(), rect.height() + 2 * border),
634+
QtGui.QColor(0, 0, 0, transparency))
624635

625-
painter.drawText((self.width() - rect.width()) / 2, border, rect.width(),
626-
rect.height(), QtCore.Qt.AlignCenter | QtCore.Qt.TextWordWrap,
636+
painter.drawText((self.width() - rect.width()) / 2, border,
637+
rect.width(),
638+
rect.height(),
639+
QtCore.Qt.AlignCenter | QtCore.Qt.TextWordWrap,
627640
self.text)
628641

629642

630643
if __name__ == '__main__':
631644
def TestOverPainting():
632-
class AppFrame(QtGui.QWidget):
645+
class AppFrame(QtWidgets.QWidget):
633646
def __init__(self, parent=None):
634-
QtGui.QWidget.__init__(self, parent)
647+
QtWidgets.QWidget.__init__(self, parent)
635648
self.setWindowTitle(self.tr("qtDisplay3d overpainting example"))
636649
self.resize(1280, 1024)
637650
self.canva = GLWidget(self)
638-
mainLayout = QtGui.QHBoxLayout()
651+
mainLayout = QtWidgets.QHBoxLayout()
639652
mainLayout.addWidget(self.canva)
640653
mainLayout.setContentsMargins(0, 0, 0, 0)
641654
self.setLayout(mainLayout)
642655

643656
def runTests(self):
644657
self.canva._display.Test()
645658

646-
app = QtGui.QApplication(sys.argv)
659+
app = QtWidgets.QApplication(sys.argv)
647660
frame = AppFrame()
648661
frame.show()
649662
frame.canva.InitDriver()
650663
frame.runTests()
651664
app.exec_()
652665

666+
653667
TestOverPainting()

0 commit comments

Comments
 (0)