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
QtView.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// Qt-Security score:critical reason:execute-external-code
4
5package org.qtproject.qt.android;
6
7import android.content.Context;
8import android.os.Handler;
9import android.os.Looper;
10import android.util.Log;
11import android.view.View;
12import android.view.ViewGroup;
13
14import java.security.InvalidParameterException;
15import java.util.Objects;
16
17// Base class for embedding QWindow into native Android view hierarchy. Extend to implement
18// the creation of appropriate window to embed.
19abstract class QtView extends ViewGroup implements QtNative.AppStateDetailsListener {
20 private final static String TAG = "QtView";
21
22 interface QtWindowListener {
23 // Called when the QWindow has been created and it's Java counterpart embedded into
24 // QtView
26 }
27
28 private QtWindow m_window;
29 private long m_windowReference;
30 private long m_parentWindowReference;
31 private QtWindowListener m_windowListener;
32 private final QtEmbeddedViewInterface m_viewInterface;
33 // Implement in subclass to handle the creation of the QWindow and its parent container.
34 // TODO could we take care of the parent window creation and parenting outside of the
35 // window creation method to simplify things if user would extend this? Preferably without
36 // too much JNI back and forth. Related to parent window creation, so handle with QTBUG-121511.
37 abstract protected void createWindow(long parentWindowRef);
38
39 static native void createRootWindow(View rootView, int x, int y, int width, int height);
40 static native void deleteWindow(long windowReference);
41 private static native void setWindowVisible(long windowReference, boolean visible);
42 private static native void resizeWindow(long windowReference,
43 int x, int y, int width, int height);
44
50 QtView(Context context) {
51 super(context);
52 QtNative.registerAppStateListener(this);
53 m_viewInterface = QtEmbeddedViewInterfaceFactory.create(context);
54 addOnLayoutChangeListener(
55 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
56 if (m_windowReference != 0L) {
57 final int oldWidth = oldRight - oldLeft;
58 final int oldHeight = oldBottom - oldTop;
59 final int newWidth = right - left;
60 final int newHeight = bottom - top;
61 if (oldWidth != newWidth || oldHeight != newHeight || left != oldLeft ||
62 top != oldTop) {
63 resizeWindow(m_windowReference, left, top, newWidth, newHeight);
64 }
65 }
66 });
67 if (getId() == -1)
68 setId(View.generateViewId());
69 }
77 QtView(Context context, String appLibName) throws InvalidParameterException {
78 this(context);
79 if (appLibName == null || appLibName.isEmpty()) {
80 throw new InvalidParameterException("QtView: argument 'appLibName' may not be empty "+
81 "or null");
82 }
83 loadQtLibraries(appLibName);
84 }
85
86 @Override
87 protected void onAttachedToWindow() {
88 super.onAttachedToWindow();
89 m_viewInterface.addView(this);
90 }
91
92 @Override
93 protected void onDetachedFromWindow() {
94 super.onDetachedFromWindow();
95 destroyWindow();
96 m_viewInterface.removeView(this);
97 }
98
99 @Override
100 public void onLayout(boolean changed, int l, int t, int r, int b) {
101 if (m_window != null)
102 m_window.layout(0 /* left */, 0 /* top */, r - l /* right */, b - t /* bottom */);
103 }
104
105 @Override
106 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
107 {
108 measureChildren(widthMeasureSpec, heightMeasureSpec);
109
110 final int count = getChildCount();
111
112 int maxHeight = 0;
113 int maxWidth = 0;
114
115 // Find out how big everyone wants to be
116 measureChildren(widthMeasureSpec, heightMeasureSpec);
117
118 // Find rightmost and bottom-most child
119 for (int i = 0; i < count; i++) {
120 View child = getChildAt(i);
121 if (child.getVisibility() != GONE) {
122 maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
123 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
124 }
125 }
126
127 // Check against minimum height and width
128 maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
129 maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
130
131 setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
132 resolveSize(maxHeight, heightMeasureSpec));
133 }
134
135
136 void setQtWindowListener(QtWindowListener listener) {
137 m_windowListener = listener;
138 }
139
140 void loadQtLibraries(String appLibName) {
141 QtEmbeddedLoader loader;
142 try {
143 loader = QtEmbeddedLoader.getEmbeddedLoader(getContext());
144 } catch (IllegalArgumentException e) {
145 Log.e(TAG, Objects.requireNonNull(e.getMessage()));
146 QtEmbeddedViewInterfaceFactory.remove(getContext());
147 return;
148 }
149
150 loader.setMainLibraryName(appLibName);
151 QtLoader.LoadingResult result = loader.loadQtLibraries();
152 if (result == QtLoader.LoadingResult.Failed) {
153 // If we weren't able to load the libraries, remove the delegate from the factory
154 // as it's holding a reference to the Context, and we don't want it leaked
155 QtEmbeddedViewInterfaceFactory.remove(getContext());
156 } else {
157 // Start Native Qt application
158 m_viewInterface.startQtApplication(loader.getApplicationParameters(),
159 loader.getMainLibraryPath());
160 }
161 }
162
163 void setWindowReference(long windowReference) {
164 m_windowReference = windowReference;
165 }
166
167 long getWindowReference() {
168 return m_windowReference;
169 }
170
171 void setQtWindow(QtWindow qtWindow) {
172 m_window = qtWindow;
173 }
174
175 QtWindow getQtWindow() {
176 return m_window;
177 }
178
179 long getParentWindowReference()
180 {
181 return m_parentWindowReference;
182 }
183
184 void setParentWindowReference(long reference)
185 {
186 m_parentWindowReference = reference;
187 }
188
189 QtWindowListener getWindowListener()
190 {
191 return m_windowListener;
192 }
193
194 void setWindowListener(QtWindowListener listner)
195 {
196 m_windowListener = listner;
197 }
198
199 QtEmbeddedViewInterface getViewInterface()
200 {
201 return m_viewInterface;
202 }
203
204 // Set the visibility of the underlying QWindow. If visible is true, showNormal() is called.
205 // If false, the window is hidden.
206 void setWindowVisible(boolean visible) {
207 if (m_windowReference != 0L)
208 setWindowVisible(m_windowReference, true);
209 }
210
211 // Called from Qt when the QWindow has been created.
212 // window - the Java QtWindow of the created QAndroidPlatformWindow, to embed into the QtView
213 // viewReference - the reference to the created QQuickView
214 void addQtWindow(QtWindow window, long viewReference, long parentWindowRef) {
215 setWindowReference(viewReference);
216 m_parentWindowReference = parentWindowRef;
217 final Handler handler = new Handler(Looper.getMainLooper());
218 handler.post(() -> {
219 m_window = window;
220 m_window.setLayoutParams(new LayoutParams(
221 LayoutParams.MATCH_PARENT,
222 LayoutParams.MATCH_PARENT));
223 addView(m_window, 0);
224 // Call show window + parent
225 setWindowVisible(true);
226 if (m_windowListener != null)
227 m_windowListener.onQtWindowLoaded();
228 });
229 }
230
231 // Destroy the underlying QWindow
232 void destroyWindow() {
233 if (m_parentWindowReference != 0L)
234 deleteWindow(m_parentWindowReference);
235 m_parentWindowReference = 0L;
236 setWindowReference(0L);
237 }
238
239 @Override
240 public void onAppStateDetailsChanged(QtNative.ApplicationStateDetails details) {
241 if (!details.isStarted) {
242 ViewGroup parent = (ViewGroup)getParent();
243 if (parent != null)
244 parent.removeView(this);
245 }
246 }
247}
QPainter Context
static const QString context()
Definition java.cpp:398
Catch::Generators::GeneratorWrapper< T > handler(Catch::Generators::GeneratorWrapper< T > &&generator)
Returns a generator wrapping generator that ensures that changes its semantics so that the first call...
GLboolean GLboolean GLboolean b
GLdouble GLdouble right
GLint GLint GLint GLint GLint x
GLint GLint bottom
GLboolean r
GLenum GLenum GLsizei count
GLint reference
GLint GLsizei width
GLdouble GLdouble GLdouble GLdouble top
GLsizei const GLfloat * v
GLint y
GLint left
GLdouble GLdouble t
Definition qopenglext.h:243
GLuint64EXT * result
[6]
@ Handler
QLayoutItem * child
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]