From defa7e41fa87918a087608eda2b41351535aad1b Mon Sep 17 00:00:00 2001 From: fourtf Date: Mon, 6 Aug 2018 20:00:04 +0200 Subject: [PATCH] simplified Image a bit --- src/messages/Image.cpp | 42 +++++++++++++----------------------------- src/messages/Image.hpp | 20 ++++++-------------- 2 files changed, 19 insertions(+), 43 deletions(-) diff --git a/src/messages/Image.cpp b/src/messages/Image.cpp index 309d2f0a0..44c855dd7 100644 --- a/src/messages/Image.cpp +++ b/src/messages/Image.cpp @@ -20,28 +20,12 @@ namespace chatterino { namespace { -// Frame -Frame::Frame(const QPixmap *nonOwning, int duration) - : nonOwning_(nonOwning) - , duration_(duration) +const QPixmap *getPixmap(const Pixmap &pixmap) { -} - -Frame::Frame(std::unique_ptr owning, int duration) - : owning_(std::move(owning)) - , duration_(duration) -{ -} - -int Frame::duration() const -{ - return this->duration_; -} - -const QPixmap *Frame::pixmap() const -{ - if (this->nonOwning_) return this->nonOwning_; - return this->owning_.get(); + if (pixmap.which() == 0) + return boost::get(pixmap); + else + return boost::get>(pixmap).get(); } // Frames @@ -65,12 +49,12 @@ Frames::~Frames() void Frames::advance() { - this->timeOffset_ += GIF_FRAME_LENGTH; + this->durationOffset_ += GIF_FRAME_LENGTH; while (true) { this->index_ %= this->items_.size(); - if (this->timeOffset_ > this->items_[this->index_].duration()) { - this->timeOffset_ -= this->items_[this->index_].duration(); + if (this->durationOffset_ > this->items_[this->index_].duration) { + this->durationOffset_ -= this->items_[this->index_].duration; this->index_ = (this->index_ + 1) % this->items_.size(); } else { break; @@ -86,13 +70,13 @@ bool Frames::animated() const const QPixmap *Frames::current() const { if (this->items_.size() == 0) return nullptr; - return this->items_[this->index_].pixmap(); + return getPixmap(this->items_[this->index_].pixmap); } const QPixmap *Frames::first() const { if (this->items_.size() == 0) return nullptr; - return this->items_.front().pixmap(); + return getPixmap(this->items_.front().pixmap); } // functions @@ -111,7 +95,7 @@ std::vector readFrames(QImageReader &reader, const Url &url) auto pixmap = std::make_unique(QPixmap::fromImage(image)); int duration = std::max(20, reader.nextImageDelay()); - frames.push_back(Frame(std::move(pixmap), duration)); + frames.push_back(Frame{std::move(pixmap), duration}); } } @@ -191,7 +175,7 @@ Image::Image(std::unique_ptr owning, qreal scale) : scale_(scale) { std::vector vec; - vec.push_back(Frame(std::move(owning))); + vec.push_back(Frame{std::move(owning)}); this->frames_ = std::move(vec); } @@ -199,7 +183,7 @@ Image::Image(QPixmap *nonOwning, qreal scale) : scale_(scale) { std::vector vec; - vec.push_back(Frame(nonOwning)); + vec.push_back(Frame{nonOwning}); this->frames_ = std::move(vec); } diff --git a/src/messages/Image.hpp b/src/messages/Image.hpp index 7db43580c..01ccd68eb 100644 --- a/src/messages/Image.hpp +++ b/src/messages/Image.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -13,19 +14,10 @@ namespace chatterino { namespace { -class Frame -{ -public: - explicit Frame(const QPixmap *nonOwning, int duration = 1); - explicit Frame(std::unique_ptr owning, int duration = 1); - - const QPixmap *pixmap() const; - int duration() const; - -private: - const QPixmap *nonOwning_{nullptr}; - std::unique_ptr owning_{}; - int duration_{}; +using Pixmap = boost::variant>; +struct Frame { + Pixmap pixmap; + int duration; }; class Frames { @@ -44,7 +36,7 @@ public: private: std::vector items_; int index_{0}; - int timeOffset_{0}; + int durationOffset_{0}; }; } // namespace