1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2015 Gábor Péterffy <peterffy95@gmail.com>
4//
5
6import QtQuick 2.7
7import QtQuick.Controls 2.0
8import QtQuick.Window 2.2
9
10import org.kde.marble 0.20
11
12Item {
13    id: root
14
15    property var marbleQuickItem: null
16
17    signal itemSelected(var suggestedPlacemark)
18    signal menuButtonClicked()
19    readonly property alias searchResultPlacemark: backend.selectedPlacemark
20    readonly property alias searchResultsVisible: searchResults.visible
21
22    onVisibleChanged: {
23        if( !visible ) {
24            searchResults.visible = false;
25            searchField.query = "";
26        }
27    }
28
29    SystemPalette {
30        id: palette
31        colorGroup: SystemPalette.Active
32    }
33
34    SystemPalette {
35        id: paletteDisabled
36        colorGroup: SystemPalette.Disabled
37    }
38
39    SearchResults {
40        id: searchResults
41        anchors {
42            top: searchField.bottom
43            left: searchField.left
44        }
45        width: searchField.width
46        height: delegateHeight * Math.min(10,count)
47
48        visible: false
49        onItemSelected: {
50            backend.setSelectedPlacemark(index);
51            root.itemSelected(backend.selectedPlacemark);
52            searchResults.visible = false;
53        }
54    }
55
56    Rectangle {
57        id: background
58        visible: searchField.hasFocus && searchField.query === ""
59        anchors.top: searchField.bottom
60        anchors.left: searchField.left
61        width: searchField.width
62        height: childrenRect.height
63        color: palette.base
64
65        property int delegateHeight: 0
66
67        property double itemSpacing: Screen.pixelDensity * 1
68
69        Column {
70            anchors.top: parent.top
71            anchors.left: parent.left
72            anchors.right: parent.right
73            spacing: background.itemSpacing
74
75            ListView {
76                id: bookmarksView
77                anchors.left: parent.left
78                anchors.right: parent.right
79                height: background.delegateHeight * Math.min(6, model.count) + 2 * background.itemSpacing
80                clip: true
81                ScrollIndicator.vertical: ScrollIndicator { }
82
83                model: bookmarks.model
84
85                MouseArea {
86                    anchors.bottom: parent.bottom
87                    height: 2 * background.itemSpacing
88                    width: parent.width
89                    visible: bookmarks.model.count <= 6
90                    onClicked: {
91                        app.selectedPlacemark = bookmarks.placemark(bookmarks.model.count - 1)
92                        itemSelected(bookmarks.placemark(bookmarks.model.count - 1))
93                        marbleMaps.centerOn(selectedPlacemark.longitude, selectedPlacemark.latitude)
94                        dialogLoader.focus = true
95                    }
96                }
97
98                delegate: Row {
99                    width: bookmarksView.width
100                    height: background.itemSpacing + Math.max(bookmarkIcon.height, bookmarkText.height)
101                    spacing: background.itemSpacing
102
103                    leftPadding: 10
104                    rightPadding: 10
105
106                    Image {
107                        id: bookmarkIcon
108                        anchors.verticalCenter: parent.verticalCenter
109                        source: iconPath.substr(0,1) === '/' ? "file://" + iconPath : iconPath
110                        width: Screen.pixelDensity * 4
111                        height: width
112                        sourceSize.width: width
113                        sourceSize.height: height
114                    }
115
116                    Text {
117                        id: bookmarkText
118                        anchors.verticalCenter: parent.verticalCenter
119                        anchors.leftMargin: Screen.pixelDensity * 2
120                        width: bookmarksView.width - bookmarksView.spacing - bookmarkIcon.width
121                        text: display
122                        font.pointSize: 14
123                        color: palette.text
124                        elide: Text.ElideMiddle
125
126                        MouseArea {
127                            anchors.fill: parent
128                            onClicked: {
129                                bookmarksView.currentIndex = index
130                                app.selectedPlacemark = bookmarks.placemark(index);
131                                itemSelected(bookmarks.placemark(index))
132                                marbleMaps.centerOn(selectedPlacemark.longitude, selectedPlacemark.latitude)
133                                dialogLoader.focus = true
134                            }
135                        }
136                    }
137
138                    onHeightChanged: {
139                        if( background.delegateHeight !== height ) {
140                            background.delegateHeight = height;
141                        }
142                    }
143                }
144            }
145
146            Row {
147                visible: bookmarksView.model.count === 0
148                width: parent.width
149                anchors.left: parent.left
150                anchors.right: parent.right
151                spacing: Screen.pixelDensity * 2
152                anchors.margins: Screen.pixelDensity * 2
153
154                Text {
155                    anchors.bottom: parent.bottom
156                    leftPadding: 10
157                    bottomPadding: 3
158                    width: parent.width - Screen.pixelDensity * 2 - emptyImage.width
159                    font.pointSize: 14
160                    color: paletteDisabled.text
161                    text: qsTr("Your bookmarks will appear here.")
162
163                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
164                    elide: Text.ElideRight
165                }
166
167                Image {
168                    id: emptyImage
169                    anchors.bottom: parent.bottom
170                    width: Screen.pixelDensity* 10
171
172                    fillMode: Image.PreserveAspectFit
173                    source: "qrc:/konqi/books.png"
174                }
175            }
176        }
177    }
178
179    SearchBackend {
180        id: backend
181        marbleQuickItem: root.marbleQuickItem
182        onSearchResultChanged: {
183            searchResults.model = model;
184            searchResults.visible = true;
185        }
186        onSearchFinished: searchField.busy = false
187    }
188
189    SearchField {
190        id: searchField
191        width: parent.width
192        anchors {
193            top: parent.top
194            left: parent.left
195            right: parent.right
196        }
197        completionModel: backend.completionModel
198        onSearchRequested: backend.search(query)
199        onCompletionRequested: backend.setCompletionPrefix(query)
200        onCleared: searchResults.visible = false
201        onMenuButtonClicked: root.menuButtonClicked()
202    }
203
204    Bookmarks {
205        id: bookmarks
206        map: root.marbleQuickItem
207    }
208}
209