splitguides 0.10.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. splitguides/__init__.py +1 -0
  2. splitguides/__main__.py +15 -0
  3. splitguides/_version.py +2 -0
  4. splitguides/build_ui.py +71 -0
  5. splitguides/exceptions.py +4 -0
  6. splitguides/hotkeys.py +54 -0
  7. splitguides/livesplit_client.py +303 -0
  8. splitguides/note_parser.py +260 -0
  9. splitguides/server/__init__.py +1 -0
  10. splitguides/server/__main__.py +58 -0
  11. splitguides/server/split_server.py +129 -0
  12. splitguides/settings.py +231 -0
  13. splitguides/static/desktop.css +146 -0
  14. splitguides/static/server.css +160 -0
  15. splitguides/static/streamlistener.js +18 -0
  16. splitguides/templates/desktop.html +25 -0
  17. splitguides/templates/server.html +12 -0
  18. splitguides/ui/__init__.py +0 -0
  19. splitguides/ui/color.py +50 -0
  20. splitguides/ui/custom_elements/__init__.py +18 -0
  21. splitguides/ui/hotkey_manager.py +92 -0
  22. splitguides/ui/layouts/__init__.py +8 -0
  23. splitguides/ui/layouts/build/__init__.py +0 -0
  24. splitguides/ui/layouts/build/main_window.py +61 -0
  25. splitguides/ui/layouts/build/server_settings.py +349 -0
  26. splitguides/ui/layouts/build/settings.py +318 -0
  27. splitguides/ui/layouts/main_window.ui +64 -0
  28. splitguides/ui/layouts/server_settings.ui +394 -0
  29. splitguides/ui/layouts/settings.ui +382 -0
  30. splitguides/ui/main.pyproject +3 -0
  31. splitguides/ui/main_window.py +492 -0
  32. splitguides/ui/server_settings_ui.py +191 -0
  33. splitguides/ui/settings_ui.py +241 -0
  34. splitguides-0.10.1.dist-info/METADATA +199 -0
  35. splitguides-0.10.1.dist-info/RECORD +39 -0
  36. splitguides-0.10.1.dist-info/WHEEL +5 -0
  37. splitguides-0.10.1.dist-info/entry_points.txt +3 -0
  38. splitguides-0.10.1.dist-info/licenses/COPYING +674 -0
  39. splitguides-0.10.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,492 @@
1
+ """
2
+ Define the main windows for the desktop application
3
+ """
4
+ from __future__ import annotations
5
+
6
+ import sys
7
+ import time
8
+ from pathlib import Path
9
+ from concurrent.futures import ThreadPoolExecutor
10
+
11
+ from jinja2 import Environment, FileSystemLoader, Template
12
+ from PySide6 import QtCore
13
+ from PySide6.QtGui import QColorConstants, QCursor, QIcon, QMouseEvent, QAction
14
+ from PySide6.QtWidgets import QMainWindow, QFileDialog, QMenu, QErrorMessage
15
+
16
+ from .color import rgba_to_qss
17
+ from .custom_elements import ExtLinkWebEnginePage
18
+ from .hotkey_manager import HotkeyManager
19
+ from .layouts import Ui_MainWindow
20
+ from .settings_ui import SettingsDialog
21
+
22
+ from ..livesplit_client import get_client, LivesplitMessaging
23
+ from ..note_parser import Notes
24
+ from ..settings import DesktopSettings
25
+
26
+
27
+ # Get correct paths
28
+ if getattr(sys, "frozen", False): # pragma: nocover
29
+ # PyInstaller .exe
30
+ base_path = Path(sys.executable).parent
31
+ icon_file = str(base_path / "logo_alpha.png")
32
+ else:
33
+ # Running locally
34
+ base_path = Path(__file__).parent
35
+ icon_file = str(base_path.parents[2] / "resources" / "logo_alpha.png")
36
+
37
+
38
+ class MainWindow(QMainWindow):
39
+ ui: Ui_MainWindow
40
+ icon: QIcon
41
+
42
+ settings: DesktopSettings
43
+
44
+ # Right click menu and options
45
+ rc_menu: QMenu
46
+ menu_on_top: QAction
47
+ menu_transparency: QAction
48
+
49
+ # Hotkeys
50
+ if sys.platform == "win32":
51
+ hotkeys_toggle: QAction
52
+ hotkey_manager: HotkeyManager
53
+ else:
54
+ hotkeys_toggle: None
55
+ hotkey_manager: None
56
+
57
+ notefile: None | str
58
+ notes: None | Notes
59
+
60
+ j2_environment: Environment
61
+
62
+ template: Template
63
+ css: str
64
+
65
+ client: LivesplitMessaging
66
+ ls: LivesplitLink
67
+
68
+ split_index: int
69
+ split_offset: int
70
+
71
+ def __init__(self):
72
+ super().__init__()
73
+ # Setup the UI and get an icon
74
+ self.ui = Ui_MainWindow()
75
+ self.ui.setupUi(self)
76
+ self.icon = QIcon(icon_file)
77
+ self.setWindowIcon(self.icon)
78
+
79
+ # Initial statusbar message
80
+ self.ui.statusbar.showMessage("Not connected to server.")
81
+
82
+ # Get settings
83
+ self.settings = DesktopSettings.load()
84
+
85
+ # Window size
86
+ self.resize(self.settings.width, self.settings.height)
87
+
88
+ # noinspection PyUnresolvedReferences
89
+ self.setWindowFlag(QtCore.Qt.WindowType.WindowStaysOnTopHint, self.settings.on_top)
90
+
91
+ # WA_TranslucentBackground attribute required to enable transparency
92
+ # QT Docs state "Toggling this attribute after the widget has been shown is not uniformly supported"
93
+ # So this is only set once to True in __init__ unlike the other transparency settings
94
+ self.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground)
95
+ self.refresh_transparency()
96
+
97
+ # Setup notes variables
98
+ self.notefile = None
99
+ self.notes = None
100
+
101
+ # Build the right click menu
102
+ # Creates rc_menu, menu_on_top, menu_transparency, hotkeys_toggle
103
+ self.build_menu()
104
+ self.setup_actions()
105
+
106
+ # Set up hotkey manager - windows only
107
+ if sys.platform == "win32":
108
+ self.hotkey_manager = HotkeyManager(self)
109
+
110
+ if self.settings.hotkeys_enabled:
111
+ try:
112
+ self.enable_hotkeys()
113
+ except AttributeError:
114
+ QErrorMessage(parent=self).showMessage("Could not enable hotkeys.")
115
+ self.disable_hotkeys()
116
+ self.settings.hotkeys_enabled = False
117
+ self.hotkeys_toggle.setChecked(False)
118
+ else:
119
+ self.hotkey_manager = None
120
+
121
+ self.j2_environment = Environment(
122
+ loader=FileSystemLoader(self.settings.html_template_folder),
123
+ autoescape=False,
124
+ )
125
+
126
+ self.load_template() # sets self.template
127
+ self.load_css() # sets self.css
128
+
129
+ self.render_blank()
130
+
131
+ self.client = get_client(self.settings.hostname, self.settings.port)
132
+
133
+ self.ls = LivesplitLink(self.client, self)
134
+ self.split_index = 0
135
+
136
+ self.split_offset = 0 # Offset for advancing/reversing split
137
+
138
+ self.start_loops() # Start livesplit checking loops
139
+
140
+ def toggle_on_top(self):
141
+ """Toggle window always on top, update settings and window flag to match."""
142
+ self.settings.on_top = not self.settings.on_top
143
+ self.menu_on_top.setChecked(self.settings.on_top)
144
+ # noinspection PyUnresolvedReferences
145
+ self.setWindowFlag(QtCore.Qt.WindowType.WindowStaysOnTopHint, self.settings.on_top)
146
+ self.show()
147
+
148
+ def refresh_transparency(self):
149
+ """
150
+ Redo all of the transparency display configuration
151
+ """
152
+ qss_bg_color = rgba_to_qss(self.settings.background_color)
153
+ qss_font_color = rgba_to_qss(self.settings.font_color)
154
+ qss_style = (
155
+ f"background-color: {qss_bg_color}; color: {qss_font_color}"
156
+ )
157
+
158
+ # Flags and attributes
159
+ # Needs the FramelessWindowHint flag set for the translucency to work.
160
+ self.setWindowFlag(
161
+ QtCore.Qt.WindowType.FramelessWindowHint,
162
+ self.settings.transparency
163
+ )
164
+ self.setAttribute(
165
+ QtCore.Qt.WidgetAttribute.WA_NoSystemBackground,
166
+ self.settings.transparency
167
+ )
168
+
169
+ # Central widget always matches CSS style, statusbar only when transparent
170
+ self.ui.centralWidget.setStyleSheet(qss_style)
171
+ if self.settings.transparency:
172
+ self.ui.statusbar.setStyleSheet(qss_style)
173
+ else:
174
+ self.ui.statusbar.setStyleSheet("")
175
+
176
+ def toggle_transparency(self):
177
+ """Toggle window transparency, update settings and window flag to match."""
178
+ self.settings.transparency = not self.settings.transparency
179
+ self.menu_transparency.setChecked(self.settings.transparency)
180
+ self.refresh_transparency()
181
+ self.show()
182
+
183
+ def toggle_hotkey_enable(self):
184
+ if sys.platform == "win32":
185
+ try:
186
+ if self.settings.hotkeys_enabled:
187
+ self.disable_hotkeys()
188
+ self.settings.hotkeys_enabled = False
189
+ self.hotkeys_toggle.setChecked(False)
190
+ else:
191
+ self.enable_hotkeys()
192
+ self.settings.hotkeys_enabled = True
193
+ self.hotkeys_toggle.setChecked(True)
194
+ except AttributeError:
195
+ QErrorMessage(parent=self).showMessage("Could not enable hotkeys.")
196
+ self.settings.hotkeys_enabled = False
197
+ self.hotkeys_toggle.setChecked(False)
198
+
199
+ def enable_hotkeys(self):
200
+ if sys.platform == "win32":
201
+ if (
202
+ (increase_key := self.settings.increase_offset_hotkey)
203
+ and (decrease_key := self.settings.decrease_offset_hotkey)
204
+ ):
205
+ self.hotkey_manager.enable_hotkeys(increase_key.scancodes, decrease_key.scancodes)
206
+ else:
207
+ raise AttributeError("Hotkeys not set")
208
+
209
+ def disable_hotkeys(self): # type: ignore
210
+ if sys.platform == "win32":
211
+ self.hotkey_manager.disable_hotkeys()
212
+
213
+ self.split_offset = 0 # Reset the offset as you can no longer change it
214
+ if not self.ls.connected:
215
+ self.update_notes(0)
216
+
217
+ def increase_offset(self):
218
+ self.split_offset += 1
219
+ # Rerender if not connected (if connected this will happen automatically)
220
+ if not self.ls.connected:
221
+ self.update_notes(0)
222
+ self.ui.statusbar.showMessage(
223
+ f"Trying to connect to Livesplit. | Split Offset: {self.split_offset}"
224
+ )
225
+ else:
226
+ self.ui.statusbar.showMessage(
227
+ f"Connected to Livesplit. | Split Offset: {self.split_offset}"
228
+ )
229
+
230
+ def decrease_offset(self):
231
+ self.split_offset -= 1
232
+ # Rerender if not connected (if connected this will happen automatically)
233
+ if not self.ls.connected:
234
+ self.update_notes(0)
235
+ self.ui.statusbar.showMessage(
236
+ f"Trying to connect to Livesplit. | Split Offset: {self.split_offset}"
237
+ )
238
+ else:
239
+ self.ui.statusbar.showMessage(
240
+ f"Connected to Livesplit. | Split Offset: {self.split_offset}"
241
+ )
242
+
243
+ def start_loops(self):
244
+ """Start the livesplit server connection thread."""
245
+ self.ls.start_loops()
246
+
247
+ def mousePressEvent(self, event: QMouseEvent):
248
+ if event.button() == QtCore.Qt.MouseButton.LeftButton:
249
+ window = self.windowHandle()
250
+ window.startSystemMove()
251
+ return super().mousePressEvent(event)
252
+
253
+ def closeEvent(self, event):
254
+ """On close save settings and close the livesplit connection."""
255
+ self.settings.save()
256
+ if sys.platform == "win32":
257
+ self.hotkey_manager.disable_all() # Kill any hotkeys
258
+ self.ls.close()
259
+ event.accept()
260
+
261
+ def resizeEvent(self, event):
262
+ """Store the new window height and width to keep it between launches."""
263
+ self.settings.width = self.width()
264
+ self.settings.height = self.height()
265
+ event.accept()
266
+
267
+ def setup_actions(self):
268
+ """Setup the browser element with custom options"""
269
+ # Replace the context menu with the app context menu
270
+ # noinspection PyUnresolvedReferences
271
+ self.ui.notes.setContextMenuPolicy(QtCore.Qt.ContextMenuPolicy.CustomContextMenu)
272
+ self.ui.notes.customContextMenuRequested.connect(self.show_menu)
273
+ # Allow links to open in an external browser
274
+ page = ExtLinkWebEnginePage(self, backgroundColor=QColorConstants.Transparent)
275
+ self.ui.notes.setPage(page)
276
+
277
+ def build_menu(self):
278
+ """Create the custom context menu."""
279
+ self.rc_menu = QMenu()
280
+ open_notes = self.rc_menu.addAction("Open Notes")
281
+ open_notes.triggered.connect(self.open_notes)
282
+
283
+ open_settings = self.rc_menu.addAction("Settings")
284
+ open_settings.triggered.connect(self.open_settings)
285
+
286
+ self.menu_on_top = self.rc_menu.addAction("Always on top")
287
+ self.menu_on_top.setCheckable(True)
288
+ self.menu_on_top.setChecked(self.settings.on_top)
289
+ self.menu_on_top.triggered.connect(self.toggle_on_top)
290
+
291
+ self.menu_transparency = self.rc_menu.addAction("Enable Transparency")
292
+ self.menu_transparency.setCheckable(True)
293
+ self.menu_transparency.setChecked(self.settings.transparency)
294
+ self.menu_transparency.triggered.connect(self.toggle_transparency)
295
+
296
+ if sys.platform == "win32":
297
+ self.hotkeys_toggle = self.rc_menu.addAction("Enable Hotkeys")
298
+ self.hotkeys_toggle.setCheckable(True)
299
+ self.hotkeys_toggle.setChecked(self.settings.hotkeys_enabled)
300
+ self.hotkeys_toggle.triggered.connect(self.toggle_hotkey_enable)
301
+ else:
302
+ self.hotkeys_toggle = None
303
+
304
+ exit_action = self.rc_menu.addAction("Exit")
305
+ exit_action.triggered.connect(self.close)
306
+
307
+ def show_menu(self):
308
+ """Display the context menu at the cursor position."""
309
+ if not self.rc_menu:
310
+ self.build_menu()
311
+ self.rc_menu.popup(QCursor.pos())
312
+
313
+ def load_template(self):
314
+ """Load the HTML template for the split rendering."""
315
+ self.template = self.j2_environment.get_template(
316
+ str(self.settings.html_template_file)
317
+ )
318
+
319
+ def load_css(self):
320
+ """Read the CSS file into memory."""
321
+ self.css = self.settings.full_css_path.read_text()
322
+
323
+ def open_notes(self):
324
+ """Open the a file dialog and create a Notes instance from the file."""
325
+ notefile, _ = QFileDialog.getOpenFileName(
326
+ self,
327
+ "Open Notes",
328
+ self.settings.notes_folder,
329
+ "Note Files (*.txt *.md *.html);;All Files (*.*)",
330
+ )
331
+
332
+ if notefile:
333
+ self.notefile = notefile
334
+ # Reset split index and load notes
335
+ self.notes = Notes.from_file(
336
+ notefile, separator=self.settings.split_separator
337
+ )
338
+ # Remember this notes folder next time notes are loaded.
339
+ self.settings.notes_folder = str(Path(notefile).parent)
340
+ # Reset the split offset
341
+ self.split_offset = 0
342
+
343
+ self.update_notes(idx=0, refresh=True)
344
+
345
+ def render_blank(self):
346
+ """Render the initial blank template."""
347
+ html = self.template.render(
348
+ font_size=self.settings.font_size,
349
+ font_color=self.settings.font_color,
350
+ bg_color="transparent",
351
+ css=self.css,
352
+ notes=["<h1>Right Click to Load Notes</h1>"],
353
+ )
354
+ self.ui.notes.setHtml(html)
355
+
356
+ def update_notes(self, idx, refresh=False):
357
+ """
358
+ Update the notes to the index given.
359
+
360
+ :param idx: The new index
361
+ :param refresh: force a refresh even if the index is the same
362
+ """
363
+
364
+ # Initial index can be -1 when livesplit is stopped
365
+ # Add the offset and then make sure we're still above 0
366
+ idx = max(idx, 0) + self.split_offset
367
+ idx = max(idx, 0)
368
+
369
+ if self.notefile and self.notes and (idx != self.split_index or refresh):
370
+ start = idx - self.settings.previous_splits
371
+ end = idx + self.settings.next_splits + 1
372
+
373
+ html = self.template.render(
374
+ font_size=self.settings.font_size,
375
+ font_color=self.settings.font_color,
376
+ bg_color="transparent",
377
+ css=self.css,
378
+ notes=self.notes.render_splits(start, end),
379
+ )
380
+
381
+ self.ui.notes.setHtml(html, baseUrl=self.notefile)
382
+ self.split_index = idx
383
+
384
+ def open_settings(self):
385
+ """Open the settings dialog, refresh everything if the settings have changed."""
386
+ # Block hotkeys while in the settings menu
387
+ if sys.platform == "win32" and self.hotkey_manager.enabled:
388
+ self.disable_hotkeys()
389
+
390
+ settings_dialog = SettingsDialog(
391
+ parent=self, settings=self.settings, hotkey_manager=self.hotkey_manager
392
+ )
393
+ settings_dialog.setWindowIcon(self.icon)
394
+ result = settings_dialog.exec()
395
+ if result == 1:
396
+ # Kill and restart connection if server ip or port change
397
+ if (
398
+ self.client.connection.server != self.settings.hostname
399
+ or self.client.connection.port != self.settings.port
400
+ ):
401
+ self.ls.close()
402
+ self.client = get_client(self.settings.hostname, self.settings.port)
403
+ self.ls = LivesplitLink(self.client, self)
404
+ self.ls.start_loops()
405
+
406
+ # Redraw transparency settings (colours may have changed)
407
+ self.refresh_transparency()
408
+
409
+ # Reread notes with separator
410
+ if self.notefile:
411
+ self.notes = Notes.from_file(
412
+ self.notefile, separator=self.settings.split_separator
413
+ )
414
+ # Reset the offset
415
+ self.split_offset = 0
416
+ self.update_notes(self.split_index, refresh=True)
417
+ else:
418
+ self.render_blank()
419
+
420
+ # Re-enable hotkeys if enabled
421
+ if sys.platform == "win32" and self.settings.hotkeys_enabled:
422
+ try:
423
+ self.enable_hotkeys()
424
+ except AttributeError:
425
+ # Disable hotkeys if the attributes are unset
426
+ self.settings.hotkeys_enabled = False
427
+ self.hotkeys_toggle.setChecked(False)
428
+
429
+
430
+ class LivesplitLink(QtCore.QObject):
431
+ """
432
+ Handle the thread running the livesplit connection and linking to the main window.
433
+ """
434
+
435
+ # Have to message via signals or the program crashes
436
+ note_signal = QtCore.Signal(int)
437
+
438
+ def __init__(self, client, main_window):
439
+ super().__init__()
440
+ self.client = client
441
+ self.main_window = main_window # type: MainWindow
442
+ self.connected = False
443
+ self.break_loop = False
444
+ self.pool = None
445
+ # noinspection PyUnresolvedReferences
446
+ self.note_signal.connect(self.main_window.update_notes)
447
+
448
+ def start_loops(self):
449
+ self.break_loop = False
450
+ self.pool = ThreadPoolExecutor(max_workers=1)
451
+ self.pool.submit(self.loop_update_split)
452
+
453
+ def stop_loops(self):
454
+ self.break_loop = True
455
+ if self.pool:
456
+ self.pool.shutdown(wait=False)
457
+
458
+ def close(self):
459
+ self.stop_loops()
460
+ self.client.close()
461
+
462
+ def update_status(self, message):
463
+ self.main_window.ui.statusbar.showMessage(message)
464
+
465
+ def ls_connect(self):
466
+ self.update_status(
467
+ f"Trying to connect to Livesplit. | "
468
+ f"Split Offset: {self.main_window.split_offset}"
469
+ )
470
+ self.connected = self.client.connect()
471
+ if self.connected:
472
+ self.update_status(
473
+ f"Connected to Livesplit. | "
474
+ f"Split Offset: {self.main_window.split_offset}"
475
+ )
476
+
477
+ def loop_update_split(self):
478
+ while not self.break_loop:
479
+ # If not connected attempt to connect
480
+ if self.connected:
481
+ try:
482
+ split_index = self.client.get_split_index()
483
+ except (ConnectionError, TimeoutError):
484
+ self.connected = False
485
+ self.client.close()
486
+ else:
487
+ # Send the signal to the main window to update.
488
+ # noinspection PyUnresolvedReferences
489
+ self.note_signal.emit(split_index)
490
+ else:
491
+ self.ls_connect()
492
+ time.sleep(0.1)
@@ -0,0 +1,191 @@
1
+ import sys
2
+
3
+ from pathlib import Path
4
+
5
+ from PySide6 import QtCore
6
+ from PySide6.QtWidgets import QDialog, QColorDialog, QFileDialog
7
+ from PySide6.QtCore import QRegularExpression
8
+ from PySide6.QtGui import (
9
+ QIntValidator,
10
+ QDoubleValidator,
11
+ QRegularExpressionValidator,
12
+ )
13
+
14
+ from ..settings import ServerSettings
15
+ from .color import rgba_to_qcolor, qcolor_to_rgba
16
+ from .layouts import Ui_ServerSettings
17
+
18
+
19
+ # Get correct paths
20
+ if getattr(sys, "frozen", False): # pragma: nocover
21
+ base_path = Path(sys.executable).parent
22
+ icon_file = str(base_path / "logo_alpha.png")
23
+ else:
24
+ base_path = Path(__file__).parent
25
+ icon_file = str(base_path.parents[2] / "resources" / "logo_alpha.png")
26
+
27
+
28
+ class ServerSettingsDialog(QDialog):
29
+ def __init__(
30
+ self,
31
+ parent,
32
+ settings: ServerSettings,
33
+ ):
34
+ super().__init__(parent=parent)
35
+
36
+ self.ui = Ui_ServerSettings()
37
+ self.ui.setupUi(self)
38
+
39
+ self.settings = settings
40
+
41
+ # noinspection PyUnresolvedReferences
42
+ self.setWindowFlag(QtCore.Qt.WindowType.WindowStaysOnTopHint, True)
43
+
44
+ # self.hotkey_manager = hotkey_manager
45
+ self.nextsplitkey = None
46
+ self.previoussplitkey = None
47
+
48
+ self.temp_html_path = self.settings.full_template_path
49
+ self.temp_css_path = self.settings.full_css_path
50
+
51
+ self.setup_validators()
52
+ self.fill_settings()
53
+
54
+ self.ui.textcolor_button.clicked.connect(self.font_color_dialog)
55
+ self.ui.bgcolor_button.clicked.connect(self.bg_color_dialog)
56
+ self.ui.htmltemplate_button.clicked.connect(self.html_template_dialog)
57
+ self.ui.css_button.clicked.connect(self.css_dialog)
58
+
59
+ # Next and previous split keys are currently non-functioning
60
+ # self.ui.nextsplitkey_button.clicked.connect(self.get_increase_hotkey)
61
+ # self.ui.previoussplitkey_button.clicked.connect(self.get_decrease_hotkey)
62
+ # self.pool = ThreadPoolExecutor(max_workers=1)
63
+ self.ui.nextsplitkey_button.setDisabled(True)
64
+ self.ui.previoussplitkey_button.setDisabled(True)
65
+ self.ui.nextsplitkey_label.hide()
66
+ self.ui.nextsplitkey_edit.hide()
67
+ self.ui.nextsplitkey_button.hide()
68
+ self.ui.previoussplitkey_label.hide()
69
+ self.ui.previoussplitkey_edit.hide()
70
+ self.ui.previoussplitkey_button.hide()
71
+ self.ui.divider_4.hide()
72
+ self.adjustSize()
73
+
74
+ self.ui.confirm_cancel_box.accepted.connect(self.accept)
75
+ self.ui.confirm_cancel_box.rejected.connect(self.reject)
76
+
77
+ def setup_validators(self):
78
+ color_re = QRegularExpression(r"#([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})")
79
+ color_validator = QRegularExpressionValidator(color_re, None)
80
+ self.ui.port_edit.setValidator(QIntValidator(1024, 65535, None))
81
+ # 255 splits seems like a lot
82
+ self.ui.previous_edit.setValidator(QIntValidator(0, 255, None))
83
+ self.ui.advance_edit.setValidator(QIntValidator(0, 255, None))
84
+ # I don't know why you'd set a font size of 10k but sure why not
85
+ self.ui.fontsize_edit.setValidator(QDoubleValidator(0.0, 10000.0, 2, None))
86
+ self.ui.textcolor_edit.setValidator(color_validator)
87
+ self.ui.bgcolor_edit.setValidator(color_validator)
88
+
89
+ def fill_settings(self):
90
+ self.ui.hostname_edit.setText(self.settings.hostname)
91
+ self.ui.port_edit.setText(str(self.settings.port))
92
+ self.ui.previous_edit.setText(str(self.settings.previous_splits))
93
+ self.ui.advance_edit.setText(str(self.settings.next_splits))
94
+ self.ui.separator_edit.setText(self.settings.split_separator)
95
+ self.ui.fontsize_edit.setText(str(self.settings.font_size))
96
+ self.ui.textcolor_edit.setText(self.settings.font_color)
97
+ self.ui.bgcolor_edit.setText(self.settings.background_color)
98
+ self.ui.htmltemplate_edit.setText(str(self.settings.html_template_file))
99
+ self.ui.css_edit.setText(str(self.settings.css_file))
100
+
101
+ if self.settings.increase_offset_hotkey:
102
+ self.ui.nextsplitkey_edit.setText(self.settings.increase_offset_hotkey.name)
103
+ if self.settings.decrease_offset_hotkey:
104
+ self.ui.previoussplitkey_edit.setText(
105
+ self.settings.decrease_offset_hotkey.name
106
+ )
107
+ self.nextsplitkey = self.settings.increase_offset_hotkey
108
+ self.previoussplitkey = self.settings.decrease_offset_hotkey
109
+
110
+ self.ui.noteserverhost_edit.setText(self.settings.server_hostname)
111
+ self.ui.noteserverport_edit.setText(str(self.settings.server_port))
112
+
113
+ def store_settings(self):
114
+ self.settings.hostname = self.ui.hostname_edit.text()
115
+ self.settings.port = int(self.ui.port_edit.text())
116
+ self.settings.previous_splits = int(self.ui.previous_edit.text())
117
+ self.settings.next_splits = int(self.ui.advance_edit.text())
118
+ self.settings.split_separator = self.ui.separator_edit.text()
119
+ self.settings.font_size = float(self.ui.fontsize_edit.text())
120
+ self.settings.font_color = self.ui.textcolor_edit.text()
121
+ self.settings.background_color = self.ui.bgcolor_edit.text()
122
+
123
+ self.settings.increase_offset_hotkey = self.nextsplitkey
124
+ self.settings.decrease_offset_hotkey = self.previoussplitkey
125
+
126
+ # Paths get stored in temporary variables
127
+ self.settings.html_template_folder = Path(self.temp_html_path).parent
128
+ self.settings.html_template_file = Path(self.temp_html_path).name
129
+
130
+ self.settings.css_folder = Path(self.temp_css_path).parent
131
+ self.settings.css_file = Path(self.temp_css_path).name
132
+
133
+ self.settings.server_hostname = self.ui.noteserverhost_edit.text()
134
+ self.settings.server_port = int(self.ui.noteserverport_edit.text())
135
+
136
+ def font_color_dialog(self):
137
+ """
138
+ Pop up a color dialog for the text color.
139
+ """
140
+ color = QColorDialog.getColor(
141
+ rgba_to_qcolor(self.ui.textcolor_edit.text()),
142
+ parent=self,
143
+ title="Text Color",
144
+ options=QColorDialog.ColorDialogOption.ShowAlphaChannel,
145
+ )
146
+ if color.isValid():
147
+ self.ui.textcolor_edit.setText(qcolor_to_rgba(color))
148
+
149
+ def bg_color_dialog(self):
150
+ """
151
+ Pop up a color dialog for the background color.
152
+ """
153
+ color = QColorDialog.getColor(
154
+ rgba_to_qcolor(self.ui.bgcolor_edit.text()),
155
+ parent=self,
156
+ title="Background Color",
157
+ options=QColorDialog.ColorDialogOption.ShowAlphaChannel,
158
+ )
159
+ if color.isValid():
160
+ self.ui.bgcolor_edit.setText(qcolor_to_rgba(color))
161
+
162
+ def html_template_dialog(self):
163
+ htmlfile, _ = QFileDialog.getOpenFileName(
164
+ self,
165
+ "Select Template File",
166
+ str(self.settings.html_template_folder),
167
+ "html templates (*.html);;All Files (*.*)",
168
+ )
169
+
170
+ if htmlfile:
171
+ self.temp_html_path = htmlfile
172
+ self.ui.htmltemplate_edit.setText(Path(htmlfile).name)
173
+
174
+ def css_dialog(self):
175
+ cssfile, _ = QFileDialog.getOpenFileName(
176
+ self,
177
+ "Select Template File",
178
+ str(self.settings.css_folder),
179
+ "css files (*.css);;All Files (*.*)",
180
+ )
181
+
182
+ if cssfile:
183
+ self.temp_css_path = cssfile
184
+ self.ui.css_edit.setText(Path(cssfile).name)
185
+
186
+ def accept(self):
187
+ """If the dialog is accepted save the settings"""
188
+ # Normal cleanup
189
+ super().accept()
190
+ # Store the settings in the settings object
191
+ self.store_settings()