Merged Rolf Schmidt's illysam branch
[hooke.git] / hooke / ui / gui / panels / playlist.py
1 #!/usr/bin/env python\r
2 \r
3 '''\r
4 playlist.py\r
5 \r
6 Playlist panel for Hooke.\r
7 \r
8 Copyright 2009 by Dr. Rolf Schmidt (Concordia University, Canada)\r
9 \r
10 This program is released under the GNU General Public License version 2.\r
11 '''\r
12 \r
13 import wx\r
14 \r
15 class Playlists(wx.Panel):\r
16 \r
17     def __init__(self, parent):\r
18         # Use the WANTS_CHARS style so the panel doesn't eat the Return key.\r
19         wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS|wx.NO_BORDER, size=(160, 200))\r
20 \r
21         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
22         imglist = wx.ImageList(16, 16, True, 2)\r
23         imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, wx.Size(16, 16)))\r
24         imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, wx.Size(16, 16)))\r
25         self.PlaylistsTree.AssignImageList(imglist)\r
26         self.PlaylistsTree.AddRoot('Playlists', 0)\r
27         self.PlaylistsTree.Bind(wx.EVT_RIGHT_DOWN , self.OnContextMenu)\r
28 \r
29         self.Playlists = {}\r
30 \r
31         sizer = wx.BoxSizer(wx.VERTICAL)\r
32         sizer.Add(self.PlaylistsTree, 1, wx.EXPAND)\r
33         self.SetSizer(sizer)\r
34         sizer.Fit(self)\r
35 \r
36     def OnContextMenu(self, event):\r
37         hit_item, hit_flags = self.PlaylistsTree.HitTest(event.GetPosition())\r
38         if (hit_flags & wx.TREE_HITTEST_ONITEM) != 0:\r
39             self.PlaylistsTree.SelectItem(hit_item)\r
40             # only do this part the first time so the events are only bound once\r
41             # Yet another alternate way to do IDs. Some prefer them up top to\r
42             # avoid clutter, some prefer them close to the object of interest\r
43             # for clarity.\r
44             if not hasattr(self, 'ID_popupAdd'):\r
45                 #self.ID_popupAdd = wx.NewId()\r
46                 self.ID_popupDelete = wx.NewId()\r
47                 #self.Bind(wx.EVT_MENU, self.OnPopupAdd, id=self.ID_popupAdd)\r
48                 self.Bind(wx.EVT_MENU, self.OnPopupDelete, id=self.ID_popupDelete)\r
49             # make a menu\r
50             menu = wx.Menu()\r
51             #items = [['Add', self.ID_popupAdd] , ['Delete', self.ID_popupDelete]]\r
52             items = [['Delete', self.ID_popupDelete]]\r
53             for item in items:\r
54                 menu.Append(item[1], item[0])\r
55             # Popup the menu.  If an item is selected then its handler\r
56             # will be called before PopupMenu returns.\r
57             self.PopupMenu(menu)\r
58             menu.Destroy()\r
59 \r
60     def OnPopupAdd(self, event):\r
61         pass\r
62 \r
63     def OnPopupDelete(self, event):\r
64         item = self.PlaylistsTree.GetSelection()\r
65         playlist = self.Parent.GetActivePlaylist()\r
66         if self.PlaylistsTree.ItemHasChildren(item):\r
67             playlist_name = self.PlaylistsTree.GetItemText(item)\r
68             notebook = self.Parent.plotNotebook\r
69             index = self.Parent._GetPlaylistTab(playlist_name)\r
70             notebook.SetSelection(index)\r
71             notebook.DeletePage(notebook.GetSelection())\r
72             self.Parent.DeleteFromPlaylists(playlist_name)\r
73         else:\r
74             if playlist is not None:\r
75                 if playlist.count == 1:\r
76                     notebook = self.Parent.plotNotebook\r
77                     index = self.Parent._GetPlaylistTab(playlist.name)\r
78                     notebook.SetSelection(index)\r
79                     notebook.DeletePage(notebook.GetSelection())\r
80                     self.Parent.DeleteFromPlaylists(playlist.name)\r
81                 else:\r
82                     file_name = self.PlaylistsTree.GetItemText(item)\r
83                     playlist.delete_file(file_name)\r
84                     self.PlaylistsTree.Delete(item)\r
85                     self.Parent.UpdatePlaylistsTreeSelection()\r