-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateEntity.py
437 lines (332 loc) · 15 KB
/
CreateEntity.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
#!/usr/bin/env python
import os
import sys
from PyQt4 import QtCore, QtGui
#-------------------------------------------------------------------------------
# GLOBAL VARIABLES
#-------------------------------------------------------------------------------
# the path to the entities parent folder
ENTITY_PATH ="src/entities"
# style sheets
style_sheet_title = "font-size: 10pt; font-family: San-Serif;"
style_sheet_text = "font-size: 10pt; font-family: San-Serif;"
style_sheet_required = "font-size: 10pt; font-family: San-Serif; color: red"
style_sheet_button = "font-size: 10pt; font-family: San-Serif;"
#-------------------------------------------------------------------------------
# MAIN CLASS
#-------------------------------------------------------------------------------
class CreateEntity(QtGui.QWidget):
#---------------------------------------------------------------------------
# CONSTRUCTOR
#---------------------------------------------------------------------------
def __init__(self, parent = None):
# super call
super(CreateEntity, self).__init__()
# a map from required settings to the associated required label
self._required_map = {}
# set up important variables
self._file_path = ''
self._project_name = ''
self._sub_groups = ''
self._entity_name = ''
self._include_constructor = False
self._include_destructor = False
self._description = ''
# set up the UI components
self._init_ui()
#---------------------------------------------------------------------------
# PRIVATE METHODS
#---------------------------------------------------------------------------
# writes a line to the given file
def _write_line(self, file, text):
file.write(text + '\n')
# writes a title to the file
def _write_title(self, file, indent, text):
# divider lines
divider_line = (' ' * indent) + '//'
divider_line += '-' * (80 - (indent + 2))
# centered text
title = (' ' * indent) + '//'
centre = (80 - (len(title) + len(text) - 2)) / 2
title += ' ' * centre
title += str(text).upper()
self._write_line(file, divider_line)
self._write_line(file, title)
self._write_line(file, divider_line)
# write the entity header file to disk
def _write_entity_header(self):
# create the file name
filename = self._file_path + '.hpp'
# open the file
file = open(filename, 'wb')
# include guard
include_guard = str(self._project_name).upper()
include_guard += "_ENTITIES"
for group in str(self._sub_groups).split('/'):
if group == '' or group == ' ':
continue
include_guard += '_' + group.upper()
include_guard += '_' + str(self._entity_name).upper()
include_guard += '_H_'
self._write_line(file, '#ifndef ' + include_guard)
self._write_line(file, '# define ' + include_guard)
self._write_line(file, '')
# include entity super class
self._write_line(file, '#include "src/omicron/entity/Entity.hpp"')
self._write_line(file, '')
# class description
if self._description != '':
# sort the description so it's no longer than 80 lines
box = []
lines = str(self._description).split('\n')
for line in lines:
if (len(line) <= 76):
box.append(line + ' ')
else:
shortened_line = ''
for word in line.split(' '):
if len(shortened_line) + len(word) <= 76:
shortened_line += word + ' '
else:
box.append(shortened_line)
shortened_line = word + ' '
box.append(shortened_line)
# find the longest line
longest_line = 0
for line in box:
if len(line) > longest_line:
longest_line = len(line)
# add head and tail to the lines
temp_box = box
box = []
for line in temp_box:
box.append('| ' + line +
(' ' * (longest_line - len(line))) + '|')
# write the description
self._write_line(file, '/' + ('*' * longest_line) + '*\\')
for line in box:
self._write_line(file, line)
self._write_line(file, '\\' + ('*' * longest_line) + '*/')
# class begin
self._write_line(file,
'class ' + str(self._entity_name) + ' : public omi::Entity {')
self._write_line(file, 'public:')
self._write_line(file, '')
# constructor if specified
if self._include_constructor:
self._write_title(file, 4, 'CONSTRUCTOR')
self._write_line(file, '')
self._write_line(file, ' ' + str(self._entity_name) + '();')
self._write_line(file, '')
# destructor if specified
if self._include_destructor:
self._write_title(file, 4, 'DESTRUCTOR')
self._write_line(file, '')
self._write_line(file, ' ~' + str(self._entity_name) + '();')
self._write_line(file, '')
# public member functions
self._write_title(file, 4, 'PUBLIC MEMBER FUNCTIONS')
self._write_line(file, '')
self._write_line(file, ' /** #Override */')
self._write_line(file, ' void init();')
self._write_line(file, '')
self._write_line(file, ' /** #Override */')
self._write_line(file, ' void update();')
# class end
self._write_line(file, '};')
# include guard end
self._write_line(file, '')
self._write_line(file, '#endif')
# close the file
file.close()
# write the entity source file to disk
def _write_entity_source(self):
# create the file
filename = self._file_path + '.cpp'
# open the file
file = open(filename, 'wb')
# include header
self._write_line(file, '#include "' + str(self._entity_name) + '.hpp"')
self._write_line(file, '')
# constructor if included
if self._include_constructor:
self._write_title(file, 0, 'CONSTRUCTOR')
self._write_line(file, '')
self._write_line(file, str(self._entity_name) + '::' +
str(self._entity_name) + '()\n{')
self._write_line(file, '}')
self._write_line(file, '')
# destructor if included
if self._include_destructor:
self._write_title(file, 0, 'DESTRUCTOR')
self._write_line(file, '')
self._write_line(file, str(self._entity_name) + '::~' +
str(self._entity_name) + '()\n{')
self._write_line(file, '}')
self._write_line(file, '')
# public member functions
self._write_title(file, 0, 'PUBLIC MEMBER FUNCTIONS')
self._write_line(file, '')
self._write_line(file, 'void ' + str(self._entity_name) + '::init()\n{')
self._write_line(file, '}')
self._write_line(file, '')
self._write_line(
file, 'void ' + str(self._entity_name) + '::update()\n{')
self._write_line(file, '}')
self._write_line(file, '')
# close the file
file.close();
# write the entity files to disk
def _write_entity_files(self):
# create the file path
self._file_path = 'src/entities/' + self._sub_groups
# check the file path exists if not create it
if not os.path.exists(str(self._file_path)):
os.makedirs(str(self._file_path))
# finish making the file path
self._file_path += '/' + self._entity_name
# write the files
self._write_entity_header()
self._write_entity_source()
#------------------------------CALLBACK METHODS-----------------------------
def _project_name_callback(self, text):
self._project_name = text
def _sub_group_callback(self, text):
self._sub_groups = text
def _entity_name_callback(self, text):
self._entity_name = text
def _constructor_callback(self):
# invert the setting
self._include_constructor = not self._include_constructor
def _destructor_callback(self):
# invert the setting
self._include_destructor = not self._include_destructor
def _description_callback(self):
self._description = self.description_edit.toPlainText()
def _done_button_pressed(self):
# check to make sure the required attributes are set
fufilled = True
for t, d in self._required_map.items():
if t.text() == '':
# we've found an empty required setting
fufilled = False
# show the required dialog
d.setText('Required')
d.adjustSize()
else:
# make sure the required dialog isn't showing
d.setText('')
d.adjustSize()
# don't actually do anything since we don't have all the required data
if not fufilled:
return
# write the entities file
self._write_entity_files()
# TODO: if this fails don't close
# exit
QtGui.QApplication.quit()
def _cancel_button_pressed(self):
# exit
QtGui.QApplication.quit()
#---------------------------------UI METHODS--------------------------------
# creates a divider in the UI
def _create_divider(self):
divider = QtGui.QFrame()
divider.setFrameShape(QtGui.QFrame.HLine)
self.layout.addWidget(divider)
# creates a title in the UI
def _create_title(self, text):
title = QtGui.QLabel(self)
title.setStyleSheet(style_sheet_title)
title.setText(str(text))
title.adjustSize()
self.layout.addWidget(title)
# create a new text line setting
def _create_text_line(self, call_back, required):
text_edit = QtGui.QLineEdit(self)
text_edit.setStyleSheet(style_sheet_text)
text_edit.textChanged[str].connect(call_back)
# add the required dialog if need
if required:
# make the label
required_dialog = QtGui.QLabel(self)
required_dialog.setStyleSheet(style_sheet_required)
required_dialog.setText('Required')
required_dialog.adjustSize()
required_dialog.setText('')
required_dialog.adjustSize()
# set up a layout we can put the text box and dialog into
required_layout = QtGui.QHBoxLayout()
required_layout.addWidget(text_edit)
required_layout.addWidget(required_dialog)
# add to the main layout
self.layout.addLayout(required_layout)
# add to the map of required settings so we can check them later
self._required_map[text_edit] = required_dialog
# add just the text box the layout
else:
self.layout.addWidget(text_edit)
# creates a check box setting
def _create_check_box(self, text, call_back):
check_box = QtGui.QCheckBox(text)
check_box.setStyleSheet(style_sheet_title)
check_box.toggled.connect(call_back)
self.layout.addWidget(check_box)
# sets up all the UI components
def _init_ui(self):
# the main layout
self.layout = QtGui.QVBoxLayout()
self._create_divider()
#------------------------------PROJECT NAME-------------------------------------
self._create_title('Project Name:')
self._create_text_line(self._project_name_callback, True)
#-------------------------------SUB GROUPS------------------------------
self._create_title('Sub Groups:')
self._create_text_line(self._sub_group_callback, False)
#------------------------------ENTITY NAME------------------------------
self._create_title('Entity Name:')
self._create_text_line(self._entity_name_callback, True)
#------------------------------CONSTRUCTOR------------------------------
self._create_divider()
self._create_check_box(
'include constructor', self._constructor_callback)
#-------------------------------DESTRUCTOR------------------------------
self._create_check_box(
'include destructor', self._destructor_callback)
self._create_divider()
#------------------------------DESCRIPTION------------------------------
self._create_title('Description:')
self.description_edit = QtGui.QTextEdit(self)
self.description_edit.setStyleSheet(style_sheet_text)
self.description_edit.textChanged.connect(self._description_callback)
self.layout.addWidget(self.description_edit)
#------------------------------DONE BUTTON------------------------------
done_button = QtGui.QPushButton("Done", self)
done_button.setStyleSheet(style_sheet_button)
done_button.clicked.connect(self._done_button_pressed)
#-----------------------------CANCEL BUTTON-----------------------------
cancel_button = QtGui.QPushButton("Cancel", self)
cancel_button.setStyleSheet(style_sheet_button)
cancel_button.clicked.connect(self._cancel_button_pressed)
#------------------------------FINAL SET UP-----------------------------
# do the final set up for the buttons
button_layout = QtGui.QHBoxLayout()
button_layout.addStretch(1)
button_layout.addWidget(done_button)
button_layout.addWidget(cancel_button)
button_layout.addStretch(1)
self.layout.addStretch(1)
self.layout.addLayout(button_layout)
# set the layout on the window
self.setLayout(self.layout)
# set up and open the window
self.setGeometry(200, 100, 600, 600)
self.setWindowTitle('Create Omicron Entity')
self.show()
#-------------------------------------------------------------------------------
# MAIN FUNCTION
#-------------------------------------------------------------------------------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
entityCreator = CreateEntity()
sys.exit(app.exec_())