mirror-chatterino2/src/messages/Image.hpp

87 lines
2.1 KiB
C++
Raw Normal View History

2017-06-06 21:18:05 +02:00
#pragma once
2017-01-04 15:12:31 +01:00
2018-08-02 14:23:27 +02:00
#include "common/Common.hpp"
2017-01-13 18:59:11 +01:00
#include <QPixmap>
2017-01-11 18:52:09 +01:00
#include <QString>
2018-08-02 14:23:27 +02:00
#include <atomic>
#include <boost/noncopyable.hpp>
2018-08-02 14:23:27 +02:00
#include <memory>
#include <mutex>
2018-08-02 14:23:27 +02:00
#include "common/NullablePtr.hpp"
2018-01-19 22:45:33 +01:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
2018-08-02 14:23:27 +02:00
class Image;
using ImagePtr = std::shared_ptr<Image>;
class Image : public std::enable_shared_from_this<Image>, boost::noncopyable
2017-01-04 15:12:31 +01:00
{
public:
2018-08-02 14:23:27 +02:00
static ImagePtr fromUrl(const Url &url, qreal scale = 1);
static ImagePtr fromOwningPixmap(std::unique_ptr<QPixmap> pixmap, qreal scale = 1);
static ImagePtr fromNonOwningPixmap(QPixmap *pixmap, qreal scale = 1);
static ImagePtr getEmpty();
2018-08-02 14:23:27 +02:00
const Url &getUrl() const;
NullablePtr<const QPixmap> getPixmap() const;
2017-09-12 19:06:16 +02:00
qreal getScale() const;
bool isAnimated() const;
2017-09-12 19:06:16 +02:00
int getWidth() const;
int getHeight() const;
2018-08-02 14:23:27 +02:00
bool isLoaded() const;
bool isError() const;
bool isValid() const;
bool isNull() const;
2017-01-11 01:08:20 +01:00
2018-08-02 14:23:27 +02:00
bool operator==(const Image &image) const;
bool operator!=(const Image &image) const;
2017-01-05 16:07:20 +01:00
private:
2018-08-02 14:23:27 +02:00
class Frame
{
public:
QPixmap *getPixmap() const;
int getDuration() const;
Frame(QPixmap *nonOwning, int duration = 1);
Frame(std::unique_ptr<QPixmap> nonOwning, int duration = 1);
private:
QPixmap *nonOwning_;
std::unique_ptr<QPixmap> owning_;
int duration_;
2017-02-06 17:42:28 +01:00
};
2018-08-02 14:23:27 +02:00
Image();
Image(const Url &url, qreal scale);
Image(std::unique_ptr<QPixmap> owning, qreal scale);
Image(QPixmap *nonOwning, qreal scale);
2018-08-02 14:23:27 +02:00
void load();
Outcome parse(const QByteArray &data);
std::vector<Frame> readFrames(QImageReader &reader);
Outcome setFrames(std::vector<Frame> frames);
void updateAnimation();
void queueLoadedEvent();
2017-01-06 23:28:48 +01:00
2018-08-02 14:23:27 +02:00
Url url_;
bool isLoaded_{false};
bool isLoading_{false};
bool isAnimated_{false};
bool isError_{false};
bool isNull_ = false;
qreal scale_ = 1;
QObject object_;
2017-01-11 18:52:09 +01:00
2018-08-02 14:23:27 +02:00
std::vector<Frame> frames_;
std::mutex framesMutex_;
NullablePtr<QPixmap> currentFramePixmap_;
int currentFrameIndex_ = 0;
int currentFrameOffset_ = 0;
2017-01-11 18:52:09 +01:00
2018-08-02 14:23:27 +02:00
static std::atomic<bool> loadedEventQueued;
2017-01-04 15:12:31 +01:00
};
2017-04-14 17:52:22 +02:00
} // namespace chatterino