-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_guide_dialog.py
More file actions
78 lines (61 loc) · 2.42 KB
/
setup_guide_dialog.py
File metadata and controls
78 lines (61 loc) · 2.42 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
"""In-app documentation viewer for setup and help guides."""
from pathlib import Path
from PyQt6.QtCore import QUrl
from PyQt6.QtGui import QDesktopServices
from PyQt6.QtWidgets import (
QDialog,
QDialogButtonBox,
QMessageBox,
QTextBrowser,
QVBoxLayout,
)
class SetupGuideDialog(QDialog):
"""Render local markdown help content without leaving the application."""
def __init__(self, guide_path, parent=None):
super().__init__(parent)
self.docs_root = Path(guide_path).resolve().parent
self.current_path = None
self.setWindowTitle("Setup Guide")
self.resize(820, 620)
layout = QVBoxLayout(self)
self.browser = QTextBrowser(self)
self.browser.setOpenExternalLinks(False)
self.browser.anchorClicked.connect(self._handle_anchor_clicked)
layout.addWidget(self.browser, 1)
button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Close, self)
button_box.rejected.connect(self.close)
layout.addWidget(button_box)
self.load_markdown(guide_path)
def load_markdown(self, guide_path):
resolved_path = Path(guide_path).resolve()
if not resolved_path.exists():
QMessageBox.warning(
self,
"Guide Missing",
f"The guide could not be found at {resolved_path.name}.",
)
return
self.current_path = resolved_path
self.setWindowTitle(self._title_from_path(resolved_path))
self.browser.setMarkdown(resolved_path.read_text(encoding="utf-8"))
self.browser.moveCursor(self.browser.textCursor().MoveOperation.Start)
def _handle_anchor_clicked(self, url):
if url.isLocalFile():
self.load_markdown(url.toLocalFile())
return
target = url.toString()
if target.startswith(("http://", "https://", "mailto:")):
QDesktopServices.openUrl(url)
return
if not self.current_path:
return
local_path = (self.current_path.parent / target).resolve()
try:
local_path.relative_to(self.docs_root)
except ValueError:
QDesktopServices.openUrl(QUrl(target))
return
self.load_markdown(local_path)
def _title_from_path(self, guide_path):
title = guide_path.stem.replace("_", " ").replace("-", " ").title()
return f"{title} - Text To Speech Help"