-
Notifications
You must be signed in to change notification settings - Fork 0
/
katex.py
1358 lines (1023 loc) · 57 KB
/
katex.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Break function save_to_problems into two functions: save_current_problem and save_all_problems
"""
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMessageBox, QMainWindow, QLabel, QScrollArea, QFileDialog, QVBoxLayout, QHBoxLayout, QWidget, QTextEdit, QPushButton, QSpacerItem, QSizePolicy
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QPixmap
from styles import *
from PIL import Image, ImageOps
from OneLineTextEdit import *
import requests, webbrowser, uuid, os, zipfile, json
from settings import Settings
import appdirs
class CustomScrollArea(QScrollArea):
def __init__(self, main_instance, parent=None):
super().__init__(parent)
self.main_instance = main_instance
self.scroll_layout = None
def showEvent(self, event):
super().showEvent(event)
if self.scroll_layout:
self.main_instance.scale_images(self.scroll_layout, self)
class NotekeeEditor(QMainWindow):
def __init__(self):
super().__init__()
self.settings = Settings()
self.styles = Styles(self.settings)
self.export_or_import = 'import'
self.current_problem_index = 0
# Initialize current mode
self.current_mode = 'question'
# Initialize current chapter
self.current_chapter = ''
self.show_chapter_picker = False
# Initialize question and solution strings
self.question = ''
self.solution = ''
self.next_button_enabled = True
self.previous_button_enabled = False
self.zoomed_image_widget = None
self.all_problems_layout_widget = None
try:
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
# Create the problems.json and chapters.json files if they do not exist
problems_file_path = os.path.join(app_dir, 'problems.json')
chapters_file_path = os.path.join(app_dir, 'chapters.json')
if not os.path.exists(problems_file_path):
with open(problems_file_path, 'w') as problems_file:
json.dump([], problems_file)
if not os.path.exists(chapters_file_path):
with open(chapters_file_path, 'w') as chapters_file:
json.dump([], chapters_file)
# Load problems and chapters from json files in the application data directory
with open(problems_file_path, 'r') as problems_file, open(chapters_file_path, 'r') as chapters_file:
# Put the data from the json files into lists
self.problems = json.load(problems_file)
self.chapters = json.load(chapters_file)
if self.problems == []:
raise FileNotFoundError
temp_current_problem_index = self.settings.get_current_problem_index()
if temp_current_problem_index > len(self.problems) - 1:
self.current_problem_index = len(self.problems) - 1
else:
self.current_problem_index = temp_current_problem_index
current_problem = self.problems[self.current_problem_index]
self.question = current_problem.get('question', '')
self.solution = current_problem.get('solution', '')
self.current_chapter = current_problem.get('chapter', '')
if len(self.problems) > 1:
self.export_or_import = 'export'
except FileNotFoundError:
print("JSON FILE DOES NOT EXIST")
self.problems = [{'question': '', 'solution': '', 'chapter': '', 'question_images': [], 'solution_images': []}]
self.chapters = []
except json.JSONDecodeError:
print("FAILED TO LOAD JSON FILE")
self.problems = [{'question': '', 'solution': '', 'chapter': '', 'question_images': [], 'solution_images': []}]
self.chapters = []
self.init_ui()
def build_top_button_layout(self) -> QHBoxLayout:
button_layout = QHBoxLayout()
chapter_button_title = self.current_chapter if self.current_chapter != '' else 'Set Chapter'
self.chapter_button = QPushButton(chapter_button_title, self)
self.question_button = QPushButton('Question', self)
self.solution_button = QPushButton('Solution', self)
self.previous_button = QPushButton('Previous', self)
self.next_button = QPushButton('Next', self)
# Connect the buttons to their respective functions
self.question_button.clicked.connect(lambda: self.toggle_current_input_mode(self.question_button))
self.solution_button.clicked.connect(lambda: self.toggle_current_input_mode(self.solution_button))
self.previous_button.clicked.connect(lambda: self.previous_problem())
self.next_button.clicked.connect(lambda: self.next_problem())
self.chapter_button.clicked.connect(lambda: self.toggle_chapter_picker())
if self.current_chapter != '':
self.chapter_button.setStyleSheet(self.styles.top_chapter_button_style)
else:
self.chapter_button.setStyleSheet(unselected_top_chapter_button_style)
# Set previous and next button styles
if self.current_problem_index == 0:
self.disable_button(self.previous_button)
self.previous_button_enabled = False
else:
self.previous_button.setStyleSheet(self.styles.button_style)
if self.current_problem_index == len(self.problems) - 1 and self.question == '':
self.disable_button(self.next_button)
self.next_button_enabled = False
else:
self.next_button.setStyleSheet(self.styles.button_style)
# Set focus button style
if self.current_mode == 'question':
self.question_button.setStyleSheet(self.styles.focused_button_style)
self.solution_button.setStyleSheet(self.styles.unfocused_button_style)
else:
self.question_button.setStyleSheet(self.styles.unfocused_button_style)
self.solution_button.setStyleSheet(self.styles.focused_button_style)
# Add the buttons to the layout
button_layout.addWidget(self.chapter_button)
button_layout.addStretch(1)
button_layout.addWidget(self.question_button)
button_layout.addWidget(self.solution_button)
button_layout.addStretch(1)
button_layout.addWidget(self.previous_button)
button_layout.addWidget(self.next_button)
# Setting pointing hand cursor for buttons
for button in [self.chapter_button, self.previous_button, self.next_button, self.question_button, self.solution_button]:
button.setCursor(Qt.PointingHandCursor)
return button_layout
def delete_image(self, image):
try:
# Remove image from problems dictionary
current_problem = self.problems[self.current_problem_index]
current_problem[self.current_mode + '_images'].remove(image)
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
# Delete image file from the images folder in the application data directory
os.remove(os.path.join(app_dir, 'images', f'{image}.webp'))
if self.zoomed_image_widget is not None:
self.zoom_out_image()
except FileNotFoundError:
pass
finally:
self.save_to_problems()
self.update_images()
def zoom_in_image(self, image):
# Hide the right_layout widget
self.right_layout_widget.hide()
# Create a new QWidget for the zoomed image
self.zoomed_image_widget = QWidget()
# Create a QVBoxLayout for the zoomed image widget
zoomed_image_layout = QVBoxLayout(self.zoomed_image_widget)
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
# Load the image from the images folder in the application data directory
label = QLabel(self)
pixmap = QPixmap(os.path.join(app_dir, 'images', f'{image}.webp'))
label.setPixmap(pixmap)
label.setProperty('originalPixmap', pixmap) # Set the original QPixmap as a property of the label
zoomed_image_layout.addWidget(label)
zoomed_image_layout.setAlignment(Qt.AlignCenter)
# Create a QScrollArea and set zoomed_image_widget as its widget
self.zoomed_image_scroll_area = QScrollArea() # Store the scroll area as an instance variable
self.zoomed_image_scroll_area.setWidget(self.zoomed_image_widget)
self.zoomed_image_scroll_area.setWidgetResizable(True)
zoomed_image_layout.setSpacing(0)
# Add the QScrollArea to the workspace_layout
self.workspace_layout.addWidget(self.zoomed_image_scroll_area, 1)
# Add a QHBoxLayout to the bottom of the workspace_layout for the buttons
self.image_button_layout = QVBoxLayout()
self.button_layout = QHBoxLayout() # Store the button layout as an instance variable
zoom_out_button = QPushButton('Zoom out')
delete_button = QPushButton('Delete')
zoom_out_button.setStyleSheet(self.styles.zoom_button_style)
delete_button.setStyleSheet(self.styles.delete_button_style)
zoom_out_button.setCursor(Qt.PointingHandCursor)
delete_button.setCursor(Qt.PointingHandCursor)
self.image_button_layout.addWidget(self.zoomed_image_scroll_area)
self.image_button_layout.addLayout(self.button_layout)
zoom_out_button.clicked.connect(self.zoom_out_image)
delete_button.clicked.connect(lambda: self.delete_image(image))
self.button_layout.setSpacing(15)
self.button_layout.addWidget(zoom_out_button)
self.button_layout.addWidget(delete_button)
self.workspace_layout.addLayout(self.image_button_layout, 1)
def zoom_out_image(self):
# Remove the zoomed image widget, the scroll area, and the button layout from the workspace_layout
self.workspace_layout.removeWidget(self.zoomed_image_scroll_area)
self.zoomed_image_scroll_area.deleteLater()
self.zoomed_image_scroll_area = None
self.zoomed_image_widget.deleteLater()
self.zoomed_image_widget = None
# Remove the button layout
for i in reversed(range(self.button_layout.count())): # Remove widgets from the layout
widget = self.button_layout.itemAt(i).widget()
if widget:
widget.deleteLater()
self.workspace_layout.removeItem(self.button_layout) # Remove the layout from the workspace_layout
self.workspace_layout.removeItem(self.image_button_layout)
self.button_layout.deleteLater()
self.button_layout = None
# Show the right_layout widget
self.right_layout_widget.show()
def build_images_scroll_area(self) -> QScrollArea:
scroll_area = CustomScrollArea(self, self)
scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scroll_widget = QWidget()
scroll_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
scroll_area.setWidget(scroll_widget)
scroll_area.setWidgetResizable(True)
self.scroll_layout = QHBoxLayout(scroll_widget)
self.scroll_layout.setSpacing(0)
self.scroll_layout.setContentsMargins(5, 0, 5, 10)
scroll_area.scroll_layout = self.scroll_layout
current_problem = self.problems[self.current_problem_index]
current_image_list = current_problem[self.current_mode + '_images']
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
for image in current_image_list:
image_widget = QWidget()
image_layout = QVBoxLayout(image_widget)
image_layout.setContentsMargins(5, 0, 5, 0)
image_layout.setSpacing(0) # Set spacing to 0 to remove space between image and buttons
image_layout.setAlignment(Qt.AlignTop) # Align the image and buttons to the top
label = QLabel(self)
label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
# Load the image from the images folder in the application data directory
pixmap = QPixmap(os.path.join(app_dir, 'images', f'{image}.webp'))
label.setPixmap(pixmap)
label.setProperty('originalPixmap', pixmap)
label.setAlignment(Qt.AlignCenter)
image_layout.addWidget(label)
button_layout = QHBoxLayout()
zoom_button = QPushButton('Zoom in')
delete_button = QPushButton('Delete')
button_layout.setSpacing(15)
delete_button.clicked.connect(lambda checked, img=image: self.delete_image(img))
zoom_button.clicked.connect(lambda: self.zoom_in_image(image))
zoom_button.setStyleSheet(self.styles.zoom_button_style)
delete_button.setStyleSheet(self.styles.delete_button_style)
zoom_button.setCursor(Qt.PointingHandCursor)
delete_button.setCursor(Qt.PointingHandCursor)
button_layout.addWidget(zoom_button)
button_layout.addWidget(delete_button)
image_layout.addLayout(button_layout)
self.scroll_layout.addWidget(image_widget)
scroll_area.resizeEvent = lambda event: self.scale_images(scroll_area.scroll_layout, scroll_area)
return scroll_area
def scale_images(self, layout, scroll_area):
for i in range(layout.count()):
widget = layout.itemAt(i).widget()
if isinstance(widget, QWidget):
image_layout = widget.layout()
if image_layout is not None: # Add this line
label = image_layout.itemAt(0).widget() # Get the QLabel from the QVBoxLayout
if isinstance(label, QLabel):
original_pixmap = label.property('originalPixmap') # Get the original QPixmap
if original_pixmap:
image = original_pixmap.toImage() # Convert QPixmap to QImage
# Subtract a small amount from the height to ensure the image fits within the available vertical space
image = image.scaled(scroll_area.width(), scroll_area.height() - 45, Qt.KeepAspectRatio, Qt.SmoothTransformation) # Scale the QImage
pixmap = QPixmap.fromImage(image) # Convert QImage back to QPixmap
label.setPixmap(pixmap)
def update_images(self):
# Remove the current images_scroll_area from the layout
if self.images_scroll_area_widget is not None:
self.right_layout.removeWidget(self.images_scroll_area_widget)
self.images_scroll_area_widget.deleteLater()
self.images_scroll_area_widget = None
# Add a new images_scroll_area to the layout if necessary
current_problem = self.problems[self.current_problem_index]
if current_problem[self.current_mode + '_images'] != []:
self.images_scroll_area_widget = self.build_images_scroll_area()
self.right_layout.addWidget(self.images_scroll_area_widget, 3)
def build_chapters_scroll_area(self) -> QScrollArea:
self.chapters_layout = QVBoxLayout()
# Create a new widget for the chapters_layout and set it as the widget of a QScrollArea
chapters_widget = QWidget()
chapters_widget.setLayout(self.chapters_layout)
self.chapters_scroll_area = QScrollArea()
self.chapters_scroll_area.setWidget(chapters_widget)
self.chapters_scroll_area.setWidgetResizable(True)
chapter_input_layout = QHBoxLayout()
checkmark_button = QPushButton('\u2713', self)
self.chapter_input = OneLineTextEdit(self, ref = self)
checkmark_button.setStyleSheet(self.styles.checkmark_button_style)
checkmark_button.setCursor(Qt.PointingHandCursor)
checkmark_button.clicked.connect(lambda: self.create_new_chapter(self.chapter_input.toPlainText().strip()))
chapter_input_layout.addWidget(self.chapter_input)
chapter_input_layout.addWidget(checkmark_button)
self.chapters_layout.addLayout(chapter_input_layout, 0)
for chapter in self.chapters:
temp_chapter_button = QPushButton(chapter, self)
if chapter == self.current_chapter:
temp_chapter_button.setStyleSheet(chapter_button_style_selected)
else:
temp_chapter_button.setStyleSheet(self.styles.chapter_button_style)
temp_chapter_button.setCursor(Qt.PointingHandCursor)
temp_chapter_button.clicked.connect(lambda _, chapter=chapter: self.set_chapter(chapter))
self.chapters_layout.addWidget(temp_chapter_button)
self.chapters_layout.addStretch(1)
self.chapters_scroll_area.setVisible(False)
return self.chapters_scroll_area
def build_workspace_layout(self) -> QHBoxLayout:
self.workspace_layout = QHBoxLayout()
# Create a QWebEngineView for displaying HTML
self.right_layout = QVBoxLayout()
self.webview = QWebEngineView(self)
# Create a QTextEdit for KaTeX input
self.katex_input = QTextEdit(self)
font = QFont("Courier New")
font.setPointSize(13)
self.katex_input.setFont(font)
self.katex_input.setPlaceholderText("Enter KaTeX here...")
if self.question != '':
self.katex_input.setPlainText(self.question)
self.update_math()
self.katex_input.textChanged.connect(self.update_math)
self.right_layout_widget = QWidget()
self.right_layout_widget.setLayout(self.right_layout)
self.right_layout_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.right_layout.setContentsMargins(0, 0, 0, 0)
self.right_layout.addWidget(self.webview, 4)
self.images_scroll_area_widget = None
current_problem = self.problems[self.current_problem_index]
if current_problem['question_images'] != []:
self.images_scroll_area_widget = self.build_images_scroll_area()
self.right_layout.addWidget(self.images_scroll_area_widget, 3)
# Add chapters_scroll_area to the workspace_layout instead of chapters_layout
self.workspace_layout.addWidget(self.build_chapters_scroll_area(), 1)
self.workspace_layout.addWidget(self.katex_input, 1)
self.workspace_layout.addWidget(self.right_layout_widget, 1)
return self.workspace_layout
def check_for_updates(self):
try:
response = requests.get('https://api.github.com/repos/unlucky-kit/NotekeeEditor/releases/latest')
latest_release = response.json()
latest_version = latest_release['tag_name']
current_version = 'v1.0.0' # Replace with your app's current version
if latest_version > current_version:
return latest_release['html_url']
else:
return False
except requests.exceptions.RequestException:
print("Unable to check for updates: no internet connection")
return False
except KeyError:
print("Unable to check for updates: invalid response")
return False
def build_bottom_button_layout(self) -> QHBoxLayout:
button_layout = QHBoxLayout()
all_problems_button = QPushButton('All problems', self)
self.export_import_button = QPushButton(self.export_or_import.capitalize(), self)
if self.settings.get_color_scheme() == 'dark':
color_scheme_button = QPushButton("Light mode", self)
else:
color_scheme_button = QPushButton("Dark mode", self)
upload_image_button = QPushButton('Upload Image', self)
update_url = self.check_for_updates()
if update_url:
update_button = QPushButton('Update available', self)
update_button.setStyleSheet(self.styles.link_button_style)
update_button.setCursor(Qt.PointingHandCursor)
update_button.clicked.connect(lambda: webbrowser.open(update_url))
button_layout.addWidget(all_problems_button)
button_layout.addSpacing(8)
button_layout.addWidget(self.export_import_button)
button_layout.addSpacing(8)
button_layout.addWidget(color_scheme_button)
button_layout.addStretch(1)
button_layout.addWidget(upload_image_button)
# Connect the buttons to their respective functions
color_scheme_button.clicked.connect(lambda: self.toggle_color_scheme())
all_problems_button.clicked.connect(lambda: self.toggle_all_problems_layout_widget())
self.export_import_button.clicked.connect(lambda: self.handle_zip())
upload_image_button.clicked.connect(lambda: self.upload_image())
# Setting stylesheet and cursor for buttons
for button in [all_problems_button, self.export_import_button, color_scheme_button, upload_image_button]:
button.setCursor(Qt.PointingHandCursor)
button.setStyleSheet(self.styles.link_button_style)
return button_layout
def toggle_color_scheme(self):
# Rebuild the whole UI
self.settings.toggle_color_scheme()
self.styles = Styles(self.settings)
self.rebuild_ui()
def reset_minor_variables(self):
# Reset all instance variables excluding the ones that contain information about the current problem
self.current_mode = 'question'
self.show_chapter_picker = False
self.zoomed_image_widget = None
self.all_problems_layout_widget = None
def upload_zip(self):
try:
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
# Get the path to the Desktop
desktop = os.path.join(os.path.expanduser("~"), "Desktop")
# Specify the name of the zip file
zip_file_name = QFileDialog.getOpenFileName(self, 'Open file', desktop, 'Zip files (*.zip)')[0]
# If the user didn't select a file, do nothing
if zip_file_name == '':
return
# Extract the zip file to the application data directory
with zipfile.ZipFile(zip_file_name, 'r') as zipf:
zipf.extractall(app_dir)
# Load the problems and chapters from the json files in the application data directory
with open(os.path.join(app_dir, 'problems.json'), 'r') as problems_file, open(os.path.join(app_dir, 'chapters.json'), 'r') as chapters_file:
self.problems = json.load(problems_file)
self.chapters = list(set(self.chapters + json.load(chapters_file)))
# Add images to the images folder in the application data directory
for foldername, _, filenames in os.walk(os.path.join(app_dir, 'images')):
for filename in filenames:
if filename != '.gitkeep': # Exclude .gitkeep files
os.rename(os.path.join(foldername, filename), os.path.join(app_dir, 'images', filename))
# Get the last problem's data
if self.problems:
current_problem = self.problems[self.current_problem_index]
self.question = current_problem.get('question', '')
self.solution = current_problem.get('solution', '')
self.current_chapter = current_problem.get('chapter', '')
else:
raise Exception("No problems found in the problems.json file")
if len(self.problems) > 1:
self.export_or_import = 'export'
self.export_import_button.setText('Export')
elif self.problems == []:
self.problems = [{'question': '', 'solution': '', 'chapter': '', 'question_images': [], 'solution_images': []}]
# Rebuild the whole UI
self.rebuild_ui()
except FileNotFoundError as fnf_error:
print(f"File not found: {fnf_error}")
except json.JSONDecodeError as json_error:
print(f"Invalid JSON file: {json_error}")
except zipfile.BadZipFile as zip_error:
print(f"Invalid zip file: {zip_error}")
except Exception as e:
print(f"An error occurred: {e}")
def handle_zip(self):
if self.export_or_import == 'export':
self.download_zip()
else:
self.upload_zip()
def create_zip(self, destination):
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
with zipfile.ZipFile(destination, 'w') as zipf:
# Write the problems.json and chapters.json files from the application data directory to the zip file
zipf.write(os.path.join(app_dir, 'problems.json'), arcname='problems.json')
zipf.write(os.path.join(app_dir, 'chapters.json'), arcname='chapters.json')
# Write the images from the images folder in the application data directory to the zip file
for foldername, _, filenames in os.walk(os.path.join(app_dir, 'images')):
for filename in filenames:
zipf.write(os.path.join(foldername, filename), arcname=os.path.join('images', filename))
# Will return None if everything is good, otherwise will return an error message
def problems_json_error_message(self):
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
with open(os.path.join(app_dir, 'problems.json'), 'r') as file:
data = json.load(file)
for problem in data:
if problem['question'] == '':
return "At least one problem has no question!"
elif problem['solution'] == '' and problem['solution_images'] == []:
return "At least one problem has no solution!"
return None
def download_zip(self):
self.save_to_problems(can_delete_empty_problem = False, can_skip_saving = False)
# Check if there are problems without solutions
problems_json_error_message = self.problems_json_error_message()
if problems_json_error_message != None:
QMessageBox.critical(self, "Error", f"{problems_json_error_message} Check problems colored red when you click 'All problems' button.")
return
# Get the path to the Desktop
desktop = os.path.join(os.path.expanduser("~"), "Desktop")
# Specify the name of the zip file
export_index = self.settings.get_export_index()
zip_file_name = f"notekee-{export_index}.zip"
# Create the full path
destination = os.path.join(desktop, zip_file_name)
self.create_zip(destination)
if not os.path.isfile(destination):
QMessageBox.critical(self, "Error", "Failed to export the problems!")
return
# Erase all problems
self.solution = ''
self.question = ''
self.current_problem_index = 0
self.problems = [{'question': '', 'solution': '', 'chapter': '', 'question_images': [], 'solution_images': []}]
self.current_chapter = ''
self.chapters = []
self.save_to_chapters()
self.save_to_problems()
# Empty images folder, excluding .gitkeep file
for foldername, _, filenames in os.walk('images'):
for filename in filenames:
if filename != '.gitkeep':
os.remove(os.path.join(foldername, filename))
self.export_or_import = 'import'
self.export_import_button.setText('Import')
self.settings.increment_export_index()
self.save_to_problems(can_delete_empty_problem = False, can_skip_saving = False)
self.rebuild_ui()
def rebuild_ui(self):
self.settings.save_window_position_and_size(self)
self.centralWidget().deleteLater()
self.reset_minor_variables()
self.init_ui()
self.update_images()
def remove_empty_problems(self):
for problem in self.problems:
if problem['question'] == '' and problem['solution'] == '':
if problem['question_images'] == [] and problem['solution_images'] == []:
self.problems.remove(problem)
if self.current_problem_index > len(self.problems) - 1:
self.current_problem_index = len(self.problems) - 1
def init_ui(self):
# Create central widget and layout
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
self.layout = QVBoxLayout(central_widget)
self.setStyleSheet(self.styles.app_style)
self.secondary_layout_widget = QWidget(self)
secondary_layout = QVBoxLayout(self.secondary_layout_widget)
secondary_layout.setContentsMargins(0, 0, 0, 0)
# Removing vertical padding from the buttons
spacer1 = QSpacerItem(-5, -5, QSizePolicy.Minimum, QSizePolicy.Fixed)
# spacer2 = QSpacerItem(-10, -10, QSizePolicy.Minimum, QSizePolicy.Fixed)
spacer3 = QSpacerItem(-5, -5, QSizePolicy.Minimum, QSizePolicy.Fixed)
secondary_layout.addItem(spacer1)
secondary_layout.addLayout(self.build_top_button_layout())
# secondary_layout.addItem(spacer2)
secondary_layout.addLayout(self.build_workspace_layout())
secondary_layout.addItem(spacer3)
secondary_layout.addLayout(self.build_bottom_button_layout())
self.layout.addWidget(self.secondary_layout_widget)
self.setWindowTitle("Notekee Editor")
window_position = self.settings.get_window_position()
window_size = self.settings.get_window_size()
self.setGeometry(window_position[0], window_position[1], window_size[0], window_size[1])
def toggle_all_problems_layout_widget(self):
if self.all_problems_layout_widget is None:
if self.current_chapter == '':
return
self.save_to_problems(can_skip_saving = False) # Makes sure current problem is saved
self.remove_empty_problems()
self.remove_empty_chapters()
# Delete this after first release
current_problem = self.problems[self.current_problem_index]
if self.question != current_problem['question'] or self.solution != current_problem['solution']:
self.question = current_problem['question']
self.solution = current_problem['solution']
self.save_to_problems(can_skip_saving = False) # Makes sure removal of empty problems is saved
self.save_to_chapters() # Makes sure removal of empty chapters is saved
# If current problem's chapter wasn't yet saved, assign it one.
if self.problems[self.current_problem_index]['chapter'] == '':
self.problems[self.current_problem_index]['chapter'] = self.current_chapter
if self.current_chapter not in self.chapters:
self.current_chapter = self.problems[self.current_problem_index]['chapter']
self.chapter_button.setText(self.current_chapter)
self.all_problems_layout_widget = QWidget()
all_problems_layout = QVBoxLayout(self.all_problems_layout_widget)
all_problems_scroll_area = QScrollArea()
all_problems_scroll_area.setWidgetResizable(True)
all_problems_layout_widget = QWidget()
all_problems_scroll_area_layout = QVBoxLayout(all_problems_layout_widget)
all_problems_scroll_area.setWidget(all_problems_layout_widget)
chapter_problems = {}
for chapter in self.chapters:
chapter_problems[chapter] = []
for problem in self.problems:
chapter_problems[problem['chapter']].append(problem)
for chapter in chapter_problems:
chapter_button = QPushButton(f'▼ {chapter}', self)
chapter_problem_layout = QVBoxLayout()
chapter_button.setStyleSheet(self.styles.plain_chapter_button_style)
chapter_button.setCursor(Qt.PointingHandCursor)
for i in range(len(chapter_problems[chapter])):
problem = chapter_problems[chapter][i]
problem_question = problem['question'].replace('\n', ' ')
problem_button = QPushButton(f'{i + 1}. {problem_question}', self)
problem_button.clicked.connect(lambda _, problem=problem: self.load_problem(problem))
problem_button.setCursor(Qt.PointingHandCursor)
if self.problems.index(problem) != self.current_problem_index:
if problem['solution'] == '' and problem['solution_images'] == [] or problem['question'] == '':
problem_button.setStyleSheet(self.styles.error_with_problem_button_style)
else:
problem_button.setStyleSheet(self.styles.plain_problem_button_style)
else:
problem_button.setStyleSheet(self.styles.current_problem_button_style)
chapter_problem_layout.addWidget(problem_button)
def toggle_visibility(button, layout, chapter_name):
for i in range(layout.count()):
widget = layout.itemAt(i).widget()
widget.setVisible(not widget.isVisible())
# Change the button text based on the visibility of the first problem
if layout.count() > 0 and layout.itemAt(0).widget().isVisible():
button.setText(f'▼ {chapter_name}')
else:
button.setText(f'▶ {chapter_name}')
# Shortening this line will cause all buttons to toggle only one chapter
chapter_button.clicked.connect(lambda _, button=chapter_button, layout=chapter_problem_layout, chapter=chapter: toggle_visibility(button, layout, chapter))
all_problems_scroll_area_layout.addWidget(chapter_button)
all_problems_scroll_area_layout.addLayout(chapter_problem_layout)
all_problems_scroll_area_layout.addStretch(1)
all_problems_layout.addWidget(all_problems_scroll_area)
all_problems_layout.setContentsMargins(0, 0, 0, 0)
# Create a "Back" button and add it to the layout
back_button = QPushButton("Back", self)
back_button.clicked.connect(lambda: self.toggle_all_problems_layout_widget())
back_button.setCursor(Qt.PointingHandCursor)
back_button.setStyleSheet(self.styles.link_button_style)
back_button_layout = QHBoxLayout()
back_button_layout.addWidget(back_button)
back_button_layout.addStretch(1)
all_problems_layout.addLayout(back_button_layout)
self.layout.addWidget(self.all_problems_layout_widget)
self.secondary_layout_widget.setVisible(False)
else:
if (self.solution == '' and self.question == '') or self.question != self.problems[self.current_problem_index]['question']:
problem = self.problems[self.current_problem_index]
self.question = problem['question']
self.solution = problem['solution']
self.current_chapter = problem['chapter']
self.katex_input.setPlainText(self.question if self.current_mode == 'question' else self.solution)
self.update_math()
# Else if katex_input is empty and if there is something in the current problem's question or solution, load it
elif self.katex_input != (self.question if self.current_mode == 'question' else self.solution):
if self.current_mode == 'question':
self.katex_input.setPlainText(self.question)
if self.problems[self.current_problem_index]['question_images'] != []:
self.update_images()
else:
self.katex_input.setPlainText(self.solution)
if self.problems[self.current_problem_index]['solution_images'] != []:
self.update_images()
self.update_math()
self.layout.removeWidget(self.all_problems_layout_widget)
self.all_problems_layout_widget.deleteLater()
self.all_problems_layout_widget = None
self.secondary_layout_widget.setVisible(True)
def load_problem(self, problem):
# If the problem is already loaded, do nothing
if self.current_problem_index == self.problems.index(problem):
if self.katex_input.toPlainText() == (self.question if self.current_mode == 'question' else self.solution):
self.toggle_all_problems_layout_widget()
return
# Zoom out the image if it is zoomed in
if self.zoomed_image_widget is not None:
self.zoom_out_image()
# Load new problem's data
self.question = problem['question']
self.solution = problem['solution']
self.current_problem_index = self.problems.index(problem)
if self.current_chapter != problem['chapter']:
self.set_chapter(problem['chapter'])
self.chapter_button.setText(self.current_chapter)
if self.current_mode != 'question':
self.katex_input.setPlainText(self.solution) # Prevents from self.solution getting the value of the previous problem's katex_input
self.toggle_current_input_mode(self.question_button)
if self.show_chapter_picker:
self.toggle_chapter_picker()
self.katex_input.setPlainText(self.question)
self.toggle_all_problems_layout_widget()
self.update_math()
self.update_images()
if self.current_problem_index == 0:
self.disable_button(self.previous_button)
self.previous_button_enabled = False
else:
self.enable_button(self.previous_button)
self.previous_button_enabled = True
if self.current_problem_index == len(self.problems) - 1 and self.question == '':
self.disable_button(self.next_button)
self.next_button_enabled = False
else:
self.enable_button(self.next_button)
self.next_button_enabled = True
def save_to_chapters(self):
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
with open(os.path.join(app_dir, 'chapters.json'), 'w') as file:
json.dump(self.chapters, file, indent=4)
def delete_empty_problems(self):
for problem in self.problems:
if problem['question'] == '' and problem['solution'] == '':
if problem['question_images'] == [] and problem['solution_images'] == []:
self.problems.remove(problem)
if self.current_problem_index > len(self.problems) - 1:
self.current_problem_index = len(self.problems) - 1
# can_delete_empty_problem = True and can_skip_saving = False will always crash the program if current problem is empty
def save_to_problems(self, can_delete_empty_problem = False, can_skip_saving = True):
# If the current problem is empty and there are no images, skip saving
if self.solution == '' and self.question == '':
# Because we don't have self.solution_images and self.question_images, we have to get them from the problems list
solution_images = self.problems[self.current_problem_index]['solution_images']
question_images = self.problems[self.current_problem_index]['question_images']
if solution_images == [] and question_images == []:
if can_delete_empty_problem:
self.problems.pop(self.current_problem_index)
if can_skip_saving:
return
current_problem = self.problems[self.current_problem_index]
# Update the current problem's data if it has changes
if current_problem['question'] != self.question:
current_problem['question'] = self.question
if current_problem['solution'] != self.solution:
current_problem['solution'] = self.solution
if current_problem['chapter'] != self.current_chapter:
current_problem['chapter'] = self.current_chapter
# Remove empty problems from json's version of data
clean_problems = []
for problem in self.problems:
if problem['question'] != '' or problem['solution'] != '' or problem['question_images'] != [] or problem['solution_images'] != []:
clean_problems.append(problem)
if self.export_or_import == 'import' and len(clean_problems) > 1:
self.export_or_import = 'export'
self.export_import_button.setText('Export')
elif self.export_or_import == 'export' and len(clean_problems) <= 1:
self.export_or_import = 'import'
self.export_import_button.setText('Import')
# This is the name of your application
app_name = 'NotekeeEditor'
# This is the path to the application data directory
app_dir = appdirs.user_data_dir(app_name)
# Save the data to the json file in the application data directory
with open(os.path.join(app_dir, 'problems.json'), 'w') as file:
json.dump(clean_problems, file, indent=4)
def create_new_chapter(self, chapter_name):
chapter_name = chapter_name.strip()
self.chapter_input.clear()
if chapter_name == '' or chapter_name in self.chapters:
return
self.chapters.insert(0, chapter_name) # Add chapter to the top of the list
# Create a new chapter button and add it to the layout
new_chapter_button = QPushButton(chapter_name, self)
new_chapter_button.setStyleSheet(chapter_button_style_selected)
new_chapter_button.setCursor(Qt.PointingHandCursor)
new_chapter_button.clicked.connect(lambda _, chapter=chapter_name: self.set_chapter(chapter))
self.chapters_layout.insertWidget(1, new_chapter_button) # Insert button at the top
self.set_chapter(chapter_name)