QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsquickscalebarkit.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsquickscalebarkit.cpp
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 
16 #include <QSize>
17 #include <QPoint>
18 
19 #include "qgsdistancearea.h"
20 #include "qgspointxy.h"
21 
22 #include "qgsquickmapsettings.h"
23 #include "qgsquickscalebarkit.h"
24 #include "qgsquickutils.h"
25 #include "qgsunittypes.h"
26 
28  : QObject( parent )
29  , mPreferredWidth( 300 )
30  , mWidth( mPreferredWidth )
31  , mDistance( 0 )
32  , mUnits( "" )
33 {
36 }
37 
39 {
40  if ( mMapSettings == mapSettings )
41  return;
42 
43  // If we have already something connected, disconnect it!
44  if ( mMapSettings )
45  {
46  disconnect( mMapSettings, nullptr, this, nullptr );
47  }
48 
49  mMapSettings = mapSettings;
50 
51  // Connect all signals to change scale bar when needed!
52  if ( mMapSettings )
53  {
60  }
61 
62  emit mapSettingsChanged();
63 }
64 
66 {
67  return mWidth;
68 }
69 
70 QString QgsQuickScaleBarKit::units() const
71 {
72  return mUnits;
73 }
74 
76 {
77  return mDistance;
78 }
79 
81 {
82  if ( !mMapSettings )
83  return;
84 
85  double distInMeters = QgsQuickUtils().screenUnitsToMeters( mMapSettings, mPreferredWidth ); // meters
86  double dist;
89  mSystemOfMeasurement,
90  dist, distUnits );
91 
92  mUnits = QgsUnitTypes::toAbbreviatedString( distUnits );
93 
94  // we want to show nice round distances e.g. 200 km instead of e.g. 273 km
95  // so we determine which "nice" number to use and also update the scale bar
96  // length accordingly. First digit will be 1, 2 or 5, the rest will be zeroes.
97  int digits = int( floor( log10( ( dist ) ) ) ); // number of digits after first one
98  double base = pow( 10, digits ); // e.g. for 1234 this will be 1000
99  double first_digit = dist / base; // get the first digit
100  int round_digit;
101  if ( first_digit < 2 )
102  round_digit = 1;
103  else if ( first_digit < 5 )
104  round_digit = 2;
105  else
106  round_digit = 5;
107 
108  mDistance = int( round_digit * base );
109  mWidth = int( mPreferredWidth * mDistance / dist );
110 
111  emit scaleBarChanged();
112 }
void visibleExtentChanged()
Returns the actual extent derived from requested extent that takes takes output image size into accou...
Encapsulating the common utilies for QgsQuick library.
Definition: qgsquickutils.h:51
void extentChanged()
Geographical coordinates of the rectangle that should be rendered.
void preferredWidthChanged()
Preferred width of scalebar in pixels.
void setMapSettings(QgsQuickMapSettings *mapSettings)
Associated map settings.
QString units() const
Units of distance (e.g.
The QgsQuickMapSettings class encapsulates QgsMapSettings class to offer settings of configuration of...
static Q_INVOKABLE double screenUnitsToMeters(QgsQuickMapSettings *mapSettings, int baseLengthPixels)
Calculates the distance in meter representing baseLengthPixels pixels on the screen based on the curr...
void mapSettingsChanged()
Associated map settings.
void outputDpiChanged()
Output DPI used for conversion between real world units (e.g.
void mapUnitsPerPixelChanged()
Returns the distance in geographical coordinates that equals to one pixel in the map.
int distance() const
Units of distance (e.g.
void updateScaleBar()
recalculate width, distance and units.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
static void humanReadableDistance(double srcDistance, QgsUnitTypes::DistanceUnit srcUnits, QgsUnitTypes::SystemOfMeasurement destSystem, double &destDistance, QgsUnitTypes::DistanceUnit &destUnits)
Converts distance to human readable distance in destination system of measurement.
void destinationCrsChanged()
CRS of destination coordinate reference system.
QgsQuickMapSettings mapSettings
Associated map settings.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:54
void outputSizeChanged()
The size of the resulting map image.
int width() const
Calculated width of scalebar in pixels representing distance + units.
void scaleBarChanged()
width, distance and/or units changed
QgsQuickScaleBarKit(QObject *parent=nullptr)
create new scale bar kit