Package muntjac :: Package ui :: Module abstract_media
[hide private]
[frames] | no frames]

Source Code for Module muntjac.ui.abstract_media

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  from muntjac.ui.abstract_component import AbstractComponent 
 17  from muntjac.terminal.gwt.client.ui.v_media_base import VMediaBase 
 18   
 19   
20 -class AbstractMedia(AbstractComponent):
21 """Abstract base class for the HTML5 media components. 22 23 @author: Vaadin Ltd 24 @author: Richard Lincoln 25 """ 26
27 - def __init__(self):
28 self._sources = list() 29 self._showControls = None 30 self._altText = None 31 self._htmlContentAllowed = None 32 self._autoplay = None 33 self._muted = None 34 self._pause = None 35 self._play = None
36 37
38 - def setSource(self, source):
39 """Sets a single media file as the source of the media component. 40 """ 41 del self._sources[:] 42 self.addSource(source)
43 44
45 - def addSource(self, source):
46 """Adds an alternative media file to the sources list. Which of the 47 sources is used is selected by the browser depending on which file 48 formats it supports. See <a 49 href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a> 50 for a table of formats supported by different browsers. 51 """ 52 if source is not None: 53 self._sources.append(source) 54 self.requestRepaint()
55 56
57 - def setSources(self, *sources):
58 """Set multiple sources at once. Which of the sources is used is 59 selected by the browser depending on which file formats it supports. 60 See <a 61 href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a> 62 for a table of formats supported by different browsers. 63 """ 64 self._sources.extend(sources) 65 self.requestRepaint()
66 67
68 - def getSources(self):
69 """@return: The sources pointed to in this media.""" 70 return list(self._sources)
71 72
73 - def setShowControls(self, showControls):
74 """Sets whether or not the browser should show native media controls. 75 """ 76 self._showControls = showControls 77 self.requestRepaint()
78 79
80 - def isShowControls(self):
81 """@return: true if the browser is to show native media controls.""" 82 return self._showControls
83 84
85 - def setAltText(self, text):
86 """Sets the alternative text to be displayed if the browser does not 87 support HTML5. This text is rendered as HTML if 88 L{setHtmlContentAllowed} is set to true. With HTML rendering, this 89 method can also be used to implement fallback to a flash-based player, 90 see the <a href= 91 "https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Using_Flash" 92 >Mozilla Developer Network</a> for details. 93 """ 94 self._altText = text 95 self.requestRepaint()
96 97
98 - def getAltText(self):
99 """@return: The text/html that is displayed when a browser doesn't 100 support HTML5. 101 """ 102 return self._altText
103 104
105 - def setHtmlContentAllowed(self, htmlContentAllowed):
106 """Set whether the alternative text (L{setAltText}) is rendered as 107 HTML or not. 108 """ 109 self._htmlContentAllowed = htmlContentAllowed 110 self.requestRepaint()
111 112
113 - def isHtmlContentAllowed(self):
114 """@return: true if the alternative text (L{setAltText}) is to be 115 rendered as HTML. 116 """ 117 return self._htmlContentAllowed
118 119
120 - def setAutoplay(self, autoplay):
121 """Sets whether the media is to automatically start playback when 122 enough data has been loaded. 123 """ 124 self._autoplay = autoplay 125 self.requestRepaint()
126 127
128 - def isAutoplay(self):
129 """@return: true if the media is set to automatically start playback. 130 """ 131 return self._autoplay
132 133
134 - def setMuted(self, muted):
135 """Set whether to mute the audio or not. 136 """ 137 self._muted = muted 138 self.requestRepaint()
139 140
141 - def isMuted(self):
142 """@return: true if the audio is muted.""" 143 return self._muted
144 145
146 - def pause(self):
147 """Pauses the media.""" 148 # cancel any possible play command 149 self._play = False 150 self._pause = True 151 self.requestRepaint()
152 153
154 - def play(self):
155 """Starts playback of the media.""" 156 # cancel any possible pause command. 157 self._pause = False 158 self._play = True 159 self.requestRepaint()
160 161
162 - def paintContent(self, target):
163 super(AbstractMedia, self).paintContent(target) 164 target.addAttribute(VMediaBase.ATTR_CONTROLS, self.isShowControls()) 165 166 if self.getAltText() is not None: 167 target.addAttribute(VMediaBase.ATTR_ALT_TEXT, self.getAltText()) 168 169 target.addAttribute(VMediaBase.ATTR_HTML, self.isHtmlContentAllowed()) 170 171 target.addAttribute(VMediaBase.ATTR_AUTOPLAY, self.isAutoplay()) 172 173 for r in self.getSources(): 174 target.startTag(VMediaBase.TAG_SOURCE) 175 target.addAttribute(VMediaBase.ATTR_RESOURCE, r) 176 target.addAttribute(VMediaBase.ATTR_RESOURCE_TYPE, r.getMIMEType()) 177 target.endTag(VMediaBase.TAG_SOURCE) 178 179 target.addAttribute(VMediaBase.ATTR_MUTED, self.isMuted()) 180 181 if self._play: 182 target.addAttribute(VMediaBase.ATTR_PLAY, True) 183 self._play = False 184 185 if self._pause: 186 target.addAttribute(VMediaBase.ATTR_PAUSE, True) 187 self._pause = False
188