e01d4569d80bdb199e6dcdbdb55cf4ab60cd3cbd
[hooke.git] / hooke / ui / gui / hookeplaylist.py
1 # Copyright
2
3 #import os
4 #import os.path
5 import wx
6 #import xml.dom.minidom
7
8 class Playlists(wx.Panel):
9
10     def __init__(self, parent):
11         # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
12         wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS|wx.NO_BORDER, size=(160, 200))
13
14         self.PlaylistsTree = wx.TreeCtrl(self, -1, wx.Point(0, 0), wx.Size(160, 250), wx.TR_DEFAULT_STYLE | wx.NO_BORDER | wx.TR_HIDE_ROOT)
15         imglist = wx.ImageList(16, 16, True, 2)
16         imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, wx.Size(16, 16)))
17         imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, wx.Size(16, 16)))
18         self.PlaylistsTree.AssignImageList(imglist)
19         self.PlaylistsTree.AddRoot('Playlists', 0)
20         self.PlaylistsTree.Bind(wx.EVT_RIGHT_DOWN , self.OnContextMenu)
21
22         self.Playlists = {}
23
24         sizer = wx.BoxSizer(wx.VERTICAL)
25         sizer.Add(self.PlaylistsTree, 1, wx.EXPAND)
26         self.SetSizer(sizer)
27         sizer.Fit(self)
28
29     #def add_playlist(self, files=[], name='Untitled'):
30         ##TODO: change cursor or progressbar (maybe in statusbar)
31         ##self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
32         #if files:
33             #playlist = hookeplaylist.Playlist(self.drivers)
34             #for item in files:
35                 #playlist.append_curve_by_path(item)
36         #if playlist.count > 0:
37             #playlist_name = name
38             #count = 1
39             #while playlist_name in self.Playlists:
40                 #playlist_name = ''.join([name, str(count)])
41                 #count += 1
42             #playlist.name = playlist_name
43             #playlist.reset()
44             #self.AddToPlaylists(playlist)
45
46     #def FilterPlaylist(self, curves_to_keep=[]):
47         #playlist_active = self.GetActivePlaylist()
48         #playlist_new = Playlist(self.drivers)
49         #for curve_index in curves_to_keep:
50             #playlist_new.curves.append(playlist_active.curves[curve_index])
51         #return playlist_new
52
53     def GetActivePlaylist(self):
54         playlist_name = self.GetActivePlaylistName()
55         if playlist_name in self.playlists:
56             return self.playlists[playlist_name][0]
57         else:
58             return None
59
60     def GetActivePlaylistName(self):
61         #get the selected item from the tree
62         selected_item = self.PlaylistsTree.GetSelection()
63         #test if a playlist or a curve was double-clicked
64         if self.PlaylistsTree.ItemHasChildren(selected_item):
65             playlist_item = selected_item
66         else:
67             #get the name of the playlist
68             playlist_item = self.PlaylistsTree.GetItemParent(selected_item)
69         #now we have a playlist
70         return self.PlaylistsTree.GetItemText(playlist_item)
71
72     def OnContextMenu(self, event):
73         hit_item, hit_flags = self.PlaylistsTree.HitTest(event.GetPosition())
74         if (hit_flags & wx.TREE_HITTEST_ONITEM) != 0:
75             self.PlaylistsTree.SelectItem(hit_item)
76             # only do this part the first time so the events are only bound once
77             #
78             # Yet another alternate way to do IDs. Some prefer them up top to
79             # avoid clutter, some prefer them close to the object of interest
80             # for clarity.
81             if not hasattr(self, 'ID_popupAdd'):
82                 self.ID_popupAdd = wx.NewId()
83                 self.ID_popupClose = wx.NewId()
84                 self.Bind(wx.EVT_MENU, self.OnPopupAdd, id=self.ID_popupAdd)
85                 self.Bind(wx.EVT_MENU, self.OnPopupClose, id=self.ID_popupClose)
86             # make a menu
87             menu = wx.Menu()
88             items = [['Add', self.ID_popupAdd] , ['Close', self.ID_popupClose]]
89             for item in items:
90                 menu.Append(item[1], item[0])
91             # Popup the menu.  If an item is selected then its handler
92             # will be called before PopupMenu returns.
93             self.PopupMenu(menu)
94             menu.Destroy()
95
96     def OnPopupAdd(self, event):
97         pass
98
99     def OnPopupClose(self, event):
100         item = self.PlaylistsTree.GetSelection()
101         if self.PlaylistsTree.ItemHasChildren(item):
102             playlist_name = self.PlaylistsTree.GetItemText(item)
103             self.Parent.DeletePlotPage(playlist_name)
104             #del self.Playlists[playlist_name]
105             #TODO: delete playlist, close notebook tab
106             #self.Parent.AddToPlaylists()
107         else:
108             pass