show playlist in TreeView
Stefan Schuermans

Stefan Schuermans commited on 2013-11-16 14:29:29
Showing 3 changed files, with 57 additions and 32 deletions.

... ...
@@ -7,7 +7,7 @@ import time_fmt
7 7
 class Playlist:
8 8
   def __init__(self):
9 9
     self.entries = []
10
-    self.reEntry = re.compile('^\s*([A-Za-z0-9_]+)\s+([0-9:.]+)\s*$')
10
+    self.reEntry = re.compile("^\s*([A-Za-z0-9_]+)\s+([0-9:.]+)\s*$")
11 11
 
12 12
   def read(self, filename):
13 13
     self.entries = []
... ...
@@ -17,13 +17,24 @@ class Playlist:
17 17
       if mEntry:
18 18
         name = mEntry.group(1)
19 19
         duration = time_fmt.str2sec(mEntry.group(2))
20
-        self.entries.append({'type':     'normal',
21
-                             'name':     name,
22
-                             'duration': duration})
23
-        print("entry normal %s %f" % (self.entries[-1]['name'],
24
-                                      self.entries[-1]['duration']))
20
+        self.entries.append({"type":     "normal",
21
+                             "name":     name,
22
+                             "duration": duration})
23
+        print("entry normal %s %f" % (self.entries[-1]["name"],
24
+                                      self.entries[-1]["duration"]))
25 25
       else:
26
-        self.entries.append({'type': 'stop'})
26
+        self.entries.append({"type": "stop"})
27 27
         print("entry stop")
28 28
     f.close()
29 29
 
30
+  def update(self, store):
31
+    store.clear()
32
+    for entry in self.entries:
33
+      if entry["type"] == "normal":
34
+        name = entry["name"]
35
+        duration = time_fmt.sec2str(entry["duration"])
36
+      else:
37
+        name = ""
38
+        duration = "STOP"
39
+      store.append([name, duration])
40
+
... ...
@@ -1,6 +1,28 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <interface>
3 3
   <!-- interface-requires gtk+ 3.0 -->
4
+  <object class="GtkListStore" id="PlaylistStore">
5
+    <columns>
6
+      <!-- column-name Name -->
7
+      <column type="gchararray"/>
8
+      <!-- column-name Dauer -->
9
+      <column type="gchararray"/>
10
+    </columns>
11
+    <data>
12
+      <row>
13
+        <col id="0" translatable="yes">Erster Akt</col>
14
+        <col id="1" translatable="yes"/>
15
+      </row>
16
+      <row>
17
+        <col id="0" translatable="yes">Zweiter Akt</col>
18
+        <col id="1" translatable="yes"/>
19
+      </row>
20
+      <row>
21
+        <col id="0" translatable="yes">Weltuntergang</col>
22
+        <col id="1" translatable="yes"/>
23
+      </row>
24
+    </data>
25
+  </object>
4 26
   <object class="GtkWindow" id="MainWindow">
5 27
     <property name="visible">True</property>
6 28
     <property name="can_focus">False</property>
... ...
@@ -13,8 +35,8 @@
13 35
         <property name="orientation">vertical</property>
14 36
         <child>
15 37
           <object class="GtkScrolledWindow" id="PlaylistScroll">
16
-            <property name="width_request">100</property>
17
-            <property name="height_request">100</property>
38
+            <property name="width_request">320</property>
39
+            <property name="height_request">240</property>
18 40
             <property name="visible">True</property>
19 41
             <property name="can_focus">True</property>
20 42
             <property name="border_width">3</property>
... ...
@@ -23,7 +45,7 @@
23 45
               <object class="GtkTreeView" id="PlaylistView">
24 46
                 <property name="visible">True</property>
25 47
                 <property name="can_focus">True</property>
26
-                <property name="model">Playlist</property>
48
+                <property name="model">PlaylistStore</property>
27 49
                 <child internal-child="selection">
28 50
                   <object class="GtkTreeSelection" id="treeview-selection2"/>
29 51
                 </child>
... ...
@@ -279,28 +301,6 @@
279 301
       </object>
280 302
     </child>
281 303
   </object>
282
-  <object class="GtkListStore" id="Playlist">
283
-    <columns>
284
-      <!-- column-name Name -->
285
-      <column type="gchararray"/>
286
-      <!-- column-name Dauer -->
287
-      <column type="gfloat"/>
288
-    </columns>
289
-    <data>
290
-      <row>
291
-        <col id="0" translatable="yes">Erster Akt</col>
292
-        <col id="1">1</col>
293
-      </row>
294
-      <row>
295
-        <col id="0" translatable="yes">Zweiter Akt</col>
296
-        <col id="1">2</col>
297
-      </row>
298
-      <row>
299
-        <col id="0" translatable="yes">Weltuntergang</col>
300
-        <col id="1">99</col>
301
-      </row>
302
-    </data>
303
-  </object>
304 304
   <object class="GtkAdjustment" id="Position">
305 305
     <property name="upper">100</property>
306 306
     <property name="value">50</property>
... ...
@@ -12,6 +12,8 @@ class SyncGui:
12 12
   def __init__(self):
13 13
     self.builder = Gtk.Builder()
14 14
     self.builder.add_from_file(scriptdir + "/sync_gui.glade")
15
+    self.playlistView = self.builder.get_object("PlaylistView")
16
+    self.playlistStore = self.builder.get_object("PlaylistStore")
15 17
     self.position = self.builder.get_object("Position")
16 18
     self.positionScale = self.builder.get_object("PositionScale")
17 19
     self.positionAt = self.builder.get_object("PositionAt")
... ...
@@ -19,6 +21,7 @@ class SyncGui:
19 21
     self.btnPause = self.builder.get_object("Pause")
20 22
     self.btnPlay = self.builder.get_object("Play")
21 23
     self.status = self.builder.get_object("Status")
24
+    self.configPlaylistColumns()
22 25
     handlers = {
23 26
       "onDestroy":     self.onDestroy,
24 27
       "onNewPosition": self.onNewPosition,
... ...
@@ -33,9 +36,20 @@ class SyncGui:
33 36
     self.builder.connect_signals(handlers)
34 37
     self.playlist = playlist.Playlist()
35 38
     self.playlist.read("playlist.txt")
39
+    self.playlist.update(self.playlistStore)
36 40
     self.status.push(0, "TODO...")
37 41
     self.showCurrentPosition()
38 42
 
43
+  def configPlaylistColumns(self):
44
+    i = 0
45
+    for title in ["Name", "Dauer"]:
46
+      column = Gtk.TreeViewColumn(title)
47
+      self.playlistView.append_column(column)
48
+      cell = Gtk.CellRendererText()
49
+      column.pack_start(cell, False)
50
+      column.add_attribute(cell, "text", i)
51
+      i = i + 1
52
+
39 53
   def showPosition(self, sec):
40 54
     if sec < 0:
41 55
       sec = 0
42 56