add menu, implement file open
Stefan Schuermans

Stefan Schuermans commited on 2013-11-23 18:42:49
Showing 2 changed files, with 102 additions and 5 deletions.

... ...
@@ -11,6 +11,61 @@
11 11
         <property name="visible">True</property>
12 12
         <property name="can_focus">False</property>
13 13
         <property name="orientation">vertical</property>
14
+        <child>
15
+          <object class="GtkMenuBar" id="Menu">
16
+            <property name="visible">True</property>
17
+            <property name="can_focus">False</property>
18
+            <child>
19
+              <object class="GtkMenuItem" id="MiFile">
20
+                <property name="use_action_appearance">False</property>
21
+                <property name="visible">True</property>
22
+                <property name="can_focus">False</property>
23
+                <property name="label" translatable="yes">_File</property>
24
+                <property name="use_underline">True</property>
25
+                <child type="submenu">
26
+                  <object class="GtkMenu" id="MenuFile">
27
+                    <property name="visible">True</property>
28
+                    <property name="can_focus">False</property>
29
+                    <child>
30
+                      <object class="GtkImageMenuItem" id="MiFileOpen">
31
+                        <property name="label">gtk-open</property>
32
+                        <property name="use_action_appearance">False</property>
33
+                        <property name="visible">True</property>
34
+                        <property name="can_focus">False</property>
35
+                        <property name="use_underline">True</property>
36
+                        <property name="use_stock">True</property>
37
+                        <signal name="activate" handler="onFileOpen" swapped="no"/>
38
+                      </object>
39
+                    </child>
40
+                    <child>
41
+                      <object class="GtkSeparatorMenuItem" id="MsFile1">
42
+                        <property name="use_action_appearance">False</property>
43
+                        <property name="visible">True</property>
44
+                        <property name="can_focus">False</property>
45
+                      </object>
46
+                    </child>
47
+                    <child>
48
+                      <object class="GtkImageMenuItem" id="MiFileExit">
49
+                        <property name="label">gtk-quit</property>
50
+                        <property name="use_action_appearance">False</property>
51
+                        <property name="visible">True</property>
52
+                        <property name="can_focus">False</property>
53
+                        <property name="use_underline">True</property>
54
+                        <property name="use_stock">True</property>
55
+                        <signal name="activate" handler="onFileExit" swapped="no"/>
56
+                      </object>
57
+                    </child>
58
+                  </object>
59
+                </child>
60
+              </object>
61
+            </child>
62
+          </object>
63
+          <packing>
64
+            <property name="expand">False</property>
65
+            <property name="fill">True</property>
66
+            <property name="position">0</property>
67
+          </packing>
68
+        </child>
14 69
         <child>
15 70
           <object class="GtkScrolledWindow" id="PlaylistScroll">
16 71
             <property name="width_request">320</property>
... ...
@@ -72,7 +127,7 @@
72 127
           <packing>
73 128
             <property name="expand">True</property>
74 129
             <property name="fill">True</property>
75
-            <property name="position">0</property>
130
+            <property name="position">1</property>
76 131
           </packing>
77 132
         </child>
78 133
         <child>
... ...
@@ -131,7 +186,7 @@
131 186
           <packing>
132 187
             <property name="expand">False</property>
133 188
             <property name="fill">True</property>
134
-            <property name="position">1</property>
189
+            <property name="position">2</property>
135 190
           </packing>
136 191
         </child>
137 192
         <child>
... ...
@@ -261,7 +316,7 @@
261 316
           <packing>
262 317
             <property name="expand">False</property>
263 318
             <property name="fill">True</property>
264
-            <property name="position">2</property>
319
+            <property name="position">3</property>
265 320
           </packing>
266 321
         </child>
267 322
         <child>
... ...
@@ -274,7 +329,7 @@
274 329
           <packing>
275 330
             <property name="expand">False</property>
276 331
             <property name="fill">True</property>
277
-            <property name="position">3</property>
332
+            <property name="position">4</property>
278 333
           </packing>
279 334
         </child>
280 335
       </object>
... ...
@@ -4,6 +4,7 @@ import os
4 4
 from gi.repository import Gtk
5 5
 import gobject
6 6
 import pango
7
+import sys
7 8
 import time
8 9
 
9 10
 import playlist
... ...
@@ -17,6 +18,7 @@ class SyncGui:
17 18
     """construct a SyncGui object"""
18 19
     self.builder = Gtk.Builder()
19 20
     self.builder.add_from_file(scriptdir + "/sync_gui.glade")
21
+    self.widMainWindow = self.builder.get_object("MainWindow")
20 22
     self.widPlaylistView = self.builder.get_object("PlaylistView")
21 23
     self.widPlaylistStore = self.builder.get_object("PlaylistStore")
22 24
     self.widPosition = self.builder.get_object("Position")
... ...
@@ -28,6 +30,8 @@ class SyncGui:
28 30
     self.widStatus = self.builder.get_object("Status")
29 31
     handlers = {
30 32
       "onDestroy":          self.onDestroy,
33
+      "onFileOpen":         self.onFileOpen,
34
+      "onFileExit":         self.onFileExit,
31 35
       "onPlaylistDblClick": self.onPlaylistDblClick,
32 36
       "onNewPosition":      self.onNewPosition,
33 37
       "onPrevious":         self.onPrevious,
... ...
@@ -38,7 +42,8 @@ class SyncGui:
38 42
     }
39 43
     self.builder.connect_signals(handlers)
40 44
     self.playlist = playlist.Playlist()
41
-    self.playlist.read("playlist.txt")
45
+    if len(sys.argv) >= 2: # load initial playlist from command line
46
+      self.playlist.read(sys.argv[1])
42 47
     self.playlist.update(self.widPlaylistStore)
43 48
     self.widStatus.push(0, "TODO...")
44 49
     self.stEntryIdx = -1 # no entry selected
... ...
@@ -99,6 +104,11 @@ class SyncGui:
99 104
     # sanity check for entry index
100 105
     if self.stEntryIdx < -1 or self.stEntryIdx >= len(self.playlist.entries):
101 106
       self.stEntryIdx = -1
107
+    # get name of current entry
108
+    self.stName = ""
109
+    if self.stEntryIdx >= 0 and \
110
+       self.playlist.entries[self.stEntryIdx]["type"] == "normal":
111
+      self.stName = self.playlist.entries[self.stEntryIdx]["name"]
102 112
     # make current entry bold, all others non-bold
103 113
     def update(model, path, it, user_data):
104 114
       (idx,) = model.get(it, 0)
... ...
@@ -126,6 +136,38 @@ class SyncGui:
126 136
     """window will be destroyed"""
127 137
     Gtk.main_quit()
128 138
 
139
+  def onFileOpen(self, widget):
140
+    """File Open clicked in menu"""
141
+    #print("DEBUG sync_gui File Open")
142
+    # run file chooser dialog
143
+    dialog = Gtk.FileChooserDialog("BlinkenArea Sync GUI - File Open..",
144
+                                   self.widMainWindow,
145
+                                   Gtk.FileChooserAction.OPEN,
146
+                                   (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
147
+                                    Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
148
+    dialog.set_default_response(Gtk.ResponseType.OK)
149
+    filt = Gtk.FileFilter()
150
+    filt.set_name("All files")
151
+    filt.add_pattern("*")
152
+    dialog.add_filter(filt)
153
+    response = dialog.run()
154
+    if response == Gtk.ResponseType.OK:
155
+      # dialog closed with OK -> load new playlist
156
+      filename = dialog.get_filename()
157
+      self.playlist.read(filename)
158
+      self.playlist.update(self.widPlaylistStore)
159
+      self.stEntryIdx = -1 # no entry selected
160
+      self.stPlaying = False # not playing
161
+      self.updateEntry()
162
+      self.updateButtonVisibility()
163
+    # cleanup
164
+    dialog.destroy()
165
+
166
+  def onFileExit(self, widget):
167
+    """File Exit clicked in menu"""
168
+    #print("DEBUG sync_gui File Exit")
169
+    Gtk.main_quit()
170
+
129 171
   def onPlaylistDblClick(self, widget, row, col):
130 172
     """playlist entry has been double-clicked"""
131 173
     # get index of selected entry
132 174