bc6b4825c56a2cc8d04b4b703bf2d103ebc56cd1
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
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

6) import pango
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

7) import time
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

8) 
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

10) import time_fmt
11) 
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

15) 
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

20)     self.widPlaylistView = self.builder.get_object("PlaylistView")
21)     self.widPlaylistStore = self.builder.get_object("PlaylistStore")
22)     self.widPosition = self.builder.get_object("Position")
23)     self.widPositionScale = self.builder.get_object("PositionScale")
24)     self.widPositionAt = self.builder.get_object("PositionAt")
25)     self.widPositionRemaining = self.builder.get_object("PositionRemaining")
26)     self.widBtnPause = self.builder.get_object("Pause")
27)     self.widBtnPlay = self.builder.get_object("Play")
28)     self.widStatus = self.builder.get_object("Status")
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)       "onPlaylistDblClick": self.onPlaylistDblClick,
32)       "onNewPosition":      self.onNewPosition,
33)       "onPrevious":         self.onPrevious,
34)       "onBackward":         self.onBackward,
35)       "onStop":             self.onStop,
36)       "onPause":            self.onPause,
37)       "onPlay":             self.onPlay,
38)       "onForward":          self.onForward,
39)       "onNext":             self.onNext,
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

52)     self.updateEntry()
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

53)     self.updateButtonVisibility()
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 10 years ago

54) 
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

55)   def showPosition(self):
56)     """update the position texts next to the position slider"""
57)     # format current time and remaining time
58)     posAt = time_fmt.sec2str(self.stPosition)
59)     posRemaining = time_fmt.sec2str(self.stDuration - self.stPosition)
60)     self.widPositionAt.set_text(posAt)
61)     self.widPositionRemaining.set_text(posRemaining)
62) 
63)   def updatePositionState(self):
64)     """update the position in the state, but not the slider"""
65)     # calculate (virtual) start time of playing
66)     # i.e. the time the playing would have had started to arrive at the
67)     # current position now if it had played continuosly
68)     self.stPlayStart = time.time() - self.stPosition
69)     # update position texts
70)     self.showPosition()
71) 
72)   def updatePosition(self):
73)     """update the position including the position slider"""
74)     # update GUI slider
75)     self.widPositionScale.set_value(self.stPosition)
76)     # update position state
77)     self.updatePositionState()
78) 
79)   def updateDuration(self):
80)     """update the duration (i.e. range for the slider) based on the current
81)        playlist entry"""
82)     # get duration of new playlist entry
83)     self.stDuration = 0
84)     if self.stEntryIdx >= 0:
85)       entry = self.playlist.entries[self.stEntryIdx]
86)       if entry["type"] == "normal":
87)         self.stDuration = entry["duration"]
88)     # set position to begin
89)     self.stPosition = 0
90)     # update value range
91)     self.widPosition.set_upper(self.stDuration)
92)     # update position of slider
93)     self.updatePosition()
94) 
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

95)   def updateEntry(self):
96)     """update current entry of playlist and duration, position, ..."""
97)     # sanity check for entry index
98)     if self.stEntryIdx < -1 or self.stEntryIdx >= len(self.playlist.entries):
99)       self.stEntryIdx = -1
100)     # make current entry bold, all others non-bold
101)     def update(model, path, it, user_data):
102)       (idx,) = model.get(it, 0)
103)       if idx == self.stEntryIdx:
104)         weight = pango.WEIGHT_BOLD
105)       else:
106)         weight = pango.WEIGHT_NORMAL
107)       print("DEBUG sync_gui idx=%d weight=%d" % (idx, weight))
108)       model.set(it, 1, weight)
109)     self.widPlaylistStore.foreach(update, None)
110)     # update duration, position, ...
111)     self.updateDuration()
112) 
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

113)   def updateButtonVisibility(self):
114)     """update the visibility of the buttons based on if playing or not"""
115)     self.widBtnPause.set_visible(self.stPlaying)
116)     self.widBtnPlay.set_visible(not self.stPlaying)
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

120)     Gtk.main_quit()
121) 
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

122)   def onPlaylistDblClick(self, widget, row, col):
123)     """playlist entry has been double-clicked"""
124)     # get index of selected entry
125)     idx = -1
126)     sel = self.widPlaylistView.get_selection()
127)     if sel is not None:
128)       (model, it) = sel.get_selected()
129)       if it is not None:
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

130)         (idx,) = model.get(it, 0)
131)     print("DEBUG sync_gui playlist double-click idx=%d" % (idx))
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

132)     # update playlist entry
133)     self.stEntryIdx = idx
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

134)     # update entry
135)     self.updateEntry()
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

136)     # start playing
137)     # TODO
138) 
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

140)     """slider has been moved to a new position"""
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

141)     print("DEBUG sync_gui new position " + str(value));
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

142)     # clamp position to valid range
143)     if value < 0:
144)       value = 0
145)     if value > self.stDuration:
146)       value = self.stDuration
147)     # update current position - and play start time if playing
148)     self.stPosition = value
149)     # update position state (do not touch the slider)
150)     self.updatePositionState()
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 10 years ago

151) 
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

153)     """previous button as been pressed"""
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

154)     print("DEBUG sync_gui previous")
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

157)     """backward button has been pressed"""
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

158)     print("DEBUG sync_gui backward")
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

161)     """stop button has been pressed"""
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

162)     print("DEBUG sync_gui stop")
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

163)     self.stPlaying = False
164)     self.stPosition = 0 # stop goes back to begin
165)     self.updatePosition()
166)     self.updateButtonVisibility()
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

169)     """pause button has been pressed"""
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

170)     print("DEBUG sync_gui pause")
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

171)     self.stPlaying = False
172)     self.updateButtonVisibility()
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

175)     """play button has been pressed"""
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

176)     print("DEBUG sync_gui play")
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

177)     self.stPlaying = True
178)     self.updatePosition()
179)     self.updateButtonVisibility()
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

182)     """forward button has been pressed"""
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

183)     print("DEBUG sync_gui forward")
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

186)     """next button has been pressed"""
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

187)     print("DEBUG sync_gui next")
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

188) 
189)   def onTimer10ms(self):
190)     """timer callback, every 10ms"""
191)     # update position if playing
192)     if self.stPlaying:
193)       self.stPosition = time.time() - self.stPlayStart
194)       if self.stPosition > self.stDuration:
195)         self.stPosition = 0 # FIXME: go to next entry
196)       self.updatePosition()
197)     return True
198) 
199) # main application entry point
Stefan Schuermans initial PyGtk window

Stefan Schuermans authored 10 years ago

200) if __name__ == "__main__":
201)   app = SyncGui()
202)   Gtk.main()
203)