1
0
Fork 0
mirror of synced 2024-07-04 14:10:46 +12:00
Rare/rare/components/tabs/settings/widgets/pre_launch.py
loathingKernel ecc1bd8d5c IndicatorLineEdit: Run edit callback function asynchronously
Execute the edit callback function in a thread. By executing it in a thread
we don't have to wait for longer validation procedures to finish to
continue updating the UI. This is most notable in the MoveGamePopUp
which is heavy on disk IO.

Because we cannot use special text formatting in a thread, the
indicator messages have been reworked while also becoming extensible.

A dictionary of extended reasons can be specified through the
`IndicatorLineEdit.extend_reasons()` method.

The dictionary has to follow the following format
```
python
{
	MyIndicatorReasons.REASON: self.tr("Reason message")
	MyIndicatorReasons.OTHER_REASON: self.tr("Other reason message")
}
```

In the above example `MyIndicatorReasons` is a subclass of `IndicatorReasons`
which should be specified as follows

```
python
MyIndicatorReasons(IndicatorReasons):
	REASON = auto()
	OTHER_REASON = auto()
```
2023-02-18 14:37:37 +02:00

62 lines
2.2 KiB
Python

import os
import shutil
from typing import Tuple
from PyQt5.QtWidgets import QHBoxLayout, QCheckBox, QFileDialog
from rare.shared import LegendaryCoreSingleton
from rare.utils import config_helper
from rare.utils.extra_widgets import PathEdit, IndicatorReasonsCommon
class PreLaunchSettings(QHBoxLayout):
app_name: str
def __init__(self):
super(PreLaunchSettings, self).__init__()
self.core = LegendaryCoreSingleton()
self.edit = PathEdit(
path="",
placeholder=self.tr("Path to script"),
file_type=QFileDialog.ExistingFile,
edit_func=self.edit_command,
save_func=self.save_pre_launch_command,
)
self.layout().addWidget(self.edit)
self.wait_check = QCheckBox(self.tr("Wait for finish"))
self.layout().addWidget(self.wait_check)
self.wait_check.stateChanged.connect(self.save_wait_finish)
def edit_command(self, text: str) -> Tuple[bool, str, int]:
if not text.strip():
return True, text, IndicatorReasonsCommon.VALID
if not os.path.isfile(text.split()[0]) and not shutil.which(text.split()[0]):
return False, text, IndicatorReasonsCommon.FILE_NOT_EXISTS
else:
return True, text, IndicatorReasonsCommon.VALID
def save_pre_launch_command(self, text):
if text:
config_helper.add_option(self.app_name, "pre_launch_command", text)
self.wait_check.setDisabled(False)
else:
config_helper.remove_option(self.app_name, "pre_launch_command")
self.wait_check.setDisabled(True)
config_helper.remove_option(self.app_name, "pre_launch_wait")
def save_wait_finish(self):
config_helper.add_option(self.app_name, "pre_launch_wait", str(self.wait_check.isChecked()).lower())
def load_settings(self, app_name):
self.app_name = app_name
command = self.core.lgd.config.get(app_name, "pre_launch_command", fallback="")
self.edit.setText(command)
wait = self.core.lgd.config.getboolean(app_name, "pre_launch_wait", fallback=False)
self.wait_check.setChecked(wait)
self.wait_check.setEnabled(bool(command))