2016-05-27 10:57:11 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "profile/profile_userpic_button.h"
|
|
|
|
|
|
|
|
#include "styles/style_profile.h"
|
|
|
|
#include "observer_peer.h"
|
|
|
|
#include "mtproto/file_download.h"
|
|
|
|
|
|
|
|
namespace Profile {
|
|
|
|
|
2016-11-16 10:44:06 +00:00
|
|
|
UserpicButton::UserpicButton(QWidget *parent, PeerData *peer, int size) : AbstractButton(parent)
|
|
|
|
, _size(size ? size : st::profilePhotoSize)
|
|
|
|
, _peer(peer) {
|
|
|
|
resize(_size, _size);
|
2016-05-27 10:57:11 +00:00
|
|
|
|
|
|
|
processPeerPhoto();
|
|
|
|
_notShownYet = _waiting;
|
|
|
|
if (!_waiting) {
|
|
|
|
_userpic = prepareUserpicPixmap();
|
|
|
|
}
|
|
|
|
|
2016-09-26 18:33:34 +00:00
|
|
|
auto observeEvents = Notify::PeerUpdate::Flag::PhotoChanged;
|
|
|
|
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(observeEvents, [this](const Notify::PeerUpdate &update) {
|
2016-09-26 13:57:08 +00:00
|
|
|
notifyPeerUpdated(update);
|
2016-09-26 18:33:34 +00:00
|
|
|
}));
|
2016-09-26 13:57:08 +00:00
|
|
|
subscribe(FileDownload::ImageLoaded(), [this] {
|
|
|
|
if (_waiting && _peer->userpicLoaded()) {
|
|
|
|
_waiting = false;
|
|
|
|
startNewPhotoShowing();
|
|
|
|
}
|
|
|
|
});
|
2016-05-27 10:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserpicButton::showFinished() {
|
2016-06-09 17:30:47 +00:00
|
|
|
if (_notShownYet) {
|
2016-05-27 10:57:11 +00:00
|
|
|
_notShownYet = false;
|
2016-06-09 17:30:47 +00:00
|
|
|
if (!_waiting) {
|
|
|
|
_a_appearance.finish();
|
2016-09-26 12:09:59 +00:00
|
|
|
_a_appearance.start([this] { update(); }, 0, 1, st::profilePhotoDuration);
|
2016-06-09 17:30:47 +00:00
|
|
|
}
|
2016-05-27 10:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserpicButton::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
if (_a_appearance.animating(getms())) {
|
|
|
|
p.drawPixmap(0, 0, _oldUserpic);
|
|
|
|
p.setOpacity(_a_appearance.current());
|
|
|
|
}
|
|
|
|
p.drawPixmap(0, 0, _userpic);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserpicButton::notifyPeerUpdated(const Notify::PeerUpdate &update) {
|
|
|
|
if (update.peer != _peer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
processNewPeerPhoto();
|
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserpicButton::processPeerPhoto() {
|
|
|
|
bool hasPhoto = (_peer->photoId && _peer->photoId != UnknownPeerPhotoId);
|
|
|
|
setCursor(hasPhoto ? style::cur_pointer : style::cur_default);
|
|
|
|
_waiting = !_peer->userpicLoaded();
|
|
|
|
if (_waiting) {
|
|
|
|
_peer->loadUserpic(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserpicButton::processNewPeerPhoto() {
|
|
|
|
processPeerPhoto();
|
|
|
|
if (!_waiting) {
|
|
|
|
startNewPhotoShowing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserpicButton::startNewPhotoShowing() {
|
|
|
|
_oldUserpic = myGrab(this);
|
|
|
|
_userpic = prepareUserpicPixmap();
|
|
|
|
|
|
|
|
if (_notShownYet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_a_appearance.finish();
|
2016-09-26 12:09:59 +00:00
|
|
|
_a_appearance.start([this] { update(); }, 0, 1, st::profilePhotoDuration);
|
2016-05-27 10:57:11 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap UserpicButton::prepareUserpicPixmap() const {
|
|
|
|
auto retina = cIntRetinaFactor();
|
2016-11-16 10:44:06 +00:00
|
|
|
auto size = width() * retina;
|
2016-05-27 10:57:11 +00:00
|
|
|
QImage image(size, size, QImage::Format_ARGB32_Premultiplied);
|
2016-06-06 11:35:49 +00:00
|
|
|
image.setDevicePixelRatio(cRetinaFactor());
|
2016-11-16 10:44:06 +00:00
|
|
|
image.fill(Qt::transparent);
|
2016-05-27 10:57:11 +00:00
|
|
|
{
|
|
|
|
Painter p(&image);
|
2016-11-16 10:44:06 +00:00
|
|
|
_peer->paintUserpic(p, width(), 0, 0);
|
2016-05-27 10:57:11 +00:00
|
|
|
}
|
|
|
|
return App::pixmapFromImageInPlace(std_::move(image));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Profile
|