1/**************************************************************************** 2** 3** Copyright (C) 2017 The Qt Company Ltd. 4** Contact: https://www.qt.io/licensing/ 5** 6** This file is part of the documentation of the Qt Toolkit. 7** 8** $QT_BEGIN_LICENSE:BSD$ 9** Commercial License Usage 10** Licensees holding valid commercial Qt licenses may use this file in 11** accordance with the commercial license agreement provided with the 12** Software or, alternatively, in accordance with the terms contained in 13** a written agreement between you and The Qt Company. For licensing terms 14** and conditions see https://www.qt.io/terms-conditions. For further 15** information use the contact form at https://www.qt.io/contact-us. 16** 17** BSD License Usage 18** Alternatively, you may use this file under the terms of the BSD license 19** as follows: 20** 21** "Redistribution and use in source and binary forms, with or without 22** modification, are permitted provided that the following conditions are 23** met: 24** * Redistributions of source code must retain the above copyright 25** notice, this list of conditions and the following disclaimer. 26** * Redistributions in binary form must reproduce the above copyright 27** notice, this list of conditions and the following disclaimer in 28** the documentation and/or other materials provided with the 29** distribution. 30** * Neither the name of The Qt Company Ltd nor the names of its 31** contributors may be used to endorse or promote products derived 32** from this software without specific prior written permission. 33** 34** 35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 46** 47** $QT_END_LICENSE$ 48** 49****************************************************************************/ 50 51//! [QtQuick import] 52import QtQuick 2.0 53//! [QtQuick import] 54import QtPositioning 5.5 55import QtLocation 5.6 56 57Item { 58 width: 400; height: 400; 59 Plugin { 60 id: myPlugin 61 } 62 63 Place { 64 id: place 65 } 66 67 //! [Category] 68 Category { 69 id: category 70 71 plugin: myPlugin 72 name: "New Category" 73 visibility: Category.PrivateVisibility 74 } 75 //! [Category] 76 77 function saveCategory() { 78 //! [Category save] 79 category.save(); 80 //! [Category save] 81 } 82 83 //! [CategoryView] 84 ListView { 85 model: CategoryModel { 86 plugin: myPlugin 87 hierarchical: false 88 } 89 delegate: Text { text: category.name } 90 } 91 //! [CategoryView] 92 93 //! [ExtendedAttributes] 94 ListView { 95 model: place.extendedAttributes.keys() 96 delegate: Text { 97 text: "<b>" + place.extendedAttributes[modelData].label + ": </b>" + 98 place.extendedAttributes[modelData].text 99 } 100 } 101 //! [ExtendedAttributes] 102 103 //! [ExtendedAttributes read] 104 function printExtendedAttributes(extendedAttributes) { 105 var keys = extendedAttributes.keys(); 106 for (var i = 0; i < keys.length; ++i) { 107 var key = keys[i]; 108 if (extendedAttributes[key].label !== "") 109 console.log(extendedAttributes[key].label + ": " + extendedAttributes[key].text); 110 } 111 } 112 //! [ExtendedAttributes read] 113 114 function writeExtendedAttributes() { 115 //! [ExtendedAttributes write] 116 //assign a new attribute to a place 117 var smokingAttrib = Qt.createQmlObject('import QtLocation 5.3; PlaceAttribute {}', place); 118 smokingAttrib.label = "Smoking Allowed" 119 smokingAttrib.text = "No" 120 place.extendedAttributes.smoking = smokingAttrib; 121 122 //modify an existing attribute 123 place.extendedAttributes.smoking.text = "Yes" 124 //! [ExtendedAttributes write] 125 } 126 127 Icon { 128 id: icon 129 } 130 //! [Icon] 131 Image { 132 source: icon.url(Qt.size(64, 64)) 133 } 134 //! [Icon] 135 136 Image { 137 //! [Icon default] 138 source: icon.url() 139 //! [Icon default] 140 } 141 142 //! [SearchSuggestionModel] 143 PlaceSearchSuggestionModel { 144 id: suggestionModel 145 146 plugin: myPlugin 147 148 // Brisbane 149 searchArea: QtPositioning.circle(QtPositioning.coordinate(-27.46778, 153.02778)) 150 151 onSearchTermChanged: update() 152 } 153 154 ListView { 155 model: suggestionModel 156 delegate: Text { text: suggestion } 157 } 158 //! [SearchSuggestionModel] 159 160 //! [EditorialModel] 161 EditorialModel { 162 id: editorialModel 163 batchSize: 3 164 place: place 165 } 166 167 ListView { 168 model: editorialModel 169 delegate: Item { 170 anchors.fill: parent 171 172 Column { 173 width: parent.width 174 clip: true 175 176 Text { 177 text: title 178 width: parent.width 179 wrapMode: Text.WordWrap 180 font.pixelSize: 24 181 } 182 183 Text { 184 text: text 185 width: parent.width 186 wrapMode: Text.WordWrap 187 font.pixelSize: 20 188 } 189 190 Row { 191 Image { 192 width: 16 193 height: 16 194 195 source: supplier.icon.url(Qt.size(width, height), Icon.List) 196 } 197 198 Text { 199 text: "Provided by " + supplier.name 200 font.pixelSize: 16 201 } 202 } 203 204 Text { 205 text: "Contributed by " + user.name 206 font.pixelSize: 16 207 } 208 209 Text { 210 text: attribution 211 font.pixelSize: 8 212 } 213 } 214 } 215 } 216 //! [EditorialModel] 217 218 //! [ImageModel] 219 ImageModel { 220 id: imageModel 221 batchSize: 3 222 place: place 223 } 224 225 ListView { 226 anchors.top: parent.top 227 width: parent.width 228 spacing: 10 229 230 model: imageModel 231 orientation: ListView.Horizontal 232 snapMode: ListView.SnapOneItem 233 234 delegate: Item { 235 width: listView.width 236 height: listView.height 237 238 Image { 239 anchors.fill: parent 240 source: url 241 fillMode: Image.PreserveAspectFit 242 } 243 244 Text { 245 text: supplier.name + "\n" + supplier.url 246 width: parent.width 247 anchors.bottom: parent.bottom 248 } 249 } 250 } 251 //! [ImageModel] 252 253 //! [Supplier] 254 Supplier { 255 id: placeSupplier 256 name: "Example" 257 url: "http://www.example.com/" 258 } 259 260 Text { 261 text: "This place is was provided by " + placeSupplier.name + "\n" + placeSupplier.url 262 } 263 //! [Supplier] 264 265 //! [Ratings] 266 Text { 267 text: "This place is rated " + place.ratings.average + " out of " + place.ratings.maximum + " stars." 268 } 269 //! [Ratings] 270 271 //! [ContactDetails read] 272 function printContactDetails(contactDetails) { 273 var keys = contactDetails.keys(); 274 for (var i = 0; i < keys.length; ++i) { 275 var contactList = contactDetails[keys[i]]; 276 for (var j = 0; j < contactList.length; ++j) { 277 console.log(contactList[j].label + ": " + contactList[j].value); 278 } 279 } 280 } 281 //! [ContactDetails read] 282 283 //! [ContactDetails write single] 284 function writeSingle() { 285 var phoneNumber = Qt.createQmlObject('import QtLocation 5.3; ContactDetail {}', place); 286 phoneNumber.label = "Phone"; 287 phoneNumber.value = "555-5555" 288 place.contactDetails.phone = phoneNumber; 289 } 290 //! [ContactDetails write single] 291 292 //! [ContactDetails write multiple] 293 function writeMultiple() { 294 var bob = Qt.createQmlObject('import QtLocation 5.3; ContactDetail {}', place); 295 bob.label = "Bob"; 296 bob.value = "555-5555" 297 298 var alice = Qt.createQmlObject('import QtLocation 5.3; ContactDetail {}', place); 299 alice.label = "Alice"; 300 alice.value = "555-8745" 301 302 var numbers = new Array(); 303 numbers.push(bob); 304 numbers.push(alice); 305 306 place.contactDetails.phone = numbers; 307 } 308 //! [ContactDetails write multiple] 309 310 //! [ContactDetails phoneList] 311 ListView { 312 model: place.contactDetails.phone; 313 delegate: Text { text: modelData.label + ": " + modelData.value } 314 } 315 //! [ContactDetails phoneList] 316 317 //! [Place savePlace def] 318 Place { 319 id: myPlace 320 plugin: myPlugin 321 322 name: "Brisbane Technology Park" 323 location: Location { 324 address: Address { 325 street: "53 Brandl Street" 326 city: "Eight Mile Plains" 327 postalCode: "4113" 328 country: "Australia" 329 } 330 coordinate { 331 latitude: -27.579646 332 longitude: 153.100308 333 } 334 } 335 336 visibility: Place.PrivateVisibility 337 } 338 //! [Place savePlace def] 339 340 function fetchDetails() { 341 //! [Place fetchDetails] 342 if (!place.detailsFetched) 343 place.getDetails(); 344 //! [Place fetchDetails] 345 } 346 347 function savePlace() { 348 //! [Place savePlace] 349 myPlace.save(); 350 //! [Place savePlace] 351 } 352 353 function createAndSavePlace() { 354 //! [Place createAndSavePlace] 355 //creating and saving a place 356 var place = Qt.createQmlObject('import QtLocation 5.3; Place { }', parent); 357 place.plugin = myPlugin; 358 place.name = "New York"; 359 place.location.coordinate.latitude = 40.7 360 place.location.coordinate.longitude = -74.0 361 place.save(); 362 //! [Place createAndSavePlace] 363 } 364 365 function removePlace() { 366 //! [Place removePlace] 367 //removing a place 368 place.remove(); 369 //! [Place removePlace] 370 } 371 372 function saveToNewPlugin() { 373 //! [Place save to different plugin] 374 var place = Qt.createQmlObject('import QtLocation 5.3; Place { }', parent); 375 place.plugin = destinationPlugin; 376 place.copyFrom(originalPlace); 377 place.save(); 378 //! [Place save to different plugin] 379 } 380 381 function getPlaceForId() { 382 //! [Place placeId] 383 place.plugin = myPlugin; 384 place.placeId = "known-place-id"; 385 place.getDetails(); 386 //! [Place placeId] 387 } 388 389 function primaryContacts() { 390 //! [Place primaryPhone] 391 var primaryPhone; 392 if (place.contactDetails["phone"].length > 0) 393 primaryPhone = place.contactDetails["phone"][0].value; 394 //! [Place primaryPhone] 395 //! [Place primaryFax] 396 var primaryFax; 397 if (place.contactDetails["fax"].length > 0) 398 primaryFax = place.contactDetails["fax"][0].value; 399 //! [Place primaryFax] 400 //! [Place primaryEmail] 401 var primaryEmail; 402 if (place.contactDetails["email"].length > 0) 403 primaryEmail = place.contactDetails["email"][0].value; 404 //! [Place primaryEmail] 405 //! [Place primaryWebsite] 406 var primaryWebsite; 407 if (place.contactDetails["website"].length > 0) 408 primaryWebsite = place.contactDetails["website"][0].value; 409 //! [Place primaryWebsite] 410 } 411 412 //! [Place favorite] 413 Text { text: place.favorite ? place.favorite.name : place.name } 414 //! [Place favorite] 415 416 function saveFavorite() { 417 var place = Qt.createQmlObject('import QtLocation 5.3; Place { }', parent); 418 var destinationPlugin 419 //! [Place saveFavorite] 420 place.initializeFavorite(destinationPlugin); 421 //if necessary customizations to the favorite can be made here. 422 //... 423 place.favorite.save(); 424 //! [Place saveFavorite] 425 } 426 427 function removeFavorite() { 428 var place; 429 //! [Place removeFavorite 1] 430 place.favorite.remove(); 431 //! [Place removeFavorite 1] 432 433 //! [Place removeFavorite 2] 434 //check successful removal of the favorite by monitoring its status. 435 //once that is done we can assign null to the favorite 436 place.favorite = null; 437 //! [Place removeFavorite 2] 438 } 439 440 function connectStatusChangedHandler() { 441 //! [Place checkStatus] 442 place.statusChanged.connect(statusChangedHandler); 443 //! [Place checkStatus] 444 } 445 446 //! [Place checkStatus handler] 447 function statusChangedHandler() { 448 if (statusChangedHandler.prevStatus === Place.Saving) { 449 switch (place.status) { 450 case Place.Ready: 451 console.log('Save successful'); 452 break; 453 case Place.Error: 454 console.log('Save failed'); 455 break; 456 default: 457 break; 458 } 459 } 460 statusChangedHandler.prevStatus = place.status; 461 } 462 //! [Place checkStatus handler] 463} 464