summaryrefslogtreecommitdiffstats
path: root/examples/webengine/customdialogs/doc/src/customdialogs.qdoc
blob: 2162fe4cad3a75eebfcce1ed20047514918c961e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example webengine/customdialogs
    \title WebEngine Qt Quick Custom Dialogs Example
    \ingroup webengine-examples
    \brief Customizes UI elements of Qt WebEngine's dialogs

    \image customdialogs.png

    A web page might request dialogs for various purposes, such as
    authentication, picking colors, choosing files, and responding to JavaScript
    alerts, confirmation requests, and prompts.

    \e {Custom Dialogs} demonstrates how to use WebEngine dialog request objects
    to implement custom dialogs for use instead of the default dialogs.

    \include examples-run.qdocinc

    \section1 UI Elements of WebEngineView

    In this example, we create a simple \c index.html page that contains buttons
    and text fields for triggering a context menu and the following dialogs:

    \list
        \li HTTP Authentication Dialog
        \li Proxy Authentication Dialog
        \li JavaScript Alert, Confirm, and Prompt Dialogs
        \li Color Picker Dialog
        \li File Picker Dialog
        \li Form Validation Message
    \endlist

    \section1 Triggering Dialogs

    As mentioned, the \l {webengine/customdialogs/index.html}{index.html} file
    is responsible for triggering the dialogs from the side of HTML and
    JavaScript. Additionally, the example program starts a localhost TCP server
    returning the mocked HTTP responses needed to trigger proxy and HTTP
    authentication dialogs.

    \section1 Custom Dialogs

    The custom dialogs are just \e {Qt Quick Designer UI Forms} without any
    business logic. The point here is to present the glue code that is required
    to display the custom dialog for a particular web engine dialog or a menu
    request.

    \section1 Creating the Main Window

    In \c main.cpp, we initialize the WebEngine the same way as in the
    \l {WebEngine Qt Quick Minimal Example}:

    \quotefromfile webengine/customdialogs/main.cpp
    \skipto main
    \printuntil }

    In addition, we set up a proxy and a TCP server to be able to simulate proxy
    and HTTP authetication requests.

    In \c main.qml, we create a top level window, which contains a StackView
    with a SwitchButton and a WebView:

    \quotefromfile webengine/customdialogs/main.qml
    \skipto Window
    \printuntil Component
    \printuntil }
    \printline }

    \section1 Handling Web Engine Requests

    In this example, we implement the handling of the following web engine
    requests:

    \list
        \li ContextMenuRequest
        \li AuthenticationDialogRequest
        \li JavaScriptDialogRequest
        \li ColorDialogRequest
        \li FileDialogRequest
        \li FormValidationMessageRequest
    \endlist

    \section2 Context Menu Requests

    \l [QML]{ContextMenuRequest} is a request object that is passed as a
    parameter of the WebEngineView::contextMenuRequested signal. We use the
    \c onContextMenuRequested signal handler to handle requests for
    \c #openMenu URL links:

    \quotefromfile webengine/customdialogs/WebView.qml
    \skipto WebEngineView
    \printuntil {
    \dots 4
    \skipto onContextMenuRequested
    \printuntil }
    \printuntil }
    \printuntil }
    \dots 4
    \skipuntil onFormValidationMessageRequested
    \skipuntil }
    \skipuntil }
    \skipuntil }
    \skipto }
    \printline }

    The first text field from the top on our page triggers the request. Next,
    we check whether we should use the default menu. If not, we accept the
    request and switch the view to show the \c MenuForm:

     \image customdialogs-menu.png

    \quotefromfile webengine/customdialogs/forms/Menu.qml
    \skipto MenuForm
    \printuntil }
    \printuntil }

    To keep things simple, we do not provide any logic on component completion,
    and we simply close the form on any action.

    \section2  Authentication Dialog Requests

    \image customdialogs-auth1.png

    \l [QML]{AuthenticationDialogRequest} is a request object that is passed
    as a parameter of the WebEngineView::authenticationDialogRequested signal:

    \quotefromfile webengine/customdialogs/WebView.qml
    \skipto WebEngineView
    \printuntil {
    \dots 4
    \skipto onAuthenticationDialogRequested
    \printuntil }
    \printuntil }
    \dots 4
    \skipuntil onFormValidationMessageRequested
    \skipuntil }
    \skipuntil }
    \skipuntil }
    \skipto }
    \printline }

    We use the \c onAuthenticationDialogRequested signal handler to check
    whether we should use the default authentication dialog. If not, we accept
    the request and switch the view to show the \c AuthenticationForm:

    \image customdialogs-auth2.png

    \quotefromfile webengine/customdialogs/forms/Authentication.qml
    \skipto AuthenticationForm
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }

    On component completion, we log the request type. The user can fill in the
    credentials and click \uicontrol Login. We provide \c onClicked handlers to
    accept or reject the authentication dialog. The TCP server on localhost does
    not handle real authentication, and therefore we call \c rejectDialog()
    instead of \c acceptDialog() also for the login button \c clicked signal.

    \section2  JavaScript Dialog Requests

    \image customdialogs-prompt1.png

    \l [QML]{JavaScriptDialogRequest} is a request object that is passed as a
    parameter of the WebEngineView::javaScriptDialogRequested signal:

    \quotefromfile webengine/customdialogs/WebView.qml
    \skipto WebEngineView
    \printuntil {
    \dots 4
    \skipto onJavaScriptDialogRequested
    \printuntil }
    \printuntil }
    \dots 4
    \skipuntil onFormValidationMessageRequested
    \skipuntil }
    \skipuntil }
    \skipuntil }
    \skipto }
    \printline }

    We use the \c onJavaScriptDialogRequested signal handler to check
    whether we should use the default JavaScript dialog. If not, we accept the
    request and switch the view to show the \c JavaScriptForm:

    \image customdialogs-prompt2.png

    \quotefromfile webengine/customdialogs/forms/JavaScript.qml
    \skipto JavaScriptForm
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }

    On component completion, we customize the form based on the request type.
    For a JavaScript prompt dialog we use \c dialogAccept() with the
    \c prompt.text argument.

    \section2  Color Dialog Requests

    \image customdialogs-color1.png

    \l [QML]{ColorDialogRequest} is a request object that is passed as a
    parameter of the WebEngineView::colorDialogRequested signal:

    \quotefromfile webengine/customdialogs/WebView.qml
    \skipto WebEngineView
    \printuntil {
    \dots 4
    \skipto onColorDialogRequested
    \printuntil }
    \printuntil }
    \dots 4
    \skipuntil onFormValidationMessageRequested
    \skipuntil }
    \skipuntil }
    \skipuntil }
    \skipto }
    \printline }

    We use the \c onColorDialogRequested signal handler to check whether
    we should use the default color picker dialog. If not, we accept the request
    and switch the view to show the \c ColorPickerForm:

    \image customdialogs-color2.png

    \quotefromfile webengine/customdialogs/forms/ColorPicker.qml
    \skipto ColorPickerForm
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }

    On component completion, we create callbacks for all the color cells. When
    the user selects the color and clicks \c OK, we pass the selected color to
    the \c dialogAccept() method.

    \section2  File Dialog Requests

    \image customdialogs-file1.png

    \l [QML]{FileDialogRequest} is a request object that is passed as a
    parameter of the WebEngineView::fileDialogRequested signal:

    \quotefromfile webengine/customdialogs/WebView.qml
    \skipto WebEngineView
    \printuntil {
    \dots 4
    \skipto onFileDialogRequested
    \printuntil }
    \printuntil }
    \dots 4
    \skipuntil onFormValidationMessageRequested
    \skipuntil }
    \skipuntil }
    \skipuntil }
    \skipto }
    \printline }

    We use the \c onFileDialogRequested signal handler to check whether
    we should use the default color picker dialog. If not, we accept the request
    and switch the view to show the \c FilePickerForm:

    \image customdialogs-file2.png

    \quotefromfile webengine/customdialogs/forms/FilePicker.qml
    \skipto FilePickerForm
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }
    \printuntil }

    On component completion, we create callbacks for selecting files. When the user selects a
    file and clicks \c OK, we pass the selected file to the \c dialogAccept
    method.

    \section2  Form Validation Message Requests

    \image customdialogs-validation1.png

    \l [QML]{FormValidationMessageRequest} is a request object that is passed as a
    parameter of the WebEngineView::formValidationMessageRequested signal:

    \quotefromfile webengine/customdialogs/WebView.qml
    \skipto WebEngineView
    \printuntil url
    \dots 4
    \skipto onFormValidationMessageRequested
    \printuntil }
    \printuntil }
    \printuntil }
    \skipto }
    \printline }

    We use the \c onFormValidationMessageRequested signal handler to check
    whether we should use the default message bubble. If not, we accept the
    request and customize \c validationMessage. Depending on the type of the
    request, we show, move, or hide the message.

    \image customdialogs-validation2.png
*/