Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickmaskextruder.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtQml/qqml.h>
6#include <QtQml/qqmlinfo.h>
7#include <QtQml/qqmlcontext.h>
8
9#include <QImage>
10#include <QDebug>
11#include <QRandomGenerator>
12
33 , m_lastWidth(-1)
34 , m_lastHeight(-1)
35{
36}
37
39{
40 if (m_source != arg) {
41 m_source = arg;
42
43 m_lastHeight = -1;//Trigger reset
44 m_lastWidth = -1;
45 emit sourceChanged(m_source);
46 startMaskLoading();
47 }
48}
49
50void QQuickMaskExtruder::startMaskLoading()
51{
52 m_pix.clear(this);
53 if (m_source.isEmpty())
54 return;
55 const QQmlContext *context = qmlContext(this);
56 m_pix.load(context->engine(), context->resolvedUrl(m_source));
57 if (m_pix.isLoading())
58 m_pix.connectFinished(this, SLOT(finishMaskLoading()));
59 else
60 finishMaskLoading();
61}
62
63void QQuickMaskExtruder::finishMaskLoading()
64{
65 if (m_pix.isError())
66 qmlWarning(this) << m_pix.error();
67}
68
70{
72 if (!m_mask.size() || m_img.isNull())
73 return r.topLeft();
74 const QPointF p = m_mask[QRandomGenerator::global()->bounded(m_mask.size())];
75 //### Should random sub-pixel positioning be added?
76 return p + r.topLeft();
77}
78
79bool QQuickMaskExtruder::contains(const QRectF &bounds, const QPointF &point)
80{
81 ensureInitialized(bounds);//###Current usage patterns WILL lead to different bounds/r calls. Separate list?
82 if (m_img.isNull())
83 return false;
84
85 QPointF pt = point - bounds.topLeft();
86 QPoint p(pt.x() * m_img.width() / bounds.width(),
87 pt.y() * m_img.height() / bounds.height());
88 return m_img.rect().contains(p) && (m_img.pixel(p) & 0xff000000);
89}
90
91void QQuickMaskExtruder::ensureInitialized(const QRectF &rf)
92{
93 // Convert to integer coords to avoid comparing floats and ints which would
94 // often result in rounding errors.
95 QRect r = rf.toRect();
96 if (m_lastWidth == r.width() && m_lastHeight == r.height())
97 return;//Same as before
98 if (!m_pix.isReady())
99 return;
100 m_lastWidth = r.width();
101 m_lastHeight = r.height();
102
103 m_mask.clear();
104
105 m_img = m_pix.image();
106 // Image will in all likelyhood be in this format already, so
107 // no extra memory or conversion takes place
110
111 // resample on the fly using 16-bit
112 int sx = (m_img.width() << 16) / r.width();
113 int sy = (m_img.height() << 16) / r.height();
114 int w = r.width();
115 int h = r.height();
116 for (int y=0; y<h; ++y) {
117 const uint *sl = (const uint *) m_img.constScanLine((y * sy) >> 16);
118 for (int x=0; x<w; ++x) {
119 if (sl[(x * sx) >> 16] & 0xff000000)
120 m_mask << QPointF(x, y);
121 }
122 }
123}
125
126#include "moc_qquickmaskextruder_p.cpp"
QRgb pixel(int x, int y) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.cpp:2493
int width() const
Returns the width of the image.
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition qimage.cpp:1222
int height() const
Returns the height of the image.
@ Format_ARGB32_Premultiplied
Definition qimage.h:48
@ Format_ARGB32
Definition qimage.h:47
Format format() const
Returns the format of the image.
Definition qimage.cpp:2162
const uchar * constScanLine(int) const
Returns a pointer to the pixel data at the scanline with index i.
Definition qimage.cpp:1678
QRect rect() const
Returns the enclosing rectangle (0, 0, width(), height()) of the image.
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const &
Definition qimage.h:125
qsizetype size() const noexcept
Definition qlist.h:397
void clear()
Definition qlist.h:434
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
\inmodule QtCore\reentrant
Definition qpoint.h:25
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
QQuickMaskExtruder(QObject *parent=nullptr)
\qmltype MaskShape \instantiates QQuickMaskExtruder \inqmlmodule QtQuick.Particles \inherits Shape
QPointF extrude(const QRectF &) override
void setSource(const QUrl &arg)
void sourceChanged(const QUrl &arg)
bool contains(const QRectF &bounds, const QPointF &point) override
QImage image() const
QString error() const
void load(QQmlEngine *, const QUrl &)
bool isLoading() const
bool connectFinished(QObject *, const char *)
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
\threadsafe
Definition qrandom.h:275
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr qreal height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:732
constexpr qreal width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:729
constexpr QPointF topLeft() const noexcept
Returns the position of the rectangle's top-left corner.
Definition qrect.h:511
\inmodule QtCore\reentrant
Definition qrect.h:30
bool contains(const QRect &r, bool proper=false) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrect.cpp:855
\inmodule QtCore
Definition qurl.h:94
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition qurl.cpp:1896
Combined button and popup list for selecting options.
static void * context
static void ensureInitialized()
#define SLOT(a)
Definition qobjectdefs.h:52
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLboolean r
[2]
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLfloat GLfloat p
[1]
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
SSL_CTX int void * arg
#define emit
unsigned int uint
Definition qtypes.h:34