ViewSolutions
Loading...
Searching...
No Matches
qmlcolorpicker.cpp
Go to the documentation of this file.
1
2//#
3//# Copyright (C) 2020-2025 QuasarApp.
4//# Distributed under the GPLv3 software license, see the accompanying
5//# Everyone is permitted to copy and distribute verbatim copies
6//# of this license document, but changing it is not allowed.
7//#
8
9#include "qmlcolorpicker.h"
10
11#include <QQmlApplicationEngine>
12#include <QQuickAsyncImageProvider>
13namespace ViewSolutions {
14
16 QObject(parent) {
17
18}
19
20QColor QMLColorPicker::pick(const QString &img) const {
21
22 if (img.left(6) == "image:") {
23 int urlBegin = img.indexOf('/', 8);
24 QString id = img.mid(8, urlBegin - 8);
25
26 if (auto eng = engine()) {
27 QQuickImageProvider* provider = dynamic_cast<QQuickImageProvider*>(eng->imageProvider(id));
28
29 if (!provider) {
30 return {};
31 }
32
33 QString url = img.mid(urlBegin + 1);
34
35 if (provider->imageType() & QQmlImageProviderBase::ImageResponse) {
36 auto async = static_cast<QQuickAsyncImageProvider*>(provider);
37 auto textureFacrory = async->requestImageResponse(url, {})->textureFactory();
38
39 if (!textureFacrory) {
40 return {};
41 }
42
43 QColor responce = ColorPicker::pick(textureFacrory->image());
44
45 delete textureFacrory;
46
47 return responce;
48 }
49
50 if (provider->imageType() & QQmlImageProviderBase::Texture) {
51 return ColorPicker::pick(provider->requestTexture(url, nullptr, {})->image());
52 }
53
54 if (provider->imageType() & QQmlImageProviderBase::Pixmap) {
55 return ColorPicker::pick(provider->requestPixmap(url, nullptr, {}).toImage());
56 }
57
58 if (provider->imageType() & QQmlImageProviderBase::Image) {
59 return ColorPicker::pick(provider->requestImage(url, nullptr, {}));
60 }
61
62 return {};
63 }
64
65 }
66
67 return ColorPicker::pick(img);
68}
69
70QString QMLColorPicker::modelId() const {
71 return "ColorPicker";
72}
73
74}
QColor pick(int x, int y, const QImage &img) const
pick Get color from point.
QMLColorPicker(QObject *parent=nullptr)
QString modelId() const override
instance This is singleton instance of QMLColorPicker.
Q_INVOKABLE QColor pick(const QString &img) const
pick This is override function for qml.
QQmlApplicationEngine * engine() const
engine returns qml engine.
Definition imodel.cpp:19
the ViewSolutions namespace