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 ***************************************************************************/
15import QtQuick 2.7
16import QtQuick.Controls 2.2
17import QgsQuick 0.1 as QgsQuick
18
19Item {
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   * Preferred system of measurement for the resulting distance. Default is metric system
37   */
38  property alias systemOfMeasurement: scaleBarKit.systemOfMeasurement
39  /**
40   * Kit for all calculation of width and text of the scalebar
41   *
42   * See also QgsQuickMapCanvasMap::mapSettings
43   */
44  property QgsQuick.ScaleBarKit scaleBarKit: QgsQuick.ScaleBarKit {
45    id: scaleBarKit
46  }
47  /**
48   * Reserved text width.
49   */
50  property int textWidth: fontMetrics.averageCharacterWidth * 8
51  /**
52   * Text color
53   */
54  property color barColor: "white"
55  /**
56   * Background color
57   */
58  property color barBackgroundColor: "grey"
59  /**
60   * Opacity
61   */
62  property double barOpacity: 0.8
63  /**
64   * Textual representation of the bar length (e.g 800 m)
65   */
66  property string barText: scaleBarKit.distance + " " + scaleBarKit.units
67  /**
68   * Calculated width of bar in pixels
69   */
70  property int barWidth: scaleBarKit.width
71  /**
72   * Size of scalebar line (height of bar)
73   */
74  property int lineWidth: 5 * QgsQuick.Utils.dp
75
76  width: textWidth + barWidth
77
78  MouseArea {
79    anchors.fill: background
80    onClicked: {
81      animation.restart()
82    }
83  }
84
85  NumberAnimation {
86    id: animation
87    target: scaleBar
88    property: "barWidth"
89    to: 200
90    duration: 1000
91  }
92
93  Rectangle {
94    id: background
95    color: scaleBar.barBackgroundColor
96    opacity: scaleBar.barOpacity
97    width: parent.width
98    height: parent.height
99  }
100
101  FontMetrics {
102    id: fontMetrics
103    font: text.font
104  }
105
106  Row {
107    opacity: 1
108    spacing: 0
109
110    Text {
111      id: text
112      width: textWidth
113      height: scaleBar.height
114      text: barText
115      color: barColor
116      font.pixelSize: scaleBar.height - 2 * scaleBar.lineWidth
117      horizontalAlignment: Text.AlignHCenter
118      verticalAlignment: Text.AlignVCenter
119    }
120
121    Rectangle {
122      id: leftBar
123      width: scaleBar.lineWidth
124      height: scaleBar.height - 20 * QgsQuick.Utils.dp
125      y: (scaleBar.height - leftBar.height) / 2
126      color: barColor
127      opacity: 1
128    }
129
130    Rectangle {
131      width: scaleBar.width - text.width - 15 * QgsQuick.Utils.dp
132      height: scaleBar.lineWidth
133      y: (scaleBar.height - scaleBar.lineWidth) / 2
134      color: barColor
135    }
136
137    Rectangle {
138      id: rightBar
139      width: scaleBar.lineWidth
140      height: scaleBar.height - 20 * QgsQuick.Utils.dp
141      y: (scaleBar.height - leftBar.height) / 2
142      color: barColor
143    }
144  }
145}
146