2020-10-13 15:11:53 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "ui/chat/attach/attach_album_thumbnail.h"
|
|
|
|
|
|
|
|
#include "ui/chat/attach/attach_prepare.h"
|
|
|
|
#include "ui/image/image_prepare.h"
|
|
|
|
#include "ui/text/format_values.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/ui_utility.h"
|
|
|
|
#include "base/call_delayed.h"
|
|
|
|
#include "styles/style_chat.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
|
|
|
AlbumThumbnail::AlbumThumbnail(
|
|
|
|
const PreparedFile &file,
|
|
|
|
const GroupMediaLayout &layout,
|
|
|
|
QWidget *parent,
|
|
|
|
Fn<void()> editCallback,
|
|
|
|
Fn<void()> deleteCallback)
|
|
|
|
: _layout(layout)
|
|
|
|
, _fullPreview(file.preview)
|
|
|
|
, _shrinkSize(int(std::ceil(st::historyMessageRadius / 1.4)))
|
2021-04-25 22:50:16 +00:00
|
|
|
, _isPhoto(file.type == PreparedFile::Type::Photo)
|
2021-04-12 16:25:58 +00:00
|
|
|
, _isVideo(file.type == PreparedFile::Type::Video) {
|
2020-10-13 15:11:53 +00:00
|
|
|
Expects(!_fullPreview.isNull());
|
|
|
|
|
|
|
|
moveToLayout(layout);
|
|
|
|
|
|
|
|
using Option = Images::Option;
|
|
|
|
const auto previewWidth = _fullPreview.width();
|
|
|
|
const auto previewHeight = _fullPreview.height();
|
|
|
|
const auto imageWidth = std::max(
|
|
|
|
previewWidth / style::DevicePixelRatio(),
|
|
|
|
st::minPhotoSize);
|
|
|
|
const auto imageHeight = std::max(
|
|
|
|
previewHeight / style::DevicePixelRatio(),
|
|
|
|
st::minPhotoSize);
|
2022-01-21 12:31:39 +00:00
|
|
|
_photo = PixmapFromImage(Images::Prepare(
|
2020-10-13 15:11:53 +00:00
|
|
|
_fullPreview,
|
2022-01-21 12:31:39 +00:00
|
|
|
QSize(previewWidth, previewHeight),
|
|
|
|
{
|
|
|
|
.options = Option::RoundLarge,
|
|
|
|
.outer = { imageWidth, imageHeight },
|
|
|
|
}));
|
2020-10-13 15:11:53 +00:00
|
|
|
|
2020-10-19 15:37:59 +00:00
|
|
|
const auto &st = st::attachPreviewThumbLayout;
|
|
|
|
const auto idealSize = st.thumbSize * style::DevicePixelRatio();
|
2020-10-13 15:11:53 +00:00
|
|
|
const auto fileThumbSize = (previewWidth > previewHeight)
|
|
|
|
? QSize(previewWidth * idealSize / previewHeight, idealSize)
|
|
|
|
: QSize(idealSize, previewHeight * idealSize / previewWidth);
|
2022-01-21 12:31:39 +00:00
|
|
|
_fileThumb = PixmapFromImage(Images::Prepare(
|
2020-10-13 15:11:53 +00:00
|
|
|
_fullPreview,
|
2022-01-21 12:31:39 +00:00
|
|
|
fileThumbSize,
|
|
|
|
{
|
|
|
|
.options = Option::RoundSmall,
|
|
|
|
.outer = { st.thumbSize, st.thumbSize },
|
|
|
|
}));
|
2020-10-13 15:11:53 +00:00
|
|
|
|
|
|
|
const auto availableFileWidth = st::sendMediaPreviewSize
|
2020-10-19 15:37:59 +00:00
|
|
|
- st.thumbSize
|
|
|
|
- st.padding.right()
|
2020-10-13 15:11:53 +00:00
|
|
|
// Right buttons.
|
|
|
|
- st::sendBoxAlbumGroupButtonFile.width * 2
|
|
|
|
- st::sendBoxAlbumGroupEditInternalSkip * 2
|
|
|
|
- st::sendBoxAlbumGroupSkipRight;
|
|
|
|
const auto filepath = file.path;
|
|
|
|
if (filepath.isEmpty()) {
|
|
|
|
_name = "image.png";
|
2021-07-08 19:15:11 +00:00
|
|
|
_status = FormatImageSizeText(_fullPreview.size()
|
|
|
|
/ _fullPreview.devicePixelRatio());
|
2020-10-13 15:11:53 +00:00
|
|
|
} else {
|
|
|
|
auto fileinfo = QFileInfo(filepath);
|
|
|
|
_name = fileinfo.fileName();
|
|
|
|
_status = FormatSizeText(fileinfo.size());
|
|
|
|
}
|
|
|
|
_nameWidth = st::semiboldFont->width(_name);
|
|
|
|
if (_nameWidth > availableFileWidth) {
|
|
|
|
_name = st::semiboldFont->elided(
|
|
|
|
_name,
|
|
|
|
availableFileWidth,
|
|
|
|
Qt::ElideMiddle);
|
|
|
|
_nameWidth = st::semiboldFont->width(_name);
|
|
|
|
}
|
|
|
|
_statusWidth = st::normalFont->width(_status);
|
|
|
|
|
|
|
|
_editMedia.create(parent, st::sendBoxAlbumGroupButtonFile);
|
|
|
|
_deleteMedia.create(parent, st::sendBoxAlbumGroupButtonFile);
|
|
|
|
|
|
|
|
const auto duration = st::historyAttach.ripple.hideDuration;
|
|
|
|
_editMedia->setClickedCallback([=] {
|
|
|
|
base::call_delayed(duration, parent, editCallback);
|
|
|
|
});
|
2020-10-15 14:27:16 +00:00
|
|
|
_deleteMedia->setClickedCallback(deleteCallback);
|
2020-10-13 15:11:53 +00:00
|
|
|
|
2020-10-19 08:49:31 +00:00
|
|
|
_editMedia->setIconOverride(&st::sendBoxAlbumGroupEditButtonIconFile);
|
2020-10-13 15:11:53 +00:00
|
|
|
_deleteMedia->setIconOverride(&st::sendBoxAlbumGroupDeleteButtonIconFile);
|
|
|
|
|
|
|
|
updateFileRow(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::updateFileRow(int row) {
|
|
|
|
if (row < 0) {
|
|
|
|
_editMedia->hide();
|
|
|
|
_deleteMedia->hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_editMedia->show();
|
|
|
|
_deleteMedia->show();
|
|
|
|
|
2020-10-19 15:37:59 +00:00
|
|
|
const auto fileHeight = st::attachPreviewThumbLayout.thumbSize
|
|
|
|
+ st::sendMediaRowSkip;
|
2020-10-16 10:38:12 +00:00
|
|
|
const auto top = row * fileHeight + st::sendBoxFileGroupSkipTop;
|
2020-10-13 15:11:53 +00:00
|
|
|
|
2021-04-12 16:25:58 +00:00
|
|
|
auto right = st::sendBoxFileGroupSkipRight + st::boxPhotoPadding.right();
|
2020-10-13 15:11:53 +00:00
|
|
|
_deleteMedia->moveToRight(right, top);
|
2021-04-12 16:25:58 +00:00
|
|
|
right += st::sendBoxFileGroupEditInternalSkip + _deleteMedia->width();
|
2020-10-13 15:11:53 +00:00
|
|
|
_editMedia->moveToRight(right, top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::resetLayoutAnimation() {
|
|
|
|
_animateFromGeometry = std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::animateLayoutToInitial() {
|
|
|
|
_animateFromGeometry = countRealGeometry();
|
|
|
|
_suggestedMove = 0.;
|
|
|
|
_albumPosition = QPoint(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::moveToLayout(const GroupMediaLayout &layout) {
|
2022-01-21 12:31:39 +00:00
|
|
|
using Option = Images::Option;
|
|
|
|
|
2020-10-13 15:11:53 +00:00
|
|
|
animateLayoutToInitial();
|
|
|
|
_layout = layout;
|
|
|
|
|
|
|
|
const auto width = _layout.geometry.width();
|
|
|
|
const auto height = _layout.geometry.height();
|
|
|
|
_albumCorners = GetCornersFromSides(_layout.sides);
|
2022-01-21 12:31:39 +00:00
|
|
|
const auto corner = [&](RectPart part, Option skip) {
|
|
|
|
return !(_albumCorners & part) ? skip : Option();
|
|
|
|
};
|
|
|
|
const auto options = Option::RoundLarge
|
|
|
|
| corner(RectPart::TopLeft, Option::RoundSkipTopLeft)
|
|
|
|
| corner(RectPart::TopRight, Option::RoundSkipTopRight)
|
|
|
|
| corner(RectPart::BottomLeft, Option::RoundSkipBottomLeft)
|
|
|
|
| corner(RectPart::BottomRight, Option::RoundSkipBottomRight);
|
2020-10-13 15:11:53 +00:00
|
|
|
const auto pixSize = GetImageScaleSizeForGeometry(
|
|
|
|
{ _fullPreview.width(), _fullPreview.height() },
|
|
|
|
{ width, height });
|
|
|
|
const auto pixWidth = pixSize.width() * style::DevicePixelRatio();
|
|
|
|
const auto pixHeight = pixSize.height() * style::DevicePixelRatio();
|
|
|
|
|
2022-01-21 12:31:39 +00:00
|
|
|
_albumImage = PixmapFromImage(Images::Prepare(
|
2020-10-13 15:11:53 +00:00
|
|
|
_fullPreview,
|
2022-01-21 12:31:39 +00:00
|
|
|
QSize(pixWidth, pixHeight),
|
|
|
|
{
|
|
|
|
.options = options,
|
|
|
|
.outer = { width, height },
|
|
|
|
}));
|
2020-10-13 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AlbumThumbnail::photoHeight() const {
|
|
|
|
return _photo.height() / style::DevicePixelRatio();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::paintInAlbum(
|
|
|
|
Painter &p,
|
|
|
|
int left,
|
|
|
|
int top,
|
|
|
|
float64 shrinkProgress,
|
|
|
|
float64 moveProgress) {
|
|
|
|
const auto shrink = anim::interpolate(0, _shrinkSize, shrinkProgress);
|
|
|
|
_lastShrinkValue = shrink;
|
|
|
|
const auto geometry = countCurrentGeometry(moveProgress);
|
|
|
|
const auto x = left + geometry.x();
|
|
|
|
const auto y = top + geometry.y();
|
|
|
|
if (shrink > 0 || moveProgress < 1.) {
|
|
|
|
const auto size = geometry.size();
|
|
|
|
if (shrinkProgress < 1 && _albumCorners != RectPart::None) {
|
|
|
|
prepareCache(size, shrink);
|
|
|
|
p.drawImage(x, y, _albumCache);
|
|
|
|
} else {
|
|
|
|
const auto to = QRect({ x, y }, size).marginsRemoved(
|
|
|
|
{ shrink, shrink, shrink, shrink }
|
|
|
|
);
|
|
|
|
drawSimpleFrame(p, to, size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.drawPixmap(x, y, _albumImage);
|
|
|
|
}
|
|
|
|
if (_isVideo) {
|
2020-10-19 15:37:59 +00:00
|
|
|
const auto innerSize = st::msgFileLayout.thumbSize;
|
2020-10-13 15:11:53 +00:00
|
|
|
const auto inner = QRect(
|
2020-10-19 15:37:59 +00:00
|
|
|
x + (geometry.width() - innerSize) / 2,
|
|
|
|
y + (geometry.height() - innerSize) / 2,
|
|
|
|
innerSize,
|
|
|
|
innerSize);
|
2020-10-13 15:11:53 +00:00
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(st::msgDateImgBg);
|
|
|
|
p.drawEllipse(inner);
|
|
|
|
}
|
|
|
|
st::historyFileThumbPlay.paintInCenter(p, inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
_lastRectOfButtons = paintButtons(
|
|
|
|
p,
|
|
|
|
{ x, y },
|
|
|
|
geometry.width(),
|
|
|
|
shrinkProgress);
|
2021-04-25 22:50:16 +00:00
|
|
|
|
|
|
|
_lastRectOfModify = QRect(QPoint(x, y), geometry.size());
|
2020-10-13 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::prepareCache(QSize size, int shrink) {
|
|
|
|
const auto width = std::max(
|
|
|
|
_layout.geometry.width(),
|
|
|
|
_animateFromGeometry ? _animateFromGeometry->width() : 0);
|
|
|
|
const auto height = std::max(
|
|
|
|
_layout.geometry.height(),
|
|
|
|
_animateFromGeometry ? _animateFromGeometry->height() : 0);
|
|
|
|
const auto cacheSize = QSize(width, height) * style::DevicePixelRatio();
|
|
|
|
|
|
|
|
if (_albumCache.width() < cacheSize.width()
|
|
|
|
|| _albumCache.height() < cacheSize.height()) {
|
|
|
|
_albumCache = QImage(cacheSize, QImage::Format_ARGB32_Premultiplied);
|
2021-05-01 21:20:23 +00:00
|
|
|
_albumCache.setDevicePixelRatio(style::DevicePixelRatio());
|
2020-10-13 15:11:53 +00:00
|
|
|
}
|
|
|
|
_albumCache.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
Painter p(&_albumCache);
|
|
|
|
const auto to = QRect(QPoint(), size).marginsRemoved(
|
|
|
|
{ shrink, shrink, shrink, shrink }
|
|
|
|
);
|
|
|
|
drawSimpleFrame(p, to, size);
|
|
|
|
}
|
2022-01-21 12:31:39 +00:00
|
|
|
_albumCache = Images::Round(
|
|
|
|
std::move(_albumCache),
|
2020-10-13 15:11:53 +00:00
|
|
|
ImageRoundRadius::Large,
|
|
|
|
_albumCorners,
|
|
|
|
QRect(QPoint(), size * style::DevicePixelRatio()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::drawSimpleFrame(Painter &p, QRect to, QSize size) const {
|
|
|
|
const auto fullWidth = _fullPreview.width();
|
|
|
|
const auto fullHeight = _fullPreview.height();
|
|
|
|
const auto previewSize = GetImageScaleSizeForGeometry(
|
|
|
|
{ fullWidth, fullHeight },
|
|
|
|
{ size.width(), size.height() });
|
|
|
|
const auto previewWidth = previewSize.width() * style::DevicePixelRatio();
|
|
|
|
const auto previewHeight = previewSize.height() * style::DevicePixelRatio();
|
|
|
|
const auto width = size.width() * style::DevicePixelRatio();
|
|
|
|
const auto height = size.height() * style::DevicePixelRatio();
|
|
|
|
const auto scaleWidth = to.width() / float64(width);
|
|
|
|
const auto scaleHeight = to.height() / float64(height);
|
|
|
|
const auto Round = [](float64 value) {
|
2021-09-27 08:13:57 +00:00
|
|
|
return int(base::SafeRound(value));
|
2020-10-13 15:11:53 +00:00
|
|
|
};
|
|
|
|
const auto [from, fillBlack] = [&] {
|
|
|
|
if (previewWidth < width && previewHeight < height) {
|
|
|
|
const auto toWidth = Round(previewWidth * scaleWidth);
|
|
|
|
const auto toHeight = Round(previewHeight * scaleHeight);
|
|
|
|
return std::make_pair(
|
|
|
|
QRect(0, 0, fullWidth, fullHeight),
|
|
|
|
QMargins(
|
|
|
|
(to.width() - toWidth) / 2,
|
|
|
|
(to.height() - toHeight) / 2,
|
|
|
|
to.width() - toWidth - (to.width() - toWidth) / 2,
|
|
|
|
to.height() - toHeight - (to.height() - toHeight) / 2));
|
|
|
|
} else if (previewWidth * height > previewHeight * width) {
|
|
|
|
if (previewHeight >= height) {
|
|
|
|
const auto takeWidth = previewWidth * height / previewHeight;
|
|
|
|
const auto useWidth = fullWidth * width / takeWidth;
|
|
|
|
return std::make_pair(
|
|
|
|
QRect(
|
|
|
|
(fullWidth - useWidth) / 2,
|
|
|
|
0,
|
|
|
|
useWidth,
|
|
|
|
fullHeight),
|
|
|
|
QMargins(0, 0, 0, 0));
|
|
|
|
} else {
|
|
|
|
const auto takeWidth = previewWidth;
|
|
|
|
const auto useWidth = fullWidth * width / takeWidth;
|
|
|
|
const auto toHeight = Round(previewHeight * scaleHeight);
|
|
|
|
const auto toSkip = (to.height() - toHeight) / 2;
|
|
|
|
return std::make_pair(
|
|
|
|
QRect(
|
|
|
|
(fullWidth - useWidth) / 2,
|
|
|
|
0,
|
|
|
|
useWidth,
|
|
|
|
fullHeight),
|
|
|
|
QMargins(
|
|
|
|
0,
|
|
|
|
toSkip,
|
|
|
|
0,
|
|
|
|
to.height() - toHeight - toSkip));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (previewWidth >= width) {
|
|
|
|
const auto takeHeight = previewHeight * width / previewWidth;
|
|
|
|
const auto useHeight = fullHeight * height / takeHeight;
|
|
|
|
return std::make_pair(
|
|
|
|
QRect(
|
|
|
|
0,
|
|
|
|
(fullHeight - useHeight) / 2,
|
|
|
|
fullWidth,
|
|
|
|
useHeight),
|
|
|
|
QMargins(0, 0, 0, 0));
|
|
|
|
} else {
|
|
|
|
const auto takeHeight = previewHeight;
|
|
|
|
const auto useHeight = fullHeight * height / takeHeight;
|
|
|
|
const auto toWidth = Round(previewWidth * scaleWidth);
|
|
|
|
const auto toSkip = (to.width() - toWidth) / 2;
|
|
|
|
return std::make_pair(
|
|
|
|
QRect(
|
|
|
|
0,
|
|
|
|
(fullHeight - useHeight) / 2,
|
|
|
|
fullWidth,
|
|
|
|
useHeight),
|
|
|
|
QMargins(
|
|
|
|
toSkip,
|
|
|
|
0,
|
|
|
|
to.width() - toWidth - toSkip,
|
|
|
|
0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
p.drawImage(to.marginsRemoved(fillBlack), _fullPreview, from);
|
|
|
|
if (fillBlack.top() > 0) {
|
|
|
|
p.fillRect(to.x(), to.y(), to.width(), fillBlack.top(), st::imageBg);
|
|
|
|
}
|
|
|
|
if (fillBlack.bottom() > 0) {
|
|
|
|
p.fillRect(
|
|
|
|
to.x(),
|
|
|
|
to.y() + to.height() - fillBlack.bottom(),
|
|
|
|
to.width(),
|
|
|
|
fillBlack.bottom(),
|
|
|
|
st::imageBg);
|
|
|
|
}
|
|
|
|
if (fillBlack.left() > 0) {
|
|
|
|
p.fillRect(
|
|
|
|
to.x(),
|
|
|
|
to.y() + fillBlack.top(),
|
|
|
|
fillBlack.left(),
|
|
|
|
to.height() - fillBlack.top() - fillBlack.bottom(),
|
|
|
|
st::imageBg);
|
|
|
|
}
|
|
|
|
if (fillBlack.right() > 0) {
|
|
|
|
p.fillRect(
|
|
|
|
to.x() + to.width() - fillBlack.right(),
|
|
|
|
to.y() + fillBlack.top(),
|
|
|
|
fillBlack.right(),
|
|
|
|
to.height() - fillBlack.top() - fillBlack.bottom(),
|
|
|
|
st::imageBg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::paintPhoto(Painter &p, int left, int top, int outerWidth) {
|
2021-04-25 22:50:16 +00:00
|
|
|
const auto size = _photo.size() / style::DevicePixelRatio();
|
2020-10-13 15:11:53 +00:00
|
|
|
p.drawPixmapLeft(
|
2021-04-25 22:50:16 +00:00
|
|
|
left + (st::sendMediaPreviewSize - size.width()) / 2,
|
2020-10-13 15:11:53 +00:00
|
|
|
top,
|
|
|
|
outerWidth,
|
|
|
|
_photo);
|
|
|
|
|
2021-04-25 22:50:16 +00:00
|
|
|
const auto topLeft = QPoint{ left, top };
|
|
|
|
|
2020-10-13 15:11:53 +00:00
|
|
|
_lastRectOfButtons = paintButtons(
|
|
|
|
p,
|
2021-04-25 22:50:16 +00:00
|
|
|
topLeft,
|
2020-10-13 15:11:53 +00:00
|
|
|
st::sendMediaPreviewSize,
|
|
|
|
0);
|
2021-04-25 22:50:16 +00:00
|
|
|
|
|
|
|
_lastRectOfModify = QRect(topLeft, size);
|
2020-10-13 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 22:50:16 +00:00
|
|
|
void AlbumThumbnail::paintFile(
|
|
|
|
Painter &p,
|
|
|
|
int left,
|
|
|
|
int top,
|
|
|
|
int outerWidth) {
|
2020-10-19 15:37:59 +00:00
|
|
|
const auto &st = st::attachPreviewThumbLayout;
|
|
|
|
const auto textLeft = left + st.thumbSize + st.padding.right();
|
2020-10-13 15:11:53 +00:00
|
|
|
|
|
|
|
p.drawPixmap(left, top, _fileThumb);
|
|
|
|
p.setFont(st::semiboldFont);
|
|
|
|
p.setPen(st::historyFileNameInFg);
|
|
|
|
p.drawTextLeft(
|
|
|
|
textLeft,
|
2020-10-19 15:37:59 +00:00
|
|
|
top + st.nameTop,
|
2020-10-13 15:11:53 +00:00
|
|
|
outerWidth,
|
|
|
|
_name,
|
|
|
|
_nameWidth);
|
|
|
|
p.setFont(st::normalFont);
|
|
|
|
p.setPen(st::mediaInFg);
|
|
|
|
p.drawTextLeft(
|
|
|
|
textLeft,
|
2020-10-19 15:37:59 +00:00
|
|
|
top + st.statusTop,
|
2020-10-13 15:11:53 +00:00
|
|
|
outerWidth,
|
|
|
|
_status,
|
|
|
|
_statusWidth);
|
2021-04-25 22:50:16 +00:00
|
|
|
|
|
|
|
_lastRectOfModify = QRect(
|
|
|
|
QPoint(left, top),
|
|
|
|
_fileThumb.size() / style::DevicePixelRatio());
|
2020-10-13 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AlbumThumbnail::containsPoint(QPoint position) const {
|
|
|
|
return _layout.geometry.contains(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AlbumThumbnail::buttonsContainPoint(QPoint position) const {
|
2021-04-25 22:50:16 +00:00
|
|
|
return (_isPhoto
|
|
|
|
? _lastRectOfModify
|
|
|
|
: _lastRectOfButtons).contains(position);
|
2020-10-13 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AttachButtonType AlbumThumbnail::buttonTypeFromPoint(QPoint position) const {
|
|
|
|
if (!buttonsContainPoint(position)) {
|
|
|
|
return AttachButtonType::None;
|
|
|
|
}
|
2021-04-25 22:50:16 +00:00
|
|
|
return !_lastRectOfButtons.contains(position)
|
|
|
|
? AttachButtonType::Modify
|
|
|
|
: (position.x() < _lastRectOfButtons.center().x())
|
2020-10-13 15:11:53 +00:00
|
|
|
? AttachButtonType::Edit
|
|
|
|
: AttachButtonType::Delete;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AlbumThumbnail::distanceTo(QPoint position) const {
|
|
|
|
const auto delta = (_layout.geometry.center() - position);
|
|
|
|
return QPoint::dotProduct(delta, delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AlbumThumbnail::isPointAfter(QPoint position) const {
|
|
|
|
return position.x() > _layout.geometry.center().x();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::moveInAlbum(QPoint to) {
|
|
|
|
_albumPosition = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint AlbumThumbnail::center() const {
|
|
|
|
auto realGeometry = _layout.geometry;
|
|
|
|
realGeometry.moveTopLeft(realGeometry.topLeft() + _albumPosition);
|
|
|
|
return realGeometry.center();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::suggestMove(float64 delta, Fn<void()> callback) {
|
|
|
|
if (_suggestedMove != delta) {
|
|
|
|
_suggestedMoveAnimation.start(
|
|
|
|
std::move(callback),
|
|
|
|
_suggestedMove,
|
|
|
|
delta,
|
|
|
|
kShrinkDuration);
|
|
|
|
_suggestedMove = delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect AlbumThumbnail::countRealGeometry() const {
|
2021-09-27 08:13:57 +00:00
|
|
|
const auto addLeft = int(base::SafeRound(
|
2020-10-13 15:11:53 +00:00
|
|
|
_suggestedMoveAnimation.value(_suggestedMove) * _lastShrinkValue));
|
|
|
|
const auto current = _layout.geometry;
|
|
|
|
const auto realTopLeft = current.topLeft()
|
|
|
|
+ _albumPosition
|
|
|
|
+ QPoint(addLeft, 0);
|
|
|
|
return { realTopLeft, current.size() };
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect AlbumThumbnail::countCurrentGeometry(float64 progress) const {
|
|
|
|
const auto now = countRealGeometry();
|
|
|
|
if (_animateFromGeometry && progress < 1.) {
|
|
|
|
return {
|
|
|
|
anim::interpolate(_animateFromGeometry->x(), now.x(), progress),
|
|
|
|
anim::interpolate(_animateFromGeometry->y(), now.y(), progress),
|
|
|
|
anim::interpolate(_animateFromGeometry->width(), now.width(), progress),
|
|
|
|
anim::interpolate(_animateFromGeometry->height(), now.height(), progress)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumThumbnail::finishAnimations() {
|
|
|
|
_suggestedMoveAnimation.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect AlbumThumbnail::paintButtons(
|
|
|
|
Painter &p,
|
|
|
|
QPoint point,
|
|
|
|
int outerWidth,
|
|
|
|
float64 shrinkProgress) {
|
2021-04-12 16:25:58 +00:00
|
|
|
const auto &skipRight = st::sendBoxAlbumGroupSkipRight;
|
|
|
|
const auto &skipTop = st::sendBoxAlbumGroupSkipTop;
|
|
|
|
const auto groupWidth = _buttons.width();
|
2020-10-13 15:11:53 +00:00
|
|
|
|
|
|
|
// If the width is tiny, it would be better to not display the buttons.
|
|
|
|
if (groupWidth > outerWidth) {
|
|
|
|
return QRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the width is too small,
|
|
|
|
// it would be better to display the buttons in the center.
|
|
|
|
const auto groupX = point.x() + ((groupWidth + skipRight * 2 > outerWidth)
|
|
|
|
? (outerWidth - groupWidth) / 2
|
|
|
|
: outerWidth - skipRight - groupWidth);
|
|
|
|
const auto groupY = point.y() + skipTop;
|
|
|
|
|
2021-04-12 16:25:58 +00:00
|
|
|
const auto opacity = p.opacity();
|
2020-10-13 15:11:53 +00:00
|
|
|
p.setOpacity(1.0 - shrinkProgress);
|
2021-04-12 16:25:58 +00:00
|
|
|
_buttons.paint(p, groupX, groupY);
|
|
|
|
p.setOpacity(opacity);
|
2020-10-13 15:11:53 +00:00
|
|
|
|
2021-04-12 16:25:58 +00:00
|
|
|
return QRect(groupX, groupY, groupWidth, _buttons.height());
|
2020-10-13 15:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Ui
|