-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_key_dialog.py
More file actions
222 lines (194 loc) · 7.08 KB
/
api_key_dialog.py
File metadata and controls
222 lines (194 loc) · 7.08 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""
API Key Dialog for FS25 Language Translator
Secure dialog for entering and managing API keys
"""
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
from PyQt6.QtGui import *
class ApiKeyDialog(QDialog):
"""Dialog for entering and managing API keys"""
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("API Key Configuration")
self.setModal(True)
self.setFixedWidth(500)
# Main layout
layout = QVBoxLayout()
layout.setSpacing(15)
layout.setContentsMargins(20, 20, 20, 20)
# Header
header = QLabel("🔐 Secure API Key Storage")
header.setStyleSheet("""
QLabel {
font-size: 16px;
font-weight: bold;
color: #4CAF50;
padding: 10px;
background: rgba(76, 175, 80, 0.1);
border-radius: 8px;
}
""")
header.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(header)
# Information text
info_text = (
"Your API key will be securely stored in your system's credential manager:\n"
"• Windows: Windows Credential Manager\n"
"• macOS: Keychain\n"
"• Linux: Secret Service\n\n"
"The key is encrypted and protected by your system."
)
info_label = QLabel(info_text)
info_label.setWordWrap(True)
info_label.setStyleSheet("""
QLabel {
color: #cccccc;
padding: 10px;
background: #2a2a2a;
border-radius: 8px;
font-size: 12px;
}
""")
layout.addWidget(info_label)
# API Key input group
key_group = QGroupBox("DeepL API Key")
key_group.setStyleSheet("""
QGroupBox {
font-weight: bold;
border: 2px solid #4CAF50;
border-radius: 8px;
margin-top: 20px;
padding-top: 25px;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 20px;
padding: 5px 10px;
color: white;
background: #4CAF50;
border-radius: 4px;
}
""")
key_layout = QVBoxLayout()
# Input field
self.key_input = QLineEdit()
self.key_input.setPlaceholderText("Paste your DeepL API key here...")
self.key_input.setEchoMode(QLineEdit.EchoMode.Password)
self.key_input.setStyleSheet("""
QLineEdit {
padding: 10px;
font-size: 14px;
border: 1px solid #3a3a3a;
border-radius: 6px;
background: #1e1e1e;
color: white;
}
QLineEdit:focus {
border: 1px solid #4CAF50;
}
""")
key_layout.addWidget(self.key_input)
# Show/Hide checkbox
self.show_key_cb = QCheckBox("Show API key")
self.show_key_cb.setStyleSheet("""
QCheckBox {
color: #cccccc;
padding: 5px;
}
""")
self.show_key_cb.toggled.connect(self.toggle_key_visibility)
key_layout.addWidget(self.show_key_cb)
# Link to DeepL
deepl_link = QLabel('<a href="https://www.deepl.com/your-account/keys" style="color: #4CAF50;">Get your DeepL API key here</a>')
deepl_link.setOpenExternalLinks(True)
deepl_link.setStyleSheet("padding: 5px;")
key_layout.addWidget(deepl_link)
key_group.setLayout(key_layout)
layout.addWidget(key_group)
# Skip button (use Google only)
skip_btn = QPushButton("Skip (Use Google Translate only)")
skip_btn.setStyleSheet("""
QPushButton {
padding: 8px;
background: #666666;
border: none;
border-radius: 6px;
color: white;
font-size: 12px;
}
QPushButton:hover {
background: #777777;
}
""")
skip_btn.clicked.connect(self.reject)
layout.addWidget(skip_btn)
# Button box
buttons = QDialogButtonBox()
save_btn = buttons.addButton("Save Key", QDialogButtonBox.ButtonRole.AcceptRole)
save_btn.setStyleSheet("""
QPushButton {
padding: 10px 20px;
background: #4CAF50;
border: none;
border-radius: 6px;
color: white;
font-weight: bold;
font-size: 14px;
min-width: 100px;
}
QPushButton:hover {
background: #5cbf60;
}
""")
cancel_btn = buttons.addButton(QDialogButtonBox.StandardButton.Cancel)
cancel_btn.setStyleSheet("""
QPushButton {
padding: 10px 20px;
background: #666666;
border: none;
border-radius: 6px;
color: white;
font-weight: bold;
font-size: 14px;
min-width: 100px;
}
QPushButton:hover {
background: #777777;
}
""")
buttons.accepted.connect(self.validate_and_accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
self.setLayout(layout)
# Set dark theme for dialog
self.setStyleSheet("""
QDialog {
background-color: #1e1e1e;
}
""")
def toggle_key_visibility(self, checked):
"""Toggle visibility of the API key input"""
if checked:
self.key_input.setEchoMode(QLineEdit.EchoMode.Normal)
else:
self.key_input.setEchoMode(QLineEdit.EchoMode.Password)
def validate_and_accept(self):
"""Validate the API key before accepting"""
api_key = self.key_input.text().strip()
if not api_key:
QMessageBox.warning(self, "Invalid Key", "Please enter an API key.")
return
# Basic validation
if len(api_key) < 20:
reply = QMessageBox.question(
self,
"Short API Key",
"This API key seems unusually short. Continue anyway?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
)
if reply == QMessageBox.StandardButton.No:
return
self.accept()
def get_api_key(self) -> str:
"""Get the entered API key"""
return self.key_input.text().strip()