1
0
Fork 0
mirror of synced 2024-06-29 19:51:02 +12:00
Rare/rare/components/main_window.py

101 lines
4.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, QRect
from PyQt5.QtGui import QCloseEvent, QCursor
2021-05-02 23:15:11 +12:00
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QApplication
2021-02-10 23:48:25 +13:00
2021-10-08 07:19:24 +13:00
from rare import data_dir, shared
from rare.components.tabs import TabWidget
2021-04-13 07:55:08 +12:00
from rare.utils.rpc import DiscordRPC
2021-02-10 23:48:25 +13:00
2021-04-14 02:56:44 +12:00
logger = getLogger("Window")
2021-02-10 23:48:25 +13:00
class MainWindow(QMainWindow):
2021-04-12 08:44:18 +12:00
2021-10-08 07:19:24 +13:00
def __init__(self):
2021-02-10 23:48:25 +13:00
super(MainWindow, self).__init__()
self.setAttribute(Qt.WA_DeleteOnClose)
2021-04-13 06:31:12 +12:00
self.settings = QSettings()
2021-10-15 10:18:13 +13:00
self.core = shared.core
2021-09-30 10:22:47 +13:00
2021-10-08 07:19:24 +13:00
self.signals = shared.signals
self.offline = shared.args.offline
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)
2021-04-07 22:11:31 +12:00
self.setCentralWidget(self.tab_widget)
width, height = 1200, 800
if self.settings.value("save_size", False):
width, height = self.settings.value("window_size", (width, height), tuple)
# move the window outside the viewport
self.move(-50000, -50000)
# show the window in order for it to get decorated, otherwise windowHandle() is null
self.show()
# get the margins of the decorated window
margins = self.windowHandle().frameMargins()
# hide the window again because we don't want to show it at this point
self.hide()
# get the screen the cursor is on
current_screen = QApplication.screenAt(QCursor.pos())
# 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(width, height).boundedTo(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())
2021-10-08 07:19:24 +13:00
if not shared.args.offline:
2021-10-15 10:05:00 +13:00
self.rpc = DiscordRPC()
2021-04-20 01:44:28 +12:00
self.tab_widget.delete_presence.connect(self.rpc.set_discord_rpc)
2021-10-08 07:19:24 +13:00
if shared.args.subparser == "launch":
if shared.args.app_name in [i.app_name for i in self.tab_widget.games_tab.installed]:
logger.info("Launching " + self.core.get_installed_game(shared.args.app_name).title)
2021-10-24 12:47:49 +13:00
self.tab_widget.games_tab.widgets[shared.args.app_name][1].prepare_launch()
2021-04-14 02:56:44 +12:00
else:
logger.info(
f"Could not find {shared.args.app_name} in Games or it is not installed")
2021-04-12 08:44:18 +12:00
2021-04-15 01:14:06 +12:00
self.timer = QTimer()
self.timer.timeout.connect(self.timer_finished)
self.timer.start(1000)
def timer_finished(self):
file_path = os.path.join(data_dir, "lockfile")
2021-04-15 01:14:06 +12:00
if os.path.exists(file_path):
file = open(file_path, "r")
action = file.read()
file.close()
if action.startswith("launch"):
game = action.replace("launch ", "").replace("\n", "")
2021-10-24 12:47:49 +13:00
if game in [i.app_name for i in self.tab_widget.games_tab.game_list] and self.core.is_installed(game):
self.tab_widget.games_tab.game_utils.prepare_launch(game, offline=shared.args.offline)
2021-04-15 01:14:06 +12:00
else:
logger.info(f"Could not find {game} in Games")
elif action.startswith("start"):
self.show()
os.remove(file_path)
self.timer.start(1000)
2021-04-07 22:11:31 +12:00
def closeEvent(self, e: QCloseEvent):
2021-04-13 06:31:12 +12:00
if self.settings.value("sys_tray", True, bool):
2021-04-08 00:46:27 +12:00
self.hide()
e.ignore()
2021-04-11 00:58:34 +12:00
return
2021-04-20 01:44:28 +12:00
elif self.offline:
pass
2021-04-11 00:58:34 +12:00
elif self.tab_widget.downloadTab.active_game is not None:
2021-04-14 02:56:44 +12:00
if not QMessageBox.question(self, "Close",
self.tr("There is a download active. Do you really want to exit app?"),
QMessageBox.Yes, QMessageBox.No) == QMessageBox.Yes:
2021-04-11 00:58:34 +12:00
e.ignore()
return
2021-04-13 06:31:12 +12:00
if self.settings.value("save_size", False, bool):
2021-04-11 00:58:34 +12:00
size = self.size().width(), self.size().height()
2021-04-13 06:31:12 +12:00
self.settings.setValue("window_size", size)
2021-04-11 00:58:34 +12:00
e.accept()