-
Notifications
You must be signed in to change notification settings - Fork 32
/
pp_editor.py
1091 lines (872 loc) · 42.5 KB
/
pp_editor.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
#! /usr/bin/env python
from Tkinter import *
import Tkinter as tk
import tkFileDialog
import tkMessageBox
import tkSimpleDialog
import tkFont
import csv
import os
import ConfigParser
import shutil
import json
import copy
import string
from pp_medialist import MediaList
from pp_showlist import ShowList
from pp_utils import Monitor
from pp_options import ed_options
from pp_validate import Validator
from pp_definitions import PPdefinitions
#**************************
# Pi Presents Editor Class
# *************************
class PPEditor:
# ***************************************
# INIT
# ***************************************
def __init__(self):
self.editor_issue="1.2"
# get command options
self.command_options=ed_options()
# get directory holding the code
self.pp_dir=sys.path[0]
if not os.path.exists(self.pp_dir+os.sep+"pp_editor.py"):
tkMessageBox.showwarning("Pi Presents","Bad Application Directory")
exit()
#Initialise logging
Monitor.log_path=self.pp_dir
self.mon=Monitor()
self.mon.on()
if self.command_options['debug'] == True:
Monitor.global_enable=True
else:
Monitor.global_enable=False
self.mon.log (self, "Pi Presents Editor is starting")
self.mon.log (self," OS and separator " + os.name +' ' + os.sep)
self.mon.log(self,"sys.path[0] - location of code: code "+sys.path[0])
# set up the gui
#root is the Tkinter root widget
self.root = tk.Tk()
self.root.title("Editor for Pi Presents")
# self.root.configure(background='grey')
self.root.resizable(False,False)
#define response to main window closing
self.root.protocol ("WM_DELETE_WINDOW", self.app_exit)
# bind some display fields
self.filename = tk.StringVar()
self.display_selected_track_title = tk.StringVar()
self.display_show = tk.StringVar()
# define menu
menubar = Menu(self.root)
profilemenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
profilemenu.add_command(label='Open', command = self.open_existing_profile)
profilemenu.add_command(label='Validate', command = self.validate_profile)
menubar.add_cascade(label='Profile', menu = profilemenu)
ptypemenu = Menu(profilemenu, tearoff=0, bg="grey", fg="black")
ptypemenu.add_command(label='Exhibit', command = self.new_exhibit_profile)
ptypemenu.add_command(label='Media Show', command = self.new_mediashow_profile)
ptypemenu.add_command(label='Menu', command = self.new_menu_profile)
ptypemenu.add_command(label='Presentation', command = self.new_presentation_profile)
ptypemenu.add_command(label='Interactive', command = self.new_interactive_profile)
ptypemenu.add_command(label='Live Show', command = self.new_liveshow_profile)
ptypemenu.add_command(label='Blank', command = self.new_blank_profile)
profilemenu.add_cascade(label='New from Template', menu = ptypemenu)
showmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
showmenu.add_command(label='Remove', command = self.remove_show)
showmenu.add_command(label='Edit', command = self.m_edit_show)
menubar.add_cascade(label='Show', menu = showmenu)
stypemenu = Menu(showmenu, tearoff=0, bg="grey", fg="black")
stypemenu.add_command(label='Menu', command = self.add_menu)
stypemenu.add_command(label='Mediashow', command = self.add_mediashow)
stypemenu.add_command(label='Liveshow', command = self.add_liveshow)
showmenu.add_cascade(label='Add', menu = stypemenu)
medialistmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
menubar.add_cascade(label='MediaList', menu = medialistmenu)
medialistmenu.add_command(label='Add', command = self.add_medialist)
medialistmenu.add_command(label='Remove', command = self.remove_medialist)
trackmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
trackmenu.add_command(label='Remove', command = self.remove_track)
trackmenu.add_command(label='Edit', command = self.m_edit_track)
trackmenu.add_command(label='Add from Dir', command = self.add_tracks_from_dir)
trackmenu.add_command(label='Add from File', command = self.add_track_from_file)
menubar.add_cascade(label='Track', menu = trackmenu)
typemenu = Menu(trackmenu, tearoff=0, bg="grey", fg="black")
typemenu.add_command(label='Video', command = self.new_video_track)
typemenu.add_command(label='Audio', command = self.new_audio_track)
typemenu.add_command(label='Image', command = self.new_image_track)
typemenu.add_command(label='Message', command = self.new_message_track)
typemenu.add_command(label='Show', command = self.new_show_track)
typemenu.add_command(label='Menu Background', command = self.new_menu_background_track)
typemenu.add_command(label='Child Show', command = self.new_child_show_track)
trackmenu.add_cascade(label='New', menu = typemenu)
optionsmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
menubar.add_cascade(label='Options', menu = optionsmenu)
optionsmenu.add_command(label='Edit', command = self.edit_options)
helpmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
menubar.add_cascade(label='Help', menu = helpmenu)
helpmenu.add_command(label='Help', command = self.show_help)
helpmenu.add_command(label='About', command = self.about)
self.root.config(menu=menubar)
top_frame=Frame(self.root)
top_frame.pack(side=TOP)
bottom_frame=Frame(self.root)
bottom_frame.pack(side=TOP, fill=BOTH, expand=1)
left_frame=Frame(bottom_frame, padx=5)
left_frame.pack(side=LEFT)
middle_frame=Frame(bottom_frame,padx=5)
middle_frame.pack(side=LEFT)
right_frame=Frame(bottom_frame,padx=5,pady=10)
right_frame.pack(side=LEFT)
updown_frame=Frame(bottom_frame,padx=5)
updown_frame.pack(side=LEFT)
tracks_title_frame=Frame(right_frame)
tracks_title_frame.pack(side=TOP)
tracks_label = Label(tracks_title_frame, text="Tracks in Selected Medialist")
tracks_label.pack()
tracks_frame=Frame(right_frame)
tracks_frame.pack(side=TOP)
shows_title_frame=Frame(left_frame)
shows_title_frame.pack(side=TOP)
shows_label = Label(shows_title_frame, text="Shows")
shows_label.pack()
shows_frame=Frame(left_frame)
shows_frame.pack(side=TOP)
shows_title_frame=Frame(left_frame)
shows_title_frame.pack(side=TOP)
medialists_title_frame=Frame(left_frame)
medialists_title_frame.pack(side=TOP)
medialists_label = Label(medialists_title_frame, text="Medialists")
medialists_label.pack()
medialists_frame=Frame(left_frame)
medialists_frame.pack(side=LEFT)
# define buttons
add_button = Button(middle_frame, width = 5, height = 2, text='Edit\nShow',
fg='black', command = self.m_edit_show, bg="light grey")
add_button.pack(side=RIGHT)
add_button = Button(updown_frame, width = 5, height = 1, text='Add',
fg='black', command = self.add_track_from_file, bg="light grey")
add_button.pack(side=TOP)
add_button = Button(updown_frame, width = 5, height = 1, text='Edit',
fg='black', command = self.m_edit_track, bg="light grey")
add_button.pack(side=TOP)
add_button = Button(updown_frame, width = 5, height = 1, text='Up',
fg='black', command = self.move_track_up, bg="light grey")
add_button.pack(side=TOP)
add_button = Button(updown_frame, width = 5, height = 1, text='Down',
fg='black', command = self.move_track_down, bg="light grey")
add_button.pack(side=TOP)
# define display of showlist
scrollbar = Scrollbar(shows_frame, orient=tk.VERTICAL)
self.shows_display = Listbox(shows_frame, selectmode=SINGLE, height=7,
width = 40, bg="white",activestyle=NONE,
fg="black", yscrollcommand=scrollbar.set)
scrollbar.config(command=self.shows_display.yview)
scrollbar.pack(side=RIGHT, fill=Y)
self.shows_display.pack(side=LEFT, fill=BOTH, expand=1)
self.shows_display.bind("<ButtonRelease-1>", self.e_select_show)
# define display of medialists
scrollbar = Scrollbar(medialists_frame, orient=tk.VERTICAL)
self.medialists_display = Listbox(medialists_frame, selectmode=SINGLE, height=7,
width = 40, bg="white",activestyle=NONE,
fg="black",yscrollcommand=scrollbar.set)
scrollbar.config(command=self.medialists_display.yview)
scrollbar.pack(side=RIGHT, fill=Y)
self.medialists_display.pack(side=LEFT, fill=BOTH, expand=1)
self.medialists_display.bind("<ButtonRelease-1>", self.select_medialist)
# define display of tracks
scrollbar = Scrollbar(tracks_frame, orient=tk.VERTICAL)
self.tracks_display = Listbox(tracks_frame, selectmode=SINGLE, height=15,
width = 40, bg="white",activestyle=NONE,
fg="black",yscrollcommand=scrollbar.set)
scrollbar.config(command=self.tracks_display.yview)
scrollbar.pack(side=RIGHT, fill=Y)
self.tracks_display.pack(side=LEFT,fill=BOTH, expand=1)
self.tracks_display.bind("<ButtonRelease-1>", self.e_select_track)
# initialise editor options class
self.options=Options(self.pp_dir) #creates options file in code directory if necessary
# initialise variables
self.init()
#and enter Tkinter event loop
self.root.mainloop()
# ***************************************
# INIT AND EXIT
# ***************************************
def app_exit(self):
self.root.destroy()
exit()
def init(self):
self.options.read()
self.pp_home_dir = self.options.pp_home_dir
self.initial_media_dir = self.options.initial_media_dir
self.mon.log(self,"Data Home from options is "+self.pp_home_dir)
self.mon.log(self,"Initial Media from options is "+self.initial_media_dir)
self.current_medialist=None
self.current_showlist=None
self.current_show=None
self.shows_display.delete(0,END)
self.medialists_display.delete(0,END)
self.tracks_display.delete(0,END)
# ***************************************
# MISCELLANEOUS
# ***************************************
def edit_options(self):
"""edit the options then read them from file"""
eo = OptionsDialog(self.root, self.options.options_file,'Edit Options')
if eo.result==True: self.init()
def show_help (self):
tkMessageBox.showinfo("Help",
"Read 'manual.pdf'")
def about (self):
tkMessageBox.showinfo("About","Editor for Pi Presents Profiles\n"
+"For profile version: " + self.editor_issue + "\nAuthor: Ken Thompson - KenT")
def validate_profile(self):
val =Validator()
val.validate_profile(self.root,self.pp_dir,self.pp_home_dir,self.pp_profile_dir,self.editor_issue,True)
# **************
# PROFILES
# **************
def open_existing_profile(self):
initial_dir=self.pp_home_dir+os.sep+"pp_profiles"
if os.path.exists(initial_dir)==False:
self.mon.err(self,"Home directory not found: " + initial_dir + "\n\nHint: Data Home option must end in pp_home")
return
dir_path=tkFileDialog.askdirectory(initialdir=initial_dir)
# dir_path="C:\Users\Ken\pp_home\pp_profiles\\trigger_test"
if len(dir_path)>0:
self.open_profile(dir_path)
def open_profile(self,dir_path):
showlist_file = dir_path + os.sep + "pp_showlist.json"
if os.path.exists(showlist_file)==False:
self.mon.err(self,"Not a Profile: " + dir_path + "\n\nHint: Have you opened the profile directory?")
return
self.pp_profile_dir = dir_path
self.root.title("Editor for Pi Presents - "+ self.pp_profile_dir)
if self.open_showlist(self.pp_profile_dir)==False:
self.init()
return
self.open_medialists(self.pp_profile_dir)
self.refresh_tracks_display()
def new_profile(self,profile):
d = Edit1Dialog(self.root,"New Profile","Name", "")
if d != None:
name=str(d.result)
if name=="":
tkMessageBox.showwarning(
"New Profile",
"Name is blank"
)
return
to = self.pp_home_dir + os.sep + "pp_profiles"+ os.sep + name
if os.path.exists(to)== False:
shutil.copytree(profile, to, symlinks=False, ignore=None)
self.open_profile(to)
else:
tkMessageBox.showwarning(
"New Profile",
"Profile exists\n(%s)" % to
)
def new_exhibit_profile(self):
profile = self.pp_dir+"/pp_home/pp_profiles/ppt_exhibit"
self.new_profile(profile)
def new_interactive_profile(self):
profile = self.pp_dir+"/pp_home/pp_profiles/ppt_interactive"
self.new_profile(profile)
def new_menu_profile(self):
profile = self.pp_dir+"/pp_home/pp_profiles/ppt_menu"
self.new_profile(profile)
def new_presentation_profile(self):
profile = self.pp_dir+"/pp_home/pp_profiles/ppt_presentation"
self.new_profile(profile)
def new_blank_profile(self):
profile = self.pp_dir+"/pp_home/pp_profiles/ppt_blank"
self.new_profile(profile)
def new_mediashow_profile(self):
profile = self.pp_dir+"/pp_home/pp_profiles/ppt_mediashow"
self.new_profile(profile)
def new_liveshow_profile(self):
profile = self.pp_dir+"/pp_home/pp_profiles/ppt_liveshow"
self.new_profile(profile)
def update_profile(self):
#open showlist and update its shows
ifile = open(self.pp_profile_dir + os.sep + "pp_showlist.json", 'rb')
shows = json.load(ifile)['shows']
ifile.close()
replacement_shows=self.update_shows(shows)
dic={'issue':self.editor_issue,'shows':replacement_shows}
ofile = open(self.pp_profile_dir + os.sep + "pp_showlist.json", "wb")
json.dump(dic,ofile,sort_keys=True,indent=1)
# UPDATE MEDIALISTS AND THEIR TRACKS
for file in os.listdir(self.pp_profile_dir):
if file.endswith(".json") and file<>'pp_showlist.json':
#open a medialist and update its tracks
ifile = open(self.pp_profile_dir + os.sep + file, 'rb')
tracks = json.load(ifile)['tracks']
ifile.close()
replacement_tracks=self.update_tracks(tracks)
dic={'issue':self.editor_issue,'tracks':replacement_tracks}
ofile = open(self.pp_profile_dir + os.sep + file, "wb")
json.dump(dic,ofile,sort_keys=True,indent=1)
def update_tracks(self,old_tracks):
# get correct spec from type of field
replacement_tracks=[]
for old_track in old_tracks:
track_type=old_track['type']
spec_fields=PPdefinitions.new_tracks[track_type]
# go through track and delete fields not in spec
while True:
to_delete=""
for track_field in old_track:
if track_field not in spec_fields:
to_delete=track_field
if to_delete<>"":
del old_track[to_delete]
else:
break
replacement_track=copy.deepcopy(PPdefinitions.new_tracks[track_type])
replacement_track.update(old_track)
replacement_tracks.append(copy.deepcopy(replacement_track))
return replacement_tracks
def update_shows(self,old_shows):
# get correct spec from type of field
replacement_shows=[]
for old_show in old_shows:
show_type=old_show['type']
spec_fields=PPdefinitions.new_shows[show_type]
# go through show and delete fields not in spec
while True:
to_delete=""
for show_field in old_show:
if show_field not in spec_fields:
to_delete=show_field
if to_delete<>"":
del old_show[to_delete]
else:
break
replacement_show=copy.deepcopy(PPdefinitions.new_shows[show_type])
replacement_show.update(old_show)
replacement_shows.append(copy.deepcopy(replacement_show))
return replacement_shows
# ***************************************
# Shows
# ***************************************
def open_showlist(self,dir):
showlist_file = dir + os.sep + "pp_showlist.json"
if os.path.exists(showlist_file)==False:
self.mon.err(self,"showlist file not found at " + dir + "\n\nHint: Have you opened the profile directory?")
self.app_exit()
self.current_showlist=ShowList()
self.current_showlist.open_json(showlist_file)
if float(self.current_showlist.sissue())<float(self.editor_issue):
self.update_profile()
self.mon.err(self,"Version of profile has been updated to "+self.editor_issue+", please re-open")
return False
if float(self.current_showlist.sissue())>float(self.editor_issue):
self.mon.err(self,"Version of profile is greater than editor, must exit")
self.app_exit()
self.refresh_shows_display()
return True
def save_showlist(self,dir):
if self.current_showlist<>None:
showlist_file = dir + os.sep + "pp_showlist.json"
self.current_showlist.save_list(showlist_file)
def add_mediashow(self):
self.add_show(PPdefinitions.new_shows['mediashow'])
def add_liveshow(self):
self.add_show(PPdefinitions.new_shows['liveshow'])
def add_menu(self):
self.add_show(PPdefinitions.new_shows['menu'])
def add_start(self):
self.add_show(PPdefinitions.new_shows['start'])
def add_show(self,default):
# append it to the showlist
if self.current_showlist<>None:
self.current_showlist.append(default)
self.save_showlist(self.pp_profile_dir)
self.refresh_shows_display()
def remove_show(self):
if self.current_showlist<>None and self.current_showlist.length()>0 and self.current_showlist.show_is_selected():
index= self.current_showlist.selected_show_index()
self.current_showlist.remove(index)
self.save_showlist(self.pp_profile_dir)
self.refresh_shows_display()
def show_refs(self):
_show_refs=[]
for index in range(self.current_showlist.length()):
if self.current_showlist.show(index)['show-ref']<>"start":
_show_refs.append(copy.deepcopy(self.current_showlist.show(index)['show-ref']))
return _show_refs
def refresh_shows_display(self):
self.shows_display.delete(0,self.shows_display.size())
for index in range(self.current_showlist.length()):
self.shows_display.insert(END, self.current_showlist.show(index)['title']+" ["+self.current_showlist.show(index)['show-ref']+"]")
if self.current_showlist.show_is_selected():
self.shows_display.itemconfig(self.current_showlist.selected_show_index(),fg='red')
self.shows_display.see(self.current_showlist.selected_show_index())
def e_select_show(self,event):
if self.current_showlist<>None and self.current_showlist.length()>0:
mouse_item_index=int(event.widget.curselection()[0])
self.current_showlist.select(mouse_item_index)
self.refresh_shows_display()
def m_edit_show(self):
self.edit_show(PPdefinitions.show_types,PPdefinitions.show_field_specs)
def edit_show(self,show_types,field_specs):
if self.current_showlist<>None and self.current_showlist.show_is_selected():
d=EditItemDialog(self.root,"Edit Show",self.current_showlist.selected_show(),show_types,field_specs,self.show_refs())
if d.result == True:
self.save_showlist(self.pp_profile_dir)
self.refresh_shows_display()
# ***************************************
# Medialists
# ***************************************
def open_medialists(self,dir):
self.medialists = []
for file in os.listdir(dir):
if file.endswith(".json") and file<>'pp_showlist.json':
self.medialists = self.medialists + [file]
self.medialists_display.delete(0,self.medialists_display.size())
for index in range (len(self.medialists)):
self.medialists_display.insert(END, self.medialists[index])
self.current_medialists_index=-1
self.current_medialist=None
def add_medialist(self):
d = Edit1Dialog(self.root,"Add Medialist",
"File", "")
if d != None:
name=str(d.result)
if name=="":
tkMessageBox.showwarning(
"Add medialist",
"Name is blank"
)
return
if not name.endswith(".json"):
name=name+(".json")
path = self.pp_profile_dir + os.sep + name
if os.path.exists(path)== False:
nfile = open(path,'wb')
nfile.write("{")
nfile.write("\"issue\": \""+self.editor_issue+"\",\n")
nfile.write("\"tracks\": [")
nfile.write("]")
nfile.write("}")
nfile.close()
# append it to the list
self.medialists.append(copy.deepcopy(name))
# add title to medialists display
self.medialists_display.insert(END, name)
# and set it as the selected medialist
self.refresh_medialists_display()
else:
tkMessageBox.showwarning(
"Add medialist",
"Medialist file exists\n(%s)" % path
)
def remove_medialist(self):
if self.current_medialist<>None:
if tkMessageBox.askokcancel("Delete Medialist","Delete Medialist"):
os.remove(self.pp_profile_dir+ os.sep + self.medialists[self.current_medialists_index])
self.open_medialists(self.pp_profile_dir)
self.refresh_medialists_display()
self.refresh_tracks_display()
def select_medialist(self,event):
"""
user clicks on a medialst in a profile so try and select it.
"""
# needs forgiving int for possible tkinter upgrade
if len(self.medialists)>0:
self.current_medialists_index=int(event.widget.curselection()[0])
self.current_medialist=MediaList()
if not self.current_medialist.open_list(self.pp_profile_dir+ os.sep + self.medialists[self.current_medialists_index],self.current_showlist.sissue()):
self.mon.err(self,"medialist is a different version to showlist: "+ self.medialists[self.current_medialists_index])
self.app_exit()
self.refresh_tracks_display()
self.refresh_medialists_display()
def refresh_medialists_display(self):
self.medialists_display.delete(0,len(self.medialists))
for index in range (len(self.medialists)):
self.medialists_display.insert(END, self.medialists[index])
if self.current_medialist<>None:
self.medialists_display.itemconfig(self.current_medialists_index,fg='red')
self.medialists_display.see(self.current_medialists_index)
def save_medialist(self):
basefile=self.medialists[self.current_medialists_index]
#print type(basefile)
# basefile=str(basefile)
#print type(basefile)
file = self.pp_profile_dir+ os.sep + basefile
self.current_medialist.save_list(file)
# ***************************************
# Tracks
# ***************************************
def refresh_tracks_display(self):
self.tracks_display.delete(0,self.tracks_display.size())
if self.current_medialist<>None:
for index in range(self.current_medialist.length()):
if self.current_medialist.track(index)['track-ref']<>"":
track_ref_string=" ["+self.current_medialist.track(index)['track-ref']+"]"
else:
track_ref_string=""
self.tracks_display.insert(END, self.current_medialist.track(index)['title']+track_ref_string)
if self.current_medialist.track_is_selected():
self.tracks_display.itemconfig(self.current_medialist.selected_track_index(),fg='red')
self.tracks_display.see(self.current_medialist.selected_track_index())
def e_select_track(self,event):
if self.current_medialist<>None and self.current_medialist.length()>0:
mouse_item_index=int(event.widget.curselection()[0])
self.current_medialist.select(mouse_item_index)
self.refresh_tracks_display()
def m_edit_track(self):
self.edit_track(PPdefinitions.track_types,PPdefinitions.track_field_specs)
def edit_track(self,track_types,field_specs):
if self.current_medialist<>None and self.current_medialist.track_is_selected():
d=EditItemDialog(self.root,"Edit Track",self.current_medialist.selected_track(),track_types,field_specs,self.show_refs())
if d.result == True:
self.save_medialist()
self.refresh_tracks_display()
def move_track_up(self):
if self.current_medialist<>None and self.current_medialist.track_is_selected():
self.current_medialist.move_up()
self.refresh_tracks_display()
self.save_medialist()
def move_track_down(self):
if self.current_medialist<>None and self.current_medialist.track_is_selected():
self.current_medialist.move_down()
self.refresh_tracks_display()
self.save_medialist()
def new_track(self,fields,values):
if self.current_medialist<>None:
print '\nfields ', fields
print '\nvalues ', values
new_track=copy.deepcopy(fields)
print ',\new track ',new_track
self.current_medialist.append(new_track)
print '\nbefore values ',self.current_medialist.print_list()
if values<>None:
self.current_medialist.update(self.current_medialist.length()-1,values)
self.current_medialist.select(self.current_medialist.length()-1)
self.refresh_tracks_display()
def new_message_track(self):
self.new_track(PPdefinitions.new_tracks['message'],None)
self.save_medialist()
def new_video_track(self):
self.new_track(PPdefinitions.new_tracks['video'],None)
self.save_medialist()
def new_audio_track(self):
self.new_track(PPdefinitions.new_tracks['audio'],None)
self.save_medialist()
def new_image_track(self):
self.new_track(PPdefinitions.new_tracks['image'],None)
self.save_medialist()
def new_show_track(self):
self.new_track(PPdefinitions.new_tracks['show'],None)
self.save_medialist()
def new_menu_background_track(self):
self.new_track(PPdefinitions.new_tracks['menu-background'],None)
self.save_medialist()
def new_child_show_track(self):
self.new_track(PPdefinitions.new_tracks['child-show'],None)
self.save_medialist()
def remove_track(self):
if self.current_medialist<>None and self.current_medialist.length()>0 and self.current_medialist.track_is_selected():
if tkMessageBox.askokcancel("Delete Track","Delete Track"):
index= self.current_medialist.selected_track_index()
self.current_medialist.remove(index)
self.save_medialist()
self.refresh_tracks_display()
def add_track_from_file(self):
if self.current_medialist==None: return
# print "initial directory ", self.options.initial_media_dir
files_path=tkFileDialog.askopenfilename(initialdir=self.options.initial_media_dir, multiple=True)
# fix for tkinter bug
files_path = self.root.tk.splitlist(files_path)
for file_path in files_path:
file_path=os.path.normpath(file_path)
# print "file path ", file_path
self.add_track(file_path)
self.save_medialist()
def add_tracks_from_dir(self):
if self.current_medialist==None: return
image_specs =[
PPdefinitions.IMAGE_FILES,
PPdefinitions.VIDEO_FILES,
PPdefinitions.AUDIO_FILES,
('All files', '*')] #last one is ignored in finding files
# in directory, for dialog box only
directory=tkFileDialog.askdirectory(initialdir=self.options.initial_media_dir)
# deal with tuple returned on Cancel
if len(directory)==0: return
# make list of exts we recognise
exts = []
for image_spec in image_specs[:-1]:
image_list=image_spec[1:]
for ext in image_list:
exts.append(copy.deepcopy(ext))
for file in os.listdir(directory):
(root_file,ext_file)= os.path.splitext(file)
if ext_file.lower() in exts:
file_path=directory+os.sep+file
#print "file path before ", file_path
file_path=os.path.normpath(file_path)
#print "file path after ", file_path
self.add_track(file_path)
self.save_medialist()
def add_track(self,afile):
relpath = os.path.relpath(afile,self.pp_home_dir)
#print "relative path ",relpath
common = os.path.commonprefix([afile,self.pp_home_dir])
#print "common ",common
if common.endswith("pp_home") == False:
location = afile
else:
location = "+" + os.sep + relpath
location = string.replace(location,'\\','/')
#print "location ",location
(root,title)=os.path.split(afile)
(root,ext)= os.path.splitext(afile)
if ext.lower() in PPdefinitions.IMAGE_FILES:
self.new_track(PPdefinitions.new_tracks['image'],{'title':title,'track-ref':'','location':location,'duration':'','transition':'cut'})
elif ext.lower() in PPdefinitions.VIDEO_FILES:
self.new_track(PPdefinitions.new_tracks['video'],{'title':title,'track-ref':'','location':location,'omx-audio':''})
elif ext.lower() in PPdefinitions.AUDIO_FILES:
self.new_track(PPdefinitions.new_tracks['audio'],{'title':title,'track-ref':'','location':location,'mplayer-audio':'','audiospeaker':''})
else:
self.mon.err(self,afile + " - file extension not recognised")
# *************************************
# EDIT 1 DIALOG CLASS
# ************************************
class Edit1Dialog(tkSimpleDialog.Dialog):
def __init__(self, parent, title, label, default):
#save the extra args to instance variables
self.label_1 = label
self.default_1 = default
#and call the base class _init_which uses the args in body
tkSimpleDialog.Dialog.__init__(self, parent, title)
def body(self, master):
Label(master, text=self.label_1).grid(row=0)
self.field1 = Entry(master)
self.field1.grid(row=0, column=1)
self.field1.insert(0,self.default_1)
return self.field1 # initial focus on title
def apply(self):
self.result= self.field1.get()
return self.result
# ***************************************
# pp_editor OPTIONS CLASS
# ***************************************
class Options:
def __init__(self,app_dir):
# define options for Editor
self.pp_home_dir ="" #home directory containing profile to be edited.
self.initial_media_dir ="" #initial directory for open playlist
self.debug = False # print debug information to terminal
# create an options file if necessary
self.options_file = app_dir+os.sep+'pp_editor.cfg'
if not os.path.exists(self.options_file):
self.create()
def read(self):
"""reads options from options file to interface"""
config=ConfigParser.ConfigParser()
config.read(self.options_file)
self.pp_home_dir =config.get('config','home',0)
self.initial_media_dir =config.get('config','media',0)
def create(self):
config=ConfigParser.ConfigParser()
config.add_section('config')
if os.name=='nt':
config.set('config','home',os.path.expanduser('~')+'\pp_home')
config.set('config','media',os.path.expanduser('~'))
else:
config.set('config','home',os.path.expanduser('~')+'/pp_home')
config.set('config','media',os.path.expanduser('~'))
# config.set('config','home','/home/pi/pp_home')
# config.set('config','media','/home/pi')
with open(self.options_file, 'wb') as config_file:
config.write(config_file)
# *************************************
# PP_EDITOR OPTIONS DIALOG CLASS
# ************************************
class OptionsDialog(tkSimpleDialog.Dialog):
def __init__(self, parent, options_file, title=None, ):
# instantiate the subclass attributes
self.options_file=options_file
# init the super class
tkSimpleDialog.Dialog.__init__(self, parent, title)
def body(self, master):
self.result=False
config=ConfigParser.ConfigParser()
config.read(self.options_file)
Label(master, text="").grid(row=20, sticky=W)
Label(master, text="Pi Presents Data Home:").grid(row=21, sticky=W)
self.e_home = Entry(master,width=80)
self.e_home.grid(row=22)
self.e_home.insert(0,config.get('config','home',0))
Label(master, text="").grid(row=30, sticky=W)
Label(master, text="Inital directory for media:").grid(row=31, sticky=W)
self.e_media = Entry(master,width=80)
self.e_media.grid(row=32)
self.e_media.insert(0,config.get('config','media',0))
return None # no initial focus
def validate(self):
if os.path.exists(self.e_home.get())== False:
tkMessageBox.showwarning("Pi Presents Editor","Data Home not found")
return 0
if os.path.exists(self.e_media.get())== False:
tkMessageBox.showwarning("Pi Presents Editor","Media Directory not found")
return 0
return 1
def apply(self):
self.save_options()
self.result=True
def save_options(self):
""" save the output of the options edit dialog to file"""
config=ConfigParser.ConfigParser()
config.add_section('config')
config.set('config','home',self.e_home.get())
config.set('config','media',self.e_media.get())
with open(self.options_file, 'wb') as optionsfile:
config.write(optionsfile)
# *************************************
# EDIT SHOW AND TRACK CONTENT
# ************************************
class EditItemDialog(tkSimpleDialog.Dialog):
def __init__(self, parent, title, tp, track_types,field_specs,show_refs):
self.mon=Monitor()
self.mon.on()
#save the extra arg to instance variable
self.tp = tp # dictionary - the track parameters to be edited
self.track_types= track_types
self.field_specs=field_specs
self.show_refs=show_refs
self.show_refs.append('')
# list of stringvars from which to get edited values
self.entries=[]
#and call the base class _init_which calls body immeadiately and apply on OK pressed
tkSimpleDialog.Dialog.__init__(self, parent, title)
def body(self,root):
self.canvas = Canvas(root, borderwidth=1)
self.frame = Frame(self.canvas,relief=RIDGE,borderwidth=2,padx=5,pady=5)
self.vsb = Scrollbar(root, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.canvas.pack(side="left", fill="both", expand=True)
self.canvas.create_window((4,4), window=self.frame, anchor="nw",
tags="self.frame")
self.frame.bind("<Configure>", self.OnFrameConfigure)
self.body_fields(self.frame)
def OnFrameConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
dimensions=self.canvas.bbox("all")
fields_height=dimensions[3]
max_height=500
height=min([fields_height,max_height])
self.canvas.configure(scrollregion=dimensions,width=dimensions[2],height=height)
def body_fields(self, master):
#Label(master,text='Field', anchor=W).grid(row=0,column=0)
#Label(master,text='Value', anchor=W).grid(row=0,column=1)
# get fields for this type of track }
track_fields=self.track_types[self.tp['type']]
# populate the dialog box
row=1
self.fields=[]
self.entries=[]
for field in track_fields:
values=[]
if self.field_specs[field]['shape']in("option-menu",'spinbox'):
if self.field_specs[field]['param']in ('sub-show','start-show','controlled-show'):
values=self.show_refs
else:
values=self.field_specs[field]['values']
else:
values=[]
obj=self.make_entry(master,self.field_specs[field],row,values)
if obj<>None: self.fields.append(obj)
row +=1
return None # No initial focus
def buttonbox(self):
'''add modified button box.
override standard one to get rid of key bindings which cause trouble with text widget
'''
box = Frame(self)
w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
w.pack(side=LEFT, padx=5, pady=5)
w = Button(box, text="Cancel", width=10, command=self.cancel)
w.pack(side=LEFT, padx=5, pady=5)
#self.bind("<Return>", self.ok)
#self.bind("<Escape>", self.cancel)