def useOpenGL(self, b=True): if b: from PySide2 import QtOpenGL v = QtOpenGL.QGLWidget() else: v = QtWidgets.QWidget() self.setViewport(v)
def detectSystemResources(cls): try: from PySide2 import QtOpenGL cls.openGlAvailable = True except ImportError: cls.openGlAvailable = False if cls.openGlAvailable: version_flags = QtOpenGL.QGLFormat.openGLVersionFlags() if version_flags & QtOpenGL.QGLFormat.OpenGL_Version_2_0: cls.glVersion = "2.0 or higher" elif version_flags & QtOpenGL.QGLFormat.OpenGL_Version_1_5: cls.glVersion = "1.5" elif version_flags & QtOpenGL.QGLFormat.OpenGL_Version_1_4: cls.glVersion = "1.4" elif version_flags & QtOpenGL.QGLFormat.OpenGL_Version_1_3: cls.glVersion = "1.3 or lower" cls.debug("- OpenGL version:", cls.glVersion) glw = QtOpenGL.QGLWidget() if (not QtOpenGL.QGLFormat.hasOpenGL() or not glw.format().directRendering() or not (version_flags & QtOpenGL.QGLFormat.OpenGL_Version_1_5) or glw.depth() < 24): cls.openGlAdequate = False cls.debug("- OpenGL not recommended on this system") else: cls.openGlAdequate = False cls.debug("- OpenGL not supported by current build of Qt") if sys.platform == 'win32': # For now. cls.direct3dAvailable = False # Check if X Render is present. if hasattr(QtGui.QPixmap, 'x11PictureHandle'): tmp = QtGui.QPixmap(1, 1) if not tmp.x11PictureHandle(): cls.xRenderPresent = False cls.debug("- X render not present") w = QtGui.QWidget() cls.debug("- Color depth: %d" % w.depth())
def _SetupOpenGLContext(width=100, height=100): try: from PySide2 import QtOpenGL from PySide2.QtWidgets import QApplication except ImportError: from PySide import QtOpenGL from PySide.QtGui import QApplication application = QApplication(sys.argv) glFormat = QtOpenGL.QGLFormat() glFormat.setSampleBuffers(True) glFormat.setSamples(4) glWidget = QtOpenGL.QGLWidget(glFormat) glWidget.setFixedSize(width, height) glWidget.show() glWidget.setHidden(True) return glWidget
def __init__(self, parent): super(View, self).__init__(parent) self.setRenderHint(QtGui.QPainter.Antialiasing) self._manipulationMode = 0 gl_format = QtOpenGL.QGLFormat(QtOpenGL.QGL.SampleBuffers) gl_format.setSamples(10) gl_widget = QtOpenGL.QGLWidget(gl_format) self._pan = False self._panStartX = 0 self._panStartY = 0 self._numScheduledScalings = 0 self.lastMousePos = QtCore.QPoint() self.setViewport(gl_widget) self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse) self.setResizeAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse) self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setFrameShape(QtWidgets.QFrame.NoFrame)
def _SetupOpenGLContext(width=100, height=100): try: from PySide2 import QtOpenGL from PySide2.QtWidgets import QApplication except ImportError: from PySide import QtOpenGL from PySide.QtGui import QApplication application = QApplication(sys.argv) glFormat = QtOpenGL.QGLFormat() glFormat.setSampleBuffers(True) glFormat.setSamples(4) glWidget = QtOpenGL.QGLWidget(glFormat) glWidget.setFixedSize(width, height) # note that we need to bind the gl context here, instead of explicitly # showing the glWidget. Binding the gl context will make sure framebuffer is # ready for gl operations. glWidget.makeCurrent() return glWidget
def __init__(self, filepath, parent=None): ''' This is the constructor for the main widget ''' super(GraphicsWidget, self).__init__(parent) # create the layout. mainLayout = QtWidgets.QVBoxLayout() self.setMinimumSize(350, 350) self.resize(800, 800) self.scene = graphicsWidgets.GraphicsScene() self.view = graphicsWidgets.GraphicsView() self.view.setScene(self.scene) self.scene.setSceneRect(0, 0, 10000, 10000) self.view.setViewport(QtOpenGL.QGLWidget()) mainLayout.addWidget(self.view) self.setLayout(mainLayout) pickerData = picker_data.PickerData(self.scene) pickerData.read(filepath) pickerData.applyData(pickerData.getData().keys()) self.view.fitInView(self.scene.itemsBoundingRect(), QtCore.Qt.KeepAspectRatio)
def setRenderingSystem(self): if Colors.direct3dRendering: viewport.setAttribute(QtCore.Qt.WA_MSWindowsUseDirect3D) self.setCacheMode(QtGui.QGraphicsView.CacheNone) Colors.debug("- using Direct3D") elif Colors.openGlRendering: from PySide2 import QtOpenGL viewport = QtOpenGL.QGLWidget( QtOpenGL.QGLFormat(QtOpenGL.QGL.SampleBuffers)) if Colors.noScreenSync: viewport.format().setSwapInterval(0) viewport.setAutoFillBackground(False) self.setCacheMode(QtGui.QGraphicsView.CacheNone) Colors.debug("- using OpenGL") else: viewport = QtGui.QWidget() self.setCacheMode(QtGui.QGraphicsView.CacheBackground) Colors.debug("- using software rendering") self.setViewport(viewport)
def __init__(self, **kwargs): super().__init__(**kwargs) # Specify OpenGL context parameters gl = QtOpenGL.QGLFormat() gl.setVersion(self.gl_version[0], self.gl_version[1]) gl.setProfile(QtOpenGL.QGLFormat.CoreProfile) gl.setDepthBufferSize(24) gl.setDoubleBuffer(True) gl.setSwapInterval(1 if self.vsync else 0) # Configure multisampling if needed if self.samples > 1: gl.setSampleBuffers(True) gl.setSamples(self.samples) # We need an application object, but we are bypassing the library's # internal event loop to avoid unnecessary work self._app = QtWidgets.QApplication([]) # Create the OpenGL widget self._widget = QtOpenGL.QGLWidget(gl) self.title = self._title # If fullscreen we change the window to match the desktop on the primary screen if self.fullscreen: rect = QtWidgets.QDesktopWidget().screenGeometry() self._width = rect.width() self._height = rect.height() self._buffer_width = rect.width() * self._widget.devicePixelRatio() self._buffer_height = rect.height( ) * self._widget.devicePixelRatio() if self.resizable: # Ensure a valid resize policy when window is resizable size_policy = QtWidgets.QSizePolicy( QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding, ) self._widget.setSizePolicy(size_policy) self._widget.resize(self.width, self.height) else: self._widget.setFixedSize(self.width, self.height) # Center the window on the screen if in window mode if not self.fullscreen: self._widget.move(QtWidgets.QDesktopWidget().rect().center() - self._widget.rect().center()) # Needs to be set before show() self._widget.resizeGL = self.resize self.cursor = self._cursor if self.fullscreen: self._widget.showFullScreen() else: self._widget.show() # We want mouse position events self._widget.setMouseTracking(True) # Override event functions in qt self._widget.keyPressEvent = self.key_pressed_event self._widget.keyReleaseEvent = self.key_release_event self._widget.mouseMoveEvent = self.mouse_move_event self._widget.mousePressEvent = self.mouse_press_event self._widget.mouseReleaseEvent = self.mouse_release_event self._widget.wheelEvent = self.mouse_wheel_event self._widget.closeEvent = self.close_event self._widget.showEvent = self.show_event self._widget.hideEvent = self.hide_event # Attach to the context self.init_mgl_context() # Ensure retina and 4k displays get the right viewport self._buffer_width = self._width * self._widget.devicePixelRatio() self._buffer_height = self._height * self._widget.devicePixelRatio() self.set_default_viewport()
def __init__(self, width, height): super(Panel, self).__init__() self.selectedX = 0 self.selectedY = 0 self.width = width self.height = height self.flipped = False self.flipLeft = True self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setCacheMode(QtGui.QGraphicsView.CacheBackground) self.setViewportUpdateMode(QtGui.QGraphicsView.FullViewportUpdate) self.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform | QtGui.QPainter.TextAntialiasing) self.setBackgroundBrush( QtGui.QBrush(QtGui.QPixmap('./images/blue_angle_swirl.jpg'))) if QtOpenGL.QGLFormat.hasOpenGL(): self.setViewport( QtOpenGL.QGLWidget( QtOpenGL.QGLFormat(QtOpenGL.QGL.SampleBuffers))) self.setMinimumSize(50, 50) self.selectionTimeLine = QtCore.QTimeLine(150, self) self.flipTimeLine = QtCore.QTimeLine(500, self) bounds = QtCore.QRectF((-width / 2.0) * 150, (-height / 2.0) * 150, width * 150, height * 150) self.scene = QtGui.QGraphicsScene(bounds, self) self.setScene(self.scene) self.baseItem = RoundRectItem(bounds, QtGui.QColor(226, 255, 92, 64)) self.scene.addItem(self.baseItem) embed = QtGui.QWidget() self.ui = Ui_BackSide() self.ui.setupUi(embed) self.ui.hostName.setFocus() self.backItem = RoundRectItem(bounds, embed.palette().window(), embed) self.backItem.setTransform(QtGui.QTransform().rotate( 180, QtCore.Qt.YAxis)) self.backItem.setParentItem(self.baseItem) self.selectionItem = RoundRectItem(QtCore.QRectF(-60, -60, 120, 120), QtCore.Qt.gray) self.selectionItem.setParentItem(self.baseItem) self.selectionItem.setZValue(-1) self.selectionItem.setPos(self.posForLocation(0, 0)) self.startPos = self.selectionItem.pos() self.endPos = QtCore.QPointF() self.grid = [] for y in range(height): self.grid.append([]) for x in range(width): item = RoundRectItem(QtCore.QRectF(-54, -54, 108, 108), QtGui.QColor(214, 240, 110, 128)) item.setPos(self.posForLocation(x, y)) item.setParentItem(self.baseItem) item.setFlag(QtGui.QGraphicsItem.ItemIsFocusable) self.grid[y].append(item) rand = QtCore.qrand() % 9 if rand == 0: item.setPixmap( QtGui.QPixmap(':/images/kontact_contacts.png')) elif rand == 1: item.setPixmap( QtGui.QPixmap(':/images/kontact_journal.png')) elif rand == 2: item.setPixmap(QtGui.QPixmap(':/images/kontact_notes.png')) elif rand == 3: item.setPixmap( QtGui.QPixmap(':/images/kopeteavailable.png')) elif rand == 4: item.setPixmap( QtGui.QPixmap(':/images/metacontact_online.png')) elif rand == 5: item.setPixmap(QtGui.QPixmap(':/images/minitools.png')) elif rand == 6: item.setPixmap( QtGui.QPixmap(':/images/kontact_journal.png')) elif rand == 7: item.setPixmap( QtGui.QPixmap(':/images/kontact_contacts.png')) elif rand == 8: item.setPixmap( QtGui.QPixmap(':/images/kopeteavailable.png')) else: pass item.qobject.activated.connect(self.flip) self.grid[0][0].setFocus() self.backItem.qobject.activated.connect(self.flip) self.selectionTimeLine.valueChanged.connect(self.updateSelectionStep) self.flipTimeLine.valueChanged.connect(self.updateFlipStep) self.splash = SplashItem() self.splash.setZValue(5) self.splash.setPos(-self.splash.rect().width() / 2, self.scene.sceneRect().top()) self.scene.addItem(self.splash) self.splash.grabKeyboard() self.updateSelectionStep(0) self.setWindowTitle("Pad Navigator Example")
def __init__( self, width=256, height=256, title=None, visible=True, aspect=None, decoration=True, fullscreen=False, config=None, context=None, color=(0,0,0,1), vsync=False): window.Window.__init__(self, width=width, height=height, title=title, visible=visible, aspect=aspect, decoration=decoration, fullscreen=fullscreen, config=config, context=context, color=color) if config is None: config = configuration.Configuration() set_configuration(config) __glformat__.setSwapInterval(1 if vsync else 0) self._native_app = QtWidgets.QApplication.instance() if self._native_app is None: self._native_app = QtWidgets.QApplication(sys.argv) context = QtOpenGL.QGLContext(__glformat__) if context.isValid(): self._native_window = QtOpenGL.QGLWidget(context) else: self._native_window = QtOpenGL.QGLWidget(__glformat__) self._native_window.resize(width, height) self._native_window.makeCurrent() self._native_window.setAutoBufferSwap(False) self._native_window.setMouseTracking(True) self._native_window.setWindowTitle(self._title) #self._native_window.show() def paint_gl(): self.dispatch_event("on_draw", 0.0) self._native_window.paintGL = paint_gl def resize_gl(width, height): self.dispatch_event("on_resize", width, height) self._native_window.resizeGL = resize_gl def close_event(event): __windows__.remove(self) for i in range(len(self._timer_stack)): handler, interval = self._timer_stack[i] self._clock.unschedule(handler) self.dispatch_event("on_close") self._native_window.closeEvent = close_event def mouse_press_event(event): x = event.pos().x() y = event.pos().y() button = __mouse_map__.get(event.button(), window.mouse.UNKNOWN) self._button = button self._mouse_x = x self._mouse_y = y self.dispatch_event("on_mouse_press", x, y, button) self._native_window.mousePressEvent = mouse_press_event def mouse_release_event(event): x = event.pos().x() y = event.pos().y() button = __mouse_map__.get(event.button(), window.mouse.UNKNOWN) self._button = window.mouse.NONE self._mouse_x = x self._mouse_y = y self.dispatch_event("on_mouse_release", x, y, button) self._native_window.mouseReleaseEvent = mouse_release_event def mouse_move_event(event): x = event.pos().x() y = event.pos().y() dx = x - self._mouse_x dy = y - self._mouse_y self._mouse_x = x self._mouse_y = y if self._button is not window.mouse.NONE: self.dispatch_event('on_mouse_drag', x, y, dx, dy, self._button) else: self.dispatch_event('on_mouse_motion', x, y, dx, dy) self._native_window.mouseMoveEvent = mouse_move_event def wheel_event(event): if event.orientation == QtCore.Qt.Horizontal: offset_x = event.delta() offset_y = 0 else: offset_x = 0 offset_y = event.delta() x = event.pos().x() y = event.pos().y() self.dispatch_event("on_mouse_scroll", x, y, offset_x/10.0, offset_y/10.0) self._native_window.wheelEvent = wheel_event def key_press_event(event): code = self._keyboard_translate(event.key()) modifiers = self._modifiers_translate(event.modifiers()) self.dispatch_event("on_key_press", code, modifiers) self.dispatch_event("on_character", event.text()) self._native_window.keyPressEvent = key_press_event def key_release_event(event): code = self._keyboard_translate(event.key()) modifiers = self._modifiers_translate(event.modifiers()) self.dispatch_event("on_key_release", code, modifiers) self._native_window.keyReleaseEvent = key_release_event __windows__.append(self)