|
| 1 | +/*************************************************************************** |
| 2 | + qgsquickscalebar.qml |
| 3 | + -------------------------------------- |
| 4 | + Date : Nov 2017 |
| 5 | + Copyright : (C) 2017 by Peter Petrik |
| 6 | + Email : zilolv at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | +import QtQuick 2.7 |
| 16 | +import QtQuick.Controls 2.2 |
| 17 | +import QgsQuick 0.1 as QgsQuick |
| 18 | + |
| 19 | +Item { |
| 20 | + id: scaleBar |
| 21 | + |
| 22 | + /** |
| 23 | + * The mapSettings property contains configuration for rendering of the map. |
| 24 | + * |
| 25 | + * Must be connected before usage from QgsQuick.MapCanvas::mapSettings |
| 26 | + */ |
| 27 | + property alias mapSettings: scaleBarKit.mapSettings |
| 28 | + /** |
| 29 | + * Preferred width of scalebar in pixels on the screen. Defaults set to 300. |
| 30 | + * |
| 31 | + * barWidth is calculated to be close to preferred width, but has "nice" textual |
| 32 | + * representation |
| 33 | + */ |
| 34 | + property alias preferredWidth: scaleBarKit.preferredWidth |
| 35 | + /** |
| 36 | + * Kit for all calculation of width and text of the scalebar |
| 37 | + * |
| 38 | + * See also QgsQuickMapCanvasMap::mapSettings |
| 39 | + */ |
| 40 | + property QgsQuick.ScaleBarKit scaleBarKit: QgsQuick.ScaleBarKit { |
| 41 | + id: scaleBarKit |
| 42 | + } |
| 43 | + /** |
| 44 | + * Reserved text width. |
| 45 | + */ |
| 46 | + property int textWidth: fontMetrics.averageCharacterWidth * 8 |
| 47 | + /** |
| 48 | + * Text color |
| 49 | + */ |
| 50 | + property color barColor: "white" |
| 51 | + /** |
| 52 | + * Background color |
| 53 | + */ |
| 54 | + property color barBackgroundColor: "grey" |
| 55 | + /** |
| 56 | + * Opacity |
| 57 | + */ |
| 58 | + property double barOpacity: 0.8 |
| 59 | + /** |
| 60 | + * Textual representation of the bar length (e.g 800 m) |
| 61 | + */ |
| 62 | + property string barText: scaleBarKit.distance + " " + scaleBarKit.units |
| 63 | + /** |
| 64 | + * Calculated width of bar in pixels |
| 65 | + */ |
| 66 | + property int barWidth: scaleBarKit.width |
| 67 | + /** |
| 68 | + * Size of scalebar line (height of bar) |
| 69 | + */ |
| 70 | + property int lineWidth: 5 * QgsQuick.Utils.dp |
| 71 | + |
| 72 | + width: textWidth + barWidth |
| 73 | + |
| 74 | + MouseArea { |
| 75 | + anchors.fill: background |
| 76 | + onClicked: { |
| 77 | + animation.restart() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + NumberAnimation { |
| 82 | + id: animation |
| 83 | + target: scaleBar |
| 84 | + property: "barWidth" |
| 85 | + to: 200 |
| 86 | + duration: 1000 |
| 87 | + } |
| 88 | + |
| 89 | + Rectangle { |
| 90 | + id: background |
| 91 | + color: scaleBar.barBackgroundColor |
| 92 | + opacity: scaleBar.barOpacity |
| 93 | + width: parent.width |
| 94 | + height: parent.height |
| 95 | + } |
| 96 | + |
| 97 | + FontMetrics { |
| 98 | + id: fontMetrics |
| 99 | + font: text.font |
| 100 | + } |
| 101 | + |
| 102 | + Row { |
| 103 | + opacity: 1 |
| 104 | + spacing: 0 |
| 105 | + |
| 106 | + Text { |
| 107 | + id: text |
| 108 | + width: textWidth |
| 109 | + height: scaleBar.height |
| 110 | + text: barText |
| 111 | + color: barColor |
| 112 | + font.pixelSize: scaleBar.height - 2 * scaleBar.lineWidth |
| 113 | + horizontalAlignment: Text.AlignHCenter |
| 114 | + verticalAlignment: Text.AlignVCenter |
| 115 | + } |
| 116 | + |
| 117 | + Rectangle { |
| 118 | + id: leftBar |
| 119 | + width: scaleBar.lineWidth |
| 120 | + height: scaleBar.height - 20 * QgsQuick.Utils.dp |
| 121 | + y: (scaleBar.height - leftBar.height) / 2 |
| 122 | + color: barColor |
| 123 | + opacity: 1 |
| 124 | + } |
| 125 | + |
| 126 | + Rectangle { |
| 127 | + width: scaleBar.width - text.width - 15 * QgsQuick.Utils.dp |
| 128 | + height: scaleBar.lineWidth |
| 129 | + y: (scaleBar.height - scaleBar.lineWidth) / 2 |
| 130 | + color: barColor |
| 131 | + } |
| 132 | + |
| 133 | + Rectangle { |
| 134 | + id: rightBar |
| 135 | + width: scaleBar.lineWidth |
| 136 | + height: scaleBar.height - 20 * QgsQuick.Utils.dp |
| 137 | + y: (scaleBar.height - leftBar.height) / 2 |
| 138 | + color: barColor |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments