-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
232 lines (194 loc) · 9.68 KB
/
gui.py
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
223
224
225
226
227
228
229
230
231
232
import datetime
import os.path
import sys
import PyQt5
import PyQt5.QtWebEngineWidgets
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QMainWindow
import utils
from settings import Settings
class AppWindow(PyQt5.QtWidgets.QWidget):
def __init__(self, utils: utils.Utils, settings: Settings):
self.application = QApplication([])
self.web_engine_page = PyQt5.QtWebEngineWidgets.QWebEnginePage()
self.web_engine_page.loadStarted.connect(lambda: self.log_info("Started exporting"))
self.web_engine_page.loadFinished.connect(lambda: self.log_info("Exporting finished"))
self.web_engine_page.loadFinished.connect(lambda: self.export_pdf())
super().__init__()
self.settings = settings
self.utils = utils
self.current_md: str = ""
self.setWindowTitle("automathwork-alpha")
self.resize(1010, 760) # initial window size
# parent widgets
self.parent_layout = PyQt5.QtWidgets.QHBoxLayout()
# left widgets
self.left_layout = PyQt5.QtWidgets.QVBoxLayout()
self.parent_layout.addLayout(self.left_layout)
# right widgets
self.right_layout = PyQt5.QtWidgets.QVBoxLayout()
self.parent_layout.addLayout(self.right_layout)
# right top widgets
self.right_top_layout = PyQt5.QtWidgets.QHBoxLayout()
self.right_layout.addLayout(self.right_top_layout)
self.export_pdf_button_widget = PyQt5.QtWidgets.QPushButton("Export as PDF")
self.delete_button_widget = PyQt5.QtWidgets.QPushButton("Delete Markdown")
self.save_button_widget = PyQt5.QtWidgets.QPushButton("Save Markdown")
self.switch_subject_button = PyQt5.QtWidgets.QComboBox()
self.export_pdf_button_widget.clicked.connect(lambda: self.export_to_pdf())
self.save_button_widget.clicked.connect(lambda: self.save_new_markdown())
self.delete_button_widget.clicked.connect(lambda: self.delete_selected_markdown())
self.right_top_layout.addWidget(self.switch_subject_button)
self.right_top_layout.addWidget(self.save_button_widget)
self.right_top_layout.addWidget(self.delete_button_widget)
self.right_top_layout.addWidget(self.export_pdf_button_widget)
# right bottom widgets
self.right_bottom_layout = PyQt5.QtWidgets.QVBoxLayout()
self.right_layout.addLayout(self.right_bottom_layout)
# left paired widgets
self.history_bar = PyQt5.QtWidgets.QListWidget()
self.info_widget = PyQt5.QtWidgets.QTextEdit()
self.info_widget.setReadOnly(True)
self.info_widget.setMaximumWidth(130)
self.history_bar.setMaximumWidth(130)
self.history_bar.currentItemChanged.connect(
lambda: self.on_histories_selected(self.history_bar.currentItem())
)
self.left_layout.addWidget(self.history_bar)
self.left_layout.addWidget(self.info_widget)
# right bottom paired widgets
self.right_paired_layout = PyQt5.QtWidgets.QHBoxLayout()
self.markdown_view_widget = PyQt5.QtWidgets.QTextEdit()
self.html_renderer = PyQt5.QtWebEngineWidgets.QWebEngineView()
self.markdown_view_widget.setMinimumWidth(360)
self.html_renderer.setMinimumWidth(400)
self.markdown_view_widget.textChanged.connect(lambda: self.apply_markdown_change_rerender_html())
self.right_paired_layout.addWidget(self.markdown_view_widget)
self.right_paired_layout.addWidget(self.html_renderer)
self.right_bottom_layout.addLayout(self.right_paired_layout)
# prompt widgets
self.prompt_input_widget = PyQt5.QtWidgets.QTextEdit()
self.prompt_input_widget.setMaximumHeight(55)
self.prompt_input_widget.setFontPointSize(20)
self.prompt_input_widget.textChanged.connect(lambda: self.prompt_input_widget.setFontPointSize(20))
self.prompt_send_button_widget = PyQt5.QtWidgets.QPushButton()
self.prompt_layout = PyQt5.QtWidgets.QHBoxLayout()
self.prompt_send_button_widget.setFixedSize(60, 55)
self.prompt_send_button_widget.setText("Send")
self.prompt_layout.addWidget(self.prompt_input_widget)
self.prompt_layout.addWidget(self.prompt_send_button_widget)
self.right_bottom_layout.addLayout(self.prompt_layout)
# sum
self.setLayout(self.parent_layout)
# window widgets initialization
self.switch_subject_button.currentIndexChanged.connect(lambda: self.change_subject())
self.index_subjects()
self.index_markdowns()
self.render_html()
def export_pdf(self):
path = os.path.join(PyQt5.QtWidgets.QFileDialog.getExistingDirectory(), 'output.pdf')
if not path == 'output.pdf':
self.web_engine_page.printToPdf(path)
def set_prompts_callbacks(self, homework_callback, markdown_callback):
self.prompt_send_button_widget.clicked.connect(
lambda: self.handle_send_prompts(
homework_callable=homework_callback,
markdown_callable=markdown_callback
))
def handle_send_prompts(self, homework_callable, markdown_callable):
content = self.prompt_input_widget.toPlainText()
if len(content) > 0:
self.log_info("Prompt sent")
self.generate_homework(homework_callable, markdown_callable, content)
def generate_homework(self, homework_callable, markdown_callable, prompt):
self.prompt_input_widget.clear()
homework = homework_callable(prompt)
self.log_info("Homework generated!")
self.log_info("Generating markdown...")
markdown = markdown_callable(homework)
self.log_info("Markdown generated!")
utils.write_cache(os.path.join('saves', 'current.md'), markdown)
self.markdown_view_widget.setMarkdown(markdown)
self.index_markdowns()
# send notifications here
# update the source file
# render the corresponding web view
def apply_markdown_change_rerender_html(self):
utils.write_cache(self.current_md, self.markdown_view_widget.toMarkdown())
self.rerender_html()
def rerender_html(self):
self.utils.get_html(self.current_md, os.path.join(self.settings.config_path, 'cache.html'))
self.html_renderer.reload()
def render_html(self):
self.utils.get_html(self.current_md, os.path.join(self.settings.config_path, 'cache.html'))
self.html_renderer.load(QUrl.fromLocalFile(
os.path.join(self.settings.config_path, 'cache.html')))
def save_new_markdown(self):
filename = os.path.join('saves', datetime.datetime.now().strftime("%d-%m-%Y-%H_%M_%S"))
utils.write_cache(filename, utils.read_cache(os.path.join('saves', 'current.md')))
self.log_info("file history {} saved".format(filename))
self.index_markdowns()
# add notifications here
def delete_selected_markdown(self):
if not self.history_bar.hasFocus():
return
if self.history_bar.currentItem().text() == 'current.md':
return
path = os.path.join('saves', self.history_bar.currentItem().text())
if os.path.exists(path):
os.remove(path)
self.log_info("file history {} removed".format(self.history_bar.currentItem().text()))
# add notifications here
self.index_markdowns()
def index_markdowns(self):
def file_to_str(string: str):
if string == 'current.md':
return sys.float_info.max
date_time_parts = string.split("-")
day = int(date_time_parts[0])
month = int(date_time_parts[1])
year = int(date_time_parts[2])
hour = int(date_time_parts[3].split("_")[0])
minute = int(date_time_parts[3].split("_")[1])
second = int(date_time_parts[3].split("_")[2])
dt = datetime.datetime(year, month, day, hour, minute, second)
return (dt - datetime.datetime(dt.year, 1, 1)).total_seconds()
file_list = []
if os.path.exists('saves'):
for file_name in os.listdir('saves'):
if os.path.isfile(os.path.join('saves', file_name)):
file_list.append(file_name)
else:
os.mkdir('saves')
if len(file_list) > 0:
self.history_bar.clear()
file_list.sort(key=file_to_str, reverse=True)
for history_file in file_list:
history = PyQt5.QtWidgets.QListWidgetItem(history_file)
self.history_bar.addItem(history)
def index_subjects(self):
self.switch_subject_button.clear()
default_name = self.settings.get_default_subject_name()
for subject in self.settings.get_subject_list():
name = subject.get_name()
self.switch_subject_button.addItem(name)
self.switch_subject_button.setCurrentText(default_name)
def change_subject(self):
self.settings.set_default_subject(self.switch_subject_button.currentText())
self.rerender_html()
def on_histories_selected(self, history: PyQt5.QtWidgets.QListWidgetItem):
if history is None:
self.current_md = ""
self.markdown_view_widget.setMarkdown("")
else:
self.current_md = os.path.join('saves', history.text())
self.markdown_view_widget.setMarkdown(utils.read_cache(os.path.join('saves', history.text())))
def export_to_pdf(self):
self.web_engine_page.load(QUrl.fromLocalFile(
os.path.join(self.settings.config_path, 'cache.html')))
def log_info(self, info):
self.info_widget.append(info + "\n")
def log_error(self, error):
self.info_widget.append(error + "\n")
def exec(self):
self.application.exec()