Skip to content
OfflineMap.qml 40.6 KiB
Newer Older
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
dogmaphobic's avatar
dogmaphobic committed

import QtQuick                  2.5
import QtQuick.Controls         1.2
import QtQuick.Controls.Styles  1.2
import QtQuick.Dialogs          1.1
import QtQuick.Layouts          1.2
import QtLocation               5.5
import QtPositioning            5.5
dogmaphobic's avatar
dogmaphobic committed

Don Gagne's avatar
Don Gagne committed
import QGroundControl               1.0
import QGroundControl.Controls      1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0
import QGroundControl.FlightMap     1.0
dogmaphobic's avatar
dogmaphobic committed

Don Gagne's avatar
Don Gagne committed
QGCView {
    id:             offlineMapView
    viewPanel:      panel
    anchors.fill:   parent
dogmaphobic's avatar
dogmaphobic committed

dogmaphobic's avatar
dogmaphobic committed
    property var    _currentSelection:  null
dogmaphobic's avatar
dogmaphobic committed

dogmaphobic's avatar
dogmaphobic committed
    property string mapKey:             "lastMapType"
dogmaphobic's avatar
dogmaphobic committed

    property string mapType:            QGroundControl.flightMapSettings.mapProvider + " " + QGroundControl.flightMapSettings.mapType
    property bool   isMapInteractive:   false
    property var    savedCenter:        undefined
    property real   savedZoom:          3
    property string savedMapType:       ""
Don Gagne's avatar
Don Gagne committed
    property bool   _showPreview:       true
dogmaphobic's avatar
dogmaphobic committed
    property bool   _defaultSet:        offlineMapView && offlineMapView._currentSelection && offlineMapView._currentSelection.defaultSet
    property real   _margins:           ScreenTools.defaultFontPixelWidth /2
dogmaphobic's avatar
dogmaphobic committed

dogmaphobic's avatar
dogmaphobic committed
    property bool   _saveRealEstate:          ScreenTools.isTinyScreen || ScreenTools.isShortScreen
    property real   _adjustableFontPointSize: _saveRealEstate ? ScreenTools.smallFontPointSize : ScreenTools.defaultFontPointSize
Don Gagne's avatar
Don Gagne committed

Don Gagne's avatar
Don Gagne committed
    property var _mapAdjustedColor: _map.isSatelliteMap ? "white" : "black"

Don Gagne's avatar
Don Gagne committed
    readonly property real minZoomLevel: 3
    readonly property real maxZoomLevel: 20

dogmaphobic's avatar
dogmaphobic committed
    readonly property int _maxTilesForDownload: 100000
Don Gagne's avatar
Don Gagne committed
    QGCPalette { id: qgcPal }
dogmaphobic's avatar
dogmaphobic committed
    Component.onCompleted: {
        QGroundControl.mapEngineManager.loadTileSets()
        updateMap()
        savedCenter = _map.toCoordinate(Qt.point(_map.width / 2, _map.height / 2))
dogmaphobic's avatar
dogmaphobic committed
    }

    Connections {
        target: QGroundControl.mapEngineManager
        onTileSetsChanged: {
            setName.text = QGroundControl.mapEngineManager.getUniqueName()
        }
        onErrorMessageChanged: {
            errorDialog.visible = true
        }
    }

    ExclusiveGroup { id: setGroup }

    function handleChanges() {
        if(isMapInteractive) {
Don Gagne's avatar
Don Gagne committed
            var xl = 0
            var yl = 0
            var xr = _map.width.toFixed(0) - 1  // Must be within boundaries of visible map
            var yr = _map.height.toFixed(0) - 1 // Must be within boundaries of visible map
            var c0 = _map.toCoordinate(Qt.point(xl, yl))
            var c1 = _map.toCoordinate(Qt.point(xr, yr))
            QGroundControl.mapEngineManager.updateForCurrentView(c0.longitude, c0.latitude, c1.longitude, c1.latitude, sliderMinZoom.value, sliderMaxZoom.value, mapType)
dogmaphobic's avatar
dogmaphobic committed
        }
    }

    function updateMap() {
        for (var i = 0; i < _map.supportedMapTypes.length; i++) {
            if (mapType === _map.supportedMapTypes[i].name) {
                _map.activeMapType = _map.supportedMapTypes[i]
                handleChanges()
                return
            }
        }
    }

Don Gagne's avatar
Don Gagne committed
    function addNewSet() {
        isMapInteractive = true
        mapType = QGroundControl.flightMapSettings.mapProvider + " " + QGroundControl.flightMapSettings.mapType
        resetMapToDefaults()
        handleChanges()
        _map.visible = true
dogmaphobic's avatar
dogmaphobic committed
        _tileSetList.visible = false
Don Gagne's avatar
Don Gagne committed
        infoView.visible = false
        addNewSetView.visible = true
dogmaphobic's avatar
dogmaphobic committed
    }

    function showList() {
        isMapInteractive = false
        _map.visible = false
dogmaphobic's avatar
dogmaphobic committed
        _tileSetList.visible = true
Don Gagne's avatar
Don Gagne committed
        infoView.visible = false
        addNewSetView.visible = false
dogmaphobic's avatar
dogmaphobic committed
    }

    function showInfo() {
        isMapInteractive = false
Don Gagne's avatar
Don Gagne committed
        if(_currentSelection && !offlineMapView._currentSelection.deleting) {
dogmaphobic's avatar
dogmaphobic committed
        } else
            showList()
    }

    function toRadian(deg) {
        return deg * Math.PI / 180
    }

    function toDegree(rad) {
        return rad * 180 / Math.PI
    }

    function midPoint(lat1, lat2, lon1, lon2) {
        var dLon = toRadian(lon2 - lon1);
        lat1 = toRadian(lat1);
        lat2 = toRadian(lat2);
        lon1 = toRadian(lon1);
        var Bx = Math.cos(lat2) * Math.cos(dLon);
        var By = Math.cos(lat2) * Math.sin(dLon);
        var lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
        var lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
        return QtPositioning.coordinate(toDegree(lat3), toDegree(lon3))
    }

    function enterInfoView() {
Don Gagne's avatar
Don Gagne committed
        _map.visible = true
        isMapInteractive = false
        savedCenter = _map.toCoordinate(Qt.point(_map.width / 2, _map.height / 2))
        savedZoom = _map.zoomLevel
        savedMapType = mapType
dogmaphobic's avatar
dogmaphobic committed
        if(!offlineMapView._currentSelection.defaultSet) {
Don Gagne's avatar
Don Gagne committed
            mapType = offlineMapView._currentSelection.mapTypeStr
            _map.center = midPoint(offlineMapView._currentSelection.topleftLat, offlineMapView._currentSelection.bottomRightLat, offlineMapView._currentSelection.topleftLon, offlineMapView._currentSelection.bottomRightLon)
            //-- Delineate Set Region
Don Gagne's avatar
Don Gagne committed
            var x0 = offlineMapView._currentSelection.topleftLon
            var x1 = offlineMapView._currentSelection.bottomRightLon
            var y0 = offlineMapView._currentSelection.topleftLat
            var y1 = offlineMapView._currentSelection.bottomRightLat
            mapBoundary.topLeft     = QtPositioning.coordinate(y0, x0)
            mapBoundary.bottomRight = QtPositioning.coordinate(y1, x1)
            mapBoundary.visible = true
dogmaphobic's avatar
dogmaphobic committed
            // Some times, for whatever reason, the bounding box is correct (around ETH for instance), but the rectangle is drawn across the planet.
            // When that happens, the "_map.fitViewportToMapItems()" below makes the map to zoom to the entire earth.
            //console.log("Map boundary: " + mapBoundary.topLeft + " " + mapBoundary.bottomRight)
            _map.fitViewportToMapItems()
        }
        _tileSetList.visible = false
        addNewSetView.visible = false
dogmaphobic's avatar
dogmaphobic committed
        infoView.visible = true
        mapBoundary.visible = false
        _map.center = savedCenter
        _map.zoomLevel = savedZoom
        mapType = savedMapType
    }

    function resetMapToDefaults() {
        _map.center = QGroundControl.flightMapPosition
        _map.zoomLevel = QGroundControl.flightMapZoom
dogmaphobic's avatar
dogmaphobic committed
    ExclusiveGroup {
        id: _dropButtonsExclusiveGroup
    }

    onMapTypeChanged: {
        updateMap()
        if(isMapInteractive) {
            QGroundControl.mapEngineManager.saveSetting(mapKey, mapType)
        }
dogmaphobic's avatar
dogmaphobic committed
    }

    MessageDialog {
        id:         errorDialog
        visible:    false
        text:       QGroundControl.mapEngineManager.errorMessage
        icon:       StandardIcon.Critical
        standardButtons: StandardButton.Ok
Don Gagne's avatar
Don Gagne committed
        title:      qsTr("Error Message")
dogmaphobic's avatar
dogmaphobic committed
        onYes: {
            errorDialog.visible = false
        }
    }
Don Gagne's avatar
Don Gagne committed

Don Gagne's avatar
Don Gagne committed
    Component {
        id: optionsDialogComponent

        QGCViewDialog {
            id: optionDialog

            function accept() {
                QGroundControl.mapEngineManager.mapboxToken  = mapBoxToken.text
                QGroundControl.mapEngineManager.maxDiskCache = parseInt(maxCacheSize.text)
                QGroundControl.mapEngineManager.maxMemCache  = parseInt(maxCacheMemSize.text)
                optionDialog.hideDialog()
dogmaphobic's avatar
dogmaphobic committed
            }
Don Gagne's avatar
Don Gagne committed

Don Gagne's avatar
Don Gagne committed
            QGCFlickable {
                anchors.fill:   parent
                contentHeight:  optionsColumn.height
Don Gagne's avatar
Don Gagne committed
                Column {
                    id:                 optionsColumn
                    anchors.margins:    ScreenTools.defaultFontPixelWidth
                    anchors.left:       parent.left
                    anchors.right:      parent.right
                    anchors.top:        parent.top
                    spacing:            ScreenTools.defaultFontPixelHeight / 2
Don Gagne's avatar
Don Gagne committed
                    QGCLabel { text:       qsTr("Max Cache Disk Size (MB):") }
Don Gagne's avatar
Don Gagne committed
                    QGCTextField {
                        id:                 maxCacheSize
                        maximumLength:      6
                        inputMethodHints:   Qt.ImhDigitsOnly
                        validator:          IntValidator {bottom: 1; top: 262144;}
                        text:               QGroundControl.mapEngineManager.maxDiskCache
                    }
Don Gagne's avatar
Don Gagne committed
                    Item { width: 1; height: 1 }

                    QGCLabel { text:       qsTr("Max Cache Memory Size (MB):") }

                    QGCTextField {
                        id:                 maxCacheMemSize
                        maximumLength:      4
                        inputMethodHints:   Qt.ImhDigitsOnly
                        validator:          IntValidator {bottom: 1; top: 1024;}
Don Gagne's avatar
Don Gagne committed
                        text:               QGroundControl.mapEngineManager.maxMemCache
dogmaphobic's avatar
dogmaphobic committed
                    }
Don Gagne's avatar
Don Gagne committed

                    QGCLabel {
                        font.pointSize: _adjustableFontPointSize
                        text:           qsTr("Memory cache changes require a restart to take effect.")
                    }

                    Item { width: 1; height: 1 }

                    QGCLabel { text: qsTr("MapBox Access Token") }

                    QGCTextField {
                        id:             mapBoxToken
                        maximumLength:  256
                        width:          ScreenTools.defaultFontPixelWidth * 30
                        text:           QGroundControl.mapEngineManager.mapboxToken
                    }

                    QGCLabel {
                        text:           qsTr("With an access token, you can use MapBox Maps.")
                        font.pointSize: _adjustableFontPointSize
                    }
                } // GridLayout
            } // QGCFlickable
        } // QGCViewDialog - optionsDialog
    } // Component - optionsDialogComponent

    Component {
        id: deleteConfirmationDialogComponent
        QGCViewMessage {
dogmaphobic's avatar
dogmaphobic committed
            id:  deleteConfirmationDialog
            message: {
                if(offlineMapView._currentSelection.defaultSet)
                    return qsTr("This will delete all tiles INCLUDING the tile sets you have created yourself.\n\nIs this really what you want?");
                else
                    return qsTr("Delete %1 and all its tiles.\n\nIs this really what you want?").arg(offlineMapView._currentSelection.name);
dogmaphobic's avatar
dogmaphobic committed
            }
Don Gagne's avatar
Don Gagne committed
            function accept() {
                QGroundControl.mapEngineManager.deleteTileSet(offlineMapView._currentSelection)
dogmaphobic's avatar
dogmaphobic committed
                deleteConfirmationDialog.hideDialog()
Don Gagne's avatar
Don Gagne committed
                leaveInfoView()
                showList()
Don Gagne's avatar
Don Gagne committed
    }

    QGCViewPanel {
        id:                 panel
        anchors.fill:       parent

        Map {
            id:                 _map
            anchors.fill:       parent
            center:             QGroundControl.lastKnownHomePosition
Don Gagne's avatar
Don Gagne committed
            visible:            false
            gesture.flickDeceleration:  3000

Don Gagne's avatar
Don Gagne committed
            property bool isSatelliteMap: activeMapType.name.indexOf("Satellite") > -1 || activeMapType.name.indexOf("Hybrid") > -1

Don Gagne's avatar
Don Gagne committed
            plugin: Plugin { name: "QGroundControl" }

            MapRectangle {
                id:             mapBoundary
                border.width:   2
                border.color:   "red"
                color:          Qt.rgba(1,0,0,0.05)
                smooth:         true
                antialiasing:   true
            }

            Component.onCompleted: resetMapToDefaults()
Don Gagne's avatar
Don Gagne committed

            onCenterChanged:    handleChanges()
            onZoomLevelChanged: handleChanges()
            onWidthChanged:     handleChanges()
            onHeightChanged:    handleChanges()

            // Used to make pinch zoom work
            MouseArea {
                anchors.fill: parent
dogmaphobic's avatar
dogmaphobic committed
            }
            CenterMapDropButton {
                anchors.margins:    _margins
                anchors.left:       parent.left
                anchors.top:        parent.top
                map:                _map
                z:                  QGroundControl.zOrderTopMost
                showMission:        false
                showAllItems:       false
                visible:            addNewSetView.visible
            }

Don Gagne's avatar
Don Gagne committed
            MapScale {
                anchors.leftMargin:     ScreenTools.defaultFontPixelWidth / 2
                anchors.bottomMargin:   anchors.leftMargin
                anchors.left:           parent.left
                anchors.bottom:         parent.bottom
                mapControl:             _map
Don Gagne's avatar
Don Gagne committed

dogmaphobic's avatar
dogmaphobic committed
            //-----------------------------------------------------------------
Don Gagne's avatar
Don Gagne committed
            //-- Show Set Info
            Rectangle {
                id:                 infoView
dogmaphobic's avatar
dogmaphobic committed
                anchors.margins:    ScreenTools.defaultFontPixelHeight
Don Gagne's avatar
Don Gagne committed
                anchors.right:      parent.right
dogmaphobic's avatar
dogmaphobic committed
                anchors.verticalCenter: parent.verticalCenter
                width:              tileInfoColumn.width  + (ScreenTools.defaultFontPixelWidth  * 2)
                height:             tileInfoColumn.height + (ScreenTools.defaultFontPixelHeight * 2)
                color:              Qt.rgba(qgcPal.window.r, qgcPal.window.g, qgcPal.window.b, 0.85)
Don Gagne's avatar
Don Gagne committed
                radius:             ScreenTools.defaultFontPixelWidth * 0.5
                visible:            false
dogmaphobic's avatar
dogmaphobic committed
                property bool       _extraButton:   !_defaultSet && ((!offlineMapView._currentSelection.complete && !offlineMapView._currentSelection.downloading) || (!offlineMapView._currentSelection.complete && offlineMapView._currentSelection.downloading))
                property real       _labelWidth:    ScreenTools.defaultFontPixelWidth * 10
                property real       _valueWidth:    ScreenTools.defaultFontPixelWidth * 14
dogmaphobic's avatar
dogmaphobic committed
                    id:                 tileInfoColumn
                    anchors.margins:    ScreenTools.defaultFontPixelHeight * 0.5
                    spacing:            ScreenTools.defaultFontPixelHeight * 0.5
                    anchors.centerIn:   parent
Don Gagne's avatar
Don Gagne committed
                        anchors.left:   parent.left
                        anchors.right:  parent.right
                        wrapMode:       Text.WordWrap
                        text:           offlineMapView._currentSelection ? offlineMapView._currentSelection.name : ""
                        font.pointSize: _saveRealEstate ? ScreenTools.defaultFontPointSize : ScreenTools.mediumFontPointSize
                        horizontalAlignment: Text.AlignHCenter
Don Gagne's avatar
Don Gagne committed
                        anchors.left:   parent.left
                        anchors.right:  parent.right
                        wrapMode:       Text.WordWrap
dogmaphobic's avatar
dogmaphobic committed
                        text: {
                            if(offlineMapView._currentSelection) {
                                if(offlineMapView._currentSelection.defaultSet)
                                    return qsTr("System Wide Tile Cache");
                                else
                                    return "(" + offlineMapView._currentSelection.mapTypeStr + ")"
                            } else
                                return "";
                        }
Don Gagne's avatar
Don Gagne committed
                        horizontalAlignment: Text.AlignHCenter
dogmaphobic's avatar
dogmaphobic committed
                    //-- Tile Sets
                    Row {
                        spacing:    ScreenTools.defaultFontPixelWidth
                        anchors.horizontalCenter: parent.horizontalCenter
                        visible:    !_defaultSet
                        QGCLabel {  text: qsTr("Zoom Levels:"); width: infoView._labelWidth; }
                        QGCLabel {  text: offlineMapView._currentSelection ? (offlineMapView._currentSelection.minZoom + " - " + offlineMapView._currentSelection.maxZoom) : ""; horizontalAlignment: Text.AlignRight; width: infoView._valueWidth; }
dogmaphobic's avatar
dogmaphobic committed
                    Row {
                        spacing:    ScreenTools.defaultFontPixelWidth
                        anchors.horizontalCenter: parent.horizontalCenter
                        visible:    !_defaultSet
                        QGCLabel {  text: qsTr("Total:"); width: infoView._labelWidth; }
                        QGCLabel {  text: (offlineMapView._currentSelection ? offlineMapView._currentSelection.totalTileCountStr : "") + " (" + (offlineMapView._currentSelection ? offlineMapView._currentSelection.totalTilesSizeStr : "") + ")"; horizontalAlignment: Text.AlignRight; width: infoView._valueWidth; }
                    }
                    Row {
                        spacing:    ScreenTools.defaultFontPixelWidth
                        anchors.horizontalCenter: parent.horizontalCenter
                        visible:    offlineMapView && offlineMapView._currentSelection && !_defaultSet && offlineMapView._currentSelection.uniqueTileCount > 0
                        QGCLabel {  text: qsTr("Unique:"); width: infoView._labelWidth; }
                        QGCLabel {  text: (offlineMapView._currentSelection ? offlineMapView._currentSelection.uniqueTileCountStr : "") + " (" + (offlineMapView._currentSelection ? offlineMapView._currentSelection.uniqueTileSizeStr : "") + ")"; horizontalAlignment: Text.AlignRight; width: infoView._valueWidth; }
Don Gagne's avatar
Don Gagne committed

dogmaphobic's avatar
dogmaphobic committed
                    Row {
                        spacing:    ScreenTools.defaultFontPixelWidth
                        anchors.horizontalCenter: parent.horizontalCenter
                        visible:    offlineMapView && offlineMapView._currentSelection && !_defaultSet && !offlineMapView._currentSelection.complete
                        QGCLabel {  text: qsTr("Downloaded:"); width: infoView._labelWidth; }
                        QGCLabel {  text: (offlineMapView._currentSelection ? offlineMapView._currentSelection.savedTileCountStr : "") + " (" + (offlineMapView._currentSelection ? offlineMapView._currentSelection.savedTileSizeStr : "") + ")"; horizontalAlignment: Text.AlignRight; width: infoView._valueWidth; }
                    }
                    Row {
                        spacing:    ScreenTools.defaultFontPixelWidth
                        anchors.horizontalCenter: parent.horizontalCenter
                        visible:    offlineMapView && offlineMapView._currentSelection && !_defaultSet && !offlineMapView._currentSelection.complete && offlineMapView._currentSelection.errorCount > 0
                        QGCLabel {  text: qsTr("Error Count:"); width: infoView._labelWidth; }
                        QGCLabel {  text: offlineMapView._currentSelection ? offlineMapView._currentSelection.errorCountStr : ""; horizontalAlignment: Text.AlignRight; width: infoView._valueWidth; }
                    }
                    //-- Default Tile Set
                    Row {
                        spacing:    ScreenTools.defaultFontPixelWidth
                        anchors.horizontalCenter: parent.horizontalCenter
                        visible:    _defaultSet
                        QGCLabel { text: qsTr("Size:"); width: infoView._labelWidth; }
                        QGCLabel { text: offlineMapView._currentSelection ? offlineMapView._currentSelection.savedTileSizeStr  : ""; horizontalAlignment: Text.AlignRight; width: infoView._valueWidth; }
                    }
                    Row {
                        spacing:    ScreenTools.defaultFontPixelWidth
                        anchors.horizontalCenter: parent.horizontalCenter
                        visible:    _defaultSet
                        QGCLabel { text: qsTr("Tile Count:"); width: infoView._labelWidth; }
                        QGCLabel { text: offlineMapView._currentSelection ? offlineMapView._currentSelection.savedTileCountStr : ""; horizontalAlignment: Text.AlignRight; width: infoView._valueWidth; }
                    }
                    Row {
                        spacing:    ScreenTools.defaultFontPixelWidth
                        anchors.horizontalCenter: parent.horizontalCenter
Don Gagne's avatar
Don Gagne committed
                        QGCButton {
                            text:       qsTr("Resume Download")
dogmaphobic's avatar
dogmaphobic committed
                            visible:    offlineMapView._currentSelection && offlineMapView._currentSelection && !_defaultSet && (!offlineMapView._currentSelection.complete && !offlineMapView._currentSelection.downloading)
                            width:      ScreenTools.defaultFontPixelWidth * 16
Don Gagne's avatar
Don Gagne committed
                            onClicked: {
                                if(offlineMapView._currentSelection)
                                    offlineMapView._currentSelection.resumeDownloadTask()
                            }
Don Gagne's avatar
Don Gagne committed
                        QGCButton {
                            text:       qsTr("Cancel Download")
dogmaphobic's avatar
dogmaphobic committed
                            visible:    offlineMapView._currentSelection && offlineMapView._currentSelection && !_defaultSet && (!offlineMapView._currentSelection.complete && offlineMapView._currentSelection.downloading)
                            width:      ScreenTools.defaultFontPixelWidth * 16
Don Gagne's avatar
Don Gagne committed
                            onClicked: {
                                if(offlineMapView._currentSelection)
                                    offlineMapView._currentSelection.cancelDownloadTask()
                            }
Don Gagne's avatar
Don Gagne committed
                        QGCButton {
                            text:       qsTr("Delete")
dogmaphobic's avatar
dogmaphobic committed
                            width:      ScreenTools.defaultFontPixelWidth * (infoView._extraButton ? 6 : 10)
Don Gagne's avatar
Don Gagne committed
                            onClicked:  showDialog(deleteConfirmationDialogComponent, qsTr("Confirm Delete"), qgcView.showDialogDefaultWidth, StandardButton.Yes | StandardButton.No)
Don Gagne's avatar
Don Gagne committed
                        QGCButton {
dogmaphobic's avatar
dogmaphobic committed
                            text:       qsTr("Close")
                            width:      ScreenTools.defaultFontPixelWidth * (infoView._extraButton ? 6 : 10)
                            onClicked: {
                                leaveInfoView()
                                showList()
                            }
dogmaphobic's avatar
dogmaphobic committed
                    }
                }
            } // Rectangle - infoView
Don Gagne's avatar
Don Gagne committed

dogmaphobic's avatar
dogmaphobic committed
            //-----------------------------------------------------------------
            //-- Add new set
Don Gagne's avatar
Don Gagne committed
            Item {
                id:             addNewSetView
                anchors.fill:   parent
                visible:        false
Don Gagne's avatar
Don Gagne committed

                Column {
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.leftMargin:     _margins
                    anchors.left:           parent.left
                    spacing:                _margins

                    QGCButton {
                        text:       "Show zoom previews"
                        visible:    !_showPreview
                        onClicked:  _showPreview = !_showPreview
                    Map {
                        id:                 minZoomPreview
                        width:              addNewSetView.width / 4
                        height:             addNewSetView.height / 4
                        center:             _map.center
                        activeMapType:      _map.activeMapType
                        zoomLevel:          sliderMinZoom.value
                        gesture.enabled:    false
                        visible:            _showPreview

                        property bool isSatelliteMap: activeMapType.name.indexOf("Satellite") > -1 || activeMapType.name.indexOf("Hybrid") > -1

                        plugin: Plugin { name: "QGroundControl" }

                        MapScale {
                            anchors.leftMargin:     ScreenTools.defaultFontPixelWidth / 2
                            anchors.bottomMargin:   anchors.leftMargin
                            anchors.left:           parent.left
                            anchors.bottom:         parent.bottom
                            mapControl:             parent
                        }
                        Rectangle {
                            anchors.fill:   parent
                            border.color:   _mapAdjustedColor
                            color:          "transparent"
                            QGCMapLabel {
                                anchors.centerIn:   parent
                                map:                minZoomPreview
                                text:               qsTr("Min Zoom: %1").arg(sliderMinZoom.value)
                            }
                            MouseArea {
                                anchors.fill:   parent
                                onClicked:      _showPreview = false
                            }
                        }
                    } // Map

                    Map {
                        id:                 maxZoomPreview
                        width:              minZoomPreview.width
                        height:             minZoomPreview.height
                        center:             _map.center
                        activeMapType:      _map.activeMapType
                        zoomLevel:          sliderMaxZoom.value
                        gesture.enabled:    false
                        visible:            _showPreview

                        property bool isSatelliteMap: activeMapType.name.indexOf("Satellite") > -1 || activeMapType.name.indexOf("Hybrid") > -1

                        plugin: Plugin { name: "QGroundControl" }

                        MapScale {
                            anchors.leftMargin:     ScreenTools.defaultFontPixelWidth / 2
                            anchors.bottomMargin:   anchors.leftMargin
                            anchors.left:           parent.left
                            anchors.bottom:         parent.bottom
                            mapControl:             parent
                        }

                        Rectangle {
                            anchors.fill:   parent
                            border.color:   _mapAdjustedColor
                            color:          "transparent"
Don Gagne's avatar
Don Gagne committed

                            QGCMapLabel {
                                anchors.centerIn:   parent
                                map:                maxZoomPreview
                                text:               qsTr("Max Zoom: %1").arg(sliderMaxZoom.value)
                            }
                            MouseArea {
                                anchors.fill:   parent
                                onClicked:      _showPreview = false
                            }
                        }
                    } // Map
Don Gagne's avatar
Don Gagne committed
                }

                //-- Add new set dialog
                Rectangle {
                    anchors.margins:    ScreenTools.defaultFontPixelWidth
dogmaphobic's avatar
dogmaphobic committed
                    anchors.verticalCenter: parent.verticalCenter
Don Gagne's avatar
Don Gagne committed
                    anchors.right:      parent.right
dogmaphobic's avatar
dogmaphobic committed
                    width:              ScreenTools.defaultFontPixelWidth * 24
                    height:             Math.min(parent.height - (anchors.margins * 2), addNewSetFlickable.y + addNewSetColumn.height + addNewSetLabel.anchors.margins)
dogmaphobic's avatar
dogmaphobic committed
                    color:              Qt.rgba(qgcPal.window.r, qgcPal.window.g, qgcPal.window.b, 0.85)
Don Gagne's avatar
Don Gagne committed
                    radius:             ScreenTools.defaultFontPixelWidth * 0.5

                    QGCLabel {
                        id:                 addNewSetLabel
                        anchors.margins:    ScreenTools.defaultFontPixelHeight / 2
                        anchors.top:        parent.top
                        anchors.left:       parent.left
                        anchors.right:      parent.right
                        wrapMode:           Text.WordWrap
                        text:               qsTr("Add New Set")
                        font.pointSize:     _saveRealEstate ? ScreenTools.defaultFontPointSize : ScreenTools.mediumFontPointSize
                        horizontalAlignment: Text.AlignHCenter
                    }
Don Gagne's avatar
Don Gagne committed

Don Gagne's avatar
Don Gagne committed
                    QGCFlickable {
                        id:                     addNewSetFlickable
                        anchors.leftMargin:     ScreenTools.defaultFontPixelWidth
                        anchors.rightMargin:    anchors.leftMargin
                        anchors.topMargin:      ScreenTools.defaultFontPixelWidth / 3
                        anchors.bottomMargin:   anchors.topMargin
                        anchors.top:            addNewSetLabel.bottom
                        anchors.left:           parent.left
                        anchors.right:          parent.right
                        anchors.bottom:         parent.bottom
                        clip:                   true
                        contentHeight:          addNewSetColumn.height
Don Gagne's avatar
Don Gagne committed

                        Column {
Don Gagne's avatar
Don Gagne committed
                            id:                 addNewSetColumn
Don Gagne's avatar
Don Gagne committed
                            anchors.left:       parent.left
                            anchors.right:      parent.right
Don Gagne's avatar
Don Gagne committed
                            spacing:            ScreenTools.defaultFontPixelHeight / (ScreenTools.isTinyScreen ? 4 : 2)
Don Gagne's avatar
Don Gagne committed

Don Gagne's avatar
Don Gagne committed
                            Column {
                                anchors.left:       parent.left
                                anchors.right:      parent.right
                                QGCLabel { text: qsTr("Name:") }
                                QGCTextField {
                                    id:             setName
                                    anchors.left:   parent.left
                                    anchors.right:  parent.right
Don Gagne's avatar
Don Gagne committed
                                }
Don Gagne's avatar
Don Gagne committed

                            Column {
                                anchors.left:       parent.left
                                anchors.right:      parent.right
                                QGCLabel {
Don Gagne's avatar
Don Gagne committed
                                    text:       qsTr("Map type:")
                                    visible:    !_saveRealEstate
Don Gagne's avatar
Don Gagne committed
                                }
Don Gagne's avatar
Don Gagne committed
                                QGCComboBox {
                                    id:             mapCombo
                                    anchors.left:   parent.left
                                    anchors.right:  parent.right
                                    model:          QGroundControl.mapEngineManager.mapList
                                    onActivated: {
                                        mapType = textAt(index)
                                        if(_dropButtonsExclusiveGroup.current)
                                            _dropButtonsExclusiveGroup.current.checked = false
                                        _dropButtonsExclusiveGroup.current = null
Don Gagne's avatar
Don Gagne committed
                                    }
Don Gagne's avatar
Don Gagne committed
                                    Component.onCompleted: {
                                        var index = mapCombo.find(mapType)
                                        if (index === -1) {
                                            console.warn("Active map name not in combo", mapType)
                                        } else {
                                            mapCombo.currentIndex = index
Don Gagne's avatar
Don Gagne committed
                                        }
                                    }
Don Gagne's avatar
Don Gagne committed

Don Gagne's avatar
Don Gagne committed
                            Rectangle {
                                anchors.left:   parent.left
                                anchors.right:  parent.right
                                height:         zoomColumn.height + ScreenTools.defaultFontPixelHeight / 2
                                color:          qgcPal.window
                                border.color:   qgcPal.text
                                radius:         ScreenTools.defaultFontPixelWidth * 0.5

                                Column {
                                    id:                 zoomColumn
dogmaphobic's avatar
dogmaphobic committed
                                    anchors.margins:    ScreenTools.defaultFontPixelHeight * 0.25
Don Gagne's avatar
Don Gagne committed
                                    anchors.top:        parent.top
                                    anchors.left:       parent.left
                                    anchors.right:      parent.right
Don Gagne's avatar
Don Gagne committed

                                    QGCLabel {
Don Gagne's avatar
Don Gagne committed
                                        text:           qsTr("Min Zoom: %1").arg(sliderMinZoom.value)
Don Gagne's avatar
Don Gagne committed
                                        font.pointSize: _adjustableFontPointSize
                                    }

Don Gagne's avatar
Don Gagne committed
                                    Slider {
                                        id:                         sliderMinZoom
                                        anchors.left:               parent.left
                                        anchors.right:              parent.right
                                        height:                     setName.height
                                        minimumValue:               minZoomLevel
                                        maximumValue:               maxZoomLevel
                                        stepSize:                   1
                                        updateValueWhileDragging:   true
                                        property real _savedZoom
dogmaphobic's avatar
dogmaphobic committed
                                        Component.onCompleted:      Math.max(sliderMinZoom.value = _map.zoomLevel - 4, 2)
Don Gagne's avatar
Don Gagne committed
                                        onValueChanged: {
                                            if(sliderMinZoom.value > sliderMaxZoom.value) {
                                                sliderMaxZoom.value = sliderMinZoom.value
                                            }
                                            handleChanges()
                                        }
                                    } // Slider - min zoom

Don Gagne's avatar
Don Gagne committed
                                    QGCLabel {
dogmaphobic's avatar
dogmaphobic committed
                                        text:                       qsTr("Max Zoom: %1").arg(sliderMaxZoom.value)
                                        font.pointSize:             _adjustableFontPointSize
Don Gagne's avatar
Don Gagne committed
                                    }
Don Gagne's avatar
Don Gagne committed

                                    Slider {
                                        id:                         sliderMaxZoom
                                        anchors.left:               parent.left
                                        anchors.right:              parent.right
                                        height:                     setName.height
                                        minimumValue:               minZoomLevel
                                        maximumValue:               maxZoomLevel
                                        stepSize:                   1
                                        updateValueWhileDragging:   true
                                        property real _savedZoom
dogmaphobic's avatar
dogmaphobic committed
                                        Component.onCompleted:      Math.min(sliderMaxZoom.value = _map.zoomLevel + 2, 20)
Don Gagne's avatar
Don Gagne committed
                                        onValueChanged: {
                                            if(sliderMaxZoom.value < sliderMinZoom.value) {
                                                sliderMinZoom.value = sliderMaxZoom.value
                                            }
                                            handleChanges()
                                        }
                                    } // Slider - max zoom

                                    GridLayout {
                                        columns:    2
                                        rowSpacing: 0
                                        QGCLabel {
dogmaphobic's avatar
dogmaphobic committed
                                            text:           qsTr("Count:")
Don Gagne's avatar
Don Gagne committed
                                            font.pointSize: _adjustableFontPointSize
                                        }
                                        QGCLabel {
dogmaphobic's avatar
dogmaphobic committed
                                            text:            QGroundControl.mapEngineManager.tileCountStr
Don Gagne's avatar
Don Gagne committed
                                            font.pointSize: _adjustableFontPointSize
                                        }

                                        QGCLabel {
dogmaphobic's avatar
dogmaphobic committed
                                            text:           qsTr("Est Size:")
Don Gagne's avatar
Don Gagne committed
                                            font.pointSize: _adjustableFontPointSize
                                        }
                                        QGCLabel {
                                            text:           QGroundControl.mapEngineManager.tileSizeStr
                                            font.pointSize: _adjustableFontPointSize
                                        }
Don Gagne's avatar
Don Gagne committed
                                    }
Don Gagne's avatar
Don Gagne committed
                                } // Column - Zoom info
                            } // Rectangle - Zoom info
Don Gagne's avatar
Don Gagne committed

dogmaphobic's avatar
dogmaphobic committed
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
Don Gagne's avatar
Don Gagne committed
                                anchors.horizontalCenter: parent.horizontalCenter
dogmaphobic's avatar
dogmaphobic committed
                                QGCButton {
                                    text:       _tooManyTiles ? qsTr("Too many tiles") : qsTr("Download")
                                    enabled:    !_tooManyTiles && setName.text.length > 0
                                    property bool _tooManyTiles: QGroundControl.mapEngineManager.tileCount > _maxTilesForDownload
                                    onClicked: {
                                        if(QGroundControl.mapEngineManager.findName(setName.text)) {
                                            duplicateName.visible = true
                                        } else {
                                            QGroundControl.mapEngineManager.startDownload(setName.text, mapType);
                                            showList()
                                        }
                                    }
                                }
                                QGCButton {
                                    text: qsTr("Cancel")
                                    onClicked: {
Don Gagne's avatar
Don Gagne committed
                                        showList()
                                    }
Don Gagne's avatar
Don Gagne committed
                                }
Don Gagne's avatar
Don Gagne committed
                        } // Column
                    } // QGCFlickable
                } // Rectangle - Add new set dialog
            } // Item - Add new set view
Don Gagne's avatar
Don Gagne committed
        } // Map

        QGCFlickable {
            id:                 _tileSetList
            clip:               true
            anchors.margins:    ScreenTools.defaultFontPixelWidth
            anchors.top:        parent.top
            anchors.bottom:     _optionsButton.top
            anchors.left:       parent.left
            anchors.right:      parent.right
            contentHeight:      _cacheList.height

            Column {
                id:         _cacheList
                width:      Math.min(_tileSetList.width, (ScreenTools.defaultFontPixelWidth  * 50).toFixed(0))
                spacing:    ScreenTools.defaultFontPixelHeight * 0.5
                anchors.horizontalCenter: parent.horizontalCenter

                OfflineMapButton {
                    id:             firstButton
                    text:           qsTr("Add new set")
                    width:          _cacheList.width
                    height:         ScreenTools.defaultFontPixelHeight * 2
                    onClicked: {
                        offlineMapView._currentSelection = null
                        addNewSet()
Don Gagne's avatar
Don Gagne committed
                Repeater {
                    model: QGroundControl.mapEngineManager.tileSets
                    delegate: OfflineMapButton {
                        text:           object.name
                        size:           object.downloadStatus
dogmaphobic's avatar
dogmaphobic committed
                        tiles:          object.totalTileCount
Don Gagne's avatar
Don Gagne committed
                        complete:       object.complete
                        width:          firstButton.width
                        height:         ScreenTools.defaultFontPixelHeight * 2
                        onClicked: {
                            offlineMapView._currentSelection = object
                            showInfo()
Don Gagne's avatar
Don Gagne committed

        QGCButton {
            id:              _optionsButton
            text:            qsTr("Options")
            visible:         _tileSetList.visible
            anchors.bottom:  parent.bottom
            anchors.right:   parent.right
            anchors.margins: ScreenTools.defaultFontPixelWidth
            onClicked:       showDialog(optionsDialogComponent, qsTr("Offline Maps Options"), qgcView.showDialogDefaultWidth, StandardButton.Save | StandardButton.Cancel)
        }
    } // QGCViewPanel
} // QGCView