forked from quickshell/quickshell
service/tray: rework tray image providers
This commit is contained in:
parent
aa9f8cd001
commit
7cc1b54587
10 changed files with 163 additions and 82 deletions
|
@ -23,6 +23,7 @@ qt_add_library(quickshell-core STATIC
|
|||
lazyloader.cpp
|
||||
easingcurve.cpp
|
||||
iconimageprovider.cpp
|
||||
imageprovider.cpp
|
||||
transformwatcher.cpp
|
||||
)
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include <qtmetamacros.h>
|
||||
|
||||
#include "iconimageprovider.hpp"
|
||||
#include "imageprovider.hpp"
|
||||
#include "incubator.hpp"
|
||||
#include "plugin.hpp"
|
||||
#include "qsintercept.hpp"
|
||||
|
@ -35,6 +36,8 @@ EngineGeneration::EngineGeneration(QmlScanner scanner)
|
|||
this->engine->setIncubationController(&this->delayedIncubationController);
|
||||
|
||||
this->engine->addImageProvider("icon", new IconImageProvider());
|
||||
this->engine->addImageProvider("qsimage", new QsImageProvider());
|
||||
this->engine->addImageProvider("qspixmap", new QsPixmapProvider());
|
||||
|
||||
QuickshellPlugin::runConstructGeneration(*this);
|
||||
}
|
||||
|
|
83
src/core/imageprovider.cpp
Normal file
83
src/core/imageprovider.cpp
Normal file
|
@ -0,0 +1,83 @@
|
|||
#include "imageprovider.hpp"
|
||||
|
||||
#include <qdebug.h>
|
||||
#include <qimage.h>
|
||||
#include <qlogging.h>
|
||||
#include <qmap.h>
|
||||
#include <qobject.h>
|
||||
#include <qpixmap.h>
|
||||
#include <qqmlengine.h>
|
||||
|
||||
static QMap<QString, QsImageHandle*> liveImages;
|
||||
|
||||
QsImageHandle::QsImageHandle(QQmlImageProviderBase::ImageType type, QObject* parent)
|
||||
: QObject(parent)
|
||||
, type(type) {
|
||||
{
|
||||
auto dbg = QDebug(&this->id);
|
||||
dbg.nospace() << static_cast<void*>(this);
|
||||
}
|
||||
|
||||
liveImages.insert(this->id, this);
|
||||
}
|
||||
|
||||
QsImageHandle::~QsImageHandle() { liveImages.remove(this->id); }
|
||||
|
||||
QString QsImageHandle::url() const {
|
||||
QString url = "image://";
|
||||
if (this->type == QQmlImageProviderBase::Image) url += "qsimage";
|
||||
else if (this->type == QQmlImageProviderBase::Pixmap) url += "qspixmap";
|
||||
url += "/" + this->id;
|
||||
return url;
|
||||
}
|
||||
|
||||
QImage
|
||||
QsImageHandle::requestImage(const QString& /*unused*/, QSize* /*unused*/, const QSize& /*unused*/) {
|
||||
qWarning() << "Image handle" << this << "does not provide QImages";
|
||||
return QImage();
|
||||
}
|
||||
|
||||
QPixmap QsImageHandle::
|
||||
requestPixmap(const QString& /*unused*/, QSize* /*unused*/, const QSize& /*unused*/) {
|
||||
qWarning() << "Image handle" << this << "does not provide QPixmaps";
|
||||
return QPixmap();
|
||||
}
|
||||
|
||||
void parseReq(const QString& req, QString& target, QString& param) {
|
||||
auto splitIdx = req.indexOf('/');
|
||||
if (splitIdx != -1) {
|
||||
target = req.sliced(0, splitIdx);
|
||||
param = req.sliced(splitIdx + 1);
|
||||
} else {
|
||||
target = req;
|
||||
}
|
||||
}
|
||||
|
||||
QImage QsImageProvider::requestImage(const QString& id, QSize* size, const QSize& requestedSize) {
|
||||
QString target;
|
||||
QString param;
|
||||
parseReq(id, target, param);
|
||||
|
||||
auto* handle = liveImages.value(target);
|
||||
if (handle != nullptr) {
|
||||
return handle->requestImage(param, size, requestedSize);
|
||||
} else {
|
||||
qWarning() << "Requested image from unknown handle" << id;
|
||||
return QImage();
|
||||
}
|
||||
}
|
||||
|
||||
QPixmap
|
||||
QsPixmapProvider::requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) {
|
||||
QString target;
|
||||
QString param;
|
||||
parseReq(id, target, param);
|
||||
|
||||
auto* handle = liveImages.value(target);
|
||||
if (handle != nullptr) {
|
||||
return handle->requestPixmap(param, size, requestedSize);
|
||||
} else {
|
||||
qWarning() << "Requested image from unknown handle" << id;
|
||||
return QPixmap();
|
||||
}
|
||||
}
|
39
src/core/imageprovider.hpp
Normal file
39
src/core/imageprovider.hpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
|
||||
#include <qimage.h>
|
||||
#include <qmap.h>
|
||||
#include <qobject.h>
|
||||
#include <qqmlengine.h>
|
||||
#include <qquickimageprovider.h>
|
||||
#include <qtclasshelpermacros.h>
|
||||
#include <qtmetamacros.h>
|
||||
|
||||
class QsImageProvider: public QQuickImageProvider {
|
||||
public:
|
||||
explicit QsImageProvider(): QQuickImageProvider(QQuickImageProvider::Image) {}
|
||||
QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize) override;
|
||||
};
|
||||
|
||||
class QsPixmapProvider: public QQuickImageProvider {
|
||||
public:
|
||||
explicit QsPixmapProvider(): QQuickImageProvider(QQuickImageProvider::Pixmap) {}
|
||||
QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override;
|
||||
};
|
||||
|
||||
class QsImageHandle: public QObject {
|
||||
Q_OBJECT;
|
||||
|
||||
public:
|
||||
explicit QsImageHandle(QQmlImageProviderBase::ImageType type, QObject* parent = nullptr);
|
||||
~QsImageHandle() override;
|
||||
Q_DISABLE_COPY_MOVE(QsImageHandle);
|
||||
|
||||
[[nodiscard]] QString url() const;
|
||||
|
||||
virtual QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize);
|
||||
virtual QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize);
|
||||
|
||||
private:
|
||||
QQmlImageProviderBase::ImageType type;
|
||||
QString id;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue