2016-07-11 18:05:46 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-07-11 18:05:46 +00:00
|
|
|
*/
|
|
|
|
#include "media/view/media_clip_controller.h"
|
|
|
|
|
|
|
|
#include "media/view/media_clip_playback.h"
|
|
|
|
#include "media/view/media_clip_volume_controller.h"
|
|
|
|
#include "styles/style_mediaview.h"
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
2016-11-22 09:48:13 +00:00
|
|
|
#include "ui/widgets/continuous_sliders.h"
|
2017-09-30 18:26:45 +00:00
|
|
|
#include "ui/effects/fade_animation.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2016-07-11 18:05:46 +00:00
|
|
|
#include "media/media_audio.h"
|
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
|
|
|
|
Controller::Controller(QWidget *parent) : TWidget(parent)
|
|
|
|
, _playPauseResume(this, st::mediaviewPlayButton)
|
2017-05-18 16:10:39 +00:00
|
|
|
, _playbackSlider(this, st::mediaviewPlayback)
|
|
|
|
, _playback(std::make_unique<Playback>())
|
2016-07-11 18:05:46 +00:00
|
|
|
, _volumeController(this)
|
|
|
|
, _fullScreenToggle(this, st::mediaviewFullScreenButton)
|
|
|
|
, _playedAlready(this, st::mediaviewPlayProgressLabel)
|
|
|
|
, _toPlayLeft(this, st::mediaviewPlayProgressLabel)
|
2017-02-21 13:45:56 +00:00
|
|
|
, _fadeAnimation(std::make_unique<Ui::FadeAnimation>(this)) {
|
2016-07-11 18:05:46 +00:00
|
|
|
_fadeAnimation->show();
|
2016-09-26 12:09:59 +00:00
|
|
|
_fadeAnimation->setFinishedCallback([this] { fadeFinished(); });
|
|
|
|
_fadeAnimation->setUpdatedCallback([this](float64 opacity) { fadeUpdated(opacity); });
|
2016-07-12 14:11:59 +00:00
|
|
|
|
|
|
|
_volumeController->setVolume(Global::VideoVolume());
|
|
|
|
|
2016-07-11 18:05:46 +00:00
|
|
|
connect(_playPauseResume, SIGNAL(clicked()), this, SIGNAL(playPressed()));
|
|
|
|
connect(_fullScreenToggle, SIGNAL(clicked()), this, SIGNAL(toFullScreenPressed()));
|
|
|
|
connect(_volumeController, SIGNAL(volumeChanged(float64)), this, SIGNAL(volumeChanged(float64)));
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2017-05-18 16:10:39 +00:00
|
|
|
_playback->setInLoadingStateChangedCallback([this](bool loading) {
|
|
|
|
_playbackSlider->setDisabled(loading);
|
|
|
|
});
|
|
|
|
_playback->setValueChangedCallback([this](float64 value) {
|
|
|
|
_playbackSlider->setValue(value);
|
|
|
|
});
|
|
|
|
_playbackSlider->setChangeProgressCallback([this](float64 value) {
|
|
|
|
_playback->setValue(value, false);
|
2017-05-31 08:58:43 +00:00
|
|
|
handleSeekProgress(value); // This may destroy Controller.
|
2016-09-23 16:04:26 +00:00
|
|
|
});
|
2017-05-18 16:10:39 +00:00
|
|
|
_playbackSlider->setChangeFinishedCallback([this](float64 value) {
|
|
|
|
_playback->setValue(value, false);
|
2017-05-31 08:58:43 +00:00
|
|
|
handleSeekFinished(value);
|
2016-09-23 16:04:26 +00:00
|
|
|
});
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
void Controller::handleSeekProgress(float64 progress) {
|
2016-07-13 17:34:57 +00:00
|
|
|
if (!_lastDurationMs) return;
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
auto positionMs = snap(static_cast<TimeMs>(progress * _lastDurationMs), 0LL, _lastDurationMs);
|
2016-07-13 17:34:57 +00:00
|
|
|
if (_seekPositionMs != positionMs) {
|
|
|
|
_seekPositionMs = positionMs;
|
|
|
|
refreshTimeTexts();
|
2016-07-29 16:33:49 +00:00
|
|
|
emit seekProgress(positionMs); // This may destroy Controller.
|
2016-07-13 17:34:57 +00:00
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
void Controller::handleSeekFinished(float64 progress) {
|
2016-07-13 17:34:57 +00:00
|
|
|
if (!_lastDurationMs) return;
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
auto positionMs = snap(static_cast<TimeMs>(progress * _lastDurationMs), 0LL, _lastDurationMs);
|
2016-07-13 17:34:57 +00:00
|
|
|
_seekPositionMs = -1;
|
|
|
|
emit seekFinished(positionMs);
|
|
|
|
refreshTimeTexts();
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::showAnimated() {
|
2016-07-12 11:38:16 +00:00
|
|
|
startFading([this]() {
|
2016-12-23 13:21:01 +00:00
|
|
|
_fadeAnimation->fadeIn(st::mediaviewShowDuration);
|
2016-07-12 11:38:16 +00:00
|
|
|
});
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::hideAnimated() {
|
2016-07-12 11:38:16 +00:00
|
|
|
startFading([this]() {
|
2016-12-23 13:21:01 +00:00
|
|
|
_fadeAnimation->fadeOut(st::mediaviewHideDuration);
|
2016-07-12 11:38:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callback>
|
|
|
|
void Controller::startFading(Callback start) {
|
|
|
|
start();
|
2017-05-18 16:10:39 +00:00
|
|
|
_playbackSlider->show();
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::fadeFinished() {
|
|
|
|
fadeUpdated(1.);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::fadeUpdated(float64 opacity) {
|
2017-05-18 16:10:39 +00:00
|
|
|
_playbackSlider->setFadeOpacity(opacity);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
void Controller::updatePlayback(const Player::TrackState &state) {
|
|
|
|
updatePlayPauseResumeState(state);
|
|
|
|
_playback->updateState(state);
|
|
|
|
updateTimeTexts(state);
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
void Controller::updatePlayPauseResumeState(const Player::TrackState &state) {
|
|
|
|
auto showPause = (state.state == Player::State::Playing || state.state == Player::State::Resuming || _seekPositionMs >= 0);
|
2016-07-11 18:05:46 +00:00
|
|
|
if (showPause != _showPause) {
|
|
|
|
disconnect(_playPauseResume, SIGNAL(clicked()), this, _showPause ? SIGNAL(pausePressed()) : SIGNAL(playPressed()));
|
|
|
|
_showPause = showPause;
|
|
|
|
connect(_playPauseResume, SIGNAL(clicked()), this, _showPause ? SIGNAL(pausePressed()) : SIGNAL(playPressed()));
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
_playPauseResume->setIconOverride(_showPause ? &st::mediaviewPauseIcon : nullptr, _showPause ? &st::mediaviewPauseIconOver : nullptr);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
void Controller::updateTimeTexts(const Player::TrackState &state) {
|
2017-05-03 13:01:15 +00:00
|
|
|
qint64 position = 0, length = state.length;
|
2016-07-12 11:38:16 +00:00
|
|
|
|
2017-05-21 13:16:39 +00:00
|
|
|
if (Player::IsStoppedAtEnd(state.state)) {
|
2017-05-03 13:01:15 +00:00
|
|
|
position = state.length;
|
2017-05-21 13:16:39 +00:00
|
|
|
} else if (!Player::IsStoppedOrStopping(state.state)) {
|
|
|
|
position = state.position;
|
2016-07-12 11:38:16 +00:00
|
|
|
} else {
|
|
|
|
position = 0;
|
|
|
|
}
|
2017-01-24 21:24:39 +00:00
|
|
|
auto playFrequency = state.frequency;
|
2016-07-12 11:38:16 +00:00
|
|
|
auto playAlready = position / playFrequency;
|
2017-05-03 13:01:15 +00:00
|
|
|
auto playLeft = (state.length / playFrequency) - playAlready;
|
2016-07-12 11:38:16 +00:00
|
|
|
|
2017-05-03 13:01:15 +00:00
|
|
|
_lastDurationMs = (state.length * 1000LL) / playFrequency;
|
2016-07-13 17:34:57 +00:00
|
|
|
|
|
|
|
_timeAlready = formatDurationText(playAlready);
|
2016-07-12 11:38:16 +00:00
|
|
|
auto minus = QChar(8722);
|
2016-07-13 17:34:57 +00:00
|
|
|
_timeLeft = minus + formatDurationText(playLeft);
|
2016-07-12 11:38:16 +00:00
|
|
|
|
2016-07-13 17:34:57 +00:00
|
|
|
if (_seekPositionMs < 0) {
|
|
|
|
refreshTimeTexts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::refreshTimeTexts() {
|
2016-07-12 11:38:16 +00:00
|
|
|
auto alreadyChanged = false, leftChanged = false;
|
2016-07-13 17:34:57 +00:00
|
|
|
auto timeAlready = _timeAlready;
|
|
|
|
auto timeLeft = _timeLeft;
|
|
|
|
if (_seekPositionMs >= 0) {
|
|
|
|
auto playAlready = _seekPositionMs / 1000LL;
|
|
|
|
auto playLeft = (_lastDurationMs / 1000LL) - playAlready;
|
|
|
|
|
|
|
|
timeAlready = formatDurationText(playAlready);
|
|
|
|
auto minus = QChar(8722);
|
|
|
|
timeLeft = minus + formatDurationText(playLeft);
|
|
|
|
}
|
|
|
|
|
2016-07-12 11:38:16 +00:00
|
|
|
_playedAlready->setText(timeAlready, &alreadyChanged);
|
|
|
|
_toPlayLeft->setText(timeLeft, &leftChanged);
|
|
|
|
if (alreadyChanged || leftChanged) {
|
2016-07-16 06:54:02 +00:00
|
|
|
resizeEvent(nullptr);
|
2016-07-12 11:38:16 +00:00
|
|
|
_fadeAnimation->refreshCache();
|
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::setInFullScreen(bool inFullScreen) {
|
2016-12-05 11:01:08 +00:00
|
|
|
_fullScreenToggle->setIconOverride(inFullScreen ? &st::mediaviewFullScreenOutIcon : nullptr, inFullScreen ? &st::mediaviewFullScreenOutIconOver : nullptr);
|
2016-07-11 18:05:46 +00:00
|
|
|
disconnect(_fullScreenToggle, SIGNAL(clicked()), this, SIGNAL(toFullScreenPressed()));
|
|
|
|
disconnect(_fullScreenToggle, SIGNAL(clicked()), this, SIGNAL(fromFullScreenPressed()));
|
|
|
|
|
|
|
|
auto handler = inFullScreen ? SIGNAL(fromFullScreenPressed()) : SIGNAL(toFullScreenPressed());
|
|
|
|
connect(_fullScreenToggle, SIGNAL(clicked()), this, handler);
|
|
|
|
}
|
|
|
|
|
2016-07-12 11:38:16 +00:00
|
|
|
void Controller::grabStart() {
|
|
|
|
showChildren();
|
2017-05-18 16:10:39 +00:00
|
|
|
_playbackSlider->hide();
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::grabFinish() {
|
|
|
|
hideChildren();
|
2017-05-18 16:10:39 +00:00
|
|
|
_playbackSlider->show();
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:05:46 +00:00
|
|
|
void Controller::resizeEvent(QResizeEvent *e) {
|
|
|
|
int playTop = (height() - _playPauseResume->height()) / 2;
|
2016-08-03 11:02:53 +00:00
|
|
|
_playPauseResume->moveToLeft(st::mediaviewPlayPauseLeft, playTop);
|
2016-07-11 18:05:46 +00:00
|
|
|
|
|
|
|
int fullScreenTop = (height() - _fullScreenToggle->height()) / 2;
|
2016-08-03 12:32:44 +00:00
|
|
|
_fullScreenToggle->moveToRight(st::mediaviewFullScreenLeft, fullScreenTop);
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2016-08-03 12:32:44 +00:00
|
|
|
_volumeController->moveToRight(st::mediaviewFullScreenLeft + _fullScreenToggle->width() + st::mediaviewVolumeLeft, (height() - _volumeController->height()) / 2);
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2017-05-18 16:10:39 +00:00
|
|
|
auto playbackWidth = width() - st::mediaviewPlayPauseLeft - _playPauseResume->width() - playTop - fullScreenTop - _volumeController->width() - st::mediaviewVolumeLeft - _fullScreenToggle->width() - st::mediaviewFullScreenLeft;
|
|
|
|
_playbackSlider->resize(playbackWidth, st::mediaviewPlayback.seekSize.height());
|
|
|
|
_playbackSlider->moveToLeft(st::mediaviewPlayPauseLeft + _playPauseResume->width() + playTop, st::mediaviewPlaybackTop);
|
2016-07-12 11:38:16 +00:00
|
|
|
|
2016-08-03 11:02:53 +00:00
|
|
|
_playedAlready->moveToLeft(st::mediaviewPlayPauseLeft + _playPauseResume->width() + playTop, st::mediaviewPlayProgressTop);
|
2016-09-23 16:04:26 +00:00
|
|
|
_toPlayLeft->moveToRight(width() - (st::mediaviewPlayPauseLeft + _playPauseResume->width() + playTop) - playbackWidth, st::mediaviewPlayProgressTop);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
if (_fadeAnimation->paint(p)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-21 15:05:58 +00:00
|
|
|
App::roundRect(p, rect(), st::mediaviewSaveMsgBg, MediaviewSaveCorners);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 14:11:59 +00:00
|
|
|
void Controller::mousePressEvent(QMouseEvent *e) {
|
|
|
|
e->accept(); // Don't pass event to the MediaView.
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
Controller::~Controller() = default;
|
2016-07-19 12:31:48 +00:00
|
|
|
|
2016-07-11 18:05:46 +00:00
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|