Skip to content

Commit

Permalink
have one import for qt object
Browse files Browse the repository at this point in the history
make sure qasync/pyqtgraph rely on the same qt version
  • Loading branch information
Ptosiek committed Sep 20, 2023
1 parent db3d60d commit df016f6
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 183 deletions.
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
15 changes: 1 addition & 14 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
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
11 changes: 1 addition & 10 deletions modules/pyqt/graph/pyqt_course_profile.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
15 changes: 2 additions & 13 deletions modules/pyqt/graph/pyqt_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/pyqt/graph/pyqt_value_graph.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 2 additions & 12 deletions modules/pyqt/menu/pyqt_adjust_widget.py
Original file line number Diff line number Diff line change
@@ -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

##################################
Expand Down Expand Up @@ -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 == "":
Expand Down
27 changes: 9 additions & 18 deletions modules/pyqt/menu/pyqt_course_menu_widget.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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..."
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 0 additions & 9 deletions modules/pyqt/menu/pyqt_map_menu_widget.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
20 changes: 5 additions & 15 deletions modules/pyqt/menu/pyqt_menu_widget.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 0 additions & 9 deletions modules/pyqt/menu/pyqt_profile_widget.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
12 changes: 2 additions & 10 deletions modules/pyqt/menu/pyqt_sensor_menu_widget.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
Loading

0 comments on commit df016f6

Please sign in to comment.