From 7e009303194bafc471e53d5396344908ca5ecdba Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 3 Jan 2020 22:40:47 +0300 Subject: [PATCH] Added ability to delete items from album in SendFilesBox. --- Telegram/SourceFiles/boxes/send_files_box.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index 15d82a847..c1b184420 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -960,6 +960,10 @@ public: void setSendWay(SendFilesWay way); std::vector takeOrder(); + auto thumbDeleted() { + return _thumbDeleted.events(); + } + protected: void paintEvent(QPaintEvent *e) override; void mousePressEvent(QMouseEvent *e) override; @@ -975,6 +979,8 @@ private: void updateSizeAnimated(const std::vector &layout); void updateSize(); + void deleteThumbUnderCursor(); + void paintAlbum(Painter &p) const; void paintPhotos(Painter &p, QRect clip) const; void paintFiles(Painter &p, QRect clip) const; @@ -1003,6 +1009,8 @@ private: AlbumThumb *_paintedAbove = nullptr; QPoint _draggedStartPosition; + rpl::event_stream _thumbDeleted; + mutable Ui::Animations::Simple _thumbsHeightAnimation; mutable Ui::Animations::Simple _shrinkAnimation; mutable Ui::Animations::Simple _finishDragAnimation; @@ -1277,6 +1285,27 @@ void SendFilesBox::AlbumPreview::paintFiles(Painter &p, QRect clip) const { } } +void SendFilesBox::AlbumPreview::deleteThumbUnderCursor() { + const auto thumb = findThumb(mapFromGlobal(QCursor::pos())); + if (!thumb) { + return; + } + const auto thumbIt = ranges::find_if(_thumbs, [&](auto &t) { + return t.get() == thumb; + }); + const auto index = std::distance(_thumbs.begin(), thumbIt); + const auto orderIt = ranges::find(_order, index); + Expects(orderIt != _order.end()); + + _order.erase(orderIt); + for (auto i = orderIt; i != _order.end(); i++) { + if ((*i) > index) { + (*i)--; + } + } + _thumbDeleted.fire(index); +} + void SendFilesBox::AlbumPreview::mousePressEvent(QMouseEvent *e) { if (_finishDragAnimation.animating()) { return; @@ -1442,6 +1471,32 @@ void SendFilesBox::prepareAlbumPreview() { this, _list, _sendWay->value())); + + _albumPreview->thumbDeleted( + ) | rpl::start_with_next([=](auto index) { + + _list.files.erase(_list.files.begin() + index); + applyAlbumOrder(); + + if (_preview) { + _preview->deleteLater(); + } + _albumPreview = nullptr; + + if (_list.files.size() == 1) { + _list.albumIsPossible = false; + if (_sendWay->value() == SendFilesWay::Album) { + _sendWay->setValue(SendFilesWay::Photos); + } + } + + _compressConfirm = _compressConfirmInitial; + refreshAlbumMediaCount(); + preparePreview(); + captionResized(); + + }, _albumPreview->lifetime()); + _preview = wrap; _albumPreview->show(); setupShadows(wrap, _albumPreview);