diff --git a/modules/_pyqt.py b/modules/_pyqt.py new file mode 100644 index 00000000..538a8879 --- /dev/null +++ b/modules/_pyqt.py @@ -0,0 +1,31 @@ +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 diff --git a/modules/gui_pyqt.py b/modules/gui_pyqt.py index 2af93e88..17d81ca5 100644 --- a/modules/gui_pyqt.py +++ b/modules/gui_pyqt.py @@ -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 diff --git a/modules/pyqt/graph/pyqt_base_map.py b/modules/pyqt/graph/pyqt_base_map.py index 8f6560ab..fd27bd20 100644 --- a/modules/pyqt/graph/pyqt_base_map.py +++ b/modules/pyqt/graph/pyqt_base_map.py @@ -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) @@ -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) @@ -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 diff --git a/modules/pyqt/graph/pyqt_course_profile.py b/modules/pyqt/graph/pyqt_course_profile.py index 7f5414e6..dbc410cd 100644 --- a/modules/pyqt/graph/pyqt_course_profile.py +++ b/modules/pyqt/graph/pyqt_course_profile.py @@ -1,15 +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 modules._pyqt import QtGui, pg pg.setConfigOptions(antialias=True) pg.setConfigOption("background", "w") diff --git a/modules/pyqt/graph/pyqt_map.py b/modules/pyqt/graph/pyqt_map.py index 82b6a072..5f09925d 100644 --- a/modules/pyqt/graph/pyqt_map.py +++ b/modules/pyqt/graph/pyqt_map.py @@ -7,19 +7,8 @@ import numpy as np from PIL import Image -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 logger import app_logger +from modules._pyqt import QtCore, QtGui, pg, qasync from modules.pyqt.pyqt_cuesheet_widget import CueSheetWidget from modules.pyqt.graph.pyqtgraph.CoursePlotItem import CoursePlotItem from modules.utils.timer import Timer, log_timers @@ -585,7 +574,7 @@ def init_course(self): self.course_loaded = False self.resizeEvent(None) - @asyncSlot() + @qasync.asyncSlot() async def search_route(self): if self.lock_status: return diff --git a/modules/pyqt/graph/pyqt_value_graph.py b/modules/pyqt/graph/pyqt_value_graph.py index 24033f84..ae99b14d 100644 --- a/modules/pyqt/graph/pyqt_value_graph.py +++ b/modules/pyqt/graph/pyqt_value_graph.py @@ -1,6 +1,6 @@ import numpy as np -import pyqtgraph as pg +from modules._pyqt import pg from modules.pyqt.pyqt_screen_widget import ScreenWidget pg.setConfigOptions(antialias=True) diff --git a/modules/pyqt/menu/pyqt_adjust_widget.py b/modules/pyqt/menu/pyqt_adjust_widget.py index cf940577..e8addb5f 100644 --- a/modules/pyqt/menu/pyqt_adjust_widget.py +++ b/modules/pyqt/menu/pyqt_adjust_widget.py @@ -1,15 +1,5 @@ -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 - -from qasync import asyncSlot - from logger import app_logger +from modules._pyqt import QtWidgets, qasync from .pyqt_menu_widget import MenuWidget ################################## @@ -89,7 +79,7 @@ def digit_clicked(self): self.display.setText("") self.display.setText(self.display.text() + str(digit_value)) - @asyncSlot() + @qasync.asyncSlot() async def set_value(self): value = self.display.text() if value == "": diff --git a/modules/pyqt/menu/pyqt_course_menu_widget.py b/modules/pyqt/menu/pyqt_course_menu_widget.py index d2b701fc..93ca0ced 100644 --- a/modules/pyqt/menu/pyqt_course_menu_widget.py +++ b/modules/pyqt/menu/pyqt_course_menu_widget.py @@ -1,17 +1,8 @@ -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 asyncio import os import shutil -from qasync import asyncSlot +from modules._pyqt import QtCore, QtWidgets, QtGui, qasync from .pyqt_menu_widget import MenuWidget, ListWidget, ListItemWidget @@ -57,12 +48,12 @@ def setup_menu(self): def preprocess(self): self.onoff_course_cancel_button() - @asyncSlot() + @qasync.asyncSlot() async def load_local_courses(self): await self.change_course_page("Local Storage") await self.parentWidget().widget(self.child_index).list_local_courses() - @asyncSlot() + @qasync.asyncSlot() async def load_rwgps_courses(self): asyncio.gather( self.change_course_page("Ride with GPS"), @@ -90,7 +81,7 @@ def cancel_course(self, replace=False): self.config.logger.reset_course(delete_course_file=True, replace=replace) self.onoff_course_cancel_button() - @asyncSlot() + @qasync.asyncSlot() async def receive_route(self): self.config.gui.show_dialog_cancel_only( self.cancel_receive_route, "Share directions > Bluetooth..." @@ -137,7 +128,7 @@ async def receive_route(self): stdout, stderr = await self.proc_receive_route.communicate() - @asyncSlot() + @qasync.asyncSlot() async def cancel_receive_route(self): self.is_check_folder = False if self.proc_receive_route.returncode is None: @@ -198,20 +189,20 @@ def setup_menu_extra(self): self.vertical_scrollbar = self.list.verticalScrollBar() self.vertical_scrollbar.valueChanged.connect(self.detect_bottom) - @asyncSlot(int) + @qasync.asyncSlot(int) async def detect_bottom(self, value): if self.list_type == "Ride with GPS": if value == self.vertical_scrollbar.maximum(): await self.list_ride_with_gps(add=True) - @asyncSlot() + @qasync.asyncSlot() async def button_func(self): if self.list_type == "Local Storage": self.set_course() elif self.list_type == "Ride with GPS": await self.change_course_detail_page() - @asyncSlot() + @qasync.asyncSlot() async def change_course_detail_page(self): if self.selected_item is None: return @@ -430,7 +421,7 @@ async def load_images(self): def on_back_menu(self): self.timer.stop() - @asyncSlot() + @qasync.asyncSlot() async def update_display(self): if self.check_all_image_and_draw(): self.timer.stop() diff --git a/modules/pyqt/menu/pyqt_map_menu_widget.py b/modules/pyqt/menu/pyqt_map_menu_widget.py index f6812041..38c580b8 100644 --- a/modules/pyqt/menu/pyqt_map_menu_widget.py +++ b/modules/pyqt/menu/pyqt_map_menu_widget.py @@ -1,14 +1,5 @@ import asyncio -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 - from .pyqt_menu_widget import MenuWidget, ListWidget diff --git a/modules/pyqt/menu/pyqt_menu_widget.py b/modules/pyqt/menu/pyqt_menu_widget.py index 8988ebf0..ccbd486c 100644 --- a/modules/pyqt/menu/pyqt_menu_widget.py +++ b/modules/pyqt/menu/pyqt_menu_widget.py @@ -1,14 +1,4 @@ -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 - -from qasync import asyncSlot - +from modules._pyqt import QtCore, QtWidgets, QtGui, qasync ################################# # Menu @@ -511,7 +501,7 @@ def connect_buttons(self): self.list.itemSelectionChanged.connect(self.changed_item) self.list.itemClicked.connect(self.button_func) - @asyncSlot() + @qasync.asyncSlot() async def button_func(self): await self.button_func_extra() self.back() @@ -675,15 +665,15 @@ def setup_menu(self): ) self.add_buttons(button_conf) - @asyncSlot() + @qasync.asyncSlot() async def strava_upload(self): await self.button["Strava"].run(self.config.network.api.strava_upload) - @asyncSlot() + @qasync.asyncSlot() async def garmin_upload(self): await self.button["Garmin"].run(self.config.network.api.garmin_upload) - @asyncSlot() + @qasync.asyncSlot() async def rwgps_upload(self): await self.button["Ride with GPS"].run(self.config.network.api.rwgps_upload) diff --git a/modules/pyqt/menu/pyqt_profile_widget.py b/modules/pyqt/menu/pyqt_profile_widget.py index c476fd00..4c836f0b 100644 --- a/modules/pyqt/menu/pyqt_profile_widget.py +++ b/modules/pyqt/menu/pyqt_profile_widget.py @@ -1,12 +1,3 @@ -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 - from .pyqt_menu_widget import MenuWidget diff --git a/modules/pyqt/menu/pyqt_sensor_menu_widget.py b/modules/pyqt/menu/pyqt_sensor_menu_widget.py index ac3cb467..5400ff4e 100644 --- a/modules/pyqt/menu/pyqt_sensor_menu_widget.py +++ b/modules/pyqt/menu/pyqt_sensor_menu_widget.py @@ -1,15 +1,7 @@ -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 - -from .pyqt_menu_widget import MenuWidget, ListWidget, ListItemWidget from logger import app_logger +from modules._pyqt import QtCore, QtWidgets, QtGui import modules.pyqt.pyqt_multiscan_widget as pyqt_multiscan +from .pyqt_menu_widget import MenuWidget, ListWidget, ListItemWidget class SensorMenuWidget(MenuWidget): diff --git a/modules/pyqt/menu/pyqt_system_menu_widget.py b/modules/pyqt/menu/pyqt_system_menu_widget.py index 46ed07cc..8f19bf76 100644 --- a/modules/pyqt/menu/pyqt_system_menu_widget.py +++ b/modules/pyqt/menu/pyqt_system_menu_widget.py @@ -1,16 +1,4 @@ -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 - -from qasync import asyncSlot - +from modules._pyqt import QtWidgets, QtGui, qasync from .pyqt_menu_widget import MenuWidget, ListWidget @@ -103,7 +91,7 @@ def show_ip_address(self): # Button is OK only self.config.gui.show_dialog_ok_only(None, self.config.G_IP_ADDRESS) - @asyncSlot() + @qasync.asyncSlot() async def onoff_ble_uart_service(self, change=True): if change: await self.config.ble_uart.on_off_uart_service() diff --git a/modules/pyqt/pyqt_button_box_widget.py b/modules/pyqt/pyqt_button_box_widget.py index df7de2d8..d2a72834 100644 --- a/modules/pyqt/pyqt_button_box_widget.py +++ b/modules/pyqt/pyqt_button_box_widget.py @@ -1,16 +1,5 @@ from logger import app_logger - -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 +from modules._pyqt import QtWidgets, QtGui class ButtonBoxWidget(QtWidgets.QWidget): diff --git a/modules/pyqt/pyqt_cuesheet_widget.py b/modules/pyqt/pyqt_cuesheet_widget.py index c6c72d49..b38b95e6 100644 --- a/modules/pyqt/pyqt_cuesheet_widget.py +++ b/modules/pyqt/pyqt_cuesheet_widget.py @@ -1,11 +1,4 @@ -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 +from modules._pyqt import QtCore, QtWidgets, QtGui from .pyqt_screen_widget import ScreenWidget diff --git a/modules/pyqt/pyqt_graph_debug.py b/modules/pyqt/pyqt_graph_debug.py index 19d0e640..28729c11 100644 --- a/modules/pyqt/pyqt_graph_debug.py +++ b/modules/pyqt/pyqt_graph_debug.py @@ -1,6 +1,6 @@ import numpy as np -import pyqtgraph as pg +from modules._pyqt import pg from .pyqt_screen_widget import ScreenWidget pg.setConfigOptions(antialias=True) diff --git a/modules/pyqt/pyqt_item.py b/modules/pyqt/pyqt_item.py index 61bb314e..3c05ce2a 100644 --- a/modules/pyqt/pyqt_item.py +++ b/modules/pyqt/pyqt_item.py @@ -1,16 +1,9 @@ import time -try: - import PyQt6.QtCore as QtCore - import PyQt6.QtGui as QtGui - import PyQt6.QtWidgets as QtWidgets -except ImportError: - import PyQt5.QtCore as QtCore - import PyQt5.QtGui as QtGui - import PyQt5.QtWidgets as QtWidgets - import numpy as np +from modules._pyqt import QtWidgets + ################################# # Item Class diff --git a/modules/pyqt/pyqt_screen_widget.py b/modules/pyqt/pyqt_screen_widget.py index fdede2aa..bff5cc8c 100644 --- a/modules/pyqt/pyqt_screen_widget.py +++ b/modules/pyqt/pyqt_screen_widget.py @@ -1,13 +1,5 @@ from logger import app_logger - -try: - import PyQt6.QtCore as QtCore - import PyQt6.QtWidgets as QtWidgets -except ImportError: - import PyQt5.QtCore as QtCore - import PyQt5.QtWidgets as QtWidgets - -from qasync import asyncSlot +from modules._pyqt import QtCore, QtWidgets, qasync from .pyqt_item import Item @@ -148,7 +140,7 @@ def add_items(self): def add_extra(self): pass - @asyncSlot() + @qasync.asyncSlot() async def update_display(self): if self.items is None: return