Skip to content

Commit

Permalink
make gui_config independent of PyQt
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptosiek committed Sep 26, 2023
1 parent 19e4644 commit d38835d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 127 deletions.
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
121 changes: 120 additions & 1 deletion modules/gui_pyqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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 +113,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

0 comments on commit d38835d

Please sign in to comment.