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

Qt imports #24

Merged
merged 2 commits into from
Sep 27, 2023
Merged
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
35 changes: 35 additions & 0 deletions modules/_pyqt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

__all__ = ["USE_PYQT6", "QtCore", "QtWidgets", "QtGui", "pg", "qasync"]

USE_PYQT6 = False
try:
import PyQt6.QtCore as QtCore
import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtGui as QtGui

USE_PYQT6 = True
except (ImportError, ModuleNotFoundError):
import PyQt5.QtCore as QtCore
import PyQt5.QtWidgets as QtWidgets
import PyQt5.QtGui as QtGui

# 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_PYQT6 and qasync.QtModuleName != "PyQt6":
raise AssertionError(
f"Wrong version of PyQt6 used for qasync: {qasync.QtModuleName}"
)
elif not USE_PYQT6 and qasync.QtModuleName != "PyQt5":
raise AssertionError(
f"Wrong version of PyQt5 used for qasync: {qasync.QtModuleName}"
)

# 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
126 changes: 0 additions & 126 deletions modules/gui_config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import os

try:
import PyQt6.QtCore as QtCore
import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtGui as QtGui
except ImportError:
import PyQt5.QtCore as QtCore
import PyQt5.QtWidgets as QtWidgets
import PyQt5.QtGui as QtGui

import oyaml as yaml


Expand Down Expand Up @@ -360,123 +351,6 @@ def __init__(self, config):
if self.config.G_IS_RASPI:
self.icon_dir = self.config.G_INSTALL_PATH

def set_qt5_or_qt6_constants(self, USE_PYQT6):
self.key_space = QtCore.Qt.Key.Key_Space if USE_PYQT6 else QtCore.Qt.Key_Space
self.key_press = (
QtCore.QEvent.Type.KeyPress if USE_PYQT6 else QtCore.QEvent.KeyPress
)
self.key_release = (
QtCore.QEvent.Type.KeyRelease if USE_PYQT6 else QtCore.QEvent.KeyRelease
)
self.no_modifier = (
QtCore.Qt.KeyboardModifier.NoModifier if USE_PYQT6 else QtCore.Qt.NoModifier
)

self.align_left = (
QtCore.Qt.AlignmentFlag.AlignLeft if USE_PYQT6 else QtCore.Qt.AlignLeft
)
self.align_center = (
QtCore.Qt.AlignmentFlag.AlignCenter if USE_PYQT6 else QtCore.Qt.AlignCenter
)
self.align_h_center = (
QtCore.Qt.AlignmentFlag.AlignHCenter
if USE_PYQT6
else QtCore.Qt.AlignHCenter
)
self.align_v_center = (
QtCore.Qt.AlignmentFlag.AlignVCenter
if USE_PYQT6
else QtCore.Qt.AlignVCenter
)
self.align_right = (
QtCore.Qt.AlignmentFlag.AlignRight if USE_PYQT6 else QtCore.Qt.AlignRight
)
self.align_bottom = (
QtCore.Qt.AlignmentFlag.AlignBottom if USE_PYQT6 else QtCore.Qt.AlignBottom
)
self.align_top = (
QtCore.Qt.AlignmentFlag.AlignTop if USE_PYQT6 else QtCore.Qt.AlignTop
)
self.expanding = (
QtWidgets.QSizePolicy.Policy.Expanding
if USE_PYQT6
else QtWidgets.QSizePolicy.Expanding
)
self.fixed = (
QtWidgets.QSizePolicy.Policy.Fixed
if USE_PYQT6
else QtWidgets.QSizePolicy.Fixed
)

self.no_focus = (
QtCore.Qt.FocusPolicy.NoFocus if USE_PYQT6 else QtCore.Qt.NoFocus
)
self.strong_focus = (
QtCore.Qt.FocusPolicy.StrongFocus if USE_PYQT6 else QtCore.Qt.StrongFocus
)
self.tab_focus_reason = (
QtCore.Qt.FocusReason.TabFocusReason
if USE_PYQT6
else QtCore.Qt.TabFocusReason
)
self.backtab_focus_reason = (
QtCore.Qt.FocusReason.BacktabFocusReason
if USE_PYQT6
else QtCore.Qt.BacktabFocusReason
)

self.scrollbar_alwaysoff = (
QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOff
if USE_PYQT6
else QtCore.Qt.ScrollBarAlwaysOff
)
self.qtextedit_nowrap = (
QtWidgets.QTextEdit.LineWrapMode.NoWrap
if USE_PYQT6
else QtWidgets.QTextEdit.NoWrap
)

self.stackingmode_stackone = (
QtWidgets.QStackedLayout.StackingMode.StackOne
if USE_PYQT6
else QtWidgets.QStackedLayout.StackOne
)
self.stackingmode_stackall = (
QtWidgets.QStackedLayout.StackingMode.StackAll
if USE_PYQT6
else QtWidgets.QStackedLayout.StackAll
)

self.PE_Widget = (
QtWidgets.QStyle.PrimitiveElement.PE_Widget
if USE_PYQT6
else QtWidgets.QStyle.PE_Widget
)
self.WA_TranslucentBackground = (
QtCore.Qt.WidgetAttribute.WA_TranslucentBackground
if USE_PYQT6
else QtCore.Qt.WA_TranslucentBackground
)
self.WA_TransparentForMouseEvents = (
QtCore.Qt.WidgetAttribute.WA_TransparentForMouseEvents
if USE_PYQT6
else QtCore.Qt.WA_TransparentForMouseEvents
)

# for draw_display
self.format_rgb888 = (
QtGui.QImage.Format.Format_RGB888
if USE_PYQT6
else QtGui.QImage.Format_RGB888
)
self.format_mono = (
QtGui.QImage.Format.Format_Mono if USE_PYQT6 else QtGui.QImage.Format_Mono
)
if self.config.display.has_color():
self.format = self.format_rgb888
else:
self.format = self.format_mono

def get_screen_shape(self, p):
remove_bytes = 0
if self.config.display.has_color():
Expand Down
136 changes: 121 additions & 15 deletions modules/gui_pyqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,10 @@
import asyncio
import numpy as np

USE_PYQT6 = False
try:
import PyQt6.QtCore as QtCore
import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtGui as QtGui

USE_PYQT6 = True
except ImportError:
import PyQt5.QtCore as QtCore
import PyQt5.QtWidgets as QtWidgets
import PyQt5.QtGui as QtGui

import qasync

from logger import app_logger
from modules.gui_config import GUI_Config
from modules.pyqt.pyqt_style import PyQtStyle
from modules._pyqt import USE_PYQT6, QtCore, QtWidgets, QtGui, qasync
from modules.utils.timer import Timer, log_timers


Expand Down Expand Up @@ -99,7 +86,7 @@ def __init__(self, config):
self.config.gui = self

self.gui_config = GUI_Config(config)
self.gui_config.set_qt5_or_qt6_constants(USE_PYQT6)
self.set_qt5_or_qt6_constants()

self.style = PyQtStyle()
self.logger = self.config.logger
Expand All @@ -113,6 +100,125 @@ def __init__(self, config):

self.init_window()

def set_qt5_or_qt6_constants(self):
self.gui_config.key_space = (
QtCore.Qt.Key.Key_Space if USE_PYQT6 else QtCore.Qt.Key_Space
)
self.gui_config.key_press = (
QtCore.QEvent.Type.KeyPress if USE_PYQT6 else QtCore.QEvent.KeyPress
)
self.gui_config.key_release = (
QtCore.QEvent.Type.KeyRelease if USE_PYQT6 else QtCore.QEvent.KeyRelease
)
self.gui_config.no_modifier = (
QtCore.Qt.KeyboardModifier.NoModifier if USE_PYQT6 else QtCore.Qt.NoModifier
)

self.gui_config.align_left = (
QtCore.Qt.AlignmentFlag.AlignLeft if USE_PYQT6 else QtCore.Qt.AlignLeft
)
self.gui_config.align_center = (
QtCore.Qt.AlignmentFlag.AlignCenter if USE_PYQT6 else QtCore.Qt.AlignCenter
)
self.gui_config.align_h_center = (
QtCore.Qt.AlignmentFlag.AlignHCenter
if USE_PYQT6
else QtCore.Qt.AlignHCenter
)
self.gui_config.align_v_center = (
QtCore.Qt.AlignmentFlag.AlignVCenter
if USE_PYQT6
else QtCore.Qt.AlignVCenter
)
self.gui_config.align_right = (
QtCore.Qt.AlignmentFlag.AlignRight if USE_PYQT6 else QtCore.Qt.AlignRight
)
self.gui_config.align_bottom = (
QtCore.Qt.AlignmentFlag.AlignBottom if USE_PYQT6 else QtCore.Qt.AlignBottom
)
self.gui_config.align_top = (
QtCore.Qt.AlignmentFlag.AlignTop if USE_PYQT6 else QtCore.Qt.AlignTop
)
self.gui_config.expanding = (
QtWidgets.QSizePolicy.Policy.Expanding
if USE_PYQT6
else QtWidgets.QSizePolicy.Expanding
)
self.gui_config.fixed = (
QtWidgets.QSizePolicy.Policy.Fixed
if USE_PYQT6
else QtWidgets.QSizePolicy.Fixed
)

self.gui_config.no_focus = (
QtCore.Qt.FocusPolicy.NoFocus if USE_PYQT6 else QtCore.Qt.NoFocus
)
self.gui_config.strong_focus = (
QtCore.Qt.FocusPolicy.StrongFocus if USE_PYQT6 else QtCore.Qt.StrongFocus
)
self.gui_config.tab_focus_reason = (
QtCore.Qt.FocusReason.TabFocusReason
if USE_PYQT6
else QtCore.Qt.TabFocusReason
)
self.gui_config.backtab_focus_reason = (
QtCore.Qt.FocusReason.BacktabFocusReason
if USE_PYQT6
else QtCore.Qt.BacktabFocusReason
)

self.gui_config.scrollbar_alwaysoff = (
QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOff
if USE_PYQT6
else QtCore.Qt.ScrollBarAlwaysOff
)
self.gui_config.qtextedit_nowrap = (
QtWidgets.QTextEdit.LineWrapMode.NoWrap
if USE_PYQT6
else QtWidgets.QTextEdit.NoWrap
)

self.gui_config.stackingmode_stackone = (
QtWidgets.QStackedLayout.StackingMode.StackOne
if USE_PYQT6
else QtWidgets.QStackedLayout.StackOne
)
self.gui_config.stackingmode_stackall = (
QtWidgets.QStackedLayout.StackingMode.StackAll
if USE_PYQT6
else QtWidgets.QStackedLayout.StackAll
)

self.gui_config.PE_Widget = (
QtWidgets.QStyle.PrimitiveElement.PE_Widget
if USE_PYQT6
else QtWidgets.QStyle.PE_Widget
)
self.gui_config.WA_TranslucentBackground = (
QtCore.Qt.WidgetAttribute.WA_TranslucentBackground
if USE_PYQT6
else QtCore.Qt.WA_TranslucentBackground
)
self.gui_config.WA_TransparentForMouseEvents = (
QtCore.Qt.WidgetAttribute.WA_TransparentForMouseEvents
if USE_PYQT6
else QtCore.Qt.WA_TransparentForMouseEvents
)

# for draw_display
self.gui_config.format_rgb888 = (
QtGui.QImage.Format.Format_RGB888
if USE_PYQT6
else QtGui.QImage.Format_RGB888
)
self.gui_config.format_mono = (
QtGui.QImage.Format.Format_Mono if USE_PYQT6 else QtGui.QImage.Format_Mono
)
if self.config.display.has_color():
self.gui_config.format = self.gui_config.format_rgb888
else:
self.gui_config.format = self.gui_config.format_mono

def init_window(self):
self.app = QtWidgets.QApplication(sys.argv)
self.config.loop = qasync.QEventLoop(self.app)
Expand Down
25 changes: 7 additions & 18 deletions modules/pyqt/graph/pyqt_base_map.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import numpy as np

try:
import PyQt6.QtCore as QtCore
import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtGui as QtGui
except ImportError:
import PyQt5.QtCore as QtCore
import PyQt5.QtWidgets as QtWidgets
import PyQt5.QtGui as QtGui

import pyqtgraph as pg
from qasync import asyncSlot

from modules._pyqt import QtCore, QtWidgets, pg, qasync
from modules.pyqt.pyqt_screen_widget import ScreenWidget

pg.setConfigOptions(antialias=True)
Expand Down Expand Up @@ -162,19 +151,19 @@ def change_move(self):
self.move_factor = 1.0
self.move_adjust_mode = False

@asyncSlot()
@qasync.asyncSlot()
async def move_x_plus(self):
await self.move_x(+self.zoom / 2)

@asyncSlot()
@qasync.asyncSlot()
async def move_x_minus(self):
await self.move_x(-self.zoom / 2)

@asyncSlot()
@qasync.asyncSlot()
async def move_y_plus(self):
await self.move_y(+self.zoom / 2)

@asyncSlot()
@qasync.asyncSlot()
async def move_y_minus(self):
await self.move_y(-self.zoom / 2)

Expand All @@ -186,13 +175,13 @@ async def move_y(self, delta):
self.move_pos["y"] += delta
await self.update_extra()

@asyncSlot()
@qasync.asyncSlot()
async def zoom_plus(self):
self.zoom /= 2
self.zoomlevel += 1
await self.update_extra()

@asyncSlot()
@qasync.asyncSlot()
async def zoom_minus(self):
self.zoom *= 2
self.zoomlevel -= 1
Expand Down
Loading