QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsquickutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsquickutils.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 #include <QDesktopWidget>
16 #include <QString>
17 
18 #include "qgis.h"
19 #include "qgsdistancearea.h"
20 #include "qgslogger.h"
21 
22 #include "qgsquickmapsettings.h"
23 #include "qgsquickutils.h"
24 #include "qgsunittypes.h"
25 
26 
27 QgsQuickUtils::QgsQuickUtils( QObject *parent )
28  : QObject( parent )
29  , mScreenDensity( calculateScreenDensity() )
30 {
31 }
32 
33 double QgsQuickUtils::screenUnitsToMeters( QgsQuickMapSettings *mapSettings, int baseLengthPixels ) const
34 {
35  if ( mapSettings == nullptr ) return 0.0;
36 
37  QgsDistanceArea mDistanceArea;
38  mDistanceArea.setEllipsoid( QStringLiteral( "WGS84" ) );
39  mDistanceArea.setSourceCrs( mapSettings->destinationCrs(), mapSettings->transformContext() );
40 
41  // calculate the geographic distance from the central point of extent
42  // to the specified number of points on the right side
43  QSize s = mapSettings->outputSize();
44  QPoint pointCenter( s.width() / 2, s.height() / 2 );
45  QgsPointXY p1 = mapSettings->screenToCoordinate( pointCenter );
46  QgsPointXY p2 = mapSettings->screenToCoordinate( pointCenter + QPoint( baseLengthPixels, 0 ) );
47  return mDistanceArea.measureLine( p1, p2 );
48 }
49 
50 void QgsQuickUtils::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
51 {
52  QgsMessageLog::logMessage( message, tag, level );
53 }
54 
56 {
57  QRect rec = QApplication::desktop()->screenGeometry();
58  int dpiX = QApplication::desktop()->physicalDpiX();
59  int dpiY = QApplication::desktop()->physicalDpiY();
60  int height = rec.height();
61  int width = rec.width();
62  double sizeX = static_cast<double>( width ) / dpiX * 25.4;
63  double sizeY = static_cast<double>( height ) / dpiY * 25.4;
64 
65  QString msg;
66  msg += tr( "screen resolution: %1x%2 px\n" ).arg( width ).arg( height );
67  msg += tr( "screen DPI: %1x%2\n" ).arg( dpiX ).arg( dpiY );
68  msg += tr( "screen size: %1x%2 mm\n" ).arg( QString::number( sizeX, 'f', 0 ), QString::number( sizeY, 'f', 0 ) );
69  msg += tr( "screen density: %1" ).arg( mScreenDensity );
70  return msg;
71 }
72 
74 {
75  return mScreenDensity;
76 }
77 
78 qreal QgsQuickUtils::calculateScreenDensity()
79 {
80  // calculate screen density for calculation of real pixel sizes from density-independent pixels
81  int dpiX = QApplication::desktop()->physicalDpiX();
82  int dpiY = QApplication::desktop()->physicalDpiY();
83  int dpi = dpiX < dpiY ? dpiX : dpiY; // In case of asymmetrical DPI. Improbable
84  return dpi / 160.; // 160 DPI is baseline for density-independent pixels in Android
85 }
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:78
The QgsQuickMapSettings class encapsulates QgsMapSettings class to offer settings of configuration of...
qreal screenDensity() const
"dp" is useful for building building components that work well with different screen densities...
Q_INVOKABLE QgsPoint screenToCoordinate(const QPointF &point) const
Convert a screen coordinate to a map coordinate.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Q_INVOKABLE double screenUnitsToMeters(QgsQuickMapSettings *mapSettings, int baseLengthPixels) const
Calculate the distance in meter representing baseLengthPixels pixels on the screen based on the curre...
QgsQuickUtils(QObject *parent=nullptr)
Create new utilities.
QgsCoordinateReferenceSystem destinationCrs
CRS of destination coordinate reference system.
QSize outputSize
The size of the resulting map image.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
QString dumpScreenInfo() const
Returns a string with information about screen size and resolution.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
Q_INVOKABLE QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
Q_INVOKABLE void logMessage(const QString &message, const QString &tag=QString("QgsQuick"), Qgis::MessageLevel level=Qgis::Warning)
Log message in QgsMessageLog.