Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support both pyside2 and pyqt5 #25

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Qt5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os
import sys
import types

__version__ = "0.2.0.b2"

QT_VERBOSE = bool(os.getenv("QT_VERBOSE"))
QT_PREFERRED_BINDING = os.environ.get("QT_PREFERRED_BINDING")
QtCompat = types.ModuleType("QtCompat")


def _log(text):
if QT_VERBOSE:
sys.stdout.write(text + "\n")


try:
from PySide2 import (
QtWidgets,
QtCore,
QtGui,
QtQml,
QtQuick,
QtMultimedia,
QtMultimediaWidgets,
QtOpenGL,
)

from shiboken2 import wrapInstance, getCppPointer
QtCompat.wrapInstance = wrapInstance
QtCompat.getCppPointer = getCppPointer

try:
from PySide2 import QtUiTools
QtCompat.loadUi = QtUiTools.QUiLoader

except ImportError:
_log("QtUiTools not provided.")


except ImportError:
try:
from PyQt5 import (
QtWidgets,
QtCore,
QtGui,
QtQml,
QtQuick,
QtMultimedia,
QtMultimediaWidgets,
QtOpenGL,
)

QtCore.Signal = QtCore.pyqtSignal
QtCore.Slot = QtCore.pyqtSlot
QtCore.Property = QtCore.pyqtProperty

from sip import wrapinstance, unwrapinstance
QtCompat.wrapInstance = wrapinstance
QtCompat.getCppPointer = unwrapinstance

try:
from PyQt5 import uic
QtCompat.loadUi = uic.loadUi
except ImportError:
_log("uic not provided.")

except ImportError:

# Used during tests and installers
if QT_PREFERRED_BINDING == "None":
_log("No binding found")
else:
raise

__all__ = [
"QtWidgets",
"QtCore",
"QtGui",
"QtQml",
"QtQuick",
"QtMultimedia",
"QtMultimediaWidgets",
"QtCompat",
"QtOpenGL",
]
19 changes: 12 additions & 7 deletions bqt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

import bpy

from PySide2.QtWidgets import QApplication
import Qt5
# QApplication = Qt5.QtWidgets.QApplication

from .blender_applications import BlenderApplication

Expand Down Expand Up @@ -51,7 +52,8 @@ def instantiate_application() -> BlenderApplication:
Returns BlenderApplication: Application Instance

"""
app = QApplication.instance()

app = BlenderApplication.instance()
if not app:
app = load_os_module()
bpy.app.timers.register(on_update, persistent=True)
Expand All @@ -66,16 +68,18 @@ def load_os_module() -> object:
Returns: Instance of BlenderApplication

"""

import sys
operating_system = sys.platform
if operating_system == 'darwin':
from .blender_applications.darwin_blender_application import DarwinBlenderApplication
return DarwinBlenderApplication()
return DarwinBlenderApplication(sys.argv)
if operating_system in ['linux', 'linux2']:
# TODO: LINUX module
pass
elif operating_system == 'win32':
from .blender_applications.win32_blender_application import Win32BlenderApplication
return Win32BlenderApplication()
return Win32BlenderApplication(sys.argv)


def on_update() -> float:
Expand All @@ -85,8 +89,9 @@ def on_update() -> float:
Returns: Tick Rate

"""
app = QApplication.instance()
if app.should_close:
app = BlenderApplication.instance()

if app and app.should_close:
bpy.ops.wm.quit_blender({'window': bpy.context.window_manager.windows[0]}, 'INVOKE_DEFAULT')

return TICK
Expand Down Expand Up @@ -137,7 +142,7 @@ def on_exit():
Returns: None

"""
app = QApplication.instance()
app = BlenderApplication.instance()
if app:
app.store_window_geometry()
app.quit()
Expand Down
23 changes: 18 additions & 5 deletions bqt/blender_applications/blender_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,32 @@
from abc import abstractmethod, abstractstaticmethod, ABCMeta
from pathlib import Path

from PySide2.QtWidgets import QApplication, QWidget
from PySide2.QtGui import QCloseEvent, QIcon, QImage, QPixmap, QWindow
from PySide2.QtCore import QEvent, QObject, QRect, QSettings
import Qt5
QApplication = Qt5.QtWidgets.QApplication
QWidget = Qt5.QtWidgets.QWidget
QCloseEvent = Qt5.QtGui.QCloseEvent
QIcon = Qt5.QtGui.QIcon
QImage = Qt5.QtGui.QImage
QPixmap = Qt5.QtGui.QPixmap
QWindow = Qt5.QtGui.QWindow
QEvent = Qt5.QtCore.QEvent
QObject = Qt5.QtCore.QObject
QRect = Qt5.QtCore.QRect
QSettings = Qt5.QtCore.QSettings

# from Qt5.QtWidgets import QApplication, QWidget
# from Qt5.QtGui import QCloseEvent, QIcon, QImage, QPixmap, QWindow
# from Qt5.QtCore import QEvent, QObject, QRect, QSettings


class BlenderApplication(QApplication):
"""
Base Implementation for QT Blender Window Container
"""

def __init__(self):
def __init__(self, *args, **kwargs):
__metaclass__ = ABCMeta
super().__init__()
super().__init__(*args, **kwargs)

self._stylesheet_filepath = Path(__file__).parent / ".." / "blender_stylesheet.qss"
self._settings_key_geometry = "Geometry"
Expand Down
8 changes: 4 additions & 4 deletions bqt/blender_applications/darwin_blender_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import AppKit
import objc

from PySide2.QtGui import QIcon
from PySide2.QtCore import QObject
from Qt.QtGui import QIcon
from Qt.QtCore import QObject

from .blender_application import BlenderApplication

Expand All @@ -23,11 +23,11 @@ class DarwinBlenderApplication(BlenderApplication):
"""
Darwin (MACOS) Implementation of BlenderApplication
"""
def __init__(self):
def __init__(self, *args, **kwargs):
# OSX Specific - Needs to initialize first
self._ns_window = self.__get_application_window() or None

super().__init__()
super().__init__(*args, **kwargs)


def _get_application_hwnd(self) -> int:
Expand Down
30 changes: 24 additions & 6 deletions bqt/blender_applications/win32_blender_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

import bpy

from PySide2.QtGui import QIcon, QImage, QPixmap
from PySide2.QtCore import QByteArray, QObject

from .blender_application import BlenderApplication

import ctypes
import os
Expand Down Expand Up @@ -92,13 +88,35 @@ def get_first_blender_window():
return process_windows[0].hwnd




import Qt5
QApplication = Qt5.QtWidgets.QApplication
QWidget = Qt5.QtWidgets.QWidget
QCloseEvent = Qt5.QtGui.QCloseEvent
QIcon = Qt5.QtGui.QIcon
QImage = Qt5.QtGui.QImage
QPixmap = Qt5.QtGui.QPixmap
QWindow = Qt5.QtGui.QWindow
QEvent = Qt5.QtCore.QEvent
QObject = Qt5.QtCore.QObject
QRect = Qt5.QtCore.QRect
QSettings = Qt5.QtCore.QSettings
QByteArray = Qt5.QtCore.QByteArray

# from Qt5.QtGui import QIcon, QImage, QPixmap
# from Qt5.QtCore import QByteArray, QObject

from .blender_application import BlenderApplication


class Win32BlenderApplication(BlenderApplication):
"""
Windows implementation of BlenderApplication
"""

def __init__(self):
super().__init__()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

@staticmethod
def _get_application_hwnd() -> int:
Expand Down
11 changes: 9 additions & 2 deletions bqt/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
Jeff Hanna, [email protected], June 1, 2020
"""

from PySide2.QtCore import Qt
from PySide2.QtWidgets import QApplication, QDialog, QHBoxLayout, QLabel
import Qt5
Qt = Qt5.QtCore.Qt
QApplication = Qt5.QtWidgets.QApplication
QDialog = Qt5.QtWidgets.QDialog
QHBoxLayout = Qt5.QtWidgets.QHBoxLayout
QLabel = Qt5.QtWidgets.QLabel

# from Qt.QtCore import Qt
# from Qt.QtWidgets import QApplication, QDialog, QHBoxLayout, QLabel


class HelloWorldDialog(QDialog):
Expand Down