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 examples 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
51import QtQuick 2.5
52import QtLocation 5.6
53import QtQuick.Controls 1.4
54import QtQuick.Layouts 1.2
55import "../helper.js" as Helper
56
57Item {
58    id: root
59
60    signal showPlaceDetails(variant place,variant distance)
61    signal searchFor(string query)
62
63    width: parent.width
64    height: childrenRect.height
65
66    //! [PlaceSearchModel place delegate]
67    Component {
68        id: placeComponent
69        Item {
70            id: placeRoot
71            width: root.width
72            height: Math.max(icon.height, 3 * placeName.height)
73
74            Rectangle {
75                anchors.fill: parent
76                color: "#44ffffff"
77                visible: mouse.pressed
78            }
79
80            Rectangle {
81                anchors.fill: parent
82                color: "#dbffde"
83                visible: model.sponsored !== undefined ? model.sponsored : false
84
85                Label {
86                    text: qsTr("Sponsored result")
87                    horizontalAlignment: Text.AlignRight
88                    anchors.right: parent.right
89                    anchors.bottom: parent.bottom
90                    font.pixelSize: 8
91                    visible: model.sponsored !== undefined ? model.sponsored : false
92                }
93            }
94
95            GridLayout {
96                rows: 2
97                columns: 2
98                anchors.fill: parent
99                anchors.leftMargin: 30
100                flow: GridLayout.TopToBottom
101
102                Image {
103                    // anchors.verticalCenter: parent.verticalCenter
104                    id:icon
105                    source: place.favorite ? "../../resources/star.png" : place.icon.url()
106                    Layout.rowSpan: 2
107                }
108
109                Label {
110                    id: placeName
111                    text: place.favorite ? place.favorite.name : place.name
112                    Layout.fillWidth: true
113                }
114
115                Label {
116                    id: distanceText
117                    font.italic: true
118                    text: Helper.formatDistance(distance)
119                    Layout.fillWidth: true
120                }
121            }
122
123            Rectangle {
124                anchors.left: parent.left
125                anchors.right: parent.right
126                anchors.margins: 15
127                height: 1
128                color: "#46a2da"
129            }
130
131            MouseArea {
132                id: mouse
133                anchors.fill: parent
134                onClicked: {
135                    if (model.type === undefined || type === PlaceSearchModel.PlaceResult) {
136                        if (!place.detailsFetched)
137                            place.getDetails();
138                        root.showPlaceDetails(model.place, model.distance);
139                    }
140                }
141            }
142        }
143    }
144    //! [PlaceSearchModel place delegate]
145
146    Component {
147        id: proposedSearchComponent
148
149        Item {
150            id: proposedSearchRoot
151
152            width: root.width
153            height: Math.max(icon.height, 2 * proposedSearchTitle.height)
154
155            Rectangle {
156                anchors.fill: parent
157                color: "#11ffffff"
158                visible: mouse.pressed
159            }
160
161            RowLayout {
162                anchors.fill: parent
163                anchors.leftMargin: 30
164
165                Image {
166                    source: icon.url()
167                }
168
169                Label {
170                    id: proposedSearchTitle
171                    anchors.verticalCenter: parent.verticalCenter
172                    text: "Search for " + title
173                }
174            }
175
176            Rectangle {
177                anchors.left: parent.left
178                anchors.right: parent.right
179                anchors.margins: 15
180                height: 1
181                color: "#46a2da"
182            }
183
184            MouseArea {
185                anchors.fill: parent
186                onClicked: root.ListView.view.model.updateWith(index);
187            }
188        }
189    }
190
191    Loader {
192        anchors.left: parent.left
193        anchors.right: parent.right
194
195        sourceComponent: {
196            switch (model.type) {
197            case PlaceSearchModel.PlaceResult:
198                return placeComponent;
199            case PlaceSearchModel.ProposedSearchResult:
200                return proposedSearchComponent;
201            default:
202                //do nothing, don't assign component if result type not recognized
203            }
204        }
205    }
206}
207