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
31public class QtQuickView extends QtView {
32 private final static String TAG = "QtQuickView";
33
34 private String m_qmlUri;
35 private String[] m_qmlImportPaths = null;
36 private QtQmlStatusChangeListener m_statusChangeListener = null;
37 private QtQmlStatus m_lastStatus = QtQmlStatus.NULL;
38 private boolean m_hasQueuedStatus = false;
39 private WeakReference<QtQuickViewContent> m_loadedComponent;
40 private QtSignalQueue m_signalQueue = new QtSignalQueue();
41
42 native void createQuickView(String qmlUri, int width, int height, long parentWindowReference,
43 long viewReference, String[] qmlImportPaths);
44 native void setRootObjectProperty(long windowReference, String propertyName, Object value);
45 native Object getRootObjectProperty(long windowReference, String propertyName);
46 native boolean addRootObjectSignalListener(long windowReference, String signalName,
47 Class<?>[] argTypes, Object listener, int id);
48 native boolean removeRootObjectSignalListener(long windowReference, int signalListenerId);
49 native void invokeMethod(long windowReference, String methodName, Object[] params);
50
63 public QtQuickView(Context context, String qmlUri, String appName)
64 throws InvalidParameterException {
65 this(context, qmlUri, appName, null);
66 }
67
83 public QtQuickView(Context context, String qmlUri, String appName, String[] qmlImportPaths)
84 throws InvalidParameterException
85 {
86 super(context, appName);
87 if (qmlUri == null || qmlUri.isEmpty()) {
88 throw new InvalidParameterException(
89 "QtQuickView: argument 'qmlUri' may not be empty or null");
90 }
91 m_qmlUri = qmlUri;
92 m_qmlImportPaths = qmlImportPaths;
93 }
94
102 {
103 super(context);
104 }
105
118 // TODO: QTBUG-125620 -- Refresh/reset import paths when loading a new component
119 public <T extends QtQuickViewContent> void loadContent(T qmlContent, String[] qmlImportPaths)
120 throws InvalidParameterException
121 {
122 String libName = qmlContent.getLibraryName();
123 String qmlUri = qmlContent.getFilePath();
124
125 if (libName == null || libName.isEmpty()) {
126 throw new InvalidParameterException(
127 "QtQuickViewContent: return value of getLibraryName() may not be empty or null");
128 }
129
130 if (qmlUri == null || qmlUri.isEmpty()) {
131 throw new InvalidParameterException(
132 "QtQuickViewContent: return value of getFilePath() may not be empty or null");
133 }
134
135 m_qmlUri = qmlUri;
136 m_qmlImportPaths = qmlImportPaths;
137
138 if (m_loadedComponent != null)
139 m_loadedComponent.clear();
140
141 m_loadedComponent = new WeakReference<>(qmlContent);
142 qmlContent.detachView();
143 qmlContent.attachView(this);
144 // The first QQuickView creation happen after first libs loading
145 // and windowReference() returns a reference to native QQuickView
146 // instance, after that. We don't load library again if the view
147 // exists.
148 if (windowReference() == 0) {
149 loadQtLibraries(libName);
150 } else {
151 createQuickView(m_qmlUri, getWidth(), getHeight(), 0, windowReference(),
152 m_qmlImportPaths);
153 }
154 }
155
156 @Override
157 void setWindowReference(long windowReference) {
158 super.setWindowReference(windowReference);
159 m_signalQueue.connectQueuedSignalListeners(this);
160 }
161
162 private boolean hasUnderlyingView() {
163 return m_windowReference != 0L;
164 }
165
174 public <T extends QtQuickViewContent> void loadContent(T qmlContent)
175 throws InvalidParameterException
176 {
177 loadContent(qmlContent, null);
178 }
179
180 @Override
181 protected void createWindow(long parentWindowReference) {
182 createQuickView(m_qmlUri, getWidth(), getHeight(), parentWindowReference, windowReference(),
183 m_qmlImportPaths);
184 }
185
201 public void setProperty(String propertyName, Object value)
202 {
203 setRootObjectProperty(windowReference(), propertyName, value);
204 }
205
221 // getRootObjectProperty always returns a primitive type or an Object
222 // so it is safe to suppress the unchecked warning
223 @SuppressWarnings("unchecked")
224 public <T> T getProperty(String propertyName)
225 {
226 return (T)getRootObjectProperty(windowReference(), propertyName);
227 }
228
242 public <T> int connectSignalListener(String signalName, Class<T> argType,
243 QtSignalListener<T> listener)
244 {
245 return connectSignalListener(signalName, new Class<?>[] { argType }, listener);
246 }
247
260 public int connectSignalListener(String signalName, Class<?>[] argTypes, Object listener)
261 {
262 final int id = QtQuickViewContent.generateSignalId();
263 connectSignalListener(signalName, argTypes, listener, id);
264 return id;
265 }
266
279 void connectSignalListener(String signalName, Class<?>[] argTypes, Object listener, int id)
280 {
281 if (hasUnderlyingView())
282 addRootObjectSignalListener(windowReference(), signalName, argTypes, listener, id);
283 else
284 m_signalQueue.add(signalName, argTypes, listener, id);
285 }
286
297 public boolean disconnectSignalListener(int signalListenerId)
298 {
299 if (hasUnderlyingView())
300 return removeRootObjectSignalListener(windowReference(), signalListenerId);
301 else
302 return m_signalQueue.remove(signalListenerId);
303 }
304
321 {
322 return m_lastStatus;
323 }
324
331 {
332 m_statusChangeListener = listener;
333
334 if (m_hasQueuedStatus) {
335 sendStatusChanged(m_lastStatus);
336 m_hasQueuedStatus = false;
337 }
338 }
339
359 {
360 invokeMethod(windowReference(), methodName, params);
361 }
362
371 {
372 invokeMethod(windowReference(), methodName, new Object[] {});
373 }
374
375 private void handleStatusChange(int status)
376 {
377 try {
378 m_lastStatus = QtQmlStatus.fromInt(status);
379 } catch (IllegalArgumentException e) {
380 m_lastStatus = QtQmlStatus.NULL;
381 e.printStackTrace();
382 }
383
384 if (m_statusChangeListener == null)
385 m_hasQueuedStatus = true;
386 else
387 sendStatusChanged(m_lastStatus);
388 }
389
390 private void sendStatusChanged(QtQmlStatus status)
391 {
392 if (m_statusChangeListener != null) {
393 QtQuickViewContent content = m_loadedComponent != null ? m_loadedComponent.get() : null;
394 if (content == null)
395 m_statusChangeListener.onStatusChanged(status);
396 else
397 m_statusChangeListener.onStatusChanged(status, content);
398 }
399 }
400}
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:398
[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