Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QtQuickView.java
Go to the documentation of this file.
1// Copyright (C) 2024 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
4package org.qtproject.qt.android;
5
6import org.qtproject.qt.android.QtSignalListener;
7
8import android.content.Context;
9import android.util.Log;
10
11import java.lang.IllegalArgumentException;
12import java.lang.ref.WeakReference;
13import java.security.InvalidParameterException;
14
27public class QtQuickView extends QtView {
28 private final static String TAG = "QtQuickView";
29
30 private String m_qmlUri;
31 private String[] m_qmlImportPaths = null;
32 private QtQmlStatusChangeListener m_statusChangeListener = null;
33 private QtQmlStatus m_lastStatus = QtQmlStatus.NULL;
34 private boolean m_hasQueuedStatus = false;
35 private WeakReference<QtQuickViewContent> m_loadedComponent;
36 private QtSignalQueue m_signalQueue = new QtSignalQueue();
37
38 native void createQuickView(String qmlUri, int width, int height, long parentWindowReference,
39 long viewReference, String[] qmlImportPaths);
40 native void setRootObjectProperty(long windowReference, String propertyName, Object value);
41 native Object getRootObjectProperty(long windowReference, String propertyName);
42 native boolean addRootObjectSignalListener(long windowReference, String signalName,
43 Class<?>[] argTypes, Object listener, int id);
44 native boolean removeRootObjectSignalListener(long windowReference, int signalListenerId);
45 native void invokeMethod(long windowReference, String methodName, Object[] params);
46
59 public QtQuickView(Context context, String qmlUri, String appName)
60 throws InvalidParameterException {
61 this(context, qmlUri, appName, null);
62 }
63
79 public QtQuickView(Context context, String qmlUri, String appName, String[] qmlImportPaths)
80 throws InvalidParameterException
81 {
82 super(context, appName);
83 if (qmlUri == null || qmlUri.isEmpty()) {
84 throw new InvalidParameterException(
85 "QtQuickView: argument 'qmlUri' may not be empty or null");
86 }
87 m_qmlUri = qmlUri;
88 m_qmlImportPaths = qmlImportPaths;
89 }
90
98 {
99 super(context);
100 }
101
114 // TODO: QTBUG-125620 -- Refresh/reset import paths when loading a new component
115 public <T extends QtQuickViewContent> void loadContent(T qmlContent, String[] qmlImportPaths)
116 throws InvalidParameterException
117 {
118 String libName = qmlContent.getLibraryName();
119 String qmlUri = qmlContent.getFilePath();
120
121 if (libName == null || libName.isEmpty()) {
122 throw new InvalidParameterException(
123 "QtQuickViewContent: return value of getLibraryName() may not be empty or null");
124 }
125
126 if (qmlUri == null || qmlUri.isEmpty()) {
127 throw new InvalidParameterException(
128 "QtQuickViewContent: return value of getFilePath() may not be empty or null");
129 }
130
131 m_qmlUri = qmlUri;
132 m_qmlImportPaths = qmlImportPaths;
133
134 if (m_loadedComponent != null)
135 m_loadedComponent.clear();
136
137 m_loadedComponent = new WeakReference<>(qmlContent);
138 qmlContent.detachView();
139 qmlContent.attachView(this);
140 // The first QQuickView creation happen after first libs loading
141 // and windowReference() returns a reference to native QQuickView
142 // instance, after that. We don't load library again if the view
143 // exists.
144 if (getWindowReference() == 0) {
145 loadQtLibraries(libName);
146 } else {
147 createQuickView(m_qmlUri, getWidth(), getHeight(), 0, getWindowReference(),
148 m_qmlImportPaths);
149 }
150 }
151
152 @Override
153 void setWindowReference(long windowReference) {
154 super.setWindowReference(windowReference);
155 m_signalQueue.connectQueuedSignalListeners(this);
156 }
157
158 private boolean hasUnderlyingView() {
159 return getWindowReference() != 0L;
160 }
161
170 public <T extends QtQuickViewContent> void loadContent(T qmlContent)
171 throws InvalidParameterException
172 {
173 loadContent(qmlContent, null);
174 }
175
176 @Override
177 protected void createWindow(long parentWindowReference) {
178 createQuickView(m_qmlUri, getWidth(), getHeight(), parentWindowReference, getWindowReference(),
179 m_qmlImportPaths);
180 }
181
197 public void setProperty(String propertyName, Object value)
198 {
199 setRootObjectProperty(getWindowReference(), propertyName, value);
200 }
201
217 // getRootObjectProperty always returns a primitive type or an Object
218 // so it is safe to suppress the unchecked warning
219 @SuppressWarnings("unchecked")
220 public <T> T getProperty(String propertyName)
221 {
222 return (T)getRootObjectProperty(getWindowReference(), propertyName);
223 }
224
238 public <T> int connectSignalListener(String signalName, Class<T> argType,
239 QtSignalListener<T> listener)
240 {
241 return connectSignalListener(signalName, new Class<?>[] { argType }, listener);
242 }
243
256 public int connectSignalListener(String signalName, Class<?>[] argTypes, Object listener)
257 {
258 final int id = QtQuickViewContent.generateSignalId();
259 connectSignalListener(signalName, argTypes, listener, id);
260 return id;
261 }
262
275 void connectSignalListener(String signalName, Class<?>[] argTypes, Object listener, int id)
276 {
277 if (hasUnderlyingView())
278 addRootObjectSignalListener(getWindowReference(), signalName, argTypes, listener, id);
279 else
280 m_signalQueue.add(signalName, argTypes, listener, id);
281 }
282
293 public boolean disconnectSignalListener(int signalListenerId)
294 {
295 if (hasUnderlyingView())
296 return removeRootObjectSignalListener(getWindowReference(), signalListenerId);
297 else
298 return m_signalQueue.remove(signalListenerId);
299 }
300
317 {
318 return m_lastStatus;
319 }
320
327 {
328 m_statusChangeListener = listener;
329
330 if (m_hasQueuedStatus) {
331 sendStatusChanged(m_lastStatus);
332 m_hasQueuedStatus = false;
333 }
334 }
335
355 {
356 invokeMethod(getWindowReference(), methodName, params);
357 }
358
367 {
368 invokeMethod(getWindowReference(), methodName, new Object[] {});
369 }
370
371 private void handleStatusChange(int status)
372 {
373 try {
374 m_lastStatus = QtQmlStatus.fromInt(status);
375 } catch (IllegalArgumentException e) {
376 m_lastStatus = QtQmlStatus.NULL;
377 e.printStackTrace();
378 }
379
380 if (m_statusChangeListener == null)
381 m_hasQueuedStatus = true;
382 else
383 sendStatusChanged(m_lastStatus);
384 }
385
386 private void sendStatusChanged(QtQmlStatus status)
387 {
388 if (m_statusChangeListener != null) {
389 QtQuickViewContent content = m_loadedComponent != null ? m_loadedComponent.get() : null;
390 if (content == null)
391 m_statusChangeListener.onStatusChanged(status);
392 else
393 m_statusChangeListener.onStatusChanged(status, content);
394 }
395 }
396}
void setProperty(String propertyName, Object value)
void invokeMethod(String methodName)
QtQuickView(Context context, String qmlUri, String appName)
QtQuickView(Context context, String qmlUri, String appName, String[] qmlImportPaths)
void invokeMethod(String methodName, Object[] params)
int connectSignalListener(String signalName, Class<?>[] argTypes, Object listener)
void setStatusChangeListener(QtQmlStatusChangeListener listener)
void createWindow(long parentWindowReference)
boolean disconnectSignalListener(int signalListenerId)
QPainter Context
static QtQmlStatus fromInt(int value)
static const QString context()
Definition java.cpp:396
[vector_of_multirole_objects_0]
Definition main.cpp:188
static QString methodName(const QDBusIntrospection::Method &method)
EGLOutputLayerEXT EGLint EGLAttrib value
[3]
GLenum GLuint id
GLint GLsizei width
void ** params
static const char * signalName(int signum) noexcept