generated from obsproject/obs-plugintemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add image fetching from HTML template (#96)
- Loading branch information
Showing
4 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "CustomTextDocument.h" | ||
|
||
#include "plugin-support.h" | ||
#include "request-data.h" | ||
|
||
#include <QImageReader> | ||
#include <QPixmap> | ||
#include <QByteArray> | ||
#include <QPainter> | ||
|
||
#include <obs.h> | ||
|
||
// Initialize the static member | ||
QHash<QUrl, QImage> CustomTextDocument::imageCache; | ||
|
||
CustomTextDocument::CustomTextDocument(QObject *parent) : QTextDocument(parent) {} | ||
|
||
QVariant CustomTextDocument::loadResource(int type, const QUrl &name) | ||
{ | ||
if (type == QTextDocument::ImageResource) { | ||
if (imageCache.contains(name)) { | ||
return QVariant(imageCache.value(name)); | ||
} | ||
obs_log(LOG_INFO, "fetch %s", name.toString().toStdString().c_str()); | ||
std::string mime_type; | ||
std::vector<uint8_t> image_bytes = | ||
fetch_image(name.toString().toStdString(), mime_type); | ||
// Create a QByteArray from the std::vector<uint8_t> | ||
QByteArray imageData(reinterpret_cast<const char *>(image_bytes.data()), | ||
image_bytes.size()); | ||
QBuffer buffer(&imageData); | ||
buffer.open(QIODevice::ReadOnly); | ||
QImageReader reader(&buffer); | ||
reader.setDecideFormatFromContent(true); | ||
QImage image = reader.read(); | ||
if (!image.isNull()) { | ||
imageCache[name] = image; | ||
return image; | ||
} | ||
obs_log(LOG_ERROR, "Unable to load image from: %s", | ||
name.toString().toStdString().c_str()); | ||
return QVariant(); | ||
} | ||
return QTextDocument::loadResource(type, name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef CUSTOMTEXTDOCUMENT_H | ||
#define CUSTOMTEXTDOCUMENT_H | ||
|
||
#include <QtWidgets> | ||
#include <QTextDocument> | ||
#include <QUrl> | ||
#include <QHash> | ||
|
||
class CustomTextDocument : public QTextDocument { | ||
Q_OBJECT | ||
|
||
public: | ||
CustomTextDocument(QObject *parent = nullptr); | ||
|
||
protected: | ||
QVariant loadResource(int type, const QUrl &name) override; | ||
|
||
private: | ||
static QHash<QUrl, QImage> imageCache; // Static member for the cache | ||
}; | ||
|
||
#endif // CUSTOMTEXTDOCUMENT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters