-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
4462 lines (3520 loc) · 187 KB
/
main.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
import copy
import ctypes
import json
import math
import os
import sys
import threading
from functools import wraps
import customtkinter as ctk
import psutil
from PIL import Image, ImageTk
from customtkinter import filedialog
from file_handler import FileHandler
# -Custom Classes--
from graphics_manager import GraphicsManager, OverlayGraphicsManager
from image_processor import ImageProcessor
from keybinds import KeyBinds, CanvasKeybinds, OverlayKeyBinds
from outliner import Outliner
from tools import Tools, TextInsertWindow
# Icons used in the app were downloaded from https://icons8.com/.
# ---------------Top Level Windows------------------------#
class FileLoadWindow(ctk.CTkToplevel):
"""
Toplevel window that handles loading and verifying of Images and Project files.
"""
def __init__(self, app, *args, **kwargs):
"""
Args:
app: main ctk.CTK app
*args:
**kwargs:
"""
super().__init__(*args, **kwargs)
self.app = app
self.width = min(720, math.ceil(self.winfo_screenwidth() * 0.335))
height = self.width - 40
self.screen_width = self.winfo_screenwidth()
self.screen_height = self.screen_width * 0.5625 # 16:9 ratio
x = (self.screen_width - self.width) // 2
y = (self.screen_height - height) // 2
self.geometry(f"{self.width}x{height}+{x}+{y}")
self.title("Load Window")
self.resizable(False, False)
self.configure(fg_color="#282828")
if sys.platform.startswith('win'):
self.after(201, lambda: self.iconbitmap(os.path.join("sources", "images", "logo.ico")))
else:
self.after(201, lambda: self.iconbitmap(os.path.join("@sources", "images", "logo.xbm")))
self.columnconfigure((0, 1), weight=1)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.rowconfigure(2, weight=2)
self.rowconfigure(3, weight=1)
self.rowconfigure(4, weight=1)
self.file_list = []
self.file_names = []
self.corrupted_file_names = []
self.file_string = None
self.project_file_path = None
self.project_status = None
self.current_protocol = None
# for the Dropdown menu
sequence_search_modes = ["Auto", "Normal (_001, -001, file001)", "Pot Player (_015959.999.jpg)",
"VLC Player (vlcsnap-01_59_59...)"]
self.sequence_search_mode = "auto"
title_image = ctk.CTkImage(light_image=Image.open(os.path.join("sources", "images", "rview_title.png")),
size=(self.get_rel_width(.537), self.get_rel_width(.063)))
image_label = ctk.CTkLabel(self, image=title_image, text="")
image_label.grid(column=0, row=0, columnspan=2, sticky="we", pady=(10, 10), )
version_lbl = ctk.CTkLabel(self, text="v 1.0", fg_color="transparent")
version_lbl.place(in_=image_label, relx=.8, rely=1, anchor="ne")
self.open_btn = ctk.CTkButton(self, text="Open Images", command=self.browse_images, width=130,
height=35, font=("Arial", 18), text_color="#EFEFEF")
self.load_project_btn = ctk.CTkButton(self, text="Load Project", command=self.browse_project, width=120,
fg_color="#6C6C6C", height=35, font=("Arial", 17))
self.clear_btn = ctk.CTkButton(self, text="Clear", command=self.clear_file_list,
state="disabled", width=80, height=30,
font=("Arial", 18), fg_color="#d2190d",
hover_color="#6a0c06")
self.file_list_box = ctk.CTkTextbox(master=self, width=int(self.width * 0.833),
corner_radius=5, fg_color="#1f1f1f",
font=("Arial", 15), wrap="none",
exportselection=False)
self.file_list_box.configure(state="disabled")
self.file_list_box.grid(column=0, row=2, sticky='ns', columnspan=2)
self.open_btn.place(in_=self.file_list_box, relx=0, rely=0.01, anchor="sw")
self.clear_btn.place(in_=self.file_list_box, relx=1, rely=.99, anchor="ne", bordermode="outside")
self.load_project_btn.place(in_=self.file_list_box, relx=1, rely=0.02, anchor="se")
self.count_lbl = ctk.CTkLabel(self, text=f"Total files: {len(self.file_list)}",
fg_color="transparent", font=("Arial", 18))
self.count_lbl.place(in_=self.file_list_box, relx=0, rely=1, anchor="nw")
self.sequence_search_combobox = ctk.CTkComboBox(self, values=sequence_search_modes, dropdown_fg_color="#383838",
justify="left", state="readonly", dropdown_font=("Arial", 15),
font=("Arial", 15), width=int(self.get_rel_width(.3)),
command=self.set_sequence_search_mode)
self.sequence_search_combobox.place(in_=self.file_list_box, relx=.5, rely=1, anchor="n", bordermode="outside")
self.sequence_search_combobox.set("Sequence Search (Auto)")
self.file_load_progressbar = ctk.CTkProgressBar(self, orientation="horizontal", fg_color="#282828",
progress_color="#227AFF", height=10, corner_radius=0)
self.file_load_progressbar.set(0)
self.file_load_progressbar.grid(column=0, row=3, columnspan=2, pady=(20, 0), sticky="s")
self.main_btn = ctk.CTkButton(self, text="Load", command=self.call_load_files, state="disabled",
width=int(self.get_rel_width(0.288)),
height=int(self.get_rel_width(0.076)), font=("Arial", 25), fg_color="#3651D4",
hover_color="#343F74")
self.main_btn.grid(column=0, row=4, columnspan=2, sticky="n", pady=(10, 0))
self.path_override_frame = ctk.CTkFrame(self, fg_color="#1D3159", height=80, width=int(self.width * 0.833))
self.path_override_frame.columnconfigure((0, 2), weight=1)
self.path_override_frame.columnconfigure(1, weight=0)
self.path_override_frame.rowconfigure((0, 1), weight=1)
self.path_override_frame.grid_propagate(False)
path_override_label = ctk.CTkLabel(self.path_override_frame, text="Path to the Images:", font=("Arial", 16))
path_override_label.grid(row=0, column=0, sticky="e")
self.path_override_entry_var = ctk.StringVar()
self.path_override_entry = ctk.CTkEntry(self.path_override_frame,
width=int(self.width * 0.43), height=35,
fg_color="#C7C7C7",
placeholder_text_color="#363636",
text_color="Black", exportselection=False,
textvariable=self.path_override_entry_var, font=("Arial", 14))
self.path_override_entry.grid(row=0, column=1, )
self.path_override_entry_var.trace_add("write", self.folder_override_entrybox_updated)
self.pick_override_path_btn = ctk.CTkButton(self.path_override_frame, text="Pick", fg_color="#c43737", width=60,
hover_color="#632020", command=self.pick_override_path_btn_handler)
self.pick_override_path_btn.grid(row=0, column=2)
ignore_images_label = ctk.CTkLabel(self.path_override_frame, text="Ignore Missing Images:", font=("Arial", 16))
ignore_images_label.grid(row=1, column=0, sticky="e")
self.ignore_images_checkbox = ctk.CTkCheckBox(self.path_override_frame, text="", width=5, )
self.ignore_images_checkbox.grid(row=1, column=1, sticky="w", padx=21, )
self.ignore_images_checkbox.configure(state="disabled")
ignore_images_toolip = ctk.CTkLabel(self.path_override_frame, text="(Generates blanks in the provided path.)",
font=("Arial", 14))
ignore_images_toolip.place(in_=self.ignore_images_checkbox, relx=1, rely=0, anchor="nw")
# Closing the window kills the App.
self.protocol("WM_DELETE_WINDOW", self.kill_app)
def pick_override_path_btn_handler(self):
override_folder_path = filedialog.askdirectory(parent=self, title="Select images folder.")
if override_folder_path:
self.path_override_entry_var.set(override_folder_path)
def get_rel_width(self, times):
"""
Method that returns a relative width of the window times a value.
Args:
times(float): A value that multiplies the window width.
Returns:
float: Resulting float value.
"""
rel_width = self.width * times
return rel_width
def set_sequence_search_mode(self, choice: str):
"""
Called on selecting a search mode from the dropdown menu in the FileLoad window.
Sets the sequence search mode to the selected mode.
Args:
choice:Sequence search mode to extract the sequence pattern from the filenames. Deafault "auto".
Returns:
None
"""
# Normal (_001, -001, file001)= Normal
self.sequence_search_mode = choice.split()[0].lower()
def browse_images(self):
"""
Browse and load Images and sets the current_protocol to "images".
Returns:
None
"""
self.disable_buttons()
# opened_files = filedialog.askopenfilenames(title="Select Files",
# filetypes=([("Image Files","*.bmp;*.gif;*.icns;*.ico;*.im;*.jpeg;*.jpg;*.jfif;*.jpeg2000;*.png;*.ppm;*.tga;*.tif;*.tiff;*.webp")]))
opened_files = filedialog.askopenfilenames(parent=self, title="Select Files",
filetypes=[("Image Files",
".bmp .BMP .gif .GIF .icns .ICNS .ico .ICO .im .IM .jpeg .JPEG .jpg .JPG .jfif .JFIF .jpeg2000 .JPEG2000 .png .PNG .ppm .PPM .tga .TGA .tif .TIF .tiff .TIFF .webp .WEBP"),
("All Files", "*.*")])
if opened_files or self.file_list:
# If the user used the file load window to load an image, change protocol from project to image.
if opened_files:
self.change_protocol("images")
for file in opened_files:
extracted_filename = file.rsplit('/', 2)[-1] + "\n\n"
if file not in self.file_list:
# validating images.
valid_image = FileHandler.validate_image(image=file)
if valid_image:
self.file_names.append(extracted_filename)
self.file_list.append(file)
elif extracted_filename not in self.corrupted_file_names:
# Storing the corrupted filenames.
self.corrupted_file_names.append(extracted_filename)
file_list_string = ""
if self.file_list and self.corrupted_file_names:
file_list_string = "".join(self.file_names) + "\n\nOne or more files failed to load:\n\n" + "".join(
self.corrupted_file_names)
elif self.file_list:
file_list_string = "".join(self.file_names)
elif self.corrupted_file_names:
file_list_string = "One or more files failed to load:\n\n" + "".join(self.corrupted_file_names)
self.change_protocol("fail")
self.file_string = file_list_string
self.update_file_list_box(string=self.file_string, count=len(self.file_list))
if self.current_protocol == "images" or self.current_protocol == "project":
self.enable_buttons()
self.enable_input_buttons()
def browse_project(self):
"""
Browse and load a saved project file and sets the current_protocol to "project".
Returns:
None
"""
self.disable_buttons()
opened_file = filedialog.askopenfilename(parent=self, title="Select File", filetypes=[("RView", "*.rvp")])
if opened_file:
# If the user used the file load window to load a project, change protocol from image to project.
project_file_validated = FileHandler.validate_project_file(opened_file)
if project_file_validated:
self.change_protocol("project")
self.project_status = True
self.project_file_path = opened_file
self.update_file_list_box(string="Validating Project file...\n\nProject successfully loaded.",
count=1)
else: # project validation failed.
self.change_protocol("fail")
self.update_file_list_box(
string="Validating Project file...\n\nProject loading failed.\n\nReason: Missing source files or Corrupted save.",
count=0)
if self.current_protocol == "images" or self.current_protocol == "project":
self.enable_buttons()
self.enable_input_buttons()
def call_load_files(self):
"""
Loads images or project and calls the load_images method on App.
Returns:
None
"""
if self.current_protocol == "images":
try:
self.app.load_images()
except Exception as e: # Failed to load the image even after full validation.
error_message = '''The following error occurred:\n\n{}'''.format(e)
self.clear_file_list()
# shows the error message on the file_list_box
self.update_file_list_box(string=error_message, count=0)
self.file_list_box.see(0.0)
else:
self.destroy()
elif self.current_protocol == "project":
if self.ignore_images_checkbox.get():
ignore_missing_images = True
else:
ignore_missing_images = False
try:
if images_folder_path := self.path_override_entry.get().strip(): # Passing the new folderpath to the images.
self.update_file_list_box(string="Retrying....\nFetching images...", count=1)
self.app.load_project(project_path=self.project_file_path,
images_folder_override_path=images_folder_path,
ignore_missing_images=ignore_missing_images)
else:
self.app.load_project(project_path=self.project_file_path)
except FileNotFoundError:
if self.path_override_entry.get():
self.path_override_entry.configure(fg_color="#EEA2A2") # a red color
self.update_file_list_box()
error_message = f"The following error occurred:\n\nOne or more Image files missing.\nProvide a valid path to the images folder."
self.update_file_list_box(string=error_message, count=0)
self.file_list_box.see(0.0)
self.path_override_frame.place(in_=self.file_list_box, relx=0, rely=1, anchor="sw")
self.enable_buttons()
#
except Exception as e: # Failed to load the project even after full validation.
error_message = f"The following error occurred:\n\nCorrupted Save.\nTry loading the project again. \nError: {e}"
self.clear_file_list()
# shows the error message on the file_list_box
self.update_file_list_box(string=error_message, count=0)
self.file_list_box.see(0.0)
else:
self.destroy()
def folder_override_entrybox_updated(self, *args):
"""
Called when path_override_entry box is updated with a value. If the entry box is empty disable the ignore_missing_image checkbox.
Args:
*args:
Returns:
None
"""
entry_value = self.path_override_entry_var.get().strip()
if entry_value: # Check if the entry box is not empty
self.ignore_images_checkbox.configure(state="normal")
else:
self.ignore_images_checkbox.deselect()
self.ignore_images_checkbox.configure(state="disabled")
def change_protocol(self, new_protocol):
"""
Change the protocol between "images" , "project" or "fail".
Args:
new_protocol (str): Current protocol to use.
Returns:
None
"""
self.path_override_frame.place_forget()
self.path_override_entry.delete(0, "end")
if new_protocol == "project":
self.file_list = []
self.file_names = []
self.corrupted_file_names = []
self.file_string = None
self.current_protocol = "project"
self.enable_buttons()
elif new_protocol == "images":
self.project_status = None
self.project_file_path = None
self.current_protocol = "images"
self.enable_buttons()
elif new_protocol == "fail": # Reset everything on fail.
self.file_list = []
self.file_names = []
self.corrupted_file_names = []
self.file_string = None
self.project_status = None
self.project_file_path = None
self.current_protocol = "fail"
self.enable_buttons()
self.main_btn.configure(state="disabled")
def update_file_list_box(self, string: str = "", count: int = 0):
"""
Updates the textbox on the file load window.
Args:
string (str): Text to display
count (int): Count of files loaded.
Returns:
None
"""
self.file_list_box.configure(state="normal")
self.file_list_box.delete("0.0", "end")
self.file_list_box.insert("0.0", string)
self.file_list_box.see("end")
self.count_lbl.configure(text=f"Total files: {count}")
self.file_list_box.configure(state="disabled")
def enable_buttons(self):
"""
Enables all buttons on the FileLoad window.
Returns:
None
"""
self.main_btn.configure(state="normal")
self.clear_btn.configure(state="normal", )
self.load_project_btn.configure(state="normal")
self.open_btn.configure(state="normal")
def disable_buttons(self):
"""
Disables all buttons on the FileLoad window.
Returns:
None
"""
self.main_btn.configure(state="disabled")
self.clear_btn.configure(state="disabled")
self.load_project_btn.configure(state="disabled")
self.open_btn.configure(state="disabled")
def enable_input_buttons(self):
"""
Enables all input buttons on the FileLoad window.
Returns:
None
"""
self.load_project_btn.configure(state="normal")
self.open_btn.configure(state="normal")
def clear_file_list(self):
"""
Clears all variables and resets the FileLoad window to initial state.
Returns:
None
"""
self.path_override_frame.place_forget()
self.path_override_entry.delete(0, "end")
self.file_list = []
self.file_names = []
self.corrupted_file_names = []
self.file_string = None
self.project_file_path = None
self.project_status = None
self.current_protocol = None
self.update_file_list_box()
self.disable_buttons()
self.enable_input_buttons()
def update_file_window_progressbar(self, progress: float, progress_color: str = None):
"""
Updates the file_load_progressbar.
Args:
progress (float):A float of range 0 to 1.0 with 1.0 filling the progressbar 100%.
progress_color (str):A hex color value for the progression color in the progressbar.
Returns:
None
"""
progress = round(progress, 2)
if progress_color:
self.file_load_progressbar.configure(progress_color=progress_color)
self.file_load_progressbar.set(progress)
self.file_load_progressbar.update_idletasks()
def kill_app(self):
"""
Kills the main app.
Returns:
None
"""
self.app.kill_app()
class UserSettingsWindow(ctk.CTkToplevel):
"""
Toplevel window that handles user settings of the app.
"""
def __init__(self, app, *args, **kwargs):
super().__init__(*args, **kwargs)
self.app = app
width = 250
height = 200
self.screen_width = self.winfo_screenwidth()
self.screen_height = self.screen_width * 0.5625 # clamps window to 16:9 ratio.
x = (self.screen_width - width) // 2
y = (self.screen_height - height) // 2
MAIN_COLOR = "#28333E"
self.title("User Settings")
self.configure(fg_color=MAIN_COLOR) # Darkish blue
# 200 ms solves the bug with iconbitmap on toplevel windows.
if sys.platform.startswith('win'):
self.after(201, lambda: self.iconbitmap(os.path.join("sources", "images", "logo.ico")))
else:
self.after(201, lambda: self.iconbitmap(os.path.join("@sources", "images", "logo.xbm")))
self.geometry(f"{width}x{height}+{x}+{y}")
self.resizable(False, False)
self.columnconfigure(0, weight=1)
self.rowconfigure((0, 1, 2), weight=1)
menu_label = ctk.CTkLabel(self, text="User Settings", font=("Arial Bold", 16))
menu_label.grid(row=0, column=0, )
# ========Check Boxes=====================================
self.reset_user_settings_btn = ctk.CTkButton(self, text="Reset", command=self.reset_user_settings,
width=50, height=12, font=("Arial", 12),
fg_color="#931C14", hover_color="#4E1414", text_color="#D2D2D2",
corner_radius=5)
self.reset_user_settings_btn.place(x=0, y=0)
self.dropdown_frame = ctk.CTkFrame(self, fg_color=MAIN_COLOR)
self.dropdown_frame.columnconfigure((0, 1), weight=1)
self.dropdown_frame.rowconfigure((0, 1, 2, 3, 4, 5), weight=1)
self.dropdown_frame.grid(row=1, column=0, sticky="news")
canvas_color_label = ctk.CTkLabel(self.dropdown_frame, text="Canvas Color:", font=("Arial", 16), width=0)
canvas_color_label.grid(row=2, column=0, sticky='e')
self.canvas_color_dropdown = ctk.CTkOptionMenu(self.dropdown_frame,
values=["Default", "Black", "Grey", "White"],
width=20, height=25)
self.canvas_color_dropdown.grid(row=2, column=1, sticky='w', padx=(5, 0))
self.canvas_color_dropdown.set((self.app.user_settings["canvas_color"]).capitalize())
selection_color_label = ctk.CTkLabel(self.dropdown_frame, text="Selection Color:", font=("Arial", 16), width=0)
selection_color_label.grid(row=4, column=0, sticky='e')
self.selection_color_dropdown = ctk.CTkOptionMenu(self.dropdown_frame,
values=["Blue", "Pink", "Red", "Green"],
width=20, height=25)
self.selection_color_dropdown.grid(row=4, column=1, sticky='w', padx=(5, 0))
self.selection_color_dropdown.set((self.app.user_settings["selection_color"]).capitalize())
highlight_opacity_label = ctk.CTkLabel(self.dropdown_frame, text="Highlight Opacity:", font=("Arial", 16),
width=0)
highlight_opacity_label.grid(row=5, column=0, sticky='e')
self.highlight_opacity_slider_value_label = ctk.CTkLabel(self,
text=self.app.user_settings["highlight_opacity"],
font=("Arial", 16), width=0)
self.highlight_opacity_slider = ctk.CTkSlider(self.dropdown_frame,
from_=1, to=99, width=int(width * 0.4),
number_of_steps=98,
command=lambda
event: self.highlight_opacity_slider_value_label.configure(
text=int(event)))
self.highlight_opacity_slider_value_label.place(in_=self.highlight_opacity_slider, relx=.8, rely=0, anchor="sw")
self.highlight_opacity_slider.set(self.app.user_settings["highlight_opacity"])
self.highlight_opacity_slider.grid(row=5, column=1, sticky='w', padx=(5, 0))
# ================Output ==================================
self.save_user_settings_btn = ctk.CTkButton(self, text="Save Settings", command=self.save_user_settings,
width=150, height=35, font=("Arial bold", 17),
fg_color="#3568B5", hover_color="#213D67", text_color="white")
self.save_user_settings_btn.grid(column=0, row=3, pady=5)
def save_user_settings(self):
"""
Calls the save_user_settings on main app and Kills the toplevelwindow.
Returns:
None
"""
canvas_color = self.canvas_color_dropdown.get().lower()
selection_color = self.selection_color_dropdown.get().lower()
highlight_opacity = int(self.highlight_opacity_slider.get())
self.app.user_settings = {"canvas_color": canvas_color,
"selection_color": selection_color,
"highlight_opacity": highlight_opacity,
}
self.app.update_user_settings()
self.destroy()
def reset_user_settings(self):
"""
Replaces the user settings.json file with a new file with default values.
Returns:
None
"""
self.app.load_user_settings(reset=True)
self.destroy()
class RenderMenu(ctk.CTkToplevel):
"""
Toplevel window that handles the render settings and starting the render.
"""
def __init__(self, app, batch: bool, *args, **kwargs):
"""
Args:
app: main ctk.CTK app
batch (bool): Mode to render the images, True adds all queued images to render. False renders the current image in view.
**kwargs:
"""
super().__init__(*args, **kwargs)
self.app = app
self.is_batch = batch
width = 350
height = 350
self.screen_width = self.winfo_screenwidth()
self.screen_height = self.screen_width * 0.5625 # clamps window to 16:9 ratio.
x = (self.screen_width - width) // 2
y = (self.screen_height - height) // 2
main_color = "#263142"
self.title("Render Menu")
self.configure(fg_color=main_color) # Darkish blue
# 200 ms solves the bug with iconbitmap on toplevel windows.
if sys.platform.startswith('win'):
self.after(201, lambda: self.iconbitmap(os.path.join("sources", "images", "logo.ico")))
else:
self.after(201, lambda: self.iconbitmap(os.path.join("@sources", "images", "logo.xbm")))
self.geometry(f"{width}x{height}+{x}+{y}")
self.resizable(False, False)
self.columnconfigure(0, weight=1)
self.rowconfigure((0, 1, 2), weight=1)
menu_label = ctk.CTkLabel(self, text="Render Settings", font=("Arial Bold", 16))
menu_label.grid(row=0, column=0, )
# ========Check Boxes=====================================
checkbox_border = 1
self.checkbox_frame = ctk.CTkFrame(self, fg_color=main_color)
self.checkbox_frame.columnconfigure((0, 1), weight=1)
self.checkbox_frame.rowconfigure((0, 1, 2, 3), weight=1)
self.checkbox_frame.grid(row=1, column=0, sticky="news")
render_overlay_label = ctk.CTkLabel(self.checkbox_frame, text="Render Overlay:", font=("Arial", 16))
render_overlay_label.grid(row=1, column=0, sticky='e')
self.render_overlay_checkbox = ctk.CTkCheckBox(self.checkbox_frame, text="", onvalue=1, offvalue=0,
border_width=checkbox_border,
command=self.render_overlay_checkbox_handler)
if self.app.render_overlay:
self.render_overlay_checkbox.select()
self.render_overlay_checkbox.grid(row=1, column=1, sticky='w', padx=(25, 0))
self.overlay_trim_label = ctk.CTkLabel(self.checkbox_frame, text="Trim:", font=("Arial", 16))
self.overlay_trim_label.place(in_=self.render_overlay_checkbox, relx=0.5, rely=0, anchor="nw",
bordermode="outside")
self.overlay_trim_label.configure(text_color="grey")
self.trim_overlay_checkbox = ctk.CTkCheckBox(self.checkbox_frame, text="", onvalue=1, offvalue=0,
border_width=checkbox_border, width=0,
command=self.overlay_trim_checkbox_handler)
self.trim_overlay_checkbox.place(in_=self.overlay_trim_label, relx=1.2, rely=0, anchor="nw",
bordermode="outside", )
self.trim_overlay_checkbox.configure(state="disabled", fg_color="grey")
if self.app.render_overlay: # Populating with values.
self.overlay_trim_label.configure(text_color="#DADADA")
self.trim_overlay_checkbox.configure(state="normal", fg_color="#1F6AA5")
if self.app.trim_overlay:
self.trim_overlay_checkbox.select()
sequence_code_label = ctk.CTkLabel(self.checkbox_frame, text="Sequence Code:", font=("Arial", 16), width=0)
sequence_code_label.grid(row=2, column=0, sticky='e')
self.sequence_code_checkbox = ctk.CTkCheckBox(self.checkbox_frame, text="", onvalue=1, offvalue=0, width=0,
border_width=checkbox_border,
command=self.sequence_code_checkbox_handler)
if self.app.render_sequence_code:
self.sequence_code_checkbox.select()
self.sequence_code_checkbox.grid(row=2, column=1, sticky='w', padx=(25, 0))
self.sequence_code_position_dropdown = ctk.CTkOptionMenu(self.checkbox_frame,
values=["Top-Left", "Top-Right", "Bottom-Left",
"Bottom-Right"],
width=20,
command=self.sequence_code_position_dropdown_handler,
height=25)
self.sequence_code_position_dropdown.place(in_=self.sequence_code_checkbox, relx=2.7, rely=0.5, anchor="center",
bordermode="outside", )
if self.app.render_sequence_code:
current_position = self.app.sequence_code_render_position
if current_position == "ne":
self.sequence_code_position_dropdown.set("Top-Right")
if current_position == "sw":
self.sequence_code_position_dropdown.set("Bottom-Left")
if current_position == "se":
self.sequence_code_position_dropdown.set("Bottom-Left")
else:
self.sequence_code_position_dropdown.set("Top-Left")
else:
self.sequence_code_position_dropdown.configure(state="disabled")
anti_alias_label = ctk.CTkLabel(self.checkbox_frame, text="Enable Anti-alias:", font=("Arial", 16))
anti_alias_label.grid(row=3, column=0, sticky='e')
self.anti_alias_checkbox = ctk.CTkCheckBox(self.checkbox_frame, text="", onvalue=1, offvalue=0,
border_width=checkbox_border,
command=self.anti_alias_checkbox_handler)
if self.app.anti_alias_output:
self.anti_alias_checkbox.select()
self.anti_alias_checkbox.grid(row=3, column=1, sticky='w', padx=(25, 0))
if self.is_batch:
include_blank_label = ctk.CTkLabel(self.checkbox_frame, text="Include Blanks:", font=("Arial", 16))
include_blank_label.grid(row=4, column=0, sticky='e')
self.include_blanks_checkbox = ctk.CTkCheckBox(self.checkbox_frame, text="", onvalue=1, offvalue=0,
border_width=checkbox_border,
command=self.include_blanks_checkbox_handler)
if self.app.include_blanks:
self.include_blanks_checkbox.select()
self.include_blanks_checkbox.grid(row=4, column=1, sticky='w', padx=(25, 0))
jpeg_quality_label = ctk.CTkLabel(self.checkbox_frame, text="JPEG Quality:", font=("Arial", 16))
jpeg_quality_label.grid(row=5, column=0, sticky='e')
self.jpeg_quality_slider = ctk.CTkSlider(self.checkbox_frame, from_=0, to=100, width=int(width * 0.4),
command=self.jpeg_quality_slider_event_handler,
number_of_steps=100, )
self.jpeg_quality_slider.grid(row=5, column=1, sticky='w')
self.jpeg_quality_slider_value_label = ctk.CTkLabel(self.checkbox_frame, text=self.app.jpeg_quality,
font=("Arial Bold", 12))
self.jpeg_quality_slider_value_label.place(in_=self.jpeg_quality_slider, relx=1.1, rely=0.5, anchor="center",
bordermode="outside")
self.jpeg_quality_slider.set(self.app.jpeg_quality)
self.jpeg_quality_slider_event_handler(value=self.app.jpeg_quality)
png_compression_label = ctk.CTkLabel(self.checkbox_frame, text="PNG Compression:", font=("Arial", 16))
png_compression_label.grid(row=6, column=0, sticky='e')
self.png_compression_slider = ctk.CTkSlider(self.checkbox_frame, from_=0, to=9, width=int(width * 0.4),
command=self.png_compression_slider_event_handler,
number_of_steps=9, )
self.png_compression_slider.grid(row=6, column=1, sticky='w')
self.png_compression_slider.set(self.app.png_compression)
self.png_compression_slider_value_label = ctk.CTkLabel(self.checkbox_frame, text=self.app.png_compression,
font=("Arial Bold", 12))
self.png_compression_slider_value_label.place(in_=self.png_compression_slider, relx=1.1, rely=0.5,
anchor="center",
bordermode="outside")
self.png_compression_slider_event_handler(value=self.app.png_compression)
# ================Output ==================================
self.output_frame = ctk.CTkFrame(self, fg_color=main_color)
self.output_frame.columnconfigure((0, 1), weight=1)
self.output_frame.rowconfigure((0, 1, 2, 3), weight=1)
self.output_frame.grid(row=2, column=0, sticky="news", pady=(10, 0))
output_path_label = ctk.CTkLabel(self.output_frame, text="Output Path:", font=("Arial", 14))
output_path_label.grid(row=0, column=0, sticky="e")
self.output_path_btn = ctk.CTkButton(self.output_frame, text="Pick Path", command=self.pick_path,
state="normal",
width=80, height=15, font=("Arial", 15),
fg_color="#d2190d", hover_color="#6a0c06", corner_radius=5)
self.output_path_btn.grid(row=0, column=1, padx=(0, 40))
self.output_path_textbox = ctk.CTkTextbox(master=self.output_frame, width=200, height=4, corner_radius=5,
fg_color="#D9D9D9", wrap="none", font=('Arial', 15),
text_color="black", )
self.output_path_textbox.grid(column=0, row=1, columnspan=2)
self.output_path_textbox.insert("0.0", self.app.output_path)
if os.path.exists(self.app.output_path):
self.output_path_textbox.configure(fg_color="#b7ffb0") # A shade of green if file output path exists.
self.render_progressbar = ctk.CTkProgressBar(self.output_frame, orientation="horizontal", fg_color=main_color,
progress_color=main_color)
self.render_progressbar.set(0)
self.render_progressbar.grid(column=0, row=2, columnspan=2)
self.render_images_btn = ctk.CTkButton(self.output_frame, text="Render Images", command=self.start_render,
width=150, height=35, font=("Arial bold", 17),
fg_color="#3568B5", hover_color="#213D67", text_color="white")
self.render_images_btn.grid(column=0, row=3, columnspan=2, pady=(5, 10))
if not self.is_batch: # If not batch configure the render button to batch False.
self.render_images_btn.configure(text="Render Image")
self.render_images_btn.configure(command=lambda: self.start_render(batch=False))
self.protocol("WM_DELETE_WINDOW", self.kill_window)
def render_overlay_checkbox_handler(self):
"""
Called on toggling the render_overlay_checkbox.
Returns:
None
"""
if self.render_overlay_checkbox.get() == 1:
self.app.render_overlay = True
self.overlay_trim_label.configure(text_color="#DADADA")
self.trim_overlay_checkbox.configure(state="normal", fg_color="#1F6AA5")
else:
self.app.render_overlay = False
self.overlay_trim_label.configure(text_color="grey")
self.trim_overlay_checkbox.configure(state="disabled", fg_color="grey")
def overlay_trim_checkbox_handler(self):
"""
Called on toggling the trim_overlay_checkbox.
Returns:
None
"""
if self.trim_overlay_checkbox.get() == 1:
self.app.trim_overlay = True
else:
self.app.trim_overlay = False
def sequence_code_checkbox_handler(self):
if self.sequence_code_checkbox.get() == 1:
self.app.render_sequence_code = True
self.sequence_code_position_dropdown.configure(state="normal")
else:
self.app.render_sequence_code = False
self.sequence_code_position_dropdown.configure(state="disabled")
def sequence_code_position_dropdown_handler(self, choice):
choice = choice.lower()
if choice == "top-right":
self.app.sequence_code_render_position = "ne"
elif choice == "bottom-left":
self.app.sequence_code_render_position = "sw"
elif choice == "bottom-right":
self.app.sequence_code_render_position = "se"
else:
self.app.sequence_code_render_position = "nw"
def anti_alias_checkbox_handler(self):
if self.anti_alias_checkbox.get() == 1:
self.app.anti_alias_output = True
else:
self.app.anti_alias_output = False
def include_blanks_checkbox_handler(self):
if self.include_blanks_checkbox.get() == 1:
self.app.include_blanks = True
else:
self.app.include_blanks = False
def jpeg_quality_slider_event_handler(self, value):
"""
Called on updating the jpeg_quality_slider.
Returns:
None
"""
value = int(value)
self.app.jpeg_quality = value
if value == 75:
text_color = "white"
elif 75 < value <= 85:
text_color = "#BCFFD7"
elif value > 85:
text_color = "#9BFF99"
elif 75 > value >= 50:
text_color = "#FFE5B1"
elif 50 > value > 0:
text_color = "#FF8D4E"
elif value == 0:
text_color = "red"
else:
text_color = "white"
self.jpeg_quality_slider_value_label.configure(text=int(value), text_color=text_color)
def png_compression_slider_event_handler(self, value):
value = int(value)
self.app.png_compression = value
if value <= 3:
text_color = "white"
elif 3 < value <= 6:
text_color = "orange"
elif value > 6:
text_color = "red"
else:
text_color = "white"
self.png_compression_slider_value_label.configure(text_color=text_color, text=value)
def pick_path(self):
"""
Opens a filedialog.askdirectory window for the user to set a folder path for the output images.
Returns:
None
"""
self.output_path_btn.configure(state="disabled")
self.app.output_path = filedialog.askdirectory(parent=self)
# self.deiconify()
if not self.app.output_path:
self.app.output_path = "images"
# Clears the path_textbox and inserts the chosen filepath.
self.output_path_textbox.delete("0.0", "end")
self.output_path_textbox.insert("0.0", self.app.output_path)
self.output_path_btn.configure(state="normal")
def start_render(self, batch: bool = True):
"""
Starts the render process by calling the render_images method from ImageProcessor.
Args:
batch(bool):True sets the mode to batch and adds all images in queue to render.False renders the current image in view. Default True
Returns:
None
"""
self.app.output_path = self.output_path_textbox.get(0.0, "end").strip()
if os.path.exists(self.app.output_path):
self.output_path_textbox.configure(fg_color="#b7ffb0", )
self.render_progressbar.configure(progress_color="#008F39", fg_color="#6D7684") # dark green
# Using threading for rendering images to prevent GUI freeze during the processing.
threading.Thread(target=self.app.image_processor.render_images, kwargs={'batch': batch},
daemon=True).start()
else:
# On invalid folder path, change path_textbox to red.
self.output_path_textbox.configure(fg_color="#ff7575")
def update_progress_bar(self, progress: float, status: bool):
"""
Updates the render_progressbar widget on the RenderMenu window based on the files processed.
Args:
progress (float):A value between 0-1.
status (bool): True indicates a success transfer, False indicates a failed render.
Returns:
None
"""
if status == 1:
self.render_progressbar.configure(progress_color="#008F39")
self.render_progressbar.set(progress)
if progress == 1:
# on completion setting the progress bar to a bright green.