1
0
Fork 0
mirror of synced 2024-07-03 13:40:47 +12:00
Rare/rare/components/main_window.py

178 lines
6.2 KiB
Python
Raw Normal View History

2021-04-15 01:14:06 +12:00
import os
2021-04-14 02:56:44 +12:00
from logging import getLogger
from PyQt5.QtCore import Qt, QSettings, QTimer, QSize, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QCloseEvent, QCursor
from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QStatusBar,
QScrollArea,
QScroller,
QComboBox,
QMessageBox,
)
2021-02-10 23:48:25 +13:00
from rare.components.tabs import TabWidget
from rare.components.tray_icon import TrayIcon
from rare.shared import LegendaryCoreSingleton, GlobalSignalsSingleton, ArgumentsSingleton
from rare.utils.paths import lock_file
2021-02-10 23:48:25 +13:00
logger = getLogger("MainWindow")
2021-04-14 02:56:44 +12:00
2021-02-10 23:48:25 +13:00
class MainWindow(QMainWindow):
# int: exit code
exit_app: pyqtSignal = pyqtSignal(int)
2022-09-02 22:17:05 +12:00
def __init__(self, parent=None):
self._exit_code = 0
self._accept_close = False
self._window_launched = False
2022-09-02 22:17:05 +12:00
super(MainWindow, self).__init__(parent=parent)
self.setAttribute(Qt.WA_DeleteOnClose, True)
self.core = LegendaryCoreSingleton()
self.signals = GlobalSignalsSingleton()
self.args = ArgumentsSingleton()
2021-09-30 10:22:47 +13:00
self.settings = QSettings()
2021-05-02 23:15:11 +12:00
2021-02-10 23:48:25 +13:00
self.setWindowTitle("Rare - GUI for legendary")
2021-10-08 07:19:24 +13:00
self.tab_widget = TabWidget(self)
self.tab_widget.exit_app.connect(self.on_exit_app)
2021-04-07 22:11:31 +12:00
self.setCentralWidget(self.tab_widget)
2021-12-05 02:47:55 +13:00
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
2021-11-23 03:52:07 +13:00
width, height = 1280, 720
2021-11-26 09:54:34 +13:00
if self.settings.value("save_size", False, bool):
width, height = self.settings.value("window_size", (width, height), tuple)
self.resize(width, height)
if not self.args.offline:
2021-12-27 11:57:56 +13:00
try:
from rare.utils.rpc import DiscordRPC
2021-12-27 11:57:56 +13:00
self.rpc = DiscordRPC()
except ModuleNotFoundError:
logger.warning("Discord RPC module not found")
self.timer = QTimer()
self.timer.timeout.connect(self.timer_finished)
self.timer.start(1000)
self.tray_icon: TrayIcon = TrayIcon(self)
self.tray_icon.exit_app.connect(self.on_exit_app)
self.tray_icon.show_app.connect(self.show)
self.tray_icon.activated.connect(lambda r: self.toggle() if r == self.tray_icon.DoubleClick else None)
self.signals.application.notify.connect(
lambda title: self.tray_icon.showMessage(
self.tr("Download finished"),
self.tr("Download finished. {} is playable now").format(title),
self.tray_icon.Information,
4000,
)
if self.settings.value("notification", True, bool)
else None
)
# enable kinetic scrolling
for scroll_area in self.findChildren(QScrollArea):
if not scroll_area.property("no_kinetic_scroll"):
QScroller.grabGesture(scroll_area.viewport(), QScroller.LeftMouseButtonGesture)
# fix scrolling
for combo_box in scroll_area.findChildren(QComboBox):
combo_box.wheelEvent = lambda e: e.ignore()
def center_window(self):
# get the margins of the decorated window
margins = self.windowHandle().frameMargins()
# get the screen the cursor is on
current_screen = QApplication.screenAt(QCursor.pos())
if not current_screen:
current_screen = QApplication.primaryScreen()
# get the available screen geometry (excludes panels/docks)
screen_rect = current_screen.availableGeometry()
decor_width = margins.left() + margins.right()
decor_height = margins.top() + margins.bottom()
window_size = QSize(self.width(), self.height()).boundedTo(
2021-12-24 22:09:50 +13:00
screen_rect.size() - QSize(decor_width, decor_height)
)
self.resize(window_size)
self.move(screen_rect.center() - self.rect().adjusted(0, 0, decor_width, decor_height).center())
@pyqtSlot()
def show(self) -> None:
super(MainWindow, self).show()
if not self._window_launched:
self.center_window()
self._window_launched = True
def hide(self) -> None:
if self.settings.value("save_size", False, bool):
size = self.size().width(), self.size().height()
self.settings.setValue("window_size", size)
super(MainWindow, self).hide()
def toggle(self):
if self.isHidden():
self.show()
else:
self.hide()
2021-04-15 01:14:06 +12:00
def timer_finished(self):
file_path = lock_file()
2021-04-15 01:14:06 +12:00
if os.path.exists(file_path):
file = open(file_path, "r")
action = file.read()
file.close()
2022-09-02 22:17:05 +12:00
if action.startswith("show"):
2021-04-15 01:14:06 +12:00
self.show()
os.remove(file_path)
self.timer.start(1000)
@pyqtSlot()
@pyqtSlot(int)
def on_exit_app(self, exit_code=0) -> None:
self._exit_code = exit_code
self.close()
def close(self) -> bool:
self._accept_close = True
return super(MainWindow, self).close()
def closeEvent(self, e: QCloseEvent) -> None:
# lk: `accept_close` is set to `True` by the `close()` method, overrides exiting to tray in `closeEvent()`
# lk: ensures exiting instead of hiding when `close()` is called programmatically
if not self._accept_close:
if self.settings.value("sys_tray", False, bool):
self.hide()
e.ignore()
return
# FIXME: Fix this with the download tab redesign
if not self.args.offline and self.tab_widget.downloads_tab.is_download_active:
reply = QMessageBox.question(
self,
self.tr("Quit {}?").format(QApplication.applicationName()),
self.tr("There are active downloads. Are you sure you want to quit?"),
buttons=(QMessageBox.Yes | QMessageBox.No),
defaultButton=QMessageBox.No,
)
if reply == QMessageBox.Yes:
DownloadsTab: Refactor downloads tab When updates are queued, they are removed from the update's list. An exceptions is made when the queued item comes from repairing (without updating), in which case the update is disabled for the runtime. A queued item can be either removed (if it is an update it will be added back to the updates groups) or forced to be updated now. If a queued item is forced, the currently running item will be added to the front of the queue. Downloads will be queued if there is no active download but there is a queue already. The download thread is now responsible for emitting the progress to `RareGame` InstallDialog: Pass `RareGame` and `InstallOptionsModel` only as arguments. The `update`, `repair` and `silent` arguments are already part of `InstallOptionsModel` `RareGame` is used to query information about the game. InstallInfoWorker: Pass only `InstallOptionsModel` as argument Emit `InstallQueueItemModel` as result, to re-use the worker when queuing stopped games RareGame: Query and store metadata property about entitlement grant date RareGame: Add `RareEosOverlay` class that imitates `RareGame` to handle the overlay LibraryWidgetController: Remove dead signal routing code, these signals are handled by `RareGame` Directly parent library widgets instead of reparenting them GameWidgets: Remove unused signals EOSGroup: Set install location based on preferences and use EOSOverlayApp from legendary GamesTab: Connect the `progress` signals of dlcs to the base game's signals GamesTab: Remove dead code GlobalSignals: Remove `ProgresSignals` RareCore: Mangle internal signleton's names Signed-off-by: loathingKernel <142770+loathingKernel@users.noreply.github.com>
2023-01-21 13:15:06 +13:00
self.tab_widget.downloads_tab.stop_download(on_exit=True)
else:
e.ignore()
return
# FIXME: End of FIXME
self.timer.stop()
self.tray_icon.deleteLater()
self.hide()
self.exit_app.emit(self._exit_code)
super(MainWindow, self).closeEvent(e)