Skip to content

Commit

Permalink
improve qt imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hishizuka committed Aug 22, 2024
1 parent 91eff48 commit c41eb72
Show file tree
Hide file tree
Showing 25 changed files with 255 additions and 220 deletions.
180 changes: 0 additions & 180 deletions modules/_pyqt.py

This file was deleted.

90 changes: 90 additions & 0 deletions modules/_qt_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from modules._qt_ver import (
USE_QT6,
USE_PYQT6,
USE_PYQT5,
USE_PYSIDE6,
QtCore,
QtGui,
)


if USE_PYSIDE6:
Signal = QtCore.Signal
QtCore.QT_VERSION_STR = QtCore.__version__
Slot = QtCore.Slot
else:
Signal = QtCore.pyqtSignal
Slot = QtCore.pyqtSlot

# import qasync once pyQt is imported so the correct version is used (it starts with PyQt5 then tries PyQt6)
import qasync # noqa

# make sure the version is correct in case the underlying code for qasync changed
if USE_PYSIDE6 and qasync.QtModuleName != "PySide6":
raise AssertionError(
f"Wrong version of PySide6 used for qasync: {qasync.QtModuleName}"
)
elif USE_PYQT6 and qasync.QtModuleName != "PyQt6":
raise AssertionError(
f"Wrong version of PyQt6 used for qasync: {qasync.QtModuleName}"
)
elif USE_PYQT5 and qasync.QtModuleName != "PyQt5":
raise AssertionError(
f"Wrong version of PyQt5 used for qasync: {qasync.QtModuleName}"
)


# key
QT_KEY_BACKTAB = QtCore.Qt.Key.Key_Backtab if USE_QT6 else QtCore.Qt.Key_Backtab
QT_KEY_TAB = QtCore.Qt.Key.Key_Tab if USE_QT6 else QtCore.Qt.Key_Tab
QT_KEY_SPACE = QtCore.Qt.Key.Key_Space if USE_QT6 else QtCore.Qt.Key_Space
QT_KEY_PRESS = QtCore.QEvent.Type.KeyPress if USE_QT6 else QtCore.QEvent.KeyPress
QT_KEY_RELEASE = (
QtCore.QEvent.Type.KeyRelease if USE_QT6 else QtCore.QEvent.KeyRelease
)
QT_NO_MODIFIER = (
QtCore.Qt.KeyboardModifier.NoModifier if USE_QT6 else QtCore.Qt.NoModifier
)

#align
QT_ALIGN_LEFT = QtCore.Qt.AlignmentFlag.AlignLeft if USE_QT6 else QtCore.Qt.AlignLeft
QT_ALIGN_RIGHT = QtCore.Qt.AlignmentFlag.AlignRight if USE_QT6 else QtCore.Qt.AlignRight
QT_ALIGN_CENTER = (
QtCore.Qt.AlignmentFlag.AlignCenter if USE_QT6 else QtCore.Qt.AlignCenter
)
QT_ALIGN_H_CENTER = (
QtCore.Qt.AlignmentFlag.AlignHCenter if USE_QT6 else QtCore.Qt.AlignHCenter
)
QT_ALIGN_V_CENTER = (
QtCore.Qt.AlignmentFlag.AlignVCenter if USE_QT6 else QtCore.Qt.AlignVCenter
)
QT_ALIGN_RIGHT = (
QtCore.Qt.AlignmentFlag.AlignRight if USE_QT6 else QtCore.Qt.AlignRight
)
QT_ALIGN_BOTTOM = (
QtCore.Qt.AlignmentFlag.AlignBottom if USE_QT6 else QtCore.Qt.AlignBottom
)
QT_ALIGN_TOP = QtCore.Qt.AlignmentFlag.AlignTop if USE_QT6 else QtCore.Qt.AlignTop

#focus
QT_NO_FOCUS = QtCore.Qt.FocusPolicy.NoFocus if USE_QT6 else QtCore.Qt.NoFocus
QT_STRONG_FOCUS = (
QtCore.Qt.FocusPolicy.StrongFocus if USE_QT6 else QtCore.Qt.StrongFocus
)
QT_TAB_FOCUS_REASON = (
QtCore.Qt.FocusReason.TabFocusReason if USE_QT6 else QtCore.Qt.TabFocusReason
)
QT_BACKTAB_FOCUS_REASON = (
QtCore.Qt.FocusReason.BacktabFocusReason
if USE_QT6
else QtCore.Qt.BacktabFocusReason
)

# for draw_display
QT_FORMAT_RGB888 = (
QtGui.QImage.Format.Format_RGB888 if USE_QT6 else QtGui.QImage.Format_RGB888
)
QT_FORMAT_MONO = (
QtGui.QImage.Format.Format_Mono if USE_QT6 else QtGui.QImage.Format_Mono
)

113 changes: 113 additions & 0 deletions modules/_qt_qtwidgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import os


USE_QT6 = False
USE_PYQT6 = False
USE_PYQT5 = False
USE_PYSIDE6 = False
QT_PACKAGE = ""

try:
raise ImportError
import PySide6.QtCore as QtCore
import PySide6.QtGui as QtGui
import PySide6.QtWidgets as QtWidgets
USE_QT6 = True
USE_PYSIDE6 = True
QT_PACKAGE = "PySide6"
except (ImportError, ModuleNotFoundError):
try:
import PyQt6.QtCore as QtCore
import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtGui as QtGui
USE_QT6 = True
USE_PYQT6 = True
QT_PACKAGE = "PyQt6"
except (ImportError, ModuleNotFoundError):
import PyQt5.QtCore as QtCore
import PyQt5.QtWidgets as QtWidgets
import PyQt5.QtGui as QtGui
USE_PYQT5 = True
QT_PACKAGE = "PyQt5"

import modules._qt_ver as _qt_ver
_qt_ver.USE_QT6 = USE_QT6
_qt_ver.USE_PYQT6 = USE_PYQT6
_qt_ver.USE_PYQT5 = USE_PYQT5
_qt_ver.USE_PYSIDE6 = USE_PYSIDE6
_qt_ver.QT_PACKAGE = QT_PACKAGE
_qt_ver.QtCore = QtCore
_qt_ver.QtGui = QtGui

from modules._qt_constants import *

# pyqtgraph will check/try to import PyQT6 on load and might fail if some packages were imported
# (if pyQt6 is halfway installed): so we force the version here
os.environ.setdefault("PYQTGRAPH_QT_LIB", qasync.QtModuleName)

# make sure pyqtgraph it imported from here so PYQTGRAPH_QT_LIB will always be set
import pyqtgraph as pg # noqa

# set default configuration
pg.setConfigOptions(antialias=True)
pg.setConfigOption("background", "w")
pg.setConfigOption("foreground", "k")

# QtWidgets
QT_EXPANDING = (
QtWidgets.QSizePolicy.Policy.Expanding
if USE_QT6
else QtWidgets.QSizePolicy.Expanding
)
QT_FIXED = (
QtWidgets.QSizePolicy.Policy.Fixed if USE_QT6 else QtWidgets.QSizePolicy.Fixed
)

# for textedit
QT_SCROLLBAR_ALWAYSOFF = (
QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOff
if USE_QT6
else QtCore.Qt.ScrollBarAlwaysOff
)
QT_TEXTEDIT_NOWRAP = (
QtWidgets.QTextEdit.LineWrapMode.NoWrap if USE_QT6 else QtWidgets.QTextEdit.NoWrap
)

# for popup
QT_STACKINGMODE_STACKONE = (
QtWidgets.QStackedLayout.StackingMode.StackOne
if USE_QT6
else QtWidgets.QStackedLayout.StackOne
)
QT_STACKINGMODE_STACKALL = (
QtWidgets.QStackedLayout.StackingMode.StackAll
if USE_QT6
else QtWidgets.QStackedLayout.StackAll
)
QT_PE_WIDGET = (
QtWidgets.QStyle.PrimitiveElement.PE_Widget
if USE_QT6
else QtWidgets.QStyle.PE_Widget
)
QT_WA_TRANSLUCENT_BACKGROUND = (
QtCore.Qt.WidgetAttribute.WA_TranslucentBackground
if USE_QT6
else QtCore.Qt.WA_TranslucentBackground
)
QT_WA_TRANSPARENT_FOR_MOUSE_EVENTS = (
QtCore.Qt.WidgetAttribute.WA_TransparentForMouseEvents
if USE_QT6
else QtCore.Qt.WA_TransparentForMouseEvents
)

# for map widgets
QT_COMPOSITION_MODE_SOURCEIN = (
QtGui.QPainter.CompositionMode.CompositionMode_SourceIn
if USE_QT6
else QtGui.QPainter.CompositionMode_SourceIn
)
QT_COMPOSITION_MODE_DARKEN = (
QtGui.QPainter.CompositionMode.CompositionMode_Darken
if USE_QT6
else QtGui.QPainter.CompositionMode_Darken
)
8 changes: 8 additions & 0 deletions modules/_qt_ver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
USE_QT6 = False
USE_PYQT6 = False
USE_PYQT5 = False
USE_PYSIDE6 = False
QtCore = None
QtGui = None
QtMode = ""
QT_PACKAGE = ""
Loading

0 comments on commit c41eb72

Please sign in to comment.