-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHelpPage.py
More file actions
179 lines (151 loc) · 6.26 KB
/
HelpPage.py
File metadata and controls
179 lines (151 loc) · 6.26 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
"""
The main documentation window, using `RenderViewer` to display text.
"""
import sys, os
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QTreeView, QSplitter, QVBoxLayout, QWidget
)
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtCore import Qt, QTimer
from TextCell import MDLinter, RenderViewer
if getattr(sys, "frozen", False):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
directory = os.path.join(base_path, "Mothball_Pages")
def loadPage(filename):
try:
with open(os.path.join(base_path, "Mothball_Pages", filename)) as f:
return f.read()
except Exception as e:
return f"Could not load page {filename}: {e}"
introduction = loadPage("Introduction.txt")
documentationIntro = loadPage("DocumentationIntro.txt")
learnTheBasics = loadPage("LearnTheBasics.txt")
movementDocumentation = loadPage("MovementDocumentation.txt")
movementHelp = loadPage("MovementHelp.txt")
optimizationHelp = loadPage("OptimizationHelp.txt")
outputHelp = loadPage("OutputHelp.txt")
setterHelp = loadPage("SetterHelp.txt")
welcomePage = loadPage("WelcomePage.txt")
usingTheIDE = loadPage("UsingTheIDE.txt")
setterdocumentation = loadPage("SetterDocumentation.txt")
def getHeadings(text: str):
"""
Gets the headings in markdown formatted `text`, which are all lines that start with `#,##,###`.
Returns a list of tuples which contain the heading and the heading level. `#` heading level is 0, `##` is 1, `###` is 2.
"""
headers: list[tuple[str, int]] = []
in_code_block = False
for line in text.split("\n"):
if line.startswith("```"):
in_code_block = not in_code_block
if not in_code_block:
if line.startswith("# "):
headers.append((line[2:], 0))
elif line.startswith("## "):
headers.append((line[3:], 1))
elif line.startswith("### "):
headers.append((line[4:], 2))
return headers
class MainWindow(QMainWindow):
def __init__(self, generalOptions: dict, colorOptions: dict, textOptions: dict):
super().__init__()
self.generalOptions = generalOptions
self.colorOptions = colorOptions
self.textOptions = textOptions
self.setWindowTitle("Documentation Viewer")
self.setGeometry(100, 100, 900, 600)
self.pages = {
"Introduction": introduction,
"Documentation Intro": documentationIntro,
"Basics": learnTheBasics,
"Movement Documentation": movementDocumentation,
"Movement Help": movementHelp,
"Optimization Help": optimizationHelp,
"Output Help": outputHelp,
"Setter Help": setterHelp,
"IDE": usingTheIDE,
"Setter Documentation": setterdocumentation
}
self.current_doc = None # Track which document is currently loaded
# Layout using QSplitter
splitter = QSplitter(Qt.Horizontal)
# TreeView for the table of contents
self.tree = QTreeView()
self.model = QStandardItemModel()
self.model.setHorizontalHeaderLabels(["Contents"])
self.tree.setModel(self.model)
self.tree.setHeaderHidden(True)
splitter.addWidget(self.tree)
# QTextBrowser for the document viewer
self.text_browser = RenderViewer(generalOptions,colorOptions,textOptions,self)
splitter.addWidget(self.text_browser)
splitter.setStretchFactor(1, 1)
# Set main layout
container = QWidget()
layout = QVBoxLayout(container)
layout.addWidget(splitter)
self.setCentralWidget(container)
# Load sample pages
self.populateTree()
# Connect selection event
self.tree.selectionModel().selectionChanged.connect(self.onSelectionChanged)
QApplication.processEvents()
self.loadDocument("page9")
def populateTree(self):
"""
Populate `QTreeView` with the elements and nesting based on headings of the text.\\
Additionally, clicking on an element will direct you to the relevant page and scroll to the appropiate section.\\
Uses hmtl anchors.
"""
for page_name, page in self.pages.items():
page_root = QStandardItem(page_name)
page_root.setEditable(False)
page_root.setData({"doc": page_name, "anchor": None}, Qt.UserRole)
last_heading1 = None
last_heading2 = None
for heading, lvl in getHeadings(page):
item = QStandardItem(heading)
item.setEditable(False)
item.setData({"doc": page_name, "anchor": heading}, Qt.UserRole)
if lvl==0:
page_root.appendRow(item)
last_heading1 = item
last_heading2 = None
elif lvl == 1:
last_heading1.appendRow(item)
last_heading2 = item
elif lvl == 2: # DANGEROUS! Temp fix: use heading 1
if last_heading2 is not None:
last_heading2.appendRow(item)
else:
last_heading1.appendRow(item)
self.model.appendRow(page_root)
def onSelectionChanged(self, selected, deselected):
if not selected.indexes():
return
index = selected.indexes()[0]
data = index.data(Qt.UserRole)
if not data:
return
# print(data)
doc = data.get("doc")
anchor = data.get("anchor")
if doc != self.current_doc:
self.loadDocument(doc)
self.current_doc = doc
if anchor:
QTimer.singleShot(100, lambda: self.text_browser.scrollToAnchor(anchor))
def loadDocument(self, doc_name: str):
md = self.pages.get(doc_name, welcomePage)
self.text_browser.renderTextfromMarkdown(MDLinter(self.generalOptions,self.colorOptions,self.textOptions), md)
if __name__ == "__main__":
import FileHandler
a=FileHandler.getCodeColorSettings()
b=FileHandler.getGeneralSettings()
c=FileHandler.getTextColorSettings()
app = QApplication(sys.argv)
window = MainWindow(b,a,c)
window.show()
sys.exit(app.exec_())