QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
QgsQuickScaleBarKit::mapSettingsChanged
void mapSettingsChanged()
Associated map settings.
QgsQuickScaleBarKit::width
int width
Calculated width of scalebar in pixels representing distance + units.
Definition: qgsquickscalebarkit.h:82
QgsQuickMapSettings::extentChanged
void extentChanged()
Geographical coordinates of the rectangle that should be rendered.
QgsQuickMapSettings::destinationCrsChanged
void destinationCrsChanged()
CRS of destination coordinate reference system.
QgsUnitTypes::toAbbreviatedString
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
Definition: qgsunittypes.cpp:269
QgsQuickScaleBarKit::units
QString units
Units of distance (e.g.
Definition: qgsquickscalebarkit.h:69
qgsunittypes.h
QgsQuickScaleBarKit::QgsQuickScaleBarKit
QgsQuickScaleBarKit(QObject *parent=nullptr)
create new scale bar kit
Definition: qgsquickscalebarkit.cpp:27
QgsQuickScaleBarKit::mapSettings
QgsQuickMapSettings mapSettings
Associated map settings.
Definition: qgsquickscalebarkit.h:54
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
QgsQuickMapSettings
Definition: qgsquickmapsettings.h:46
QgsQuickMapSettings::outputDpiChanged
void outputDpiChanged()
Output DPI used for conversion between real world units (e.g.
QgsQuickScaleBarKit::preferredWidthChanged
void preferredWidthChanged()
Preferred width of scalebar in pixels.
QgsQuickScaleBarKit::updateScaleBar
void updateScaleBar()
recalculate width, distance and units.
Definition: qgsquickscalebarkit.cpp:80
QgsQuickScaleBarKit::setMapSettings
void setMapSettings(QgsQuickMapSettings *mapSettings)
Associated map settings.
Definition: qgsquickscalebarkit.cpp:38
QgsUnitTypes::DistanceMeters
@ DistanceMeters
Meters.
Definition: qgsunittypes.h:69
QgsQuickMapSettings::mapUnitsPerPixelChanged
void mapUnitsPerPixelChanged()
Returns the distance in geographical coordinates that equals to one pixel in the map.
QgsQuickScaleBarKit::scaleBarChanged
void scaleBarChanged()
width, distance and/or units changed
QgsQuickMapSettings::outputSizeChanged
void outputSizeChanged()
The size of the resulting map image.
QgsQuickUtils
Definition: qgsquickutils.h:52
QgsQuickUtils::screenUnitsToMeters
static Q_INVOKABLE double screenUnitsToMeters(QgsQuickMapSettings *mapSettings, int baseLengthPixels)
Calculates the distance in meter representing baseLengthPixels pixels on the screen based on the curr...
Definition: qgsquickutils.cpp:75
qgsquickutils.h
qgsdistancearea.h
qgsquickmapsettings.h
QgsQuickMapSettings::visibleExtentChanged
void visibleExtentChanged()
Returns the actual extent derived from requested extent that takes takes output image size into accou...
qgsquickscalebarkit.h
qgspointxy.h
QgsQuickUtils::humanReadableDistance
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.
Definition: qgsquickutils.cpp:204
QgsQuickScaleBarKit::distance
int distance
Distance rounded to "nice" number (e.g.
Definition: qgsquickscalebarkit.h:76