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