simplified Image a bit

This commit is contained in:
fourtf 2018-08-06 20:00:04 +02:00
parent c71795da17
commit defa7e41fa
2 changed files with 19 additions and 43 deletions

View file

@ -20,28 +20,12 @@
namespace chatterino { namespace chatterino {
namespace { namespace {
// Frame const QPixmap *getPixmap(const Pixmap &pixmap)
Frame::Frame(const QPixmap *nonOwning, int duration)
: nonOwning_(nonOwning)
, duration_(duration)
{ {
} if (pixmap.which() == 0)
return boost::get<const QPixmap *>(pixmap);
Frame::Frame(std::unique_ptr<QPixmap> owning, int duration) else
: owning_(std::move(owning)) return boost::get<std::unique_ptr<QPixmap>>(pixmap).get();
, duration_(duration)
{
}
int Frame::duration() const
{
return this->duration_;
}
const QPixmap *Frame::pixmap() const
{
if (this->nonOwning_) return this->nonOwning_;
return this->owning_.get();
} }
// Frames // Frames
@ -65,12 +49,12 @@ Frames::~Frames()
void Frames::advance() void Frames::advance()
{ {
this->timeOffset_ += GIF_FRAME_LENGTH; this->durationOffset_ += GIF_FRAME_LENGTH;
while (true) { while (true) {
this->index_ %= this->items_.size(); this->index_ %= this->items_.size();
if (this->timeOffset_ > this->items_[this->index_].duration()) { if (this->durationOffset_ > this->items_[this->index_].duration) {
this->timeOffset_ -= this->items_[this->index_].duration(); this->durationOffset_ -= this->items_[this->index_].duration;
this->index_ = (this->index_ + 1) % this->items_.size(); this->index_ = (this->index_ + 1) % this->items_.size();
} else { } else {
break; break;
@ -86,13 +70,13 @@ bool Frames::animated() const
const QPixmap *Frames::current() const const QPixmap *Frames::current() const
{ {
if (this->items_.size() == 0) return nullptr; 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 const QPixmap *Frames::first() const
{ {
if (this->items_.size() == 0) return nullptr; if (this->items_.size() == 0) return nullptr;
return this->items_.front().pixmap(); return getPixmap(this->items_.front().pixmap);
} }
// functions // functions
@ -111,7 +95,7 @@ std::vector<Frame> readFrames(QImageReader &reader, const Url &url)
auto pixmap = std::make_unique<QPixmap>(QPixmap::fromImage(image)); auto pixmap = std::make_unique<QPixmap>(QPixmap::fromImage(image));
int duration = std::max(20, reader.nextImageDelay()); 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<QPixmap> owning, qreal scale)
: scale_(scale) : scale_(scale)
{ {
std::vector<Frame> vec; std::vector<Frame> vec;
vec.push_back(Frame(std::move(owning))); vec.push_back(Frame{std::move(owning)});
this->frames_ = std::move(vec); this->frames_ = std::move(vec);
} }
@ -199,7 +183,7 @@ Image::Image(QPixmap *nonOwning, qreal scale)
: scale_(scale) : scale_(scale)
{ {
std::vector<Frame> vec; std::vector<Frame> vec;
vec.push_back(Frame(nonOwning)); vec.push_back(Frame{nonOwning});
this->frames_ = std::move(vec); this->frames_ = std::move(vec);
} }

View file

@ -6,6 +6,7 @@
#include <QString> #include <QString>
#include <atomic> #include <atomic>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <boost/variant.hpp>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
@ -13,19 +14,10 @@
namespace chatterino { namespace chatterino {
namespace { namespace {
class Frame using Pixmap = boost::variant<const QPixmap *, std::unique_ptr<QPixmap>>;
{ struct Frame {
public: Pixmap pixmap;
explicit Frame(const QPixmap *nonOwning, int duration = 1); int duration;
explicit Frame(std::unique_ptr<QPixmap> owning, int duration = 1);
const QPixmap *pixmap() const;
int duration() const;
private:
const QPixmap *nonOwning_{nullptr};
std::unique_ptr<QPixmap> owning_{};
int duration_{};
}; };
class Frames class Frames
{ {
@ -44,7 +36,7 @@ public:
private: private:
std::vector<Frame> items_; std::vector<Frame> items_;
int index_{0}; int index_{0};
int timeOffset_{0}; int durationOffset_{0};
}; };
} // namespace } // namespace