From 10a32c47a95b8438309841c4cb63159c4799bb8b Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 5 Jan 2018 11:06:30 +0900 Subject: Avatar: Use Connection::getThumbnail instead of callApi<> --- avatar.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'avatar.cpp') diff --git a/avatar.cpp b/avatar.cpp index 9490347d..eeb61f30 100644 --- a/avatar.cpp +++ b/avatar.cpp @@ -76,7 +76,7 @@ QImage Avatar::Private::get(QSize size, Avatar::notifier_t notifier) if (_ongoingRequest) _ongoingRequest->abandon(); notifiers.emplace_back(std::move(notifier)); - _ongoingRequest = _connection->callApi(_url, size); + _ongoingRequest = _connection->getThumbnail(_url, size); _ongoingRequest->connect( _ongoingRequest, &MediaThumbnailJob::finished, _connection, [=]() { if (_ongoingRequest->status().good()) -- cgit v1.2.3 From 0dbf41133cdc9d8b73592f78db117396bc7bd2da Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 8 Jan 2018 07:41:57 +0900 Subject: Avatar: Mark get() as const operation With all the liberty that pimpl idiom gives it's easy to get away without proper const's; but let's be consistent :) --- avatar.cpp | 22 +++++++++++----------- avatar.h | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) (limited to 'avatar.cpp') diff --git a/avatar.cpp b/avatar.cpp index eeb61f30..ee7db6ed 100644 --- a/avatar.cpp +++ b/avatar.cpp @@ -30,19 +30,19 @@ class Avatar::Private { public: Private(Connection* c, QIcon di) : _connection(c), _defaultIcon(di) { } - QImage get(QSize size, Avatar::notifier_t notifier); + QImage get(QSize size, Avatar::notifier_t notifier) const; Connection* _connection; const QIcon _defaultIcon; QUrl _url; - QImage _originalImage; - std::vector> _scaledImages; - - QSize _requestedSize; - bool _valid = false; - MediaThumbnailJob* _ongoingRequest = nullptr; - std::vector notifiers; + // The below are related to image caching, hence mutable + mutable QImage _originalImage; + mutable std::vector> _scaledImages; + mutable QSize _requestedSize; + mutable bool _valid = false; + mutable MediaThumbnailJob* _ongoingRequest = nullptr; + mutable std::vector notifiers; }; Avatar::Avatar(Connection* connection, QIcon defaultIcon) @@ -51,17 +51,17 @@ Avatar::Avatar(Connection* connection, QIcon defaultIcon) Avatar::~Avatar() = default; -QImage Avatar::get(int dimension, Avatar::notifier_t notifier) +QImage Avatar::get(int dimension, notifier_t notifier) const { return d->get({dimension, dimension}, notifier); } -QImage Avatar::get(int width, int height, Avatar::notifier_t notifier) +QImage Avatar::get(int width, int height, notifier_t notifier) const { return d->get({width, height}, notifier); } -QImage Avatar::Private::get(QSize size, Avatar::notifier_t notifier) +QImage Avatar::Private::get(QSize size, Avatar::notifier_t notifier) const { // FIXME: Alternating between longer-width and longer-height requests // is a sure way to trick the below code into constantly getting another diff --git a/avatar.h b/avatar.h index bf9cfdcd..28c16e4d 100644 --- a/avatar.h +++ b/avatar.h @@ -35,8 +35,8 @@ namespace QMatrixClient using notifier_t = std::function; - QImage get(int dimension, notifier_t notifier); - QImage get(int w, int h, notifier_t notifier); + QImage get(int dimension, notifier_t notifier) const; + QImage get(int w, int h, notifier_t notifier) const; QUrl url() const; bool updateUrl(const QUrl& newUrl); -- cgit v1.2.3 From 1990a0f67906a8f919c97cea56fc5670cc09399c Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 9 Jan 2018 11:22:16 +0900 Subject: Avatar: Use QPointer<> and isJobRunning() --- avatar.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'avatar.cpp') diff --git a/avatar.cpp b/avatar.cpp index ee7db6ed..a13507fb 100644 --- a/avatar.cpp +++ b/avatar.cpp @@ -23,6 +23,7 @@ #include "connection.h" #include +#include using namespace QMatrixClient; @@ -41,7 +42,7 @@ class Avatar::Private mutable std::vector> _scaledImages; mutable QSize _requestedSize; mutable bool _valid = false; - mutable MediaThumbnailJob* _ongoingRequest = nullptr; + mutable QPointer _ongoingRequest = nullptr; mutable std::vector notifiers; }; @@ -73,22 +74,17 @@ QImage Avatar::Private::get(QSize size, Avatar::notifier_t notifier) const { qCDebug(MAIN) << "Getting avatar from" << _url.toString(); _requestedSize = size; - if (_ongoingRequest) + if (isJobRunning(_ongoingRequest)) _ongoingRequest->abandon(); notifiers.emplace_back(std::move(notifier)); _ongoingRequest = _connection->getThumbnail(_url, size); - _ongoingRequest->connect( _ongoingRequest, &MediaThumbnailJob::finished, - _connection, [=]() { - if (_ongoingRequest->status().good()) - { - _valid = true; - _originalImage = - _ongoingRequest->scaledThumbnail(_requestedSize); - _scaledImages.clear(); - for (auto n: notifiers) - n(); - } - _ongoingRequest = nullptr; + QObject::connect( _ongoingRequest, &MediaThumbnailJob::success, [this] + { + _valid = true; + _originalImage = _ongoingRequest->scaledThumbnail(_requestedSize); + _scaledImages.clear(); + for (auto n: notifiers) + n(); }); } -- cgit v1.2.3