Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QtLayout.java
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2012 BogDan Vatra <bogdan@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5package org.qtproject.qt.android;
6
7import android.app.Activity;
8import android.content.Context;
9import android.graphics.Rect;
10import android.os.Build;
11import android.util.Log;
12import android.util.AttributeSet;
13import android.util.DisplayMetrics;
14import android.view.Display;
15import android.view.View;
16import android.view.ViewGroup;
17import android.view.WindowInsets;
18import android.view.WindowManager;
19import android.graphics.Insets;
20import android.view.WindowMetrics;
21import android.content.res.Configuration;
22import android.content.res.Resources;
23
24public class QtLayout extends ViewGroup
25{
26 private Runnable m_startApplicationRunnable;
27
28 private int m_activityDisplayRotation = -1;
29 private int m_ownDisplayRotation = -1;
30 private int m_nativeOrientation = -1;
31
32 public void setActivityDisplayRotation(int rotation)
33 {
34 m_activityDisplayRotation = rotation;
35 }
36
37 public void setNativeOrientation(int orientation)
38 {
39 m_nativeOrientation = orientation;
40 }
41
42 public int displayRotation()
43 {
44 return m_ownDisplayRotation;
45 }
46
47 public QtLayout(Context context, Runnable startRunnable)
48 {
49 super(context);
50 m_startApplicationRunnable = startRunnable;
51 }
52
53 public QtLayout(Context context, AttributeSet attrs)
54 {
55 super(context, attrs);
56 }
57
58 public QtLayout(Context context, AttributeSet attrs, int defStyle)
59 {
60 super(context, attrs, defStyle);
61 }
62
63 @Override
64 protected void onSizeChanged (int w, int h, int oldw, int oldh)
65 {
66 Activity activity = (Activity)getContext();
67 if (activity == null)
68 return;
69
70 final WindowManager windowManager = activity.getWindowManager();
72
73 final WindowInsets rootInsets = getRootWindowInsets();
74
75 int insetLeft = 0;
76 int insetTop = 0;
77
78 int maxWidth = 0;
79 int maxHeight = 0;
80
81 if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
82 display = windowManager.getDefaultDisplay();
83
84 final DisplayMetrics maxMetrics = new DisplayMetrics();
85 display.getRealMetrics(maxMetrics);
86 maxWidth = maxMetrics.widthPixels;
87 maxHeight = maxMetrics.heightPixels;
88
89 insetLeft = rootInsets.getStableInsetLeft();
90 insetTop = rootInsets.getStableInsetTop();
91 } else {
92 display = activity.getDisplay();
93
94 final WindowMetrics maxMetrics = windowManager.getMaximumWindowMetrics();
95 maxWidth = maxMetrics.getBounds().width();
96 maxHeight = maxMetrics.getBounds().height();
97
98 insetLeft = rootInsets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()).left;
99 insetTop = rootInsets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()).top;
100 }
101
102 final DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
103 double xdpi = displayMetrics.xdpi;
104 double ydpi = displayMetrics.ydpi;
105 double density = displayMetrics.density;
106 double scaledDensity = displayMetrics.scaledDensity;
107 float refreshRate = display.getRefreshRate();
108
109 QtNative.setApplicationDisplayMetrics(maxWidth, maxHeight, insetLeft,
110 insetTop, w, h,
111 xdpi,ydpi,scaledDensity, density,
112 refreshRate);
113
114 int newRotation = display.getRotation();
115 if (m_ownDisplayRotation != m_activityDisplayRotation
116 && newRotation == m_activityDisplayRotation) {
117 // If the saved rotation value does not match the one from the
118 // activity, it means that we got orientation change before size
119 // change, and the value was cached. So we need to notify about
120 // orientation change now.
121 QtNative.handleOrientationChanged(newRotation, m_nativeOrientation);
122 }
123 m_ownDisplayRotation = newRotation;
124
125 if (m_startApplicationRunnable != null) {
126 m_startApplicationRunnable.run();
127 m_startApplicationRunnable = null;
128 }
129 }
130
131 @Override
132 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
133 {
134 int count = getChildCount();
135
136 int maxHeight = 0;
137 int maxWidth = 0;
138
139 // Find out how big everyone wants to be
140 measureChildren(widthMeasureSpec, heightMeasureSpec);
141
142 // Find rightmost and bottom-most child
143 for (int i = 0; i < count; i++) {
144 View child = getChildAt(i);
145 if (child.getVisibility() != GONE) {
146 int childRight;
147 int childBottom;
148
149 QtLayout.LayoutParams lp
150 = (QtLayout.LayoutParams) child.getLayoutParams();
151
152 childRight = lp.x + child.getMeasuredWidth();
153 childBottom = lp.y + child.getMeasuredHeight();
154
155 maxWidth = Math.max(maxWidth, childRight);
156 maxHeight = Math.max(maxHeight, childBottom);
157 }
158 }
159
160 // Check against minimum height and width
161 maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
162 maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
163
164 setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
165 resolveSize(maxHeight, heightMeasureSpec));
166 }
167
174 @Override
175 protected ViewGroup.LayoutParams generateDefaultLayoutParams()
176 {
177 return new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
178 android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
179 0,
180 0);
181 }
182
183 @Override
184 protected void onLayout(boolean changed, int l, int t, int r, int b)
185 {
186 int count = getChildCount();
187
188 for (int i = 0; i < count; i++) {
189 View child = getChildAt(i);
190 if (child.getVisibility() != GONE) {
191 QtLayout.LayoutParams lp =
192 (QtLayout.LayoutParams) child.getLayoutParams();
193
194 int childLeft = lp.x;
195 int childTop = lp.y;
196 child.layout(childLeft, childTop,
197 childLeft + child.getMeasuredWidth(),
198 childTop + child.getMeasuredHeight());
199
200 }
201 }
202 }
203
204 // Override to allow type-checking of LayoutParams.
205 @Override
206 protected boolean checkLayoutParams(ViewGroup.LayoutParams p)
207 {
208 return p instanceof QtLayout.LayoutParams;
209 }
210
211 @Override
212 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
213 {
214 return new LayoutParams(p);
215 }
216
223 public static class LayoutParams extends ViewGroup.LayoutParams
224 {
228 public int x;
232 public int y;
233
245 public LayoutParams(int width, int height, int x, int y)
246 {
247 super(width, height);
248 this.x = x;
249 this.y = y;
250 }
251
255 public LayoutParams(ViewGroup.LayoutParams source)
256 {
257 super(source);
258 }
259 }
260
261 public void moveChild(View view, int index)
262 {
263 if (view == null)
264 return;
265
266 if (indexOfChild(view) == -1)
267 return;
268
269 detachViewFromParent(view);
270 requestLayout();
271 invalidate();
272 attachViewToParent(view, index, view.getLayoutParams());
273 }
274
281 public void setLayoutParams(final View childView,
282 final ViewGroup.LayoutParams params,
283 final boolean forceRedraw)
284 {
285 // Invalid view
286 if (childView == null)
287 return;
288
289 // Invalid params
291 return;
292
293 // View is already in the layout and can therefore be updated
294 final boolean canUpdate = (this == childView.getParent());
295
296 if (canUpdate) {
297 childView.setLayoutParams(params);
298 if (forceRedraw)
299 invalidate();
300 } else {
301 addView(childView, params);
302 }
303 }
304}
virtual QLayout * layout()
If this item is a QLayout, it is returned as a QLayout; otherwise \nullptr is returned.
LayoutParams(int width, int height, int x, int y)
LayoutParams(ViewGroup.LayoutParams source)
void setActivityDisplayRotation(int rotation)
Definition QtLayout.java:32
QtLayout(Context context, AttributeSet attrs)
Definition QtLayout.java:53
ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
QtLayout(Context context, Runnable startRunnable)
Definition QtLayout.java:47
QtLayout(Context context, AttributeSet attrs, int defStyle)
Definition QtLayout.java:58
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void setLayoutParams(final View childView, final ViewGroup.LayoutParams params, final boolean forceRedraw)
void onLayout(boolean changed, int l, int t, int r, int b)
void moveChild(View view, int index)
boolean checkLayoutParams(ViewGroup.LayoutParams p)
void onSizeChanged(int w, int h, int oldw, int oldh)
Definition QtLayout.java:64
ViewGroup.LayoutParams generateDefaultLayoutParams()
void setNativeOrientation(int orientation)
Definition QtLayout.java:37
static native void handleOrientationChanged(int newRotation, int nativeOrientation)
static void setApplicationDisplayMetrics(int screenWidthPixels, int screenHeightPixels, int availableLeftPixels, int availableTopPixels, int availableWidthPixels, int availableHeightPixels, double XDpi, double YDpi, double scaledDensity, double density, float refreshRate)
struct wl_display * display
Definition linuxdmabuf.h:41
static void * context
static struct AttrInfo attrs[]
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLuint index
[2]
GLboolean r
[2]
GLenum GLenum GLsizei count
GLint GLsizei width
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLsizei GLsizei GLchar * source
void ** params
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat p
[1]
struct _XDisplay Display
QLayoutItem * child
[0]
QQuickView * view
[0]