8ab741326ebac654317cb1db408bf0694d6feb84
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

1) #! /usr/bin/env python
2) 
3) import os
4) from gi.repository import Gtk
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

5) import gobject
6) import time
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

7) 
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

8) import playlist
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 10 years ago

9) import time_fmt
10) 
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

11) scriptdir = os.path.dirname(os.path.abspath(__file__))
12) 
13) class SyncGui:
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

14) 
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

15)   def __init__(self):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

16)     """construct a SyncGui object"""
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

17)     self.builder = Gtk.Builder()
18)     self.builder.add_from_file(scriptdir + "/sync_gui.glade")
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

19)     self.widPlaylistView = self.builder.get_object("PlaylistView")
20)     self.widPlaylistStore = self.builder.get_object("PlaylistStore")
21)     self.widPosition = self.builder.get_object("Position")
22)     self.widPositionScale = self.builder.get_object("PositionScale")
23)     self.widPositionAt = self.builder.get_object("PositionAt")
24)     self.widPositionRemaining = self.builder.get_object("PositionRemaining")
25)     self.widBtnPause = self.builder.get_object("Pause")
26)     self.widBtnPlay = self.builder.get_object("Play")
27)     self.widStatus = self.builder.get_object("Status")
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

28)     self.configPlaylistColumns()
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

29)     handlers = {
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

30)       "onDestroy":          self.onDestroy,
31)       "onPlaylistClick":    self.onPlaylistClick,
32)       "onPlaylistDblClick": self.onPlaylistDblClick,
33)       "onNewPosition":      self.onNewPosition,
34)       "onPrevious":         self.onPrevious,
35)       "onBackward":         self.onBackward,
36)       "onStop":             self.onStop,
37)       "onPause":            self.onPause,
38)       "onPlay":             self.onPlay,
39)       "onForward":          self.onForward,
40)       "onNext":             self.onNext,
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

41)     }
42)     self.builder.connect_signals(handlers)
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

43)     self.playlist = playlist.Playlist()
44)     self.playlist.read("playlist.txt")
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

45)     self.playlist.update(self.widPlaylistStore)
46)     self.widStatus.push(0, "TODO...")
47)     self.stEntryIdx = -1 # no entry selected
48)     self.stName = "" # no current entry name
49)     self.stDuration = 0 # current entry has zero size
50)     self.stPosition = 0 # at begin of current entry
51)     self.stPlaying = False # not playing
52)     gobject.timeout_add(10, self.onTimer10ms)
53)     self.updateDuration()
54)     self.updateButtonVisibility()
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 10 years ago

55) 
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

56)   def configPlaylistColumns(self):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

57)     """configure the columns of the playlist widget at program start"""
58)     i = 1 # first column is index (not shown)
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

59)     for title in ["Name", "Dauer"]:
60)       column = Gtk.TreeViewColumn(title)
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

61)       self.widPlaylistView.append_column(column)
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

62)       cell = Gtk.CellRendererText()
63)       column.pack_start(cell, False)
64)       column.add_attribute(cell, "text", i)
65)       i = i + 1
66) 
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

67)   def showPosition(self):
68)     """update the position texts next to the position slider"""
69)     # format current time and remaining time
70)     posAt = time_fmt.sec2str(self.stPosition)
71)     posRemaining = time_fmt.sec2str(self.stDuration - self.stPosition)
72)     self.widPositionAt.set_text(posAt)
73)     self.widPositionRemaining.set_text(posRemaining)
74) 
75)   def updatePositionState(self):
76)     """update the position in the state, but not the slider"""
77)     # calculate (virtual) start time of playing
78)     # i.e. the time the playing would have had started to arrive at the
79)     # current position now if it had played continuosly
80)     self.stPlayStart = time.time() - self.stPosition
81)     # update position texts
82)     self.showPosition()
83) 
84)   def updatePosition(self):
85)     """update the position including the position slider"""
86)     # update GUI slider
87)     self.widPositionScale.set_value(self.stPosition)
88)     # update position state
89)     self.updatePositionState()
90) 
91)   def updateDuration(self):
92)     """update the duration (i.e. range for the slider) based on the current
93)        playlist entry"""
94)     # get duration of new playlist entry
95)     self.stDuration = 0
96)     if self.stEntryIdx >= 0:
97)       entry = self.playlist.entries[self.stEntryIdx]
98)       if entry["type"] == "normal":
99)         self.stDuration = entry["duration"]
100)     # set position to begin
101)     self.stPosition = 0
102)     # update value range
103)     self.widPosition.set_upper(self.stDuration)
104)     # update position of slider
105)     self.updatePosition()
106) 
107)   def updateButtonVisibility(self):
108)     """update the visibility of the buttons based on if playing or not"""
109)     self.widBtnPause.set_visible(self.stPlaying)
110)     self.widBtnPlay.set_visible(not self.stPlaying)
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

111) 
112)   def onDestroy(self, widget):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

113)     """window will be destroyed"""
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

114)     Gtk.main_quit()
115) 
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

116)   def onPlaylistClick(self, widget):
117)     """playlist entry has been clicked or selected"""
118)     # get index of selected entry
119)     idx = -1
120)     sel = self.widPlaylistView.get_selection()
121)     if sel is not None:
122)       (model, it) = sel.get_selected()
123)       if it is not None:
124)         (idx, ) = model.get(it, 0)
125)     print("DEBUG: playlist click idx=%d" % (idx))
126)     # update playlist entry
127)     self.stEntryIdx = idx
128)     # update duration
129)     self.updateDuration()
130) 
131)   def onPlaylistDblClick(self, widget, row, col):
132)     """playlist entry has been double-clicked"""
133)     # get index of selected entry
134)     idx = -1
135)     sel = self.widPlaylistView.get_selection()
136)     if sel is not None:
137)       (model, it) = sel.get_selected()
138)       if it is not None:
139)         (idx, ) = model.get(it, 0)
140)     print("DEBUG: playlist double-click idx=%d" % (idx))
141)     # update playlist entry
142)     self.stEntryIdx = idx
143)     # update duration
144)     self.updateDuration()
145)     # start playing
146)     # TODO
147) 
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 10 years ago

148)   def onNewPosition(self, widget, scroll, value):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

149)     """slider has been moved to a new position"""
150)     print("DEBUG: new position " + str(value));
151)     # clamp position to valid range
152)     if value < 0:
153)       value = 0
154)     if value > self.stDuration:
155)       value = self.stDuration
156)     # update current position - and play start time if playing
157)     self.stPosition = value
158)     # update position state (do not touch the slider)
159)     self.updatePositionState()
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 10 years ago

160) 
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

161)   def onPrevious(self, widget):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

162)     """previous button as been pressed"""
163)     print("DEBUG: previous")
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

164) 
165)   def onBackward(self, widget):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

166)     """backward button has been pressed"""
167)     print("DEBUG: backward")
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

168) 
169)   def onStop(self, widget):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

170)     """stop button has been pressed"""
171)     print("DEBUG: stop")
172)     self.stPlaying = False
173)     self.stPosition = 0 # stop goes back to begin
174)     self.updatePosition()
175)     self.updateButtonVisibility()
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

176) 
177)   def onPause(self, widget):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

178)     """pause button has been pressed"""
179)     print("DEBUG: pause")
180)     self.stPlaying = False
181)     self.updateButtonVisibility()
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

182) 
183)   def onPlay(self, widget):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

184)     """play button has been pressed"""
185)     print("DEBUG: play")
186)     self.stPlaying = True
187)     self.updatePosition()
188)     self.updateButtonVisibility()
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

189) 
190)   def onForward(self, widget):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

191)     """forward button has been pressed"""
192)     print("DEBUG: forward")
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

193) 
194)   def onNext(self, widget):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

195)     """next button has been pressed"""
196)     print("DEBUG: next")
197) 
198)   def onTimer10ms(self):
199)     """timer callback, every 10ms"""
200)     # update position if playing
201)     if self.stPlaying:
202)       self.stPosition = time.time() - self.stPlayStart
203)       if self.stPosition > self.stDuration:
204)         self.stPosition = 0 # FIXME: go to next entry
205)       self.updatePosition()
206)     return True
207) 
208) # main application entry point
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

209) if __name__ == "__main__":
210)   app = SyncGui()
211)   Gtk.main()
212)