summaryrefslogtreecommitdiff
path: root/examples/demos/photoviewer/doc/src/photoviewer.qdoc
blob: fb71f2590deea5e9aeeb277588265eaac3230eff (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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \title Qt Quick Demo - Photo Viewer
    \ingroup qtquickdemos
    \example demos/photoviewer
    \brief A QML photo viewer that uses \l XmlListModel and \l XmlListModelRole
    to download Flickr feeds, and \l Package to display the photos in different
    views.
    \meta {tag} {demo,quick,quickcontrols,touch}

    \image qtquick-demo-photoviewer-small.png

    \e{Photo Viewer} demonstrates the following \l{Qt Quick} features:

    \list
        \li Using custom QML types.
        \li Using Qt Quick Controls to create an application window.
        \li Using the \l Package type with a \l DelegateModel to provide
            delegates with a shared context to multiple views.
        \li Using XML list models to download Flickr feeds.
        \li Using the \l Flipable type to create labels with different text on
            the front and back.
        \li Using the \l PathView, \l Path, \l PathAttribute, and \l PathLine
            types to lay out photos on a path.
        \li Providing feedback to users while data is loading.
        \li Localizing applications.
    \endlist

    \include examples-run.qdocinc

    \section1 Using Custom Types

    In the Photo Viewer app, we use the following custom types that are each
    defined in a separate .qml file:

    \list
        \li \c AlbumDelegate.qml
        \li \c BusyIndicator.qml
        \li \c Button.qml
        \li \c EditableButton.qml
        \li \c PhotoDelegate.qml
        \li \c ProgressBar.qml
        \li \c RssModel.qml
        \li \c Tag.qml
    \endlist

    To use the custom types, we add an import statement to the main QML file,
    \c main.qml, that imports the folder called \c PhotoViewerCore where the
    types are located:

    \quotefromfile demos/photoviewer/main.qml
    \skipto PhotoViewerCore
    \printuntil "

    \section1 Creating the Main Window

    In \c main.qml, we use the \l ApplicationWindow Qt Quick Control to create the
    app main window:

    \quotefromfile demos/photoviewer/main.qml
    \skipto ApplicationWindow
    \printuntil visible

    We use a ListModel type with \l ListElement types to display photo albums:

    \skipto ListModel
    \printuntil Prague
    \printuntil }

    List elements are defined like other QML types except that they contain a
    collection of \e role definitions instead of properties. Roles both define
    how the data is accessed and include the data itself. For each list element,
    we use the \c tag role to specify the photos to download.

    A \l DelegateModel type is used together with the \l Package type to provide
    delegates to multiple views. The \c model property holds the model providing
    data for the delegate model and the \c delegate property specifies the
    template defining each item instantiated by a view:

    \printuntil DelegateModel

    We use a \l GridView type to lay out the albums as a grid:

    \printuntil }

    The \c model property references the package name \c album that we specify
    in \c AlbumDelegate.qml. We use the \l Package type to allow the photos
    to move between different views. The \l Package contains the named items
    \c browser, \c fullscreen, and \c album:

    \quotefromfile demos/photoviewer/PhotoViewerCore/AlbumDelegate.qml
    \skipto Package
    \printuntil albumWrapper

    The named items are used as the delegates by the views that reference the
    special \l {DelegateModel::parts} property to select the model that provides
    the chosen delegate.

    We use a \l ListView type to lay out albums in other views:

    \quotefromfile demos/photoviewer/main.qml
    \skipto ListView
    \printuntil }
    \skipto ListView
    \printuntil }

    \section1 Displaying Photos

    We use the \c PhotoDelegate custom type that is specified in
    \c PhotoDelegate.qml to display photos. We use a \l Package type to lay
    out the photos either in a stack, list, or a grid:

    \quotefromfile demos/photoviewer/PhotoViewerCore/PhotoDelegate.qml
    \skipto Package
    \printuntil gridItem

    The photos are rotated at random angles by using the \c Math.random()
    JavaScript method:

    \printuntil stackItem

    We use a \l BorderImage type to create borders for the images:

    \printuntil border.left
    \printuntil }

    \section1 Downloading Flickr Feeds

    In \c AlbumDelegate.qml, we use the \l DelegateModel to provide the
    \c PhotoDelegate delegate to the \c RssModel model:

    \quotefromfile demos/photoviewer/PhotoViewerCore/AlbumDelegate.qml
    \skipto DelegateModel
    \printuntil RssModel
    \printuntil }

    In \c RssModel.qml, we use the \l XmlListModel type as a data source for
    \l Package objects to download photos from the selected feeds:

    \quotefromfile demos/photoviewer/PhotoViewerCore/RssModel.qml
    \skipto XmlListModel
    \printuntil encodeTags

    We use the \c tags custom property to specify which photos to download. The
    \c encodeTags custom function uses the \c encodeURIComponent JavaScript
    method to ensure that the requests to the server are correctly formatted.

    We use the \c source property to fetch photos that have the specified tags
    attached from public Flickr feeds:

    \printuntil query

    The \c query property specifies that the \l XmlListModel generates a model
    item for each feed entry.

    We use the \l XmlListModelRole type to specify the model item attributes.
    Each model item has the \c title and \c link attributes that match the
    values of the corresponding feed entry:

    \printuntil link

    \section1 Creating Flipable Labels

    When users select the \b Edit button, the album labels are flipped from
    their front side to their back side and the text on them changes from album
    name to \b Remove.

    In \c AlbumDelegate.qml, we use the \c Tag custom type to specify the
    text to display on the front and back sides of album labels:

    \quotefromfile demos/photoviewer/PhotoViewerCore/AlbumDelegate.qml
    \skipto Tag
    \printuntil onBackClicked
    \printuntil }

    The \c onTagChanged signal handler is used to change the tag based on
    which the model is populated. The \c onBackClicked signal handler is used to
    remove the album.

    In \c Tag.qml, we use a \l Flipable type with custom properties and
    signals to create the labels:

    \quotefromfile demos/photoviewer/PhotoViewerCore/Tag.qml
    \skipto Flipable
    \printuntil tagChanged

    The \c front property holds the \c EditableButton custom type that enables
    users to edit the label text:

    \printuntil onLabelChanged
    \printuntil }

    The \c back property holds the \c Button custom type that is used to remove
    the album:

    \printuntil onClicked
    \printuntil }

    \section1 Laying Out Photos on a Path

    In \c AlbumDelegate.qml, we use a \l PathView type to lay out the photos
    provided by the \c visualModel.parts.stack model on a path that has the
    form of a stack:

    \quotefromfile demos/photoviewer/PhotoViewerCore/AlbumDelegate.qml
    \skipto PathView
    \printuntil 0.0
    \printuntil }
    \printuntil }

    The \c path property holds the \l Path type that defines the path used by
    the \l PathView. The \l PathAttribute types are used to set a range of
    \c 0 to \c 9999 for the \c z attribute. This way, the path creates a stack
    of album photos. Because each \c PhotoDelegate is slightly rotated at a
    random angle, this results in a realistic-looking stack of photos.

    \section1 Providing Feedback to Users

    We use a busy indicator and a progress bar to indicate activity while
    Flickr feeds and photos are being loaded.

    In \c AlbumDelegate.qml, we use the \c BusyIndicator custom type and the
    \c on custom property to display a rotating image while the Flickr feed is
    being loaded:

    \quotefromfile demos/photoviewer/PhotoViewerCore/AlbumDelegate.qml
    \skipto BusyIndicator
    \printuntil rssModel
    \printuntil }

    In \c PhotoDelegate.qml, we use them to indicate activity while a photo is
    being loaded:

    \quotefromfile demos/photoviewer/PhotoViewerCore/PhotoDelegate.qml
    \skipto BusyIndicator
    \printuntil }

    We define the \c BusyIndicator type in \c BusyIndicator.qml. We use an
    \l [QML] {Image} type to display an image and apply a \l NumberAnimation to
    its \c rotation property to rotate the image in an infinite loop:

    \quotefromfile demos/photoviewer/PhotoViewerCore/BusyIndicator.qml
    \skipto Image
    \printuntil }
    \printuntil }

    In your apps, you can also use the \l BusyIndicator type from the
    \l {Qt Quick Controls} module.

    In \c main.qml, we use the \c ProgressBar custom type to indicate progress
    while a high quality version of a photo is being opened on full screen:

    \quotefromfile demos/photoviewer/main.qml
    \skipto ProgressBar
    \printuntil }

    We define the \c ProgressBar type in \c ProgressBar.qml. We use a
    \l Rectangle type to create the progress bar and apply a \l NumberAnimation
    to its \c opacity property to change the color of the bar from black to
    white as data loading proceeds:

    \quotefromfile demos/photoviewer/PhotoViewerCore/ProgressBar.qml
    \skipto Item
    \printuntil /^\}/

    In your apps, you can also use the \l ProgressBar type from the
    \l {Qt Quick Controls} module.

    \section1 Localizing Applications

    The example application is translated into German and French. The translated
    strings are loaded at runtime according to the current locale.

    We use a \l Column type in \c main.qml to position buttons for adding and
    editing albums and exiting the application:

    \quotefromfile demos/photoviewer/main.qml
    \skipto Column
    \printuntil quit()
    \printuntil }
    \printuntil }

    We use the \l[QML]{Qt::}{qsTr()} command to mark the button labels
    translatable.

    We use the \c lupdate tool to generate the translation source files and
    the \c lrelease tool to convert the translated strings to the QM files used
    by the application at runtime. These files are stored in the \c i18n
    directory.

    To make the application aware of the translations, we add code to the
    \c main() function in the \c {main.cpp} file. The code creates a
    \l QTranslator object, loads a translation according to the current locale
    at runtime, and installs the translator object into the application:

    \quotefromfile demos/photoviewer/main.cpp
    \skipto main
    \printuntil app.installTranslator

    \sa {QML Applications}
*/