From 95a02345e9c864b957e53038230c056627244968 Mon Sep 17 00:00:00 2001 From: Alexander Chernoskutov Date: Tue, 6 Feb 2024 12:52:16 +0400 Subject: [PATCH] Code cleanup --- src/GUI/AppGUI.py | 82 ++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/src/GUI/AppGUI.py b/src/GUI/AppGUI.py index 1423e65..a9497a9 100644 --- a/src/GUI/AppGUI.py +++ b/src/GUI/AppGUI.py @@ -1,6 +1,6 @@ import sys, os, time from enum import Enum -from typing import Callable, Literal +from typing import Callable, Literal, Optional, Tuple from PySide6 import QtCore, QtGui, QtWidgets from Backend.AWCCThermal import AWCCThermal, NoAWCCWMIClass, CannotInstAWCCWMI from GUI.QRadioButtonSet import QRadioButtonSet @@ -36,16 +36,29 @@ def autorunTask(action: Literal['add', 'remove']) -> int: else: return os.system(removeCmd) -def alert(title: str, message: str, type: QtWidgets.QMessageBox.Icon = QtWidgets.QMessageBox.Icon.Information, *, message2: str = None) -> None: - msg = QtWidgets.QMessageBox() +def alert(title: str, message: str, type: QtWidgets.QMessageBox.Icon = QtWidgets.QMessageBox.Icon.Information, *, message2: Optional[str] = None) -> None: + msg = QtWidgets.QMessageBox(type, title, message) msg.setWindowIcon(QtGui.QIcon(resourcePath(GUI_ICON))) - msg.setIcon(type) - msg.setWindowTitle(title) - msg.setText(message) if message2: msg.setInformativeText(message2) msg.setStandardButtons(QtWidgets.QMessageBox.Ok) msg.exec() +def confirm(title: str, message: str, options: Optional[Tuple[str, str]] = None, dontAskAgain: bool = False) -> Tuple[bool, Optional[bool]]: + msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Question, title, message, QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) + msg.setWindowIcon(QtGui.QIcon(resourcePath(GUI_ICON))) + + if options is not None: + msg.button(QtWidgets.QMessageBox.Yes).setText(options[0]) + msg.button(QtWidgets.QMessageBox.No).setText(options[1]) + + cbDontAskAgain = None + if dontAskAgain: + cbDontAskAgain = QtWidgets.QCheckBox('Don\'t ask me again.', msg) + msg.setCheckBox(cbDontAskAgain) + + return (msg.exec_() == QtWidgets.QMessageBox.Yes, cbDontAskAgain is not None and cbDontAskAgain.isChecked() or None) + + class QPeriodic: def __init__(self, parent: QtCore.QObject, periodMs: int, callback: Callable) -> None: self._tmr = QtCore.QTimer(parent) @@ -68,9 +81,9 @@ class SettingsKey(Enum): CPUThresholdTemp = "app/fan/cpu/threshold_temp" GPUFanSpeed = "app/fan/gpu/speed" GPUThresholdTemp = "app/fan/gpu/threshold_temp" - MinimizeOnCloseFlag = "app/exit" + MinimizeOnCloseFlag = "app/minimize_on_close_flag" -def errorExit(message: str, message2: str = None) -> None: +def errorExit(message: str, message2: Optional[str] = None) -> None: if not QtWidgets.QApplication.instance(): QtWidgets.QApplication([]) alert("Oh-oh", message, QtWidgets.QMessageBox.Icon.Critical, message2 = message2) @@ -299,41 +312,24 @@ def updateOutput(): self._loadAppSettings() def closeEvent(self, event): - exit_value = self.settings.value(SettingsKey.MinimizeOnCloseFlag.value) - if exit_value == 1: - self.onExit() - elif exit_value == 2: + minimizeOnClose = self.settings.value(SettingsKey.MinimizeOnCloseFlag.value) + if minimizeOnClose is None: + # minimizeOnClose is not set, prompt user + (toExit, dontAskAgain) = confirm("Exit", "Do you want to exit or minimize to tray?", ("Exit", "Minimize"), True) + minimizeOnClose = not toExit + if dontAskAgain: + self.settings.setValue(SettingsKey.MinimizeOnCloseFlag.value, minimizeOnClose) + + if minimizeOnClose: event.ignore() self.hide() else: - msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Question, 'Exit', - "Do you want to exit or minimize to tray?", QtWidgets.QMessageBox.Yes | - QtWidgets.QMessageBox.No, self) - - exit_button = msg_box.button(QtWidgets.QMessageBox.Yes) - exit_button.setText('Exit') - - hide_button = msg_box.button(QtWidgets.QMessageBox.No) - hide_button.setText('Minimize') - - cbClose = QtWidgets.QCheckBox('Don\'t ask me again.', self) - msg_box.setCheckBox(cbClose) - - reply = msg_box.exec_() - - if reply == QtWidgets.QMessageBox.Yes: - if cbClose.isChecked(): - self.settings.setValue(SettingsKey.MinimizeOnCloseFlag.value, 1) - self.onExit() - else: - if cbClose.isChecked(): - self.settings.setValue(SettingsKey.MinimizeOnCloseFlag.value, 2) - event.ignore() - self.hide() + self.onExit() + return # onExit() connected to systray_Exit def onExit(self): - # print("exit") + print("exit") self._saveAppSettings() # Set mode to Balanced before exit self._updateGaugesTask.stop() @@ -363,18 +359,10 @@ def _loadAppSettings(self): if savedTemp is not None: self._limitTempGPU.setCurrentText(str(savedTemp)) def clearAppSettings(self): - msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Restore Default', - "Do you want to restore all settings to default?", QtWidgets.QMessageBox.Yes | - QtWidgets.QMessageBox.No, self) - reply = msg_box.exec_() - if reply == QtWidgets.QMessageBox.Yes: + (isYes, _) = confirm("Reset to Default", "Do you want to reset all settings to default?", ("Reset", "Cancel")) + if isYes: self.settings.clear() - def testWMIsupport(self): - try: - pass - except: - pass def runApp(startMinimized = False) -> int: app = QtWidgets.QApplication([])