tdesktop/Telegram/SourceFiles/ui/image/image_source.h

328 lines
7.7 KiB
C
Raw Normal View History

2018-10-11 15:54:57 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "ui/image/image.h"
2018-10-11 15:54:57 +00:00
namespace Images {
2018-10-11 15:54:57 +00:00
class ImageSource : public Source {
public:
ImageSource(QImage &&data, const QByteArray &format);
2018-10-11 15:54:57 +00:00
void load(
2018-10-11 15:54:57 +00:00
Data::FileOrigin origin,
bool loadFirst,
bool prior) override;
void loadEvenCancelled(
Data::FileOrigin origin,
bool loadFirst,
bool prior) override;
QImage takeLoaded() override;
void unload() override;
2018-10-11 15:54:57 +00:00
void automaticLoad(
2018-10-11 15:54:57 +00:00
Data::FileOrigin origin,
const HistoryItem *item) override;
void automaticLoadSettingsChanged() override;
2018-10-11 15:54:57 +00:00
bool loading() override;
bool displayLoading() override;
void cancel() override;
float64 progress() override;
int loadOffset() override;
2018-10-11 15:54:57 +00:00
const StorageImageLocation &location() override;
void refreshFileReference(const QByteArray &data) override;
std::optional<Storage::Cache::Key> cacheKey() override;
void setDelayedStorageLocation(
const StorageImageLocation &location) override;
void performDelayedLoad(Data::FileOrigin origin) override;
bool isDelayedStorageImage() const override;
void setImageBytes(const QByteArray &bytes) override;
2018-10-11 15:54:57 +00:00
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
2018-10-11 15:54:57 +00:00
QByteArray bytesForCache() override;
2018-10-11 15:54:57 +00:00
private:
QImage _data;
QByteArray _format;
2018-10-23 13:04:42 +00:00
QByteArray _bytes;
2018-10-30 05:44:48 +00:00
int _width = 0;
int _height = 0;
2018-10-11 15:54:57 +00:00
};
2018-10-11 15:54:57 +00:00
class LocalFileSource : public Source {
public:
LocalFileSource(
const QString &path,
const QByteArray &content,
const QByteArray &format,
QImage &&data = QImage());
2018-10-11 15:54:57 +00:00
void load(
Data::FileOrigin origin,
bool loadFirst,
bool prior) override;
void loadEvenCancelled(
Data::FileOrigin origin,
bool loadFirst,
bool prior) override;
QImage takeLoaded() override;
void unload() override;
2018-10-11 15:54:57 +00:00
void automaticLoad(
Data::FileOrigin origin,
const HistoryItem *item) override;
void automaticLoadSettingsChanged() override;
2018-10-11 15:54:57 +00:00
bool loading() override;
bool displayLoading() override;
void cancel() override;
float64 progress() override;
int loadOffset() override;
const StorageImageLocation &location() override;
void refreshFileReference(const QByteArray &data) override;
std::optional<Storage::Cache::Key> cacheKey() override;
void setDelayedStorageLocation(
const StorageImageLocation &location) override;
void performDelayedLoad(Data::FileOrigin origin) override;
bool isDelayedStorageImage() const override;
void setImageBytes(const QByteArray &bytes) override;
2018-10-11 15:54:57 +00:00
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
QByteArray bytesForCache() override;
2018-10-11 15:54:57 +00:00
private:
void ensureDimensionsKnown();
QString _path;
QByteArray _bytes;
QByteArray _format;
QImage _data;
int _width = 0;
int _height = 0;
2018-10-11 15:54:57 +00:00
};
class RemoteSource : public Source {
2018-10-11 15:54:57 +00:00
public:
void load(
Data::FileOrigin origin,
bool loadFirst,
bool prior) override;
void loadEvenCancelled(
Data::FileOrigin origin,
bool loadFirst,
bool prior) override;
QImage takeLoaded() override;
void unload() override;
2018-10-11 15:54:57 +00:00
void automaticLoad(
Data::FileOrigin origin,
const HistoryItem *item) override;
2018-10-11 15:54:57 +00:00
void automaticLoadSettingsChanged() override;
bool loading() override;
bool displayLoading() override;
2018-10-11 15:54:57 +00:00
void cancel() override;
float64 progress() override;
int loadOffset() override;
2018-10-11 15:54:57 +00:00
const StorageImageLocation &location() override;
void refreshFileReference(const QByteArray &data) override;
void setDelayedStorageLocation(
const StorageImageLocation &location) override;
void performDelayedLoad(Data::FileOrigin origin) override;
bool isDelayedStorageImage() const override;
void setImageBytes(const QByteArray &bytes) override;
2018-10-11 15:54:57 +00:00
QByteArray bytesForCache() override;
2018-10-11 15:54:57 +00:00
~RemoteSource();
2018-10-11 15:54:57 +00:00
protected:
// If after loading the image we need to shrink it to fit into a
// specific size, you can return this size here.
virtual QSize shrinkBox() const = 0;
2018-10-11 15:54:57 +00:00
virtual FileLoader *createLoader(
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) = 0;
void loadLocal();
private:
bool loaderValid() const;
void destroyLoaderDelayed(FileLoader *newValue = nullptr);
2018-10-11 15:54:57 +00:00
FileLoader *_loader = nullptr;
2018-10-11 15:54:57 +00:00
};
class StorageSource : public RemoteSource {
2018-10-11 15:54:57 +00:00
public:
StorageSource(
const StorageImageLocation &location,
int size);
2018-10-11 15:54:57 +00:00
const StorageImageLocation &location() override;
std::optional<Storage::Cache::Key> cacheKey() override;
void refreshFileReference(const QByteArray &data) override;
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
2018-10-11 15:54:57 +00:00
protected:
QSize shrinkBox() const override;
2018-10-11 15:54:57 +00:00
FileLoader *createLoader(
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) override;
StorageImageLocation _location;
int _size = 0;
2018-10-11 15:54:57 +00:00
};
class WebCachedSource : public RemoteSource {
2018-10-11 15:54:57 +00:00
public:
WebCachedSource(const WebFileLocation &location, QSize box, int size = 0);
WebCachedSource(
2018-10-11 15:54:57 +00:00
const WebFileLocation &location,
int width,
int height,
int size = 0);
std::optional<Storage::Cache::Key> cacheKey() override;
2018-10-11 15:54:57 +00:00
int width() override;
int height() override;
int bytesSize() override;
2018-10-11 15:54:57 +00:00
void setInformation(int size, int width, int height) override;
protected:
QSize shrinkBox() const override;
2018-10-11 15:54:57 +00:00
FileLoader *createLoader(
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) override;
WebFileLocation _location;
QSize _box;
int _width = 0;
int _height = 0;
int _size = 0;
};
class GeoPointSource : public RemoteSource {
2018-10-11 15:54:57 +00:00
public:
GeoPointSource(const GeoPointLocation &location);
2018-10-11 15:54:57 +00:00
std::optional<Storage::Cache::Key> cacheKey() override;
2018-10-11 15:54:57 +00:00
int width() override;
int height() override;
int bytesSize() override;
2018-10-11 15:54:57 +00:00
void setInformation(int size, int width, int height) override;
protected:
QSize shrinkBox() const override;
2018-10-11 15:54:57 +00:00
FileLoader *createLoader(
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) override;
GeoPointLocation _location;
int _size = 0;
};
class DelayedStorageSource : public StorageSource {
2018-10-11 15:54:57 +00:00
public:
DelayedStorageSource();
DelayedStorageSource(int width, int height);
2018-10-11 15:54:57 +00:00
void load(
2018-10-11 15:54:57 +00:00
Data::FileOrigin origin,
bool loadFirst,
bool prior) override;
void loadEvenCancelled(
Data::FileOrigin origin,
bool loadFirst,
bool prior) override;
void setDelayedStorageLocation(
const StorageImageLocation &location) override;
bool isDelayedStorageImage() const override;
void performDelayedLoad(Data::FileOrigin origin) override;
2018-10-11 15:54:57 +00:00
void automaticLoad(
Data::FileOrigin origin,
const HistoryItem *item) override; // auto load photo
void automaticLoadSettingsChanged() override;
bool loading() override {
return _location.isNull() ? _loadRequested : StorageSource::loading();
2018-10-11 15:54:57 +00:00
}
bool displayLoading() override;
2018-10-11 15:54:57 +00:00
void cancel() override;
private:
bool _loadRequested = false;
bool _loadCancelled = false;
bool _loadFromCloud = false;
2018-10-11 15:54:57 +00:00
};
class WebUrlSource : public RemoteSource {
2018-10-11 15:54:57 +00:00
public:
// If !box.isEmpty() then resize the image to fit in this box.
explicit WebUrlSource(const QString &url, QSize box = QSize());
WebUrlSource(const QString &url, int width, int height);
2018-10-11 15:54:57 +00:00
std::optional<Storage::Cache::Key> cacheKey() override;
2018-10-11 15:54:57 +00:00
int width() override;
int height() override;
int bytesSize() override;
void setInformation(int size, int width, int height) override;
2018-10-11 15:54:57 +00:00
protected:
QSize shrinkBox() const override;
2018-10-11 15:54:57 +00:00
FileLoader *createLoader(
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) override;
private:
QString _url;
QSize _box;
int _size = 0;
int _width = 0;
int _height = 0;
};
} // namespace Images