Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified TSH.exe
Binary file not shown.
43 changes: 43 additions & 0 deletions layout/settings_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"assets": {
"type": "asset"
},
"japanese_transcription": {
"enabled": {
"type": "bool",
"default": true
},
"to": {
"type": "combobox",
"options": [
"romaji",
"haragana",
"katakana"
],
"default": "romaji"
},
"mode": {
"type": "combobox",
"options": [
"normal",
"spaced",
"okurigana",
"furigana"
],
"default": "normal"
},
"romajiSystem": {
"type": "combobox",
"options": [
"nippon",
"passport",
"hepburn"
],
"default": "nippon"
}
},
"automatic_theme": {
"type": "bool",
"default": false
}
}
118 changes: 93 additions & 25 deletions src/Settings/SettingsWidget.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
from qtpy.QtGui import *
from qtpy.QtWidgets import *
from qtpy.QtCore import *
from ..TSHHotkeys import TSHHotkeys
from ..SettingsManager import SettingsManager
from dataclasses import dataclass
from ..TSHGameAssetManager import TSHGameAssetManager


@dataclass
class SETTINGS:
name: str = None
path: str = None
type: str = None
default: any = None
callback: callable = lambda: None
options: list = None


class SettingsWidget(QWidget):
Expand All @@ -16,49 +30,103 @@ def __init__(self, settingsBase="", settings=[]):
# Set the layout for the widget
self.setLayout(layout)

# Keep reference of all settings related to assets
self.assetsSettings = []

for setting in settings:
self.AddSetting(*setting)
self.AddSetting(setting)

TSHGameAssetManager.instance.signals.onLoad.connect(self.GamesReloaded)

def AddSetting(self, name: str, setting: str, type: str, defaultValue, callback=lambda: None):
def GamesReloaded(self):
for (gameCombo, assetCombo) in self.assetsSettings:
gameCombo.clear()

for (key, val) in TSHGameAssetManager.instance.games.items():
item = QStandardItem()
item.setText(f'{val.get("name")} ({key})')
item.setData(val)
gameCombo.model().appendRow(item)

def AddSetting(self, settings: SETTINGS = SETTINGS()):
lastRow = self.layout().rowCount()

self.layout().addWidget(QLabel(name), lastRow, 0)
self.layout().addWidget(QLabel(settings.name), lastRow, 0)

resetButton = QPushButton(
QApplication.translate("settings", "Default"))

if type == "checkbox":
settingWidget = None

if settings.type == "bool":
settingWidget = QCheckBox()
settingWidget.setChecked(SettingsManager.Get(
self.settingsBase+"."+setting, defaultValue))
settingWidget.stateChanged.connect(
lambda val=None: SettingsManager.Set(self.settingsBase+"."+setting, settingWidget.isChecked()))
resetButton.clicked.connect(
lambda bt=None, settingWidget=settingWidget:
settingWidget.setChecked(defaultValue)
)
elif type == "hotkey":
self.settingsBase+"."+settings.path, settings.default))
settingWidget.stateChanged.connect(lambda val=None: SettingsManager.Set(
self.settingsBase+"."+settings.path, settingWidget.isChecked()))
resetButton.clicked.connect(lambda bt=None, settingWidget=settingWidget:
settingWidget.setChecked(
settings.default)
)
elif settings.type == "hotkey":
settingWidget = QKeySequenceEdit()
settingWidget.keySequenceChanged.connect(
lambda keySequence, settingWidget=settingWidget:
settingWidget.setKeySequence(keySequence.toString().split(",")[
0]) if keySequence.count() > 0 else None
)
settingWidget.keySequenceChanged.connect(lambda keySequence, settingWidget=settingWidget:
settingWidget.setKeySequence(keySequence.toString().split(",")[
0]) if keySequence.count() > 0 else None
)
settingWidget.setKeySequence(SettingsManager.Get(
self.settingsBase+"."+setting, defaultValue))
self.settingsBase+"."+settings.path, settings.default))
settingWidget.keySequenceChanged.connect(
lambda sequence=None, setting=setting: [
lambda sequence=None, setting=settings.path: [
SettingsManager.Set(
self.settingsBase+"."+setting, sequence.toString()),
callback()
settings.callback()
]
)
resetButton.clicked.connect(
lambda bt=None, setting=setting, settingWidget=settingWidget: [
settingWidget.setKeySequence(defaultValue),
callback()
lambda bt=None, setting=settings.path, settingWidget=settingWidget: [
settingWidget.setKeySequence(settings.default),
settings.callback()
]
)
elif settings.type == "combobox":
settingWidget = QComboBox()

if settings.options:
for option in settings.options:
settingWidget.addItem(option)

defaultIndex = settings.options.index(settings.default)
settingWidget.setCurrentIndex(defaultIndex)

settingWidget.currentIndexChanged.connect(
lambda sequence, setting=settings.path: [
SettingsManager.Set(
self.settingsBase+"."+setting, settingWidget.currentText()),
settings.callback()
]
)

resetButton.clicked.connect(
lambda bt, setting=settings.path, settingWidget=settingWidget: [
settingWidget.setCurrentIndex(defaultIndex),
settings.callback()
]
)
elif settings.type == "asset":
settingWidget = QWidget()
settingWidget.setLayout(QHBoxLayout())
settingWidget.setContentsMargins(0, 0, 0, 0)
settingWidget.layout().setContentsMargins(0, 0, 0, 0)
gameCombo = QComboBox()
assetCombo = QComboBox()
settingWidget.layout().addWidget(gameCombo)
settingWidget.layout().addWidget(assetCombo)
self.assetsSettings.append((gameCombo, assetCombo))
else:
settingWidget = QLabel(
f'Could not identify "{settings.type}" type for {settings.name}')

self.layout().addWidget(settingWidget, lastRow, 1)
self.layout().addWidget(resetButton, lastRow, 2)
if settingWidget:
self.layout().addWidget(settingWidget, lastRow, 1)
self.layout().addWidget(resetButton, lastRow, 2)
76 changes: 55 additions & 21 deletions src/Settings/TSHSettingsWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,25 @@
from qtpy.QtCore import *
from qtpy.QtWidgets import *
from .SettingsWidget import SettingsWidget
from .SettingsWidget import SETTINGS as SettingsWidgetSettings
from ..TSHHotkeys import TSHHotkeys
import json


def iterate_json_leaves(obj, key=None):
if isinstance(obj, dict):
isLeaf = True
for k, value in obj.items():
if isinstance(value, dict):
isLeaf = False
break
if isLeaf:
yield {key: obj}
else:
for k, value in obj.items():
yield from iterate_json_leaves(value, f'{key}.{k}' if key else k)
else:
yield obj


class TSHSettingsWindow(QDialog):
Expand Down Expand Up @@ -38,26 +56,24 @@ def UiMounted(self):
# Add general settings
generalSettings = []

generalSettings.append((
QApplication.translate(
"settings.general", "Enable profanity filter"),
"profanity_filter",
"checkbox",
True
))
generalSettings.append(SettingsWidgetSettings(**{
"name": QApplication.translate("settings.general", "Enable profanity filter"),
"path": "profanity_filter",
"type": "bool",
"default": True
}))

self.add_setting_widget(QApplication.translate(
"settings", "General"), SettingsWidget("general", generalSettings))

# Add hotkey settings
hotkeySettings = []

hotkeySettings.append((
QApplication.translate("settings.hotkeys", "Enable hotkeys"),
"hotkeys_enabled",
"checkbox",
True
))
hotkeySettings.append(SettingsWidgetSettings(**{
"name": QApplication.translate("settings.hotkeys", "Enable hotkeys"),
"path": "hotkeys_enabled",
"type": "bool",
"default": True
}))

key_names = {
"load_set": QApplication.translate("settings.hotkeys", "Load set"),
Expand All @@ -70,17 +86,35 @@ def UiMounted(self):
}

for i, (setting, value) in enumerate(TSHHotkeys.instance.keys.items()):
hotkeySettings.append((
key_names[setting],
setting,
"hotkey",
value,
TSHHotkeys.instance.ReloadHotkeys
))
hotkeySettings.append(SettingsWidgetSettings(**{
"name": key_names[setting],
"path": setting,
"type": "hotkey",
"default": value,
"callback": TSHHotkeys.instance.ReloadHotkeys
}))

self.add_setting_widget(QApplication.translate(
"settings", "Hotkeys"), SettingsWidget("hotkeys", hotkeySettings))

# Layout
layoutSettings = []

layoutJson = json.load(open("./layout/settings_map.json"))

for entry in iterate_json_leaves(layoutJson):
(key, value) = list(entry.items())[0]
layoutSettings.append(SettingsWidgetSettings(**{
"name": key,
"path": key,
"type": value.get("type"),
"default": value.get("default"),
"options": value.get("options")
}))

self.add_setting_widget(QApplication.translate(
"settings", "Layout"), SettingsWidget("layout", layoutSettings))

self.resize(1000, 500)
QApplication.processEvents()
splitter.setSizes([200, self.width()-200])
Expand Down
28 changes: 14 additions & 14 deletions src/i18n/TSH_de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ p, li { white-space: pre-wrap; }
<name>Settings</name>
<message>
<location filename="../TournamentStreamHelper.py" line="470"/>
<location filename="../Settings/TSHSettingsWindow.py" line="13"/>
<location filename="../Settings/TSHSettingsWindow.py" line="31"/>
<source>Settings</source>
<translation type="unfinished"></translation>
</message>
Expand Down Expand Up @@ -1371,68 +1371,68 @@ p, li { white-space: pre-wrap; }
<context>
<name>settings</name>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="49"/>
<location filename="../Settings/TSHSettingsWindow.py" line="66"/>
<source>General</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="81"/>
<location filename="../Settings/TSHSettingsWindow.py" line="97"/>
<source>Hotkeys</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../Settings/SettingsWidget.py" line="28"/>
<source>Default</source>
<location filename="../Settings/TSHSettingsWindow.py" line="115"/>
<source>Layout</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>settings.general</name>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="42"/>
<location filename="../Settings/TSHSettingsWindow.py" line="60"/>
<source>Enable profanity filter</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>settings.hotkeys</name>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="63"/>
<location filename="../Settings/TSHSettingsWindow.py" line="79"/>
<source>Load set</source>
<translation type="unfinished">Set laden</translation>
</message>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="64"/>
<location filename="../Settings/TSHSettingsWindow.py" line="80"/>
<source>Team 1 score up</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="65"/>
<location filename="../Settings/TSHSettingsWindow.py" line="81"/>
<source>Team 1 score down</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="66"/>
<location filename="../Settings/TSHSettingsWindow.py" line="82"/>
<source>Team 2 score up</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="67"/>
<location filename="../Settings/TSHSettingsWindow.py" line="83"/>
<source>Team 2 score down</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="68"/>
<location filename="../Settings/TSHSettingsWindow.py" line="84"/>
<source>Reset scores</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="69"/>
<location filename="../Settings/TSHSettingsWindow.py" line="85"/>
<source>Swap teams</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../Settings/TSHSettingsWindow.py" line="56"/>
<location filename="../Settings/TSHSettingsWindow.py" line="72"/>
<source>Enable hotkeys</source>
<translation type="unfinished"></translation>
</message>
Expand Down
Loading