-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings_window.py
More file actions
115 lines (90 loc) · 4.01 KB
/
settings_window.py
File metadata and controls
115 lines (90 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import webbrowser
from typing import override
from PyQt6.QtCore import QObject, Qt
from PyQt6.QtWidgets import (
QComboBox,
QDialog,
QFormLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QPushButton,
QVBoxLayout,
)
from config import Config
class SettingsWindow(QDialog):
def __init__(self, config: Config, parent: QObject | None = None) -> None:
super().__init__(parent)
self._config = config
self.setWindowTitle("Settings")
self.setMinimumWidth(500)
layout = QVBoxLayout(self)
form_layout = QFormLayout()
self._user_language_input = QLineEdit()
self._user_language_input.setPlaceholderText("e.g., en, ja, es")
form_layout.addRow("User Language:", self._user_language_input)
api_key_layout = QHBoxLayout()
api_key_layout.setSpacing(8)
self._google_api_key_input = QLineEdit()
self._google_api_key_input.setEchoMode(QLineEdit.EchoMode.Password)
self._google_api_key_input.setPlaceholderText("Enter your Google Cloud API key")
self._google_api_key_input.textChanged.connect(
self._update_api_key_field_styling
)
api_key_layout.addWidget(self._google_api_key_input)
api_key_help_link = QLabel(
'<a style="text-decoration: underline; color: #0066cc;" href="#">how to create</a>'
)
api_key_help_link.setCursor(Qt.CursorShape.PointingHandCursor)
api_key_help_link.linkActivated.connect(
lambda: webbrowser.open(
"https://github.com/ivanyu/GameTran/blob/main/docs/api_key.md"
)
)
api_key_layout.addWidget(api_key_help_link)
form_layout.addRow("Google Cloud API Key:", api_key_layout)
self._global_hotkey_input = QLineEdit()
self._global_hotkey_input.setPlaceholderText("e.g., <alt>+p, <ctrl>+<shift>+s")
form_layout.addRow("Global Hotkey:", self._global_hotkey_input)
self._prompt_language_combo = QComboBox()
self._prompt_language_combo.addItem("en")
form_layout.addRow("Prompt Language:", self._prompt_language_combo)
layout.addLayout(form_layout)
button_layout = QHBoxLayout()
button_layout.addStretch()
self._save_button = QPushButton("Save")
self._save_button.clicked.connect(self._on_save)
button_layout.addWidget(self._save_button)
self._cancel_button = QPushButton("Cancel")
self._cancel_button.clicked.connect(self.reject)
button_layout.addWidget(self._cancel_button)
layout.addLayout(button_layout)
self._user_language_input.setText(self._config.user_language)
self._google_api_key_input.setText(self._config.google_api_key)
self._global_hotkey_input.setText(self._config.global_hotkey)
# Set current prompt language value
index = self._prompt_language_combo.findText(self._config.prompt_language)
if index >= 0:
self._prompt_language_combo.setCurrentIndex(index)
self._update_api_key_field_styling()
def _update_api_key_field_styling(self, text: str = "") -> None:
if not self._google_api_key_input.text().strip():
self._google_api_key_input.setStyleSheet(
"QLineEdit { border: 2px solid #e74c3c; background-color: #fde8e8; }"
)
else:
self._google_api_key_input.setStyleSheet("")
def _on_save(self) -> None:
self._config.user_language = self._user_language_input.text()
self._config.google_api_key = self._google_api_key_input.text()
self._config.global_hotkey = self._global_hotkey_input.text()
self._config.prompt_language = self._prompt_language_combo.currentText()
self.accept()
@override
def keyPressEvent(self, event) -> None:
if event.key() == Qt.Key.Key_Return or event.key() == Qt.Key.Key_Enter:
self._on_save()
elif event.key() == Qt.Key.Key_Escape:
self.reject()
else:
super().keyPressEvent(event)