2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2016-02-08 10:56:18 +00:00
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "lang.h"
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
#include "media/media_clip_reader.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "layerwidget.h"
|
|
|
|
#include "application.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-07 18:05:28 +00:00
|
|
|
#include "ui/filedialog.h"
|
2016-09-10 20:54:59 +00:00
|
|
|
#include "styles/style_stickers.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr int kStickerPreviewEmojiLimit = 10;
|
|
|
|
|
|
|
|
} // namespace
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerWidget::setInnerFocus() {
|
|
|
|
auto focused = App::wnd()->focusWidget();
|
|
|
|
if (!isAncestorOf(focused)) {
|
|
|
|
doSetInnerFocus();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
class LayerStackWidget::BackgroundWidget : public TWidget {
|
|
|
|
public:
|
|
|
|
BackgroundWidget(QWidget *parent) : TWidget(parent)
|
|
|
|
, _shadow(st::boxShadow) {
|
|
|
|
}
|
|
|
|
|
2016-08-19 17:26:31 +00:00
|
|
|
void setLayerBox(const QRect &box, const QRect &hiddenSpecialBox) {
|
2016-08-16 16:53:10 +00:00
|
|
|
_box = box;
|
2016-08-19 17:26:31 +00:00
|
|
|
_hiddenSpecialBox = hiddenSpecialBox;
|
2016-08-16 16:53:10 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
void setOpacity(float64 opacity) {
|
|
|
|
_opacity = opacity;
|
|
|
|
}
|
2014-12-12 16:27:03 +00:00
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override {
|
|
|
|
Painter p(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
p.setOpacity(st::layerAlpha * _opacity);
|
|
|
|
if (_box.isNull()) {
|
|
|
|
p.fillRect(rect(), st::layerBg);
|
|
|
|
} else {
|
|
|
|
auto clip = QRegion(rect()) - _box;
|
|
|
|
for (auto &r : clip.rects()) {
|
|
|
|
p.fillRect(r, st::layerBg);
|
|
|
|
}
|
|
|
|
p.setClipRegion(clip);
|
|
|
|
p.setOpacity(_opacity);
|
|
|
|
_shadow.paint(p, _box, st::boxShadowShift);
|
2016-08-19 17:26:31 +00:00
|
|
|
if (!_hiddenSpecialBox.isNull()) {
|
|
|
|
p.setClipRegion(QRegion(rect()) - _hiddenSpecialBox);
|
|
|
|
_shadow.paint(p, _hiddenSpecialBox, st::boxShadowShift);
|
|
|
|
}
|
2016-08-16 16:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-08-19 17:26:31 +00:00
|
|
|
QRect _box, _hiddenSpecialBox;
|
2016-08-16 16:53:10 +00:00
|
|
|
float64 _opacity = 0.;
|
|
|
|
|
2016-10-01 12:34:23 +00:00
|
|
|
Ui::RectShadow _shadow;
|
2016-08-16 16:53:10 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
LayerStackWidget::LayerStackWidget(QWidget *parent) : TWidget(parent)
|
|
|
|
, _background(this)
|
|
|
|
, a_bg(0)
|
|
|
|
, a_layer(0)
|
|
|
|
, _a_background(animation(this, &LayerStackWidget::step_background)) {
|
|
|
|
setGeometry(parentWidget()->rect());
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::paintEvent(QPaintEvent *e) {
|
|
|
|
if (!layer() && !_specialLayer && _layerCache.isNull()) {
|
|
|
|
return;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
if (!_layerCache.isNull()) {
|
|
|
|
Painter p(this);
|
|
|
|
p.setClipRect(rect());
|
|
|
|
p.setOpacity(a_layer.current());
|
2016-08-19 17:26:31 +00:00
|
|
|
if (!_hiddenSpecialLayerCache.isNull()) {
|
|
|
|
p.drawPixmap(_hiddenSpecialLayerCacheBox.topLeft(), _hiddenSpecialLayerCache);
|
|
|
|
}
|
2016-08-16 16:53:10 +00:00
|
|
|
p.drawPixmap(_layerCacheBox.topLeft(), _layerCache);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::keyPressEvent(QKeyEvent *e) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (e->key() == Qt::Key_Escape) {
|
2016-08-22 17:16:21 +00:00
|
|
|
onCloseCurrent();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::mousePressEvent(QMouseEvent *e) {
|
2016-08-22 17:16:21 +00:00
|
|
|
onCloseCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::onCloseCurrent() {
|
2016-08-19 17:26:31 +00:00
|
|
|
if (layer()) {
|
|
|
|
onCloseLayers();
|
|
|
|
} else {
|
|
|
|
onClose();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 15:14:08 +00:00
|
|
|
void LayerStackWidget::onCloseLayers() {
|
|
|
|
if (_specialLayer) {
|
|
|
|
clearLayers();
|
|
|
|
fixOrder();
|
|
|
|
if (App::wnd()) App::wnd()->setInnerFocus();
|
|
|
|
} else {
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::onClose() {
|
2014-05-30 08:53:19 +00:00
|
|
|
startHide();
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::onLayerClosed(LayerWidget *l) {
|
|
|
|
l->deleteLater();
|
|
|
|
if (l == _specialLayer) {
|
2014-05-30 08:53:19 +00:00
|
|
|
onClose();
|
2016-08-16 16:53:10 +00:00
|
|
|
_specialLayer = nullptr;
|
|
|
|
} else if (l == layer()) {
|
|
|
|
_layers.pop_back();
|
|
|
|
if (auto newLayer = layer()) {
|
|
|
|
l->hide();
|
|
|
|
newLayer->parentResized();
|
|
|
|
if (!_a_background.animating()) {
|
|
|
|
newLayer->show();
|
|
|
|
}
|
|
|
|
} else if (_specialLayer) {
|
|
|
|
l->hide();
|
|
|
|
} else {
|
|
|
|
_layers.push_back(l); // For animation cache grab.
|
|
|
|
onClose();
|
|
|
|
_layers.pop_back();
|
|
|
|
}
|
|
|
|
fixOrder();
|
|
|
|
if (App::wnd()) App::wnd()->setInnerFocus();
|
|
|
|
updateLayerBox();
|
2016-08-19 17:26:31 +00:00
|
|
|
sendFakeMouseEvent();
|
2016-08-16 16:53:10 +00:00
|
|
|
} else {
|
|
|
|
for (auto i = _layers.begin(), e = _layers.end(); i != e; ++i) {
|
|
|
|
if (l == *i) {
|
|
|
|
_layers.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-08-16 16:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::onLayerResized() {
|
|
|
|
updateLayerBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::updateLayerBox() {
|
|
|
|
auto getLayerBox = [this]() {
|
|
|
|
if (!_layerCache.isNull()) {
|
|
|
|
return _layerCacheBox;
|
|
|
|
} else if (auto l = layer()) {
|
|
|
|
return l->geometry();
|
|
|
|
} else if (_specialLayer) {
|
|
|
|
return _specialLayer->geometry();
|
|
|
|
}
|
|
|
|
return QRect();
|
|
|
|
};
|
2016-08-19 17:26:31 +00:00
|
|
|
auto getSpecialLayerBox = [this]() {
|
|
|
|
if (!_layerCache.isNull()) {
|
|
|
|
return _hiddenSpecialLayerCacheBox;
|
|
|
|
} else if (auto l = layer()) {
|
|
|
|
return _specialLayer ? _specialLayer->geometry() : QRect();
|
|
|
|
}
|
|
|
|
return QRect();
|
|
|
|
};
|
|
|
|
_background->setLayerBox(getLayerBox(), getSpecialLayerBox());
|
2014-11-25 12:15:29 +00:00
|
|
|
update();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::startShow() {
|
|
|
|
startAnimation(1);
|
|
|
|
show();
|
|
|
|
}
|
2015-10-15 11:51:10 +00:00
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::showFast() {
|
|
|
|
if (_a_background.animating()) {
|
|
|
|
_a_background.step(getms() + st::layerSlideDuration + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::startHide() {
|
|
|
|
if (isHidden() || _hiding) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_hiding = true;
|
|
|
|
startAnimation(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::startAnimation(float64 toOpacity) {
|
|
|
|
if (App::app()) App::app()->mtpPause();
|
|
|
|
a_bg.start(toOpacity);
|
|
|
|
a_layer.start(toOpacity);
|
2015-12-08 12:33:37 +00:00
|
|
|
_a_background.start();
|
2016-08-16 16:53:10 +00:00
|
|
|
if (_layerCache.isNull()) {
|
2016-08-28 20:23:32 +00:00
|
|
|
if (auto cacheLayer = layer() ? layer() : _specialLayer.ptr()) {
|
2016-08-16 16:53:10 +00:00
|
|
|
_layerCache = myGrab(cacheLayer);
|
|
|
|
_layerCacheBox = cacheLayer->geometry();
|
2016-08-19 17:26:31 +00:00
|
|
|
if (layer() && _specialLayer) {
|
|
|
|
_hiddenSpecialLayerCache = myGrab(_specialLayer);
|
|
|
|
_hiddenSpecialLayerCacheBox = _specialLayer->geometry();
|
|
|
|
}
|
2016-08-16 16:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_specialLayer) {
|
|
|
|
_specialLayer->hide();
|
|
|
|
}
|
|
|
|
if (auto l = layer()) {
|
|
|
|
l->hide();
|
|
|
|
}
|
|
|
|
updateLayerBox();
|
|
|
|
if (App::wnd()) App::wnd()->setInnerFocus();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
bool LayerStackWidget::canSetFocus() const {
|
|
|
|
return (layer() || _specialLayer) && !_hiding;
|
2015-09-16 13:04:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::setInnerFocus() {
|
|
|
|
if (_a_background.animating()) {
|
|
|
|
setFocus();
|
|
|
|
} else if (auto l = layer()) {
|
|
|
|
l->setInnerFocus();
|
|
|
|
} else if (_specialLayer) {
|
|
|
|
_specialLayer->setInnerFocus();
|
2015-09-16 13:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
bool LayerStackWidget::contentOverlapped(const QRect &globalRect) {
|
|
|
|
if (isHidden()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_specialLayer && _specialLayer->overlaps(globalRect)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (auto l = layer()) {
|
|
|
|
return l->overlaps(globalRect);
|
|
|
|
}
|
|
|
|
return false;
|
2015-10-01 14:05:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::resizeEvent(QResizeEvent *e) {
|
|
|
|
_background->setGeometry(rect());
|
|
|
|
if (_specialLayer) {
|
|
|
|
_specialLayer->parentResized();
|
|
|
|
}
|
|
|
|
if (auto l = layer()) {
|
|
|
|
l->parentResized();
|
|
|
|
}
|
|
|
|
updateLayerBox();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::showLayer(LayerWidget *l) {
|
|
|
|
clearLayers();
|
|
|
|
appendLayer(l);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::showSpecialLayer(LayerWidget *l) {
|
|
|
|
clearLayers();
|
|
|
|
if (_specialLayer) {
|
|
|
|
_specialLayer->hide();
|
|
|
|
_specialLayer->deleteLater();
|
|
|
|
}
|
|
|
|
_specialLayer = l;
|
|
|
|
activateLayer(l);
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::appendLayer(LayerWidget *l) {
|
|
|
|
if (auto oldLayer = layer()) {
|
|
|
|
oldLayer->hide();
|
|
|
|
}
|
|
|
|
_layers.push_back(l);
|
|
|
|
activateLayer(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::prependLayer(LayerWidget *l) {
|
|
|
|
if (_layers.empty()) {
|
|
|
|
showLayer(l);
|
|
|
|
} else {
|
|
|
|
l->hide();
|
|
|
|
_layers.push_front(l);
|
|
|
|
initChildLayer(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::clearLayers() {
|
|
|
|
for_const (auto oldLayer, _layers) {
|
|
|
|
oldLayer->hide();
|
|
|
|
oldLayer->deleteLater();
|
|
|
|
}
|
|
|
|
_layers.clear();
|
2016-08-19 17:26:31 +00:00
|
|
|
updateLayerBox();
|
|
|
|
sendFakeMouseEvent();
|
2016-08-16 16:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::initChildLayer(LayerWidget *l) {
|
|
|
|
l->setParent(this);
|
|
|
|
connect(l, SIGNAL(closed(LayerWidget*)), this, SLOT(onLayerClosed(LayerWidget*)));
|
|
|
|
connect(l, SIGNAL(resized()), this, SLOT(onLayerResized()));
|
|
|
|
connect(l, SIGNAL(destroyed(QObject*)), this, SLOT(onLayerDestroyed(QObject*)));
|
|
|
|
l->parentResized();
|
|
|
|
fixOrder();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::activateLayer(LayerWidget *l) {
|
|
|
|
initChildLayer(l);
|
|
|
|
if (isHidden()) {
|
|
|
|
startShow();
|
|
|
|
} else {
|
|
|
|
l->show();
|
2016-09-10 20:54:59 +00:00
|
|
|
l->showDone();
|
2016-08-16 16:53:10 +00:00
|
|
|
if (App::wnd()) App::wnd()->setInnerFocus();
|
|
|
|
updateLayerBox();
|
|
|
|
}
|
|
|
|
fixOrder();
|
2016-08-19 17:26:31 +00:00
|
|
|
sendFakeMouseEvent();
|
2016-08-16 16:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayerStackWidget::fixOrder() {
|
|
|
|
if (auto l = layer()) {
|
|
|
|
_background->raise();
|
|
|
|
l->raise();
|
|
|
|
} else if (_specialLayer) {
|
|
|
|
_specialLayer->raise();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 17:26:31 +00:00
|
|
|
void LayerStackWidget::sendFakeMouseEvent() {
|
|
|
|
sendSynteticMouseEvent(this, QEvent::MouseMove, Qt::NoButton);
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::step_background(float64 ms, bool timer) {
|
|
|
|
float64 dt = ms / (_hiding ? st::layerHideDuration : st::layerSlideDuration);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (dt >= 1) {
|
2015-12-08 12:33:37 +00:00
|
|
|
a_bg.finish();
|
2016-08-16 16:53:10 +00:00
|
|
|
a_layer.finish();
|
|
|
|
_a_background.stop();
|
2016-08-19 17:26:31 +00:00
|
|
|
_layerCache = _hiddenSpecialLayerCache = QPixmap();
|
2016-08-16 16:53:10 +00:00
|
|
|
if (_hiding) {
|
2014-12-12 16:27:03 +00:00
|
|
|
App::wnd()->layerFinishedHide(this);
|
2016-08-16 16:53:10 +00:00
|
|
|
} else {
|
|
|
|
if (_specialLayer) {
|
|
|
|
_specialLayer->show();
|
|
|
|
_specialLayer->showDone();
|
|
|
|
}
|
|
|
|
if (auto l = layer()) {
|
|
|
|
l->show();
|
|
|
|
l->showDone();
|
|
|
|
}
|
|
|
|
fixOrder();
|
|
|
|
if (App::wnd()) App::wnd()->setInnerFocus();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-08-16 16:53:10 +00:00
|
|
|
updateLayerBox();
|
2015-10-15 11:51:10 +00:00
|
|
|
if (App::app()) App::app()->mtpUnpause();
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2015-12-08 12:33:37 +00:00
|
|
|
a_bg.update(dt, anim::easeOutCirc);
|
2016-08-16 16:53:10 +00:00
|
|
|
a_layer.update(dt, anim::linear);
|
|
|
|
}
|
|
|
|
_background->setOpacity(a_bg.current());
|
|
|
|
if (timer) {
|
|
|
|
_background->update();
|
|
|
|
update();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
void LayerStackWidget::onLayerDestroyed(QObject *obj) {
|
|
|
|
if (obj == _specialLayer) {
|
|
|
|
_specialLayer = nullptr;
|
|
|
|
onClose();
|
|
|
|
} else if (obj == layer()) {
|
|
|
|
_layers.pop_back();
|
|
|
|
if (auto newLayer = layer()) {
|
|
|
|
newLayer->parentResized();
|
|
|
|
if (!_a_background.animating()) {
|
|
|
|
newLayer->show();
|
|
|
|
}
|
|
|
|
} else if (!_specialLayer) {
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
fixOrder();
|
|
|
|
if (App::wnd()) App::wnd()->setInnerFocus();
|
|
|
|
updateLayerBox();
|
2015-09-16 13:04:08 +00:00
|
|
|
} else {
|
2016-08-16 16:53:10 +00:00
|
|
|
for (auto i = _layers.begin(), e = _layers.end(); i != e; ++i) {
|
|
|
|
if (obj == *i) {
|
|
|
|
_layers.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
2015-09-16 13:04:08 +00:00
|
|
|
}
|
2014-12-12 16:27:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:53:10 +00:00
|
|
|
LayerStackWidget::~LayerStackWidget() {
|
|
|
|
if (App::wnd()) App::wnd()->noLayerStack(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-12-07 13:05:00 +00:00
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
MediaPreviewWidget::MediaPreviewWidget(QWidget *parent) : TWidget(parent)
|
2015-12-07 13:05:00 +00:00
|
|
|
, a_shown(0, 0)
|
2016-09-10 20:54:59 +00:00
|
|
|
, _a_shown(animation(this, &MediaPreviewWidget::step_shown))
|
|
|
|
, _emojiSize(EmojiSizes[EIndex + 1] / cIntRetinaFactor()) {
|
2015-12-07 13:05:00 +00:00
|
|
|
setAttribute(Qt::WA_TransparentForMouseEvents);
|
2016-09-26 13:57:08 +00:00
|
|
|
subscribe(FileDownload::ImageLoaded(), [this] { update(); });
|
2015-12-07 13:05:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
void MediaPreviewWidget::paintEvent(QPaintEvent *e) {
|
2015-12-07 13:05:00 +00:00
|
|
|
Painter p(this);
|
|
|
|
QRect r(e->rect());
|
|
|
|
|
2016-09-20 13:03:42 +00:00
|
|
|
auto image = currentImage();
|
2016-09-10 20:54:59 +00:00
|
|
|
int w = image.width() / cIntRetinaFactor(), h = image.height() / cIntRetinaFactor();
|
2015-12-07 13:05:00 +00:00
|
|
|
if (_a_shown.animating()) {
|
|
|
|
float64 shown = a_shown.current();
|
|
|
|
p.setOpacity(shown);
|
|
|
|
// w = qMax(qRound(w * (st::stickerPreviewMin + ((1. - st::stickerPreviewMin) * shown)) / 2.) * 2 + int(w % 2), 1);
|
|
|
|
// h = qMax(qRound(h * (st::stickerPreviewMin + ((1. - st::stickerPreviewMin) * shown)) / 2.) * 2 + int(h % 2), 1);
|
|
|
|
}
|
|
|
|
p.fillRect(r, st::stickerPreviewBg);
|
2016-09-10 20:54:59 +00:00
|
|
|
p.drawPixmap((width() - w) / 2, (height() - h) / 2, image);
|
|
|
|
if (!_emojiList.isEmpty()) {
|
|
|
|
int emojiCount = _emojiList.size();
|
2016-09-25 19:52:11 +00:00
|
|
|
int emojiWidth = (emojiCount * _emojiSize) + (emojiCount - 1) * st::stickerEmojiSkip;
|
2016-09-10 20:54:59 +00:00
|
|
|
int emojiLeft = (width() - emojiWidth) / 2;
|
2016-09-25 19:52:11 +00:00
|
|
|
int esize = EmojiSizes[EIndex + 1];
|
2016-09-10 20:54:59 +00:00
|
|
|
for_const (auto emoji, _emojiList) {
|
2016-09-25 19:52:11 +00:00
|
|
|
p.drawPixmapLeft(emojiLeft, (height() - h) / 2 - (_emojiSize * 2), width(), App::emojiLarge(), QRect(emoji->x * esize, emoji->y * esize, esize, esize));
|
2016-09-10 20:54:59 +00:00
|
|
|
emojiLeft += _emojiSize + st::stickerEmojiSkip;
|
|
|
|
}
|
|
|
|
}
|
2015-12-07 13:05:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
void MediaPreviewWidget::resizeEvent(QResizeEvent *e) {
|
2015-12-07 13:05:00 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
void MediaPreviewWidget::step_shown(float64 ms, bool timer) {
|
2015-12-07 13:05:00 +00:00
|
|
|
float64 dt = ms / st::stickerPreviewDuration;
|
|
|
|
if (dt >= 1) {
|
|
|
|
_a_shown.stop();
|
2015-12-08 12:33:37 +00:00
|
|
|
a_shown.finish();
|
2015-12-07 13:05:00 +00:00
|
|
|
if (a_shown.current() < 0.5) hide();
|
|
|
|
} else {
|
|
|
|
a_shown.update(dt, anim::linear);
|
|
|
|
}
|
2015-12-08 12:33:37 +00:00
|
|
|
if (timer) update();
|
2015-12-07 13:05:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
void MediaPreviewWidget::showPreview(DocumentData *document) {
|
|
|
|
if (!document || (!document->isAnimation() && !document->sticker())) {
|
|
|
|
hidePreview();
|
2015-12-07 13:05:00 +00:00
|
|
|
return;
|
2016-04-10 18:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
startShow();
|
|
|
|
_photo = nullptr;
|
|
|
|
_document = document;
|
2016-09-10 20:54:59 +00:00
|
|
|
fillEmojiString();
|
2016-04-10 18:18:26 +00:00
|
|
|
resetGifAndCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaPreviewWidget::showPreview(PhotoData *photo) {
|
|
|
|
if (!photo || photo->full->isNull()) {
|
|
|
|
hidePreview();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
startShow();
|
|
|
|
_photo = photo;
|
|
|
|
_document = nullptr;
|
|
|
|
resetGifAndCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaPreviewWidget::startShow() {
|
|
|
|
_cache = QPixmap();
|
|
|
|
if (isHidden() || _a_shown.animating()) {
|
|
|
|
if (isHidden()) show();
|
|
|
|
a_shown.start(1);
|
2015-12-07 13:05:00 +00:00
|
|
|
_a_shown.start();
|
2016-04-10 18:18:26 +00:00
|
|
|
} else {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaPreviewWidget::hidePreview() {
|
|
|
|
if (isHidden()) {
|
|
|
|
return;
|
2015-12-07 13:05:00 +00:00
|
|
|
}
|
2016-04-10 18:18:26 +00:00
|
|
|
if (_gif) _cache = currentImage();
|
|
|
|
a_shown.start(0);
|
|
|
|
_a_shown.start();
|
|
|
|
_photo = nullptr;
|
|
|
|
_document = nullptr;
|
|
|
|
resetGifAndCache();
|
|
|
|
}
|
|
|
|
|
2016-09-10 20:54:59 +00:00
|
|
|
void MediaPreviewWidget::fillEmojiString() {
|
|
|
|
auto getStickerEmojiList = [this](uint64 setId) {
|
|
|
|
QList<EmojiPtr> result;
|
|
|
|
auto &sets = Global::StickerSets();
|
|
|
|
auto it = sets.constFind(setId);
|
|
|
|
if (it == sets.cend()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
for (auto i = it->emoji.cbegin(), e = it->emoji.cend(); i != e; ++i) {
|
|
|
|
for_const (auto document, *i) {
|
|
|
|
if (document == _document) {
|
|
|
|
result.append(i.key());
|
|
|
|
if (result.size() >= kStickerPreviewEmojiLimit) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (auto sticker = _document->sticker()) {
|
|
|
|
auto &inputSet = sticker->set;
|
|
|
|
if (inputSet.type() == mtpc_inputStickerSetID) {
|
|
|
|
_emojiList = getStickerEmojiList(inputSet.c_inputStickerSetID().vid.v);
|
|
|
|
} else {
|
|
|
|
_emojiList.clear();
|
|
|
|
if (auto emoji = emojiFromText(sticker->alt)) {
|
|
|
|
_emojiList.append(emoji);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_emojiList.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
void MediaPreviewWidget::resetGifAndCache() {
|
2016-09-28 20:28:53 +00:00
|
|
|
_gif.reset();
|
2015-12-07 13:05:00 +00:00
|
|
|
_cacheStatus = CacheNotLoaded;
|
2016-04-10 18:18:26 +00:00
|
|
|
_cachedSize = QSize();
|
2015-12-07 13:05:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
QSize MediaPreviewWidget::currentDimensions() const {
|
|
|
|
if (!_cachedSize.isEmpty()) {
|
|
|
|
return _cachedSize;
|
|
|
|
}
|
|
|
|
if (!_document && !_photo) {
|
|
|
|
_cachedSize = QSize(_cache.width() / cIntRetinaFactor(), _cache.height() / cIntRetinaFactor());
|
|
|
|
return _cachedSize;
|
|
|
|
}
|
2015-12-07 13:05:00 +00:00
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
QSize result, box;
|
|
|
|
if (_photo) {
|
|
|
|
result = QSize(_photo->full->width(), _photo->full->height());
|
|
|
|
box = QSize(width() - 2 * st::boxVerticalMargin, height() - 2 * st::boxVerticalMargin);
|
|
|
|
} else {
|
|
|
|
result = _document->dimensions;
|
2016-09-28 20:28:53 +00:00
|
|
|
if (_gif && _gif->ready()) {
|
2016-04-10 18:18:26 +00:00
|
|
|
result = QSize(_gif->width(), _gif->height());
|
|
|
|
}
|
|
|
|
if (_document->sticker()) {
|
|
|
|
box = QSize(st::maxStickerSize, st::maxStickerSize);
|
|
|
|
} else {
|
|
|
|
box = QSize(2 * st::maxStickerSize, 2 * st::maxStickerSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = QSize(qMax(convertScale(result.width()), 1), qMax(convertScale(result.height()), 1));
|
|
|
|
if (result.width() > box.width()) {
|
|
|
|
result.setHeight(qMax((box.width() * result.height()) / result.width(), 1));
|
|
|
|
result.setWidth(box.width());
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
2016-04-10 18:18:26 +00:00
|
|
|
if (result.height() > box.height()) {
|
|
|
|
result.setWidth(qMax((box.height() * result.width()) / result.height(), 1));
|
|
|
|
result.setHeight(box.height());
|
2015-12-07 13:05:00 +00:00
|
|
|
}
|
2016-04-10 18:18:26 +00:00
|
|
|
if (_photo) {
|
|
|
|
_cachedSize = result;
|
2015-12-07 13:05:00 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
QPixmap MediaPreviewWidget::currentImage() const {
|
|
|
|
if (_document) {
|
|
|
|
if (_document->sticker()) {
|
2015-12-27 21:37:48 +00:00
|
|
|
if (_cacheStatus != CacheLoaded) {
|
2016-04-10 18:18:26 +00:00
|
|
|
_document->checkSticker();
|
|
|
|
if (_document->sticker()->img->isNull()) {
|
|
|
|
if (_cacheStatus != CacheThumbLoaded && _document->thumb->loaded()) {
|
2015-12-27 21:37:48 +00:00
|
|
|
QSize s = currentDimensions();
|
2016-04-10 18:18:26 +00:00
|
|
|
_cache = _document->thumb->pixBlurred(s.width(), s.height());
|
2015-12-27 21:37:48 +00:00
|
|
|
_cacheStatus = CacheThumbLoaded;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QSize s = currentDimensions();
|
2016-04-10 18:18:26 +00:00
|
|
|
_cache = _document->sticker()->img->pix(s.width(), s.height());
|
2015-12-27 21:37:48 +00:00
|
|
|
_cacheStatus = CacheLoaded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-04-10 18:18:26 +00:00
|
|
|
_document->automaticLoad(nullptr);
|
|
|
|
if (_document->loaded()) {
|
2016-09-28 20:28:53 +00:00
|
|
|
if (!_gif && !_gif.isBad()) {
|
2016-06-24 10:37:29 +00:00
|
|
|
auto that = const_cast<MediaPreviewWidget*>(this);
|
2016-09-28 20:28:53 +00:00
|
|
|
that->_gif = Media::Clip::MakeReader(_document->location(), _document->data(), [this, that](Media::Clip::Notification notification) {
|
2016-09-26 12:09:59 +00:00
|
|
|
that->clipCallback(notification);
|
|
|
|
});
|
2016-09-28 20:28:53 +00:00
|
|
|
if (_gif) _gif->setAutoplay();
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-28 20:28:53 +00:00
|
|
|
if (_gif && _gif->started()) {
|
2015-12-27 21:37:48 +00:00
|
|
|
QSize s = currentDimensions();
|
|
|
|
return _gif->current(s.width(), s.height(), s.width(), s.height(), getms());
|
|
|
|
}
|
2016-04-10 18:18:26 +00:00
|
|
|
if (_cacheStatus != CacheThumbLoaded && _document->thumb->loaded()) {
|
2015-12-07 13:05:00 +00:00
|
|
|
QSize s = currentDimensions();
|
2016-04-10 18:18:26 +00:00
|
|
|
_cache = _document->thumb->pixBlurred(s.width(), s.height());
|
2015-12-07 13:05:00 +00:00
|
|
|
_cacheStatus = CacheThumbLoaded;
|
|
|
|
}
|
|
|
|
}
|
2016-04-10 18:18:26 +00:00
|
|
|
} else if (_photo) {
|
|
|
|
if (_cacheStatus != CacheLoaded) {
|
|
|
|
if (_photo->full->loaded()) {
|
|
|
|
QSize s = currentDimensions();
|
|
|
|
_cache = _photo->full->pix(s.width(), s.height());
|
|
|
|
_cacheStatus = CacheLoaded;
|
|
|
|
} else {
|
|
|
|
if (_cacheStatus != CacheThumbLoaded && _photo->thumb->loaded()) {
|
|
|
|
QSize s = currentDimensions();
|
|
|
|
_cache = _photo->thumb->pixBlurred(s.width(), s.height());
|
|
|
|
_cacheStatus = CacheThumbLoaded;
|
|
|
|
}
|
|
|
|
_photo->thumb->load();
|
|
|
|
_photo->full->load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 13:05:00 +00:00
|
|
|
}
|
|
|
|
return _cache;
|
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
void MediaPreviewWidget::clipCallback(Media::Clip::Notification notification) {
|
|
|
|
using namespace Media::Clip;
|
2015-12-27 21:37:48 +00:00
|
|
|
switch (notification) {
|
2016-06-24 10:37:29 +00:00
|
|
|
case NotificationReinit: {
|
2016-09-28 20:28:53 +00:00
|
|
|
if (_gif && _gif->state() == State::Error) {
|
|
|
|
_gif.setBad();
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 20:28:53 +00:00
|
|
|
if (_gif && _gif->ready() && !_gif->started()) {
|
2015-12-27 21:37:48 +00:00
|
|
|
QSize s = currentDimensions();
|
2016-07-19 10:54:43 +00:00
|
|
|
_gif->start(s.width(), s.height(), s.width(), s.height(), ImageRoundRadius::None);
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
} break;
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
case NotificationRepaint: {
|
2016-09-28 20:28:53 +00:00
|
|
|
if (_gif && !_gif->currentDisplayed()) {
|
2015-12-28 21:20:04 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
} break;
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
MediaPreviewWidget::~MediaPreviewWidget() {
|
2015-12-07 13:05:00 +00:00
|
|
|
}
|