Changeset View
Changeset View
Standalone View
Standalone View
src/panels/information/phononwidget.cpp
Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Line(s) | 59 | PhononWidget::PhononWidget(QWidget *parent) | |||
---|---|---|---|---|---|
64 | m_topLayout(nullptr), | 64 | m_topLayout(nullptr), | ||
65 | m_media(nullptr), | 65 | m_media(nullptr), | ||
66 | m_seekSlider(nullptr), | 66 | m_seekSlider(nullptr), | ||
67 | m_audioOutput(nullptr), | 67 | m_audioOutput(nullptr), | ||
68 | m_videoPlayer(nullptr) | 68 | m_videoPlayer(nullptr) | ||
69 | { | 69 | { | ||
70 | } | 70 | } | ||
71 | 71 | | |||
72 | void PhononWidget::setUrl(const QUrl &url) | 72 | bool PhononWidget::eventFilter(QObject *obj, QEvent *event) | ||
73 | { | ||||
broulik: Add a context `this` or else it will crash when timer fires with the widget being gone:
```… | |||||
meven: Thanks I thought I could not use this as context in a connect. | |||||
74 | if (event->type() == QEvent::MouseButtonRelease) { | ||||
75 | QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); | ||||
76 | if (mouseEvent->button() == Qt::MouseButton::LeftButton) { | ||||
77 | // a click has been detected, toggle preview playback | ||||
78 | togglePlay(); | ||||
79 | return true; | ||||
80 | } | ||||
81 | } | ||||
82 | // standard event processing | ||||
83 | return QObject::eventFilter(obj, event); | ||||
84 | } | ||||
85 | | ||||
86 | void PhononWidget::setUrl(const QUrl &url, MediaKind kind) | ||||
73 | { | 87 | { | ||
74 | if (m_url != url) { | 88 | if (m_url != url) { | ||
75 | stop(); // emits playingStopped() signal | | |||
76 | m_url = url; | 89 | m_url = url; | ||
90 | m_isVideo = kind == MediaKind::Video; | ||||
91 | } | ||||
92 | if (m_autoPlay) { | ||||
93 | play(); | ||||
94 | } else { | ||||
95 | stop(); | ||||
96 | } | ||||
97 | } | ||||
98 | | ||||
99 | void PhononWidget::setAutoPlay(bool autoPlay) | ||||
100 | { | ||||
101 | m_autoPlay = autoPlay; | ||||
102 | if (!m_url.isEmpty() && (m_media == nullptr || m_media->state() != Phonon::State::PlayingState) && m_autoPlay && isVisible()) { | ||||
103 | play(); | ||||
77 | } | 104 | } | ||
78 | } | 105 | } | ||
79 | 106 | | |||
80 | QUrl PhononWidget::url() const | 107 | QUrl PhononWidget::url() const | ||
81 | { | 108 | { | ||
82 | return m_url; | 109 | return m_url; | ||
83 | } | 110 | } | ||
84 | 111 | | |||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Line(s) | 132 | if (!m_topLayout) { | |||
140 | 167 | | |||
141 | // Creating an audio player or video player instance might take up to | 168 | // Creating an audio player or video player instance might take up to | ||
142 | // 2 seconds when doing it the first time. To prevent that the user | 169 | // 2 seconds when doing it the first time. To prevent that the user | ||
143 | // interface gets noticeable blocked, the creation is delayed until | 170 | // interface gets noticeable blocked, the creation is delayed until | ||
144 | // the play button has been pressed (see PhononWidget::play()). | 171 | // the play button has been pressed (see PhononWidget::play()). | ||
145 | } | 172 | } | ||
146 | } | 173 | } | ||
147 | 174 | | |||
175 | void PhononWidget::togglePlay() | ||||
176 | { | ||||
177 | if (m_media) { | ||||
178 | auto media_state = m_media->state(); | ||||
179 | if (media_state == Phonon::PlayingState) { | ||||
180 | // the media was playing pause it | ||||
181 | if (m_isVideo) { | ||||
182 | // change the tooltip accordingly | ||||
183 | m_videoPlayer->setToolTip(i18n("Play")); | ||||
184 | } | ||||
185 | m_media->pause(); | ||||
186 | } else { | ||||
187 | // in any other cases, it is time to play | ||||
188 | if (m_isVideo) { | ||||
189 | m_videoPlayer->setToolTip(i18n("Pause")); | ||||
190 | } | ||||
191 | play(); | ||||
elvisangelaccio: Maybe an if-else would be nicer here. | |||||
192 | } | ||||
193 | } else { | ||||
194 | play(); | ||||
195 | } | ||||
196 | } | ||||
197 | | ||||
148 | void PhononWidget::hideEvent(QHideEvent *event) | 198 | void PhononWidget::hideEvent(QHideEvent *event) | ||
149 | { | 199 | { | ||
150 | QWidget::hideEvent(event); | 200 | QWidget::hideEvent(event); | ||
151 | if (!event->spontaneous()) { | 201 | if (!event->spontaneous()) { | ||
152 | stop(); | 202 | stop(); | ||
153 | } | 203 | } | ||
154 | } | 204 | } | ||
155 | 205 | | |||
156 | void PhononWidget::stateChanged(Phonon::State newstate) | 206 | void PhononWidget::stateChanged(Phonon::State newstate) | ||
157 | { | 207 | { | ||
158 | setUpdatesEnabled(false); | 208 | setUpdatesEnabled(false); | ||
159 | switch (newstate) { | 209 | switch (newstate) { | ||
160 | case Phonon::PlayingState: | 210 | case Phonon::PlayingState: | ||
161 | case Phonon::BufferingState: | 211 | case Phonon::BufferingState: | ||
162 | m_stopButton->show(); | 212 | m_stopButton->show(); | ||
163 | m_playButton->hide(); | 213 | m_playButton->hide(); | ||
164 | break; | 214 | break; | ||
165 | case Phonon::StoppedState: | 215 | case Phonon::StoppedState: | ||
166 | if (m_videoPlayer) { | | |||
167 | m_videoPlayer->hide(); | | |||
168 | } | | |||
169 | emit hasVideoChanged(false); | | |||
170 | Q_FALLTHROUGH(); | 216 | Q_FALLTHROUGH(); | ||
171 | default: | 217 | default: | ||
172 | m_stopButton->hide(); | 218 | m_stopButton->hide(); | ||
173 | m_playButton->show(); | 219 | m_playButton->show(); | ||
174 | break; | 220 | break; | ||
175 | } | 221 | } | ||
176 | setUpdatesEnabled(true); | 222 | setUpdatesEnabled(true); | ||
177 | } | 223 | } | ||
178 | 224 | | |||
179 | void PhononWidget::play() | 225 | void PhononWidget::play() | ||
180 | { | 226 | { | ||
181 | if (!m_media) { | 227 | if (!m_media) { | ||
182 | m_media = new Phonon::MediaObject(this); | 228 | m_media = new Phonon::MediaObject(this); | ||
183 | connect(m_media, &Phonon::MediaObject::stateChanged, | 229 | connect(m_media, &Phonon::MediaObject::stateChanged, | ||
184 | this, &PhononWidget::stateChanged); | 230 | this, &PhononWidget::stateChanged); | ||
185 | connect(m_media, &Phonon::MediaObject::hasVideoChanged, | 231 | connect(m_media, &Phonon::MediaObject::finished, | ||
186 | this, &PhononWidget::slotHasVideoChanged); | 232 | this, &PhononWidget::finished); | ||
187 | m_seekSlider->setMediaObject(m_media); | 233 | m_seekSlider->setMediaObject(m_media); | ||
188 | } | 234 | } | ||
189 | 235 | | |||
190 | if (!m_videoPlayer) { | 236 | if (!m_videoPlayer) { | ||
191 | m_videoPlayer = new EmbeddedVideoPlayer(this); | 237 | m_videoPlayer = new EmbeddedVideoPlayer(this); | ||
238 | m_videoPlayer->installEventFilter(this); | ||||
192 | m_topLayout->insertWidget(0, m_videoPlayer); | 239 | m_topLayout->insertWidget(0, m_videoPlayer); | ||
193 | Phonon::createPath(m_media, m_videoPlayer); | 240 | Phonon::createPath(m_media, m_videoPlayer); | ||
194 | applyVideoSize(); | 241 | applyVideoSize(); | ||
195 | } | 242 | } | ||
196 | 243 | | |||
197 | if (!m_audioOutput) { | 244 | if (!m_audioOutput) { | ||
198 | m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this); | 245 | m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this); | ||
199 | Phonon::createPath(m_media, m_audioOutput); | 246 | Phonon::createPath(m_media, m_audioOutput); | ||
200 | } | 247 | } | ||
201 | 248 | | |||
202 | emit hasVideoChanged(false); | 249 | if (m_isVideo) { | ||
250 | m_videoPlayer->setToolTip(i18n("Pause")); | ||||
251 | emit hasVideoChanged(true); | ||||
252 | } | ||||
203 | 253 | | |||
254 | if (m_url != m_media->currentSource().url()) { | ||||
204 | m_media->setCurrentSource(m_url); | 255 | m_media->setCurrentSource(m_url); | ||
205 | m_media->hasVideo(); | 256 | } | ||
206 | m_media->play(); | 257 | m_media->play(); | ||
258 | | ||||
259 | m_videoPlayer->setVisible(m_isVideo); | ||||
207 | } | 260 | } | ||
208 | 261 | | |||
209 | void PhononWidget::stop() | 262 | void PhononWidget::finished() | ||
210 | { | 263 | { | ||
211 | if (m_media) { | 264 | if (m_isVideo) { | ||
212 | m_media->stop(); | 265 | m_videoPlayer->hide(); | ||
266 | emit hasVideoChanged(false); | ||||
213 | } | 267 | } | ||
214 | } | 268 | } | ||
215 | 269 | | |||
216 | void PhononWidget::slotHasVideoChanged(bool hasVideo) | 270 | void PhononWidget::stop() | ||
217 | { | 271 | { | ||
218 | emit hasVideoChanged(hasVideo); | 272 | if (m_media) { | ||
219 | 273 | m_media->stop(); | |||
220 | if (hasVideo) { | 274 | m_videoPlayer->hide(); | ||
221 | m_videoPlayer->show(); | 275 | emit hasVideoChanged(false); | ||
222 | } | 276 | } | ||
223 | } | 277 | } | ||
224 | 278 | | |||
225 | void PhononWidget::applyVideoSize() | 279 | void PhononWidget::applyVideoSize() | ||
226 | { | 280 | { | ||
227 | if ((m_videoPlayer) && m_videoSize.isValid()) { | 281 | if ((m_videoPlayer) && m_videoSize.isValid()) { | ||
228 | m_videoPlayer->setSizeHint(m_videoSize); | 282 | m_videoPlayer->setSizeHint(m_videoSize); | ||
229 | } | 283 | } | ||
230 | } | 284 | } | ||
231 | 285 | | |||
232 | #include "phononwidget.moc" | 286 | #include "phononwidget.moc" |
Add a context this or else it will crash when timer fires with the widget being gone:
You can also probably just connect to &PhononWidget::play directly without a lambda