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
qandroidplatformdialoghelpers.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2013 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
6#include "androidjnimain.h"
7
8#include <QTextDocument>
9
10#include <private/qguiapplication_p.h>
11#include <qpa/qplatformtheme.h>
12
14
15using namespace Qt::StringLiterals;
16
18static jclass g_messageDialogHelperClass = nullptr;
19
21 : m_javaMessageDialog(g_messageDialogHelperClass, "(Landroid/app/Activity;)V",
22 QtAndroidPrivate::activity().object())
23{
24}
25
30
32{
33 if (!m_shown)
35 m_loop.exec();
36}
37
39{
41 return text;
42 text.remove(u'\r');
43 return text.toHtmlEscaped().replace(u'\n', "<br />"_L1);
44}
45
46bool QAndroidPlatformMessageDialogHelper::show(Qt::WindowFlags windowFlags,
47 Qt::WindowModality windowModality,
48 QWindow *parent)
49{
50 Q_UNUSED(windowFlags);
51 Q_UNUSED(windowModality);
53 QSharedPointer<QMessageDialogOptions> opt = options();
54 if (!opt.data())
55 return false;
56
57 if (!opt->checkBoxLabel().isNull())
58 return false; // Can't support
59
60 m_javaMessageDialog.callMethod<void>("setStandardIcon", "(I)V", opt->standardIcon());
61
62 QString str = htmlText(opt->windowTitle());
63 if (!str.isEmpty()) {
64 m_javaMessageDialog.callMethod<void>("setTile", "(Ljava/lang/String;)V",
65 QJniObject::fromString(str).object());
66 }
67
68 str = htmlText(opt->text());
69 if (!str.isEmpty()) {
70 m_javaMessageDialog.callMethod<void>("setText", "(Ljava/lang/String;)V",
71 QJniObject::fromString(str).object());
72 }
73
74 str = htmlText(opt->informativeText());
75 if (!str.isEmpty()) {
76 m_javaMessageDialog.callMethod<void>("setInformativeText", "(Ljava/lang/String;)V",
77 QJniObject::fromString(str).object());
78 }
79
80 str = htmlText(opt->detailedText());
81 if (!str.isEmpty()) {
82 m_javaMessageDialog.callMethod<void>("setDetailedText", "(Ljava/lang/String;)V",
83 QJniObject::fromString(str).object());
84 }
85
86 const int *currentLayout = buttonLayout(Qt::Horizontal, AndroidLayout);
87 while (*currentLayout != QPlatformDialogHelper::EOL) {
88 const int role = (*currentLayout & ~QPlatformDialogHelper::Reverse);
89 addButtons(opt, static_cast<ButtonRole>(role));
90 ++currentLayout;
91 }
92
93 m_javaMessageDialog.callMethod<void>("show", "(J)V", jlong(static_cast<QObject*>(this)));
94 m_shown = true;
95 return true;
96}
97
98void QAndroidPlatformMessageDialogHelper::addButtons(QSharedPointer<QMessageDialogOptions> opt,
99 ButtonRole role)
100{
101 for (const QMessageDialogOptions::CustomButton &b : opt->customButtons()) {
102 if (b.role == role) {
103 QString label = b.label;
104 label.remove(QChar('&'));
105 m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", b.id,
106 QJniObject::fromString(label).object());
107 }
108 }
109
111 StandardButton b = static_cast<StandardButton>(i);
112 if (buttonRole(b) == role && (opt->standardButtons() & i)) {
113 const QString text = QGuiApplicationPrivate::platformTheme()->standardButtonText(b);
114 m_javaMessageDialog.callMethod<void>("addButton", "(ILjava/lang/String;)V", i,
115 QJniObject::fromString(text).object());
116 }
117 }
118}
119
121{
122 m_javaMessageDialog.callMethod<void>("hide", "()V");
123 m_shown = false;
124}
125
127{
128 if (m_loop.isRunning())
129 m_loop.exit();
130 if (buttonID < 0) {
131 emit reject();
132 return;
133 }
134
135 const StandardButton standardButton = static_cast<StandardButton>(buttonID);
136 ButtonRole role = QPlatformDialogHelper::buttonRole(standardButton);
137 if (buttonID > QPlatformDialogHelper::LastButton) {
138 // In case of a custom button
139 const QMessageDialogOptions::CustomButton *custom = options()->customButton(buttonID);
140 Q_ASSERT(custom);
141 role = custom->role;
142 }
143
144 emit clicked(standardButton, role);
145}
146
147static void dialogResult(JNIEnv * /*env*/, jobject /*thiz*/, jlong handler, int buttonID)
148{
149 QObject *object = reinterpret_cast<QObject *>(handler);
150 QMetaObject::invokeMethod(object, "dialogResult", Qt::QueuedConnection, Q_ARG(int, buttonID));
151}
152
153static const JNINativeMethod methods[] = {
154 {"dialogResult", "(JI)V", (void *)dialogResult}
155};
156
157
158#define FIND_AND_CHECK_CLASS(CLASS_NAME) \
159 clazz = env->FindClass(CLASS_NAME); \
160 if (!clazz) { \
161 __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt(), CLASS_NAME); \
162 return false; \
163 }
164
166{
167 const char QtMessageHandlerHelperClassName[] = "org/qtproject/qt/android/QtMessageDialogHelper";
168 jclass clazz = env.findClass(QtMessageHandlerHelperClassName);
169 if (!clazz) {
170 __android_log_print(ANDROID_LOG_FATAL, QtAndroid::qtTagText(), QtAndroid::classErrorMsgFmt()
171 , QtMessageHandlerHelperClassName);
172 return false;
173 }
174 g_messageDialogHelperClass = static_cast<jclass>(env->NewGlobalRef(clazz));
175
176 if (!env.registerNativeMethods("org/qtproject/qt/android/QtNativeDialogHelper",
177 methods, sizeof(methods) / sizeof(methods[0]))) {
178 __android_log_print(ANDROID_LOG_FATAL, "Qt", "RegisterNatives failed");
179 return false;
180 }
181
182 return true;
183}
184}
185
\inmodule QtCore
int exec(ProcessEventsFlags flags=AllEvents)
Enters the main event loop and waits until exit() is called.
void exit(int returnCode=0)
Tells the event loop to exit with a return code.
bool isRunning() const
Returns true if the event loop is running; otherwise returns false.
static QPlatformTheme * platformTheme()
\inmodule QtCore
const CustomButton * customButton(int id)
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static const int * buttonLayout(Qt::Orientation orientation=Qt::Horizontal, ButtonLayout policy=UnknownLayout)
static ButtonRole buttonRole(StandardButton button)
const QSharedPointer< QMessageDialogOptions > & options() const
void clicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString & replace(qsizetype i, qsizetype len, QChar after)
Definition qstring.cpp:3824
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QString & remove(qsizetype i, qsizetype len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition qstring.cpp:3466
QString toHtmlEscaped() const
\inmodule QtGui
Definition qwindow.h:63
bool show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) override
QString str
[2]
QString text
QStyleOptionButton opt
static bool registerNatives()
Combined button and popup list for selecting options.
static const JNINativeMethod methods[]
static QString htmlText(QString text)
static void dialogResult(JNIEnv *, jobject, jlong handler, int buttonID)
\preliminary \inmodule QtCorePrivate
const char * classErrorMsgFmt()
const char * qtTagText()
WindowModality
@ ApplicationModal
@ Horizontal
Definition qnamespace.h:99
Q_GUI_EXPORT bool mightBeRichText(QAnyStringView)
Returns true if the string text is likely to be rich text; otherwise returns false.
@ QueuedConnection
@ Dialog
Definition qnamespace.h:208
#define Q_ARG(Type, data)
Definition qobjectdefs.h:63
GLboolean GLboolean GLboolean b
GLuint object
[3]
GLuint GLsizei const GLchar * label
[43]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define emit
#define Q_UNUSED(x)
view show()
[18] //! [19]
QPlatformDialogHelper::ButtonRole role
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(nullptr), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
\threadsafe This is an overloaded member function, provided for convenience. It differs from the abov...