3
3
"""
4
4
# pylint: disable=import-error, too-many-instance-attributes, too-many-statements
5
5
import os
6
+ from collections .abc import Callable
7
+ from importlib import import_module
6
8
from tkinter import ttk
9
+
7
10
from PIL .ImageTk import PhotoImage
8
11
from PIL import Image
9
12
from i18n import t
10
13
import customtkinter as CTk
11
14
from CTkMenuBar import CTkMenuBar
12
15
from CTkMessagebox import CTkMessagebox
13
- import pywinstyles
16
+
14
17
from main import init_localization , get_resource_path , execute_download , get_flatc_path , \
15
18
get_binary_tuples
16
19
from flatc_funcs import deserialize
@@ -56,14 +59,20 @@ def __init__(self):
56
59
columns = ("file" , "file_size" ), show = "headings" )
57
60
self .src_binaries_table .heading ("file" , text = t ("frontend.source_file_loc" ))
58
61
self .src_binaries_table .heading ("file_size" , text = t ("frontend.file_size" ))
59
- self .src_binaries_table .pack (fill = CTk .BOTH , expand = True , padx = 10 , pady = 10 )
60
- pywinstyles .apply_dnd (self .src_binaries_table .winfo_id (), self .on_binary_dropped )
62
+ self .src_binaries_frame .grid_rowconfigure (0 , weight = 1 )
63
+ self .src_binaries_frame .grid_columnconfigure (0 , weight = 1 )
64
+ self .src_binaries_frame .grid_propagate (False )
65
+ self .src_binaries_table .grid (row = 0 , column = 0 , padx = 10 , pady = 10 , sticky = CTk .NSEW )
66
+ self .attempt_apply_dnd (self .src_binaries_table .winfo_id (), self .on_binary_dropped )
61
67
self .src_schemas_frame = ttk .LabelFrame (self .src_files_frame ,
62
68
text = t ("frontend.source_schemas" ))
63
69
self .src_schemas_frame .grid (row = 1 , column = 0 , padx = 10 , pady = 10 , sticky = CTk .NSEW )
64
70
self .src_schemas_table = ttk .Treeview (self .src_schemas_frame , columns = "file" , show = "" )
65
- self .src_schemas_table .pack (fill = CTk .BOTH , expand = True , padx = 10 , pady = 10 )
66
- pywinstyles .apply_dnd (self .src_schemas_table .winfo_id (), self .on_schema_dropped )
71
+ self .src_schemas_frame .grid_rowconfigure (0 , weight = 1 )
72
+ self .src_schemas_frame .grid_columnconfigure (0 , weight = 1 )
73
+ self .src_schemas_frame .grid_propagate (False )
74
+ self .src_schemas_table .grid (row = 0 , column = 0 , padx = 10 , pady = 10 , sticky = CTk .NSEW )
75
+ self .attempt_apply_dnd (self .src_schemas_table .winfo_id (), self .on_schema_dropped )
67
76
self .dest_files_frame = ttk .LabelFrame (self .files_frame ,
68
77
text = t ("frontend.destination_files" ))
69
78
self .dest_files_frame .grid_rowconfigure (0 , weight = 1 )
@@ -79,9 +88,13 @@ def __init__(self):
79
88
self .dest_binaries_table .heading ("file" , text = t ("frontend.destination_file_loc" ))
80
89
self .dest_binaries_table .heading ("result" , text = t ("frontend.result" ))
81
90
self .dest_binaries_table .heading ("file_size" , text = t ("frontend.file_size" ))
82
- self .dest_binaries_table .pack (fill = CTk .BOTH , expand = True , padx = 10 , pady = 10 )
91
+ self .dest_binaries_frame .grid_rowconfigure (0 , weight = 1 )
92
+ self .dest_binaries_frame .grid_columnconfigure (0 , weight = 1 )
93
+ self .dest_binaries_frame .grid_propagate (False )
94
+ self .dest_binaries_table .grid (row = 0 , column = 0 , padx = 10 , pady = 10 , sticky = CTk .NSEW )
83
95
self .dest_options_frame = ttk .LabelFrame (self .dest_files_frame ,
84
96
text = t ("frontend.destination_options" ))
97
+ self .dest_options_frame .grid_propagate (False )
85
98
self .dest_options_frame .grid (row = 1 , column = 0 , padx = 10 , pady = 10 , sticky = CTk .NSEW )
86
99
self .bottom_menu = CTkMenuBar (self , bg_color = None )
87
100
img = Image .open (get_resource_path ("images/flatbuffers-batch-logo-clean.png" ))
@@ -90,6 +103,23 @@ def __init__(self):
90
103
self .deserialize_button .configure (command = self .deserialize_button_pressed )
91
104
self .bottom_menu .pack (side = CTk .RIGHT )
92
105
106
+ @staticmethod
107
+ def attempt_apply_dnd (widget_id : int , dnd_event : Callable ):
108
+ """
109
+ Adding files drag-and-drop functionality to widget if it's supported.
110
+ :param widget_id: Widget ID.
111
+ :param dnd_event: Callable object for drag-and-drop event.
112
+ """
113
+ if os .name != "nt" :
114
+ return
115
+ try :
116
+ mod = import_module ("pywinstyles" )
117
+ except ModuleNotFoundError :
118
+ return
119
+ fun = getattr (mod , "apply_dnd" , None )
120
+ if fun is not None :
121
+ fun (widget_id , dnd_event )
122
+
93
123
@staticmethod
94
124
def flatc_button_pressed ():
95
125
"""
@@ -118,17 +148,26 @@ def add_src_binary(self, file: str):
118
148
binary_path = os .path .abspath (file )
119
149
if os .path .splitext (binary_path )[1 ].lower () == ".json" :
120
150
return
151
+ binary_exists = self .src_binaries_table .exists (binary_path .casefold ())
121
152
src_values = (binary_path , t ("frontend.size_kb" ) % (os .path .getsize (binary_path ) / 1024 ))
122
153
output_path = os .path .splitext (binary_path )[0 ] + ".json"
123
154
if os .path .isfile (output_path ):
124
155
dest_values = (output_path , t ("frontend.file_already_exists" ),
125
156
t ("frontend.size_kb" ) % (os .path .getsize (output_path ) / 1024 ))
126
157
else :
127
158
dest_values = (output_path , "" , "" )
128
- if self .src_binaries_table .exists (binary_path .casefold ()):
129
- return
130
- self .src_binaries_table .insert ("" , "end" , binary_path .casefold (), values = src_values )
131
- self .dest_binaries_table .insert ("" , "end" , binary_path .casefold (), values = dest_values )
159
+ if binary_exists :
160
+ for j in range (2 ):
161
+ self .src_binaries_table .set (binary_path .casefold (), j , src_values [j ])
162
+ self .src_binaries_table .update ()
163
+ for j in range (3 ):
164
+ self .dest_binaries_table .set (binary_path .casefold (), j , dest_values [j ])
165
+ self .dest_binaries_table .update ()
166
+ else :
167
+ self .src_binaries_table .insert ("" , "end" , binary_path .casefold (), values = src_values )
168
+ self .src_binaries_table .update ()
169
+ self .dest_binaries_table .insert ("" , "end" , binary_path .casefold (), values = dest_values )
170
+ self .dest_binaries_table .update ()
132
171
133
172
def on_schema_dropped (self , paths : list [str ]):
134
173
"""
@@ -154,6 +193,7 @@ def add_src_schema(self, file: str):
154
193
if self .src_schemas_table .exists (schema_path .casefold ()):
155
194
return
156
195
self .src_schemas_table .insert ("" , "end" , schema_path .casefold (), values = [schema_path ])
196
+ self .src_schemas_table .update ()
157
197
158
198
def deserialize_button_pressed (self ):
159
199
"""
@@ -171,7 +211,7 @@ def deserialize_button_pressed(self):
171
211
self .dest_binaries_table .get_children ("" )]
172
212
schema_paths = [self .src_schemas_table .set (i , 0 ) for i in
173
213
self .src_schemas_table .get_children ("" )]
174
- binary_tuples = get_binary_tuples (binary_paths , schema_paths )
214
+ binary_tuples = get_binary_tuples (binary_paths , schema_paths , True )
175
215
for i , binary_tuple in enumerate (binary_tuples ):
176
216
self .deserialize_and_update_table (flatc_path , binary_tuple [1 ], binary_tuple [0 ],
177
217
output_paths [i ])
@@ -187,14 +227,19 @@ def deserialize_and_update_table(self, flatc_path: str, schema_path: str, binary
187
227
"""
188
228
json_path = deserialize (flatc_path , schema_path , binary_path , output_path , False )
189
229
for i in self .dest_binaries_table .get_children ("" ):
190
- if os .path .samefile (i , binary_path ):
191
- if json_path != "" :
192
- self .dest_binaries_table .set (i , 1 , t ("frontend.result_done" ))
193
- self .dest_binaries_table .set (i , 2 , t ("frontend.size_kb" ) % (
194
- os .path .getsize (json_path ) / 1024 ))
195
- else :
196
- self .dest_binaries_table .set (i , 1 , t ("frontend.result_error" ))
197
- self .dest_binaries_table .set (i , 2 , "" )
230
+ if not os .path .samefile (i , binary_path ):
231
+ continue
232
+ if json_path != "" :
233
+ self .dest_binaries_table .set (i , 1 , t ("frontend.result_done" ))
234
+ self .dest_binaries_table .set (i , 2 , t ("frontend.size_kb" ) % (
235
+ os .path .getsize (json_path ) / 1024 ))
236
+ elif not os .path .isfile (schema_path ):
237
+ self .dest_binaries_table .set (i , 1 , t ("frontend.schema_not_found" ))
238
+ self .dest_binaries_table .set (i , 2 , "" )
239
+ else :
240
+ self .dest_binaries_table .set (i , 1 , t ("frontend.result_error" ))
241
+ self .dest_binaries_table .set (i , 2 , "" )
242
+ self .dest_binaries_table .update ()
198
243
199
244
200
245
if __name__ == "__main__" :
0 commit comments