-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
179 lines (145 loc) · 6.68 KB
/
application.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
import tkinter as tk
import tkinter.filedialog as fd
import tkinter.messagebox as mb
import time
import os
from ui.menubar import Menubar
from ui.place_object_selector import PlaceObjectSelector
from ui.object_tree_view import ObjectTreeView
from ui.development_canvas import DevelopmentCanvas
from ui.attribute_editor import AttributeEditor
import ui.ui_generics as ui
from app.ui_tree import UITree
from app.objects_data import OBJECTS_DATA
from app.ui_class_generator import UIClassGenerator
class Application(tk.Tk):
# widgets
console = None
# appflow
main_frame = None
menubar = None
place_object_selector = None
object_tree_view = None
development_canvas = None
attribute_editor = None
current_ui_tree = None
def __init__(self, geometry):
super().__init__()
self.geometry(geometry)
self.init_logic()
self.init_ui()
self.console.write_info('Application init complete.')
def init_logic(self):
self.current_ui_tree = UITree(self)
self.bind('<Control-n>', self.on_new_ui)
self.bind('<Control-s>', self.on_save_ui)
self.bind('<Control-l>', self.on_load_ui)
self.bind('<Control-g>', self.on_generate_code)
self.bind('<Control-q>', self.on_exit)
def init_ui(self):
self.last_configure_time = time.time()
self.title('TKGenerator')
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.menubar = Menubar(self)
self.config(menu=self.menubar)
self.main_frame = tk.Frame(self)
self.main_frame.grid(row=0, column=0, sticky='news')
self.main_frame.rowconfigure(0, weight=1)
self.main_frame.columnconfigure(0, weight=3, uniform="x")
self.main_frame.columnconfigure(1, weight=5, uniform="x")
self.main_frame.columnconfigure(2, weight=24, uniform="x")
self.main_frame.columnconfigure(3, weight=5, uniform="x")
# console
self.console = ui.SingleLineConsole(self)
self.console.grid(column=0, row=1, sticky='news')
# place frame
self.place_object_selector = PlaceObjectSelector(self.main_frame, self)
self.place_object_selector.grid(column=0, row=0, sticky='news')
# tree frame
self.object_tree_view = ObjectTreeView(self.main_frame, self)
self.object_tree_view.grid(row=0, column=1, sticky='news')
# canvas frame
self.development_canvas = DevelopmentCanvas(self.main_frame, self)
self.development_canvas.grid(row=0, column=2, sticky='news')
self.development_canvas.rowconfigure(0, weight=1)
self.development_canvas.columnconfigure(0, weight=1)
# attribute frame
self.attribute_editor = AttributeEditor(self.main_frame, self)
self.attribute_editor.rowconfigure(0, weight=1)
self.attribute_editor.columnconfigure(0, weight=1)
self.attribute_editor.grid(row=0, column=3, sticky='news')
self.console.write_info('UI init done.')
self.ui_updated()
def ui_updated(self):
self.object_tree_view.ui_updated()
self.development_canvas.ui_updated()
def on_object_click(self, node_uid):
# decide what to do
if self.place_object_selector.selected_object is not None:
# try to place new object
node_type_to_place = self.place_object_selector.selected_object
new_node_uid = self.current_ui_tree.add_node(node_type_to_place, node_uid)
self.place_object_selector.clear_selected_object()
if new_node_uid is not None:
self.ui_updated()
self.console.write_info('Placed ' + node_type_to_place + '#' + str(new_node_uid) + ' into #' + node_uid + '.')
else:
self.console.write_error('Cannot place new object into a non container object.')
else:
# select an object from canvas
self.development_canvas.on_object_selected(node_uid)
self.attribute_editor.on_object_selected(node_uid)
self.console.write_info('Selected object #' + node_uid + '.')
def get_objects_data(self):
return OBJECTS_DATA
def get_object_attributes(self, node_uid):
return self.current_ui_tree.get_node_attributes(node_uid)
def set_object_attributes(self, node_uid, attributes):
self.current_ui_tree.set_node_attributes(node_uid, attributes)
self.ui_updated()
self.console.write_info('Set object attributes #' + node_uid + '.')
def get_object_attributes_list(self, object_type):
return OBJECTS_DATA[object_type]['attributes_list']
def on_delete_object_click(self, node_uid):
if node_uid == '0':
self.console.write_error('Root object not removable.')
else:
self.current_ui_tree.remove_node(node_uid)
self.ui_updated()
self.console.write_info('Removed object #' + node_uid + '.')
def get_current_ui_tree(self):
return self.current_ui_tree.get_all_nodes()
def on_exit(self, event):
result = mb.askyesno('Exit', 'Do you want to exit?')
if result:
self.quit()
def on_new_ui(self, event):
result = mb.askyesno('New', 'Do you want to open a new UI?')
if result:
self.current_ui_tree = UITree(self)
self.ui_updated()
def on_save_ui(self, event):
file = fd.asksaveasfile(title='Save UI as', mode='w', initialfile = 'Untitled.tkui', defaultextension=".tkui", filetypes=[('TKGen File','*.tkui')])
if file:
file.write(self.current_ui_tree.serialize())
self.console.write_info('Saved UI to ' + file.name + '.')
def on_load_ui(self, event):
file = fd.askopenfile(title='Load UI From File', filetypes=[("TKGen File","*.tkui")])
if file:
ui_data = file.read()
ret = self.current_ui_tree.deserialize(ui_data)
if ret:
self.ui_updated()
self.console.write_info('Loaded UI from ' + file.name + '.')
else:
self.console.write_error('Load UI from ' + file.name + ' failed.')
def on_generate_code(self, event):
file = fd.asksaveasfile(title='Generate class as', mode='w', initialfile = 'NewUIClass.py', defaultextension='.py', filetypes=[('Python File', '*.py')])
if file:
generator = UIClassGenerator(self.current_ui_tree)
head, tail = os.path.split(file.name)
class_name = os.path.splitext(tail)[0]
generator.set_class_name(class_name)
ret = generator.write_to_file(file)
self.console.write_info('Class generated as ' + file.name + '.')