diff options
author | Konstantinos Sideris <sideris.konstantin@gmail.com> | 2018-09-07 12:24:09 +0300 |
---|---|---|
committer | Konstantinos Sideris <sideris.konstantin@gmail.com> | 2018-09-07 12:24:09 +0300 |
commit | 896fe069b6e1f0406ba483dd82480e32ffe2a5df (patch) | |
tree | 53aa546185b39970093b180344d75b775ca5b7f3 /src/timeline/widgets | |
parent | Put back removed links (diff) | |
download | nheko-896fe069b6e1f0406ba483dd82480e32ffe2a5df.tar.xz |
Use proxy objects on lambdas instead of raw pointers
When the object is destroyed the connections will be removed automatically by Qt. fixes #433
Diffstat (limited to 'src/timeline/widgets')
-rw-r--r-- | src/timeline/widgets/AudioItem.cpp | 16 | ||||
-rw-r--r-- | src/timeline/widgets/AudioItem.h | 3 | ||||
-rw-r--r-- | src/timeline/widgets/FileItem.cpp | 17 | ||||
-rw-r--r-- | src/timeline/widgets/FileItem.h | 3 | ||||
-rw-r--r-- | src/timeline/widgets/ImageItem.cpp | 29 | ||||
-rw-r--r-- | src/timeline/widgets/ImageItem.h | 4 |
6 files changed, 35 insertions, 37 deletions
diff --git a/src/timeline/widgets/AudioItem.cpp b/src/timeline/widgets/AudioItem.cpp index 1e3eb0f0..0c4bf9e4 100644 --- a/src/timeline/widgets/AudioItem.cpp +++ b/src/timeline/widgets/AudioItem.cpp @@ -55,7 +55,6 @@ AudioItem::init() player_->setVolume(100); player_->setNotifyInterval(1000); - connect(this, &AudioItem::fileDownloadedCb, this, &AudioItem::fileDownloaded); connect(player_, &QMediaPlayer::stateChanged, this, [this](QMediaPlayer::State state) { if (state == QMediaPlayer::StoppedState) { state_ = AudioState::Play; @@ -120,19 +119,22 @@ AudioItem::mousePressEvent(QMouseEvent *event) if (filenameToSave_.isEmpty()) return; + auto proxy = std::make_shared<MediaProxy>(); + connect(proxy.get(), &MediaProxy::fileDownloaded, this, &AudioItem::fileDownloaded); + http::client()->download( url_.toString().toStdString(), - [this](const std::string &data, - const std::string &, - const std::string &, - mtx::http::RequestErr err) { + [proxy = std::move(proxy), url = url_](const std::string &data, + const std::string &, + const std::string &, + mtx::http::RequestErr err) { if (err) { nhlog::net()->info("failed to retrieve m.audio content: {}", - url_.toString().toStdString()); + url.toString().toStdString()); return; } - emit fileDownloadedCb(QByteArray(data.data(), data.size())); + emit proxy->fileDownloaded(QByteArray(data.data(), data.size())); }); } } diff --git a/src/timeline/widgets/AudioItem.h b/src/timeline/widgets/AudioItem.h index 7b0781a2..c32b7731 100644 --- a/src/timeline/widgets/AudioItem.h +++ b/src/timeline/widgets/AudioItem.h @@ -69,9 +69,6 @@ protected: void resizeEvent(QResizeEvent *event) override; void mousePressEvent(QMouseEvent *event) override; -signals: - void fileDownloadedCb(const QByteArray &data); - private slots: void fileDownloaded(const QByteArray &data); diff --git a/src/timeline/widgets/FileItem.cpp b/src/timeline/widgets/FileItem.cpp index f8d3272d..850941ae 100644 --- a/src/timeline/widgets/FileItem.cpp +++ b/src/timeline/widgets/FileItem.cpp @@ -50,8 +50,6 @@ FileItem::init() icon_.addFile(":/icons/icons/ui/arrow-pointing-down.png"); setFixedHeight(Height); - - connect(this, &FileItem::fileDownloadedCb, this, &FileItem::fileDownloaded); } FileItem::FileItem(const mtx::events::RoomEvent<mtx::events::msg::File> &event, QWidget *parent) @@ -110,19 +108,22 @@ FileItem::mousePressEvent(QMouseEvent *event) if (filenameToSave_.isEmpty()) return; + auto proxy = std::make_shared<MediaProxy>(); + connect(proxy.get(), &MediaProxy::fileDownloaded, this, &FileItem::fileDownloaded); + http::client()->download( url_.toString().toStdString(), - [this](const std::string &data, - const std::string &, - const std::string &, - mtx::http::RequestErr err) { + [proxy = std::move(proxy), url = url_](const std::string &data, + const std::string &, + const std::string &, + mtx::http::RequestErr err) { if (err) { nhlog::ui()->warn("failed to retrieve m.file content: {}", - url_.toString().toStdString()); + url.toString().toStdString()); return; } - emit fileDownloadedCb(QByteArray(data.data(), data.size())); + emit proxy->fileDownloaded(QByteArray(data.data(), data.size())); }); } else { openUrl(); diff --git a/src/timeline/widgets/FileItem.h b/src/timeline/widgets/FileItem.h index 66543e79..d63cce88 100644 --- a/src/timeline/widgets/FileItem.h +++ b/src/timeline/widgets/FileItem.h @@ -52,9 +52,6 @@ public: QColor iconColor() const { return iconColor_; } QColor backgroundColor() const { return backgroundColor_; } -signals: - void fileDownloadedCb(const QByteArray &data); - protected: void paintEvent(QPaintEvent *event) override; void mousePressEvent(QMouseEvent *event) override; diff --git a/src/timeline/widgets/ImageItem.cpp b/src/timeline/widgets/ImageItem.cpp index b66b82ae..4c68d683 100644 --- a/src/timeline/widgets/ImageItem.cpp +++ b/src/timeline/widgets/ImageItem.cpp @@ -33,11 +33,14 @@ void ImageItem::downloadMedia(const QUrl &url) { + auto proxy = std::make_shared<MediaProxy>(); + connect(proxy.get(), &MediaProxy::imageDownloaded, this, &ImageItem::setImage); + http::client()->download(url.toString().toStdString(), - [this, url](const std::string &data, - const std::string &, - const std::string &, - mtx::http::RequestErr err) { + [proxy = std::move(proxy), url](const std::string &data, + const std::string &, + const std::string &, + mtx::http::RequestErr err) { if (err) { nhlog::net()->warn( "failed to retrieve image {}: {} {}", @@ -49,7 +52,8 @@ ImageItem::downloadMedia(const QUrl &url) QPixmap img; img.loadFromData(QByteArray(data.data(), data.size())); - emit imageDownloaded(img); + + emit proxy->imageDownloaded(img); }); } @@ -76,8 +80,6 @@ ImageItem::init() setCursor(Qt::PointingHandCursor); setAttribute(Qt::WA_Hover, true); - connect(this, &ImageItem::imageDownloaded, this, &ImageItem::setImage); - connect(this, &ImageItem::imageSaved, this, &ImageItem::saveImage); downloadMedia(url_); } @@ -240,12 +242,15 @@ ImageItem::saveAs() const auto url = url_.toString().toStdString(); + auto proxy = std::make_shared<MediaProxy>(); + connect(proxy.get(), &MediaProxy::imageSaved, this, &ImageItem::saveImage); + http::client()->download( url, - [this, filename, url](const std::string &data, - const std::string &, - const std::string &, - mtx::http::RequestErr err) { + [proxy = std::move(proxy), filename, url](const std::string &data, + const std::string &, + const std::string &, + mtx::http::RequestErr err) { if (err) { nhlog::net()->warn("failed to retrieve image {}: {} {}", url, @@ -254,6 +259,6 @@ ImageItem::saveAs() return; } - emit imageSaved(filename, QByteArray(data.data(), data.size())); + emit proxy->imageSaved(filename, QByteArray(data.data(), data.size())); }); } diff --git a/src/timeline/widgets/ImageItem.h b/src/timeline/widgets/ImageItem.h index e9d823f4..65bd962d 100644 --- a/src/timeline/widgets/ImageItem.h +++ b/src/timeline/widgets/ImageItem.h @@ -48,10 +48,6 @@ public slots: void setImage(const QPixmap &image); void saveImage(const QString &filename, const QByteArray &data); -signals: - void imageDownloaded(const QPixmap &img); - void imageSaved(const QString &filename, const QByteArray &data); - protected: void paintEvent(QPaintEvent *event) override; void mousePressEvent(QMouseEvent *event) override; |