QGIS API Documentation  3.2.0-Bonn (bc43194)
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 dist = QgsQuickUtils().screenUnitsToMeters( mMapSettings, mPreferredWidth ); // meters
86  if ( dist > 1000.0 )
87  {
88  dist = dist / 1000.0; // meters to kilometers
90  }
91  else
92  {
94  }
95 
96  // we want to show nice round distances e.g. 200 km instead of e.g. 273 km
97  // so we determine which "nice" number to use and also update the scale bar
98  // length accordingly. First digit will be 1, 2 or 5, the rest will be zeroes.
99  int digits = int( floor( log10( ( dist ) ) ) ); // number of digits after first one
100  double base = pow( 10, digits ); // e.g. for 1234 this will be 1000
101  double first_digit = dist / base; // get the first digit
102  int round_digit;
103  if ( first_digit < 2 )
104  round_digit = 1;
105  else if ( first_digit < 5 )
106  round_digit = 2;
107  else
108  round_digit = 5;
109 
110  mDistance = int( round_digit * base );
111  mWidth = int( mPreferredWidth * mDistance / dist );
112 
113  emit scaleBarChanged();
114 }
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:40
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...
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.
Q_INVOKABLE double screenUnitsToMeters(QgsQuickMapSettings *mapSettings, int baseLengthPixels) const
Calculate the distance in meter representing baseLengthPixels pixels on the screen based on the curre...
void destinationCrsChanged()
CRS of destination coordinate reference system.
QgsQuickMapSettings mapSettings
Associated map settings.
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