-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuserWindow.py
511 lines (441 loc) · 16.1 KB
/
userWindow.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
from Tkinter import *
from scroll import VerticalScrolledFrame
import webbrowser
import tkSimpleDialog,tkFileDialog
from html_source import *
from tkColorChooser import askcolor
from addComponents import *
import pickle
"""*[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]"""
shapeObjArr = []
shapeObjCount=0
shapeobAr = []
shape="rectangle"
fo = open("button.html", "wb+")
flag = 0
prev_x = 0
prev_y = 0
shapeob = object
operation = "draw"
victim = 0
move_prev_x = 0
move_prev_y = 0
event_x = 0
event_y = 0
nest_vict = 0
button_text = "none"
""""""
check_to_replace=""
root = Tk()
root.minsize(1300, 700)
root.maxsize(1300, 700)
root.title("User Window")
nav_menu_opt=""
"""*"""
def draw_rectangle():
global operation, shape
operation = "draw"
shape = "rectangle"
def draw_circle():
global operation, shape
operation = "draw"
shape = "circle"
print "circle pressed"
def draw_line():
global operation, shape
operation = "draw"
shape = "line"
print "line pressed"
def onLeftDrag(event):
global flag,prev_x,prev_y, shape, shapeob, operation, victim, move_prev_x, move_prev_y, event_x, event_y, nest_vict
if operation == "draw":
"""***"""
# Left drag started
if flag == 0:
# save initial coordinates
print "Shape: ", shape
prev_x = event.x
prev_y = event.y
flag = 1
else:
# trace cursor and draw shape using current and initial coordinates
co = prev_x, prev_y, event.x, event.y
# for nested divs
event_x = prev_x
event_y = prev_y
nest_vict = findObject()
# delete previous shape
w.delete(shapeob)
if shape == "rectangle":
shapeob = w.create_rectangle(co, outline="black")
elif shape == "circle":
shapeob = w.create_oval(co, outline="black")
elif shape == "line":
shapeob = w.create_line(co, fill="black")
elif operation == "resize":
co = shapeObjArr[victim][0], shapeObjArr[victim][1], event.x, event.y
shapeObjArr[victim] = co
if flag != 0:
w.delete(shapeob)
else:
print "resize"
print "selected: ", shapeobAr[victim]
w.delete(shapeobAr[victim])
if shape == "rectangle":
flag = 1
shapeob = w.create_rectangle(co, outline = "black")
elif shape == "circle":
flag = 1
shapeob = w.create_oval(co, outline = "black")
elif shape == "line":
flag = 1
shapeob = w.create_line(co, fill = "black")
elif operation == "move":
if flag == 0:
print "move"
move_prev_x = event.x
move_prev_y = event.y
flag = 1
w.delete(shapeobAr[victim])
else:
x_offset = event.x - move_prev_x
y_offset = event.y - move_prev_y
move_prev_x = event.x
move_prev_y = event.y
lst = list(shapeObjArr[victim])
lst[0] += x_offset
lst[2] += x_offset
lst[1] += y_offset
lst[3] += y_offset
co = lst
shapeObjArr[victim] = lst
w.delete(shapeob)
if shape == "rectangle":
shapeob = w.create_rectangle(co, outline="black")
elif shape == "circle":
shapeob = w.create_oval(co, outline="black")
elif shape == "line":
shapeob = w.create_line(co, fill ="black")
def releaseLeftDrag(event):
global flag,prev_x,prev_y,fo,shapeObjCount,shapeObjArr, shapeob, operation, victim,nav_menu_opt, nest_vict
flag = 0
if operation == "resize":
co = shapeObjArr[victim]
nm = shapeobAr[victim]
cds = w.coords(nm)
coords = shapeObjArr[victim][0], shapeObjArr[victim][1], event.x, event.y
resizeInHtml(victim, coords)
if operation == "move":
co = shapeObjArr[victim]
coords = shapeObjArr[victim][0], shapeObjArr[victim][1], event.x, event.y
moveInHtml(victim,coords)
else:
co = prev_x, prev_y, event.x, event.y
print "shapeob: ", shapeob
if operation != "draw":
shapeObjArr[victim] = co
shapeobAr[victim] = shapeob
operation = "draw"
else:
print "objCount: ", shapeObjCount
shapeObjArr.append(co)
shapeobAr.append(shapeob)
print "saved obj: ", shapeobAr[shapeObjCount]
# print("in release left drag")
"""if (nav_menu_opt == "table"):
drawTable(co)"""
# print("in release left drag")
if(nest_vict==-1):
parObjCo=0,0,0,0
else:
parObjCo=shapeObjArr[nest_vict]
write_to_html(nest_vict, "button", co, shapeObjCount,shape,parObjCo)
# webbrowser.open_new_tab("index.html")
shapeObjCount += 1
print "Coordinates: ", co
over_button = button_text = "none"
shapeob = 0
"""Root functions called when an option from popup is selected"""
# Resize
def resize_root():
# get event
global event_x, event_y, operation, victim
victim = findObject()
operation = "resize"
print "victim: ", victim
# Move
def move_root():
global event_x, event_y, operation, victim
victim = findObject()
operation = "move"
print "victim: ", victim
# Delete
def remove_root():
print "remove"
global event_x, event_y, operation, victim,shapeObjCount
victim = findObject()
delete(victim)
nm = shapeobAr[victim]
w.delete(nm)
shapeObjArr.remove(shapeObjArr[victim])
shapeobAr.remove(shapeobAr[victim])
shapeObjCount-=1
over_button = "none"
def addText():
global check_to_replace
#name of button
button_text = tkSimpleDialog.askstring("Input Text","Enter Text")
print button_text;
victim = findObject()
#size of text delete(victim)
text_size = tkSimpleDialog.askinteger("Input Text","Size of text")
text_family = tkSimpleDialog.askstring("Input Text","font family")
text_color = tkSimpleDialog.askstring("Input Text", "Text color")
# text_family = tkSimpleDialog.Radiobutton.
# print var
search_text1="font-size:;"
rep_text1="\t\t\tfont-size: "+str(text_size)+"px;\n"
search_text2="font-family:;"
rep_text2="\t\t\tfont-family: "+text_family+";\n"
search_text3 = "color:;"
rep_text3 = "\t\t\tcolor: " + text_color + ";\n"
w.create_text((shapeObjArr[victim][0] + shapeObjArr[victim][2]) / 2, shapeObjArr[victim][1] + 10, fill=text_color,text=button_text)
alterCSSFile(victim, search_text1, rep_text1, check_to_replace)
alterCSSFile(victim, search_text2, rep_text2, check_to_replace)
alterCSSFile(victim, search_text3, rep_text3, check_to_replace)
#print "%"+check_to_replace
if(check_to_replace=="anchor"):
replace_text(victim, "<!--anchor text-->", button_text)
elif (check_to_replace == "paragraph"):
replace_text(victim, "<!--paragraph text-->", button_text)
def addImage():
print "hoy na be"
global over_button
root.filename = tkFileDialog.askopenfilename(initialdir="/", title="Select file",
filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*")))
print (root.filename)
victim = findObject()
width = shapeObjArr[victim][2] - shapeObjArr[victim][0];
height = shapeObjArr[victim][3] - shapeObjArr[victim][1];
#addImageTag(victim, root.filename)
addTag(victim, shapeObjArr[victim], "img", -1)
addPath(victim, root.filename, height, width, "img", -1)
over_button = "image"
def addLink():
global over_button
root.filename = tkFileDialog.askopenfilename(initialdir="/", title="Select file",
filetypes=(("jpeg files", "*.jpg"),("all files", "*.*")))
print (root.filename)
victim = findObject()
addLinkToAnchorTag(victim, root.filename)
def addColor():
victim = findObject()
color = askcolor()
find = "background-color:;"
rep_color = "\t\t\t\tbackground-color: "+color[1]+";\n"
alterCSSFile(victim,find,rep_color,"")
ob = shapeobAr[victim]
w.itemconfig(ob, fill=color[1])
def addButton():
global check_to_replace
check_to_replace="anchor"
print 'button'
victim=findObject()
addTag(victim,shapeObjArr[victim],"anchor", -1)
def addParagraph():
global check_to_replace
check_to_replace = "paragraph"
print 'paragraph'
victim=findObject()
addTag(victim,shapeObjArr[victim],"paragraph", -1)
def addTable():
victim = findObject()
drawTable(shapeObjArr[victim])
def addNestedDiv():
print "nested div"
def readLogFile():
global shapeObjCount, shapeobAr, shapeObjArr
f = open("log.txt", mode="r")
for line in f:
print "From logs: "+line
endParanthesis=line.find(')')
if(endParanthesis==-1):
endParanthesis = line.find(']')
co= map(int, line[1:endParanthesis].split(", "))
fillS=line.find("fill:")
fillE=line.find('\n')
fillColor=line[fillS+6:fillE]
obj=w.create_rectangle(co,fill=fillColor)
shapeobAr.append(obj)
shapeObjArr.append(co)
shapeObjCount+=1
def createLogFile():
f=open("log.txt","w")
i=0
for x in shapeObjArr:
print "Into Logs: "+str(x)
f.write(str(x)+" fill: "+w.itemcget(shapeobAr[i],"fill")+"\n")
i+=1
f.close()
root.destroy()
# create a menu
popup = Menu(root, tearoff=0)
popup.add_command(label="Resize", command=resize_root)
popup.add_command(label="Move", command=move_root)
popup.add_command(label="Remove", command=remove_root)
popup.add_command(label="Image" ,command=addImage)
popup.add_command(label="Text",command=addText)
popup.add_command(label="Link",command=addLink)
popup.add_command(label="Color",command=addColor)
def key(event):
print "pressed", repr(event.char)
prev_x = 0
prev_y = 0
new_x = 0
new_y = 0
flag1 = 0
# return id of object from shapeObjArr[]
def findObject():
# x=event.x
# y=event.y
x = event_x
y = event_y
area = 0
finalObject = -1
# print "in find object"
for i in range(shapeObjCount):
nm=shapeObjArr[i]
# cds= C.coords(nm)
cds = nm
# print 'find= X=%s Y=%s Bound0=%s Bound1=%s Bound2=%s Bound3=%s' % (x, y, cds[0], cds[1], cds[2], cds[3])
if x >= cds[0] and x <= cds[2] and y >= cds[1] and y <= cds[3]:
# resize(shapeObjArr[i])
if area == 0 or area > (abs(cds[2]-cds[0]) * abs(cds[3]-cds[1])):
area = abs(cds[2]-cds[0]) * abs(cds[3]-cds[1])
finalObject = i
#print("heree")
#return i
#print "not this one"
return finalObject
def do_popup(event):
global popup
global event_x, event_y
event_x = event.x
event_y = event.y
# display the popup menu
try:
#print 'event= X=%s Y=%s' % (event.x, event.y)
popup.tk_popup(event.x_root, event.y_root, 0)
# findObject(event.x, event.y)
finally:
# make sure to release the grab (Tk 8.0a1 only)
popup.grab_release()
#C.unbind("<Button-3>", do_popup)
def run_html():
webbrowser.open_new_tab("index.html")
""""""
shapeframe = Frame(root, height="100")
optionframe = Frame(root, bg="pink", height="400", width="300")
gridframe = Frame(root, bg="white", height="400", width="1000")
shapeframe.pack(fill = X)
optionframe.pack(side = LEFT, fill = BOTH)
gridframe.pack(side = LEFT, fill = BOTH)
rectangle = PhotoImage(file="images/paintrect.gif")
circle= PhotoImage(file="images/paintcircle.gif")
line = PhotoImage(file="images/paintline.gif")
runb = PhotoImage(file="images/paintrun.gif")
topbtn1 = Button(shapeframe, text="Rectangle", height="5",width="30", command=draw_rectangle)
topbtn1.config(image = rectangle, height="80",width="200")
topbtn2 = Button(shapeframe, text="Circle", height="5",width="30", command=draw_circle)
topbtn2.config(image = circle, height="80",width="200")
topbtn3 = Button(shapeframe, text="Line", height="5",width="30", command=draw_line)
topbtn3.config(image = line, height="80",width="200")
topbtn4 = Button(shapeframe, text="Shape 4", height="5",width="30")
topbtn5 = Button(shapeframe, text="Shape 5", fg="black", height = "5", width = "30")
topbtn6 = Button(shapeframe, text="Run", height = "5", width = "30", command=run_html)
topbtn6.config(image = runb, height="80",width="200")
topbtn1.grid(row=0, column=0)
topbtn2.grid(row=0, column=1)
topbtn3.grid(row=0, column=2)
topbtn4.grid(row=0, column=3)
topbtn5.grid(row=0, column=4)
topbtn6.grid(row=0, column=5)
'''topbtn1.pack()
'''
lefttext1 = Label(optionframe, text="Options Menu")
leftbtn1 = Button(optionframe, text="Option 1", fg="black", height = "8", width = "25")
leftbtn2 = Button(optionframe, text="Option 2", fg="black", height = "8", width = "25")
leftbtn3 = Button(optionframe, text="Option 3", fg="black", height = "8", width = "25")
leftbtn4 = Button(optionframe, text="Option 4", fg="black", height = "8", width = "25")
leftbtn5 = Button(optionframe, text="Option 5", fg="black", height = "8", width = "25")
leftbtn6 = Button(optionframe, text="Option 6", fg="black", height = "8", width = "25")
leftbtn7 = Button(optionframe, text="Option 7", fg="black", height = "8", width = "25")
scframe1 = VerticalScrolledFrame(gridframe)
scframe1.pack()
canvas_width = 1000
canvas_height = 1600
w = Canvas(scframe1.interior, width=canvas_width, height=canvas_height, bg="white")
w.bind('<B1-Motion>', onLeftDrag)
w.bind('<ButtonRelease-1>', releaseLeftDrag)
w.bind("<Button-3>", do_popup)
#makes horizontal lines
for height in range(30, 1600, 10):
w.create_line(40, height, canvas_width-10, height, fill="#bbbbbb")
#makes vertical lines
for width in range(40, 1000, 10):
w.create_line(width, 30, width, canvas_height-10, fill="#bbbbbb")
readLogFile()
###############
#root.configure(background="gray99")
scframe = VerticalScrolledFrame(optionframe)
scframe.pack()
lis = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
#for i, x in enumerate(lis):
btn = Button(scframe.interior, height=8, width=25, relief=FLAT,
bg="gray99", fg="purple3",
font="Dosis", text='Button ',
command=addButton)
para = Button(scframe.interior, height=8, width=25, relief=FLAT,
bg="gray99", fg="purple3",
font="Dosis", text='Paragraph ',
command=addParagraph)
table = Button(scframe.interior, height=8, width=25, relief=FLAT,
bg="gray99", fg="purple3",
font="Dosis", text='Table',
command=addTable)
div = Button(scframe.interior, height=8, width=25, relief=FLAT,
bg="gray99", fg="purple3",
font="Dosis", text='Div',
command=addNestedDiv)
btn.pack(padx=10, pady=5, side=TOP)
para.pack(padx=10,pady=5, side=TOP)
table.pack(padx=10, pady=5, side=TOP)
div.pack(padx=10, pady=5, side=TOP)
w.pack()
def drawTable(coords):
global shapeobAr,shapeObjArr,shapeObjCount;
numRows = tkSimpleDialog.askinteger("Input Text", "No. of rows: ")
numCols = tkSimpleDialog.askinteger("Input Text", "No. of cols: ")
height=coords[3]-coords[1]
width=coords[2]-coords[0]
hinc=height/numRows
winc=width/numCols
x=coords[0]
y=coords[1]
victim = findObject()
for i in range(0,numRows):
for j in range(0,numCols):
co=x,y,x+winc,y+hinc
write_to_html(victim, "button",co,shapeObjCount,"rectangle",coords)
obj = w.create_rectangle(co)
shapeobAr.append(obj)
shapeObjArr.append(co)
shapeObjCount+=1
x+=winc
x=coords[0]
y+=hinc
def openlink(i):
global nav_menu_opt
print(lis[i])
root.protocol("WM_DELETE_WINDOW", createLogFile)
root.mainloop()