Major rework of gui.panel.playlist.
[hooke.git] / hooke / ui / gui / __init__.py
index e4c966b991dbf8be425361d765e45a156e13e3be..c70a0c738ffd748d639bd840fa7f60be23062162 100644 (file)
@@ -226,7 +226,13 @@ class HookeFrame (wx.Frame):
                     style=wx.DIRCTRL_SHOW_FILTERS,\r
                     filter=self.gui.config['folders-filters'],\r
                     defaultFilter=int(self.gui.config['folders-filter-index'])), 'left'),  #HACK: config should convert\r
-            ('playlists', panel.playlist.Playlist(self), 'left'),\r
+            ('playlists', panel.playlist.Playlist(\r
+                    config=self.gui.config,\r
+                    callbacks={},\r
+                    parent=self,\r
+                    style=wx.WANTS_CHARS|wx.NO_BORDER,\r
+                    # WANTS_CHARS so the panel doesn't eat the Return key.\r
+                    size=(160, 200)), 'left'),\r
             ('note', panel.note.Note(self), 'left'),\r
             ('notebook', Notebook(\r
                     parent=self,\r
@@ -307,9 +313,8 @@ class HookeFrame (wx.Frame):
         treeCtrl = self._c['folders'].GetTreeCtrl()\r
         treeCtrl.Bind(wx.EVT_LEFT_DCLICK, self._on_dir_ctrl_left_double_click)\r
         \r
+        # TODO: playlist callbacks\r
         return # TODO: cleanup\r
-        self._c['playlists'].PlaylistsTree.Bind(wx.EVT_LEFT_DOWN, self.OnPlaylistsLeftDown)\r
-        self._c['playlists'].PlaylistsTree.Bind(wx.EVT_LEFT_DCLICK, self.OnPlaylistsLeftDclick)\r
         #commands tree\r
         evtmgr.eventManager.Register(self.OnExecute, wx.EVT_BUTTON, self._c['commands'].ExecuteButton)\r
         evtmgr.eventManager.Register(self.OnTreeCtrlCommandsSelectionChanged, wx.EVT_TREE_SEL_CHANGED, self._c['commands']._c['tree'])\r
@@ -323,16 +328,16 @@ class HookeFrame (wx.Frame):
     def _GetActiveFileIndex(self):\r
         lib.playlist.Playlist = self.GetActivePlaylist()\r
         #get the selected item from the tree\r
-        selected_item = self._c['playlists'].PlaylistsTree.GetSelection()\r
+        selected_item = self._c['playlists']._c['tree'].GetSelection()\r
         #test if a playlist or a curve was double-clicked\r
-        if self._c['playlists'].PlaylistsTree.ItemHasChildren(selected_item):\r
+        if self._c['playlists']._c['tree'].ItemHasChildren(selected_item):\r
             return -1\r
         else:\r
             count = 0\r
-            selected_item = self._c['playlists'].PlaylistsTree.GetPrevSibling(selected_item)\r
+            selected_item = self._c['playlists']._c['tree'].GetPrevSibling(selected_item)\r
             while selected_item.IsOk():\r
                 count += 1\r
-                selected_item = self._c['playlists'].PlaylistsTree.GetPrevSibling(selected_item)\r
+                selected_item = self._c['playlists']._c['tree'].GetPrevSibling(selected_item)\r
             return count\r
 \r
     def _GetPlaylistTab(self, name):\r
@@ -341,14 +346,6 @@ class HookeFrame (wx.Frame):
                 return index\r
         return -1\r
 \r
-    def _GetUniquePlaylistName(self, name):\r
-        playlist_name = name\r
-        count = 1\r
-        while playlist_name in self.playlists:\r
-            playlist_name = ''.join([name, str(count)])\r
-            count += 1\r
-        return playlist_name\r
-\r
     def _restore_perspective(self, name):\r
         # TODO: cleanup\r
         self.gui.config['active perspective'] = name  # TODO: push to engine's Hooke\r
@@ -365,12 +362,6 @@ class HookeFrame (wx.Frame):
         perspectivesFile.write(perspective)\r
         perspectivesFile.close()\r
 \r
-    def AddPlaylist(self, playlist=None, name='Untitled'):\r
-        if playlist and playlist.count > 0:\r
-            playlist.name = self._GetUniquePlaylistName(name)\r
-            playlist.reset()\r
-            self.AddToPlaylists(playlist)\r
-\r
     def AddPlaylistFromFiles(self, files=[], name='Untitled'):\r
         if files:\r
             playlist = lib.playlist.Playlist(self, self.drivers)\r
@@ -381,34 +372,6 @@ class HookeFrame (wx.Frame):
             playlist.reset()\r
             self.AddTayliss(playlist)\r
 \r
-    def AddToPlaylists(self, playlist):\r
-        if playlist.count > 0:\r
-            #setup the playlist in the Playlist tree\r
-            tree_root = self._c['playlists'].PlaylistsTree.GetRootItem()\r
-            playlist_root = self._c['playlists'].PlaylistsTree.AppendItem(tree_root, playlist.name, 0)\r
-            #add all files to the Playlist tree\r
-#            files = {}\r
-            hide_curve_extension = self.GetBoolFromConfig('core', 'preferences', 'hide_curve_extension')\r
-            for index, file_to_add in enumerate(playlist.files):\r
-                #optionally remove the extension from the name of the curve\r
-                if hide_curve_extension:\r
-                    file_to_add.name = lh.remove_extension(file_to_add.name)\r
-                file_ID = self._c['playlists'].PlaylistsTree.AppendItem(playlist_root, file_to_add.name, 1)\r
-                if index == playlist.index:\r
-                    self._c['playlists'].PlaylistsTree.SelectItem(file_ID)\r
-            playlist.reset()\r
-            #create the plot tab and add playlist to the dictionary\r
-            plotPanel = panel.plot.PlotPanel(self, ID_FirstPlot + len(self.playlists))\r
-            notebook_tab = self._c['notebook'].AddPage(plotPanel, playlist.name, True)\r
-            #tab_index = self._c['notebook'].GetSelection()\r
-            playlist.figure = plotPanel.get_figure()\r
-            self.playlists[playlist.name] = playlist\r
-            #self.playlists[playlist.name] = [playlist, figure]\r
-            self._c['playlists'].PlaylistsTree.Expand(playlist_root)\r
-            self.statusbar.SetStatusText(playlist.get_status_string(), 0)\r
-            self.UpdateNote()\r
-            self.UpdatePlot()\r
-\r
     def AppendToOutput(self, text):\r
         self.panelOutput.AppendText(''.join([text, '\n']))\r
 \r
@@ -430,20 +393,6 @@ class HookeFrame (wx.Frame):
                     manipulated_plot = plotmanipulator.method(manipulated_plot, plot_file)\r
             return manipulated_plot\r
 \r
-    def DeleteFromPlaylists(self, name):\r
-        if name in self.playlists:\r
-            del self.playlists[name]\r
-        tree_root = self._c['playlists'].PlaylistsTree.GetRootItem()\r
-        item, cookie = self._c['playlists'].PlaylistsTree.GetFirstChild(tree_root)\r
-        while item.IsOk():\r
-            playlist_name = self._c['playlists'].PlaylistsTree.GetItemText(item)\r
-            if playlist_name == name:\r
-                try:\r
-                    self._c['playlists'].PlaylistsTree.Delete(item)\r
-                except:\r
-                    pass\r
-            item = self._c['playlists'].PlaylistsTree.GetNextSibling(item)\r
-\r
     def GetActiveFigure(self):\r
         playlist_name = self.GetActivePlaylistName()\r
         figure = self.playlists[playlist_name].figure\r
@@ -457,24 +406,6 @@ class HookeFrame (wx.Frame):
             return playlist.get_active_file()\r
         return None\r
 \r
-    def GetActivePlaylist(self):\r
-        playlist_name = self.GetActivePlaylistName()\r
-        if playlist_name in self.playlists:\r
-            return self.playlists[playlist_name]\r
-        return None\r
-\r
-    def GetActivePlaylistName(self):\r
-        #get the selected item from the tree\r
-        selected_item = self._c['playlists'].PlaylistsTree.GetSelection()\r
-        #test if a playlist or a curve was double-clicked\r
-        if self._c['playlists'].PlaylistsTree.ItemHasChildren(selected_item):\r
-            playlist_item = selected_item\r
-        else:\r
-            #get the name of the playlist\r
-            playlist_item = self._c['playlists'].PlaylistsTree.GetItemParent(selected_item)\r
-        #now we have a playlist\r
-        return self._c['playlists'].PlaylistsTree.GetItemText(playlist_item)\r
-\r
     def GetActivePlot(self):\r
         playlist = self.GetActivePlaylist()\r
         if playlist is not None:\r
@@ -707,20 +638,20 @@ class HookeFrame (wx.Frame):
         -----\r
         Syntax: next, n\r
         '''\r
-        selected_item = self._c['playlists'].PlaylistsTree.GetSelection()\r
-        if self._c['playlists'].PlaylistsTree.ItemHasChildren(selected_item):\r
+        selected_item = self._c['playlists']._c['tree'].GetSelection()\r
+        if self._c['playlists']._c['tree'].ItemHasChildren(selected_item):\r
             #GetFirstChild returns a tuple\r
             #we only need the first element\r
-            next_item = self._c['playlists'].PlaylistsTree.GetFirstChild(selected_item)[0]\r
+            next_item = self._c['playlists']._c['tree'].GetFirstChild(selected_item)[0]\r
         else:\r
-            next_item = self._c['playlists'].PlaylistsTree.GetNextSibling(selected_item)\r
+            next_item = self._c['playlists']._c['tree'].GetNextSibling(selected_item)\r
             if not next_item.IsOk():\r
-                parent_item = self._c['playlists'].PlaylistsTree.GetItemParent(selected_item)\r
+                parent_item = self._c['playlists']._c['tree'].GetItemParent(selected_item)\r
                 #GetFirstChild returns a tuple\r
                 #we only need the first element\r
-                next_item = self._c['playlists'].PlaylistsTree.GetFirstChild(parent_item)[0]\r
-        self._c['playlists'].PlaylistsTree.SelectItem(next_item, True)\r
-        if not self._c['playlists'].PlaylistsTree.ItemHasChildren(selected_item):\r
+                next_item = self._c['playlists']._c['tree'].GetFirstChild(parent_item)[0]\r
+        self._c['playlists']._c['tree'].SelectItem(next_item, True)\r
+        if not self._c['playlists']._c['tree'].ItemHasChildren(selected_item):\r
             playlist = self.GetActivePlaylist()\r
             if playlist.count > 1:\r
                 playlist.next()\r
@@ -736,48 +667,6 @@ class HookeFrame (wx.Frame):
     def OnPaneClose(self, event):\r
         event.Skip()\r
 \r
-    def OnPlaylistsLeftDclick(self, event):\r
-        if self._c['playlists'].PlaylistsTree.Count > 0:\r
-            playlist_name = self.GetActivePlaylistName()\r
-            #if that playlist already exists\r
-            #we check if it is the active playlist (ie selected in panelPlaylists)\r
-            #and switch to it if necessary\r
-            if playlist_name in self.playlists:\r
-                index = self._c['notebook'].GetSelection()\r
-                current_playlist = self._c['notebook'].GetPageText(index)\r
-                if current_playlist != playlist_name:\r
-                    index = self._GetPlaylistTab(playlist_name)\r
-                    self._c['notebook'].SetSelection(index)\r
-                #if a curve was double-clicked\r
-                item = self._c['playlists'].PlaylistsTree.GetSelection()\r
-                if not self._c['playlists'].PlaylistsTree.ItemHasChildren(item):\r
-                    index = self._GetActiveFileIndex()\r
-                else:\r
-                    index = 0\r
-                if index >= 0:\r
-                    playlist = self.GetActivePlaylist()\r
-                    playlist.index = index\r
-                    self.statusbar.SetStatusText(playlist.get_status_string(), 0)\r
-                    self.UpdateNote()\r
-                    self.UpdatePlot()\r
-            #if you uncomment the following line, the tree will collapse/expand as well\r
-            #event.Skip()\r
-\r
-    def OnPlaylistsLeftDown(self, event):\r
-        hit_item, hit_flags = self._c['playlists'].PlaylistsTree.HitTest(event.GetPosition())\r
-        if (hit_flags & wx.TREE_HITTEST_ONITEM) != 0:\r
-            self._c['playlists'].PlaylistsTree.SelectItem(hit_item)\r
-            playlist_name = self.GetActivePlaylistName()\r
-            playlist = self.GetActivePlaylist()\r
-            #if a curve was clicked\r
-            item = self._c['playlists'].PlaylistsTree.GetSelection()\r
-            if not self._c['playlists'].PlaylistsTree.ItemHasChildren(item):\r
-                index = self._GetActiveFileIndex()\r
-                if index >= 0:\r
-                    playlist.index = index\r
-            self.playlists[playlist_name] = playlist\r
-        event.Skip()\r
-\r
     def _on_previous(self, event):\r
         '''\r
         PREVIOUS\r
@@ -789,15 +678,15 @@ class HookeFrame (wx.Frame):
         #playlist = self.playlists[self.GetActivePlaylistName()][0]\r
         #select the previous curve and tell the user if we wrapped around\r
         #self.AppendToOutput(playlist.previous())\r
-        selected_item = self._c['playlists'].PlaylistsTree.GetSelection()\r
-        if self._c['playlists'].PlaylistsTree.ItemHasChildren(selected_item):\r
-            previous_item = self._c['playlists'].PlaylistsTree.GetLastChild(selected_item)\r
+        selected_item = self._c['playlists']._c['tree'].GetSelection()\r
+        if self._c['playlists']._c['tree'].ItemHasChildren(selected_item):\r
+            previous_item = self._c['playlists']._c['tree'].GetLastChild(selected_item)\r
         else:\r
-            previous_item = self._c['playlists'].PlaylistsTree.GetPrevSibling(selected_item)\r
+            previous_item = self._c['playlists']._c['tree'].GetPrevSibling(selected_item)\r
             if not previous_item.IsOk():\r
-                parent_item = self._c['playlists'].PlaylistsTree.GetItemParent(selected_item)\r
-                previous_item = self._c['playlists'].PlaylistsTree.GetLastChild(parent_item)\r
-        self._c['playlists'].PlaylistsTree.SelectItem(previous_item, True)\r
+                parent_item = self._c['playlists']._c['tree'].GetItemParent(selected_item)\r
+                previous_item = self._c['playlists']._c['tree'].GetLastChild(parent_item)\r
+        self._c['playlists']._c['tree'].SelectItem(previous_item, True)\r
         playlist = self.GetActivePlaylist()\r
         if playlist.count > 1:\r
             playlist.previous()\r
@@ -1123,7 +1012,36 @@ class HookeFrame (wx.Frame):
         #refresh the plot\r
         figure.canvas.draw()\r
 \r
+    def _on_curve_select(self, playlist, curve):\r
+        #create the plot tab and add playlist to the dictionary\r
+        plotPanel = panel.plot.PlotPanel(self, ID_FirstPlot + len(self.playlists))\r
+        notebook_tab = self._c['notebook'].AddPage(plotPanel, playlist.name, True)\r
+        #tab_index = self._c['notebook'].GetSelection()\r
+        playlist.figure = plotPanel.get_figure()\r
+        self.playlists[playlist.name] = playlist\r
+        #self.playlists[playlist.name] = [playlist, figure]\r
+        self.statusbar.SetStatusText(playlist.get_status_string(), 0)\r
+        self.UpdateNote()\r
+        self.UpdatePlot()\r
+\r
 \r
+    def _on_playlist_left_doubleclick(self):\r
+        index = self._c['notebook'].GetSelection()\r
+        current_playlist = self._c['notebook'].GetPageText(index)\r
+        if current_playlist != playlist_name:\r
+            index = self._GetPlaylistTab(playlist_name)\r
+            self._c['notebook'].SetSelection(index)\r
+        self.statusbar.SetStatusText(playlist.get_status_string(), 0)\r
+        self.UpdateNote()\r
+        self.UpdatePlot()\r
+\r
+    def _on_playlist_delete(self, playlist):\r
+        notebook = self.Parent.plotNotebook\r
+        index = self.Parent._GetPlaylistTab(playlist.name)\r
+        notebook.SetSelection(index)\r
+        notebook.DeletePage(notebook.GetSelection())\r
+        self.Parent.DeleteFromPlaylists(playlist_name)\r
+        \r
 \r
 class HookeApp (wx.App):\r
     def __init__(self, gui, commands, inqueue, outqueue, *args, **kwargs):\r
@@ -1216,6 +1134,9 @@ class GUI (UserInterface):
             Setting(section=self.setting_section, option='perspective path',\r
                     value=os.path.join('resources', 'gui', 'perspective'),\r
                     help='Directory containing perspective files.'), # TODO: allow colon separated list, like $PATH.\r
+            Setting(section=self.setting_section, option='hide extensions',\r
+                    value=False,\r
+                    help='Hide file extensions when displaying names.'),\r
             Setting(section=self.setting_section, option='folders-workdir',\r
                     value='.',\r
                     help='This should probably go...'),\r