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