QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
16 #include <QGuiApplication>
17 #include <QScreen>
18 #include <QString>
19 #include <QWindow>
20 
21 #include "qgsquickmapsettings.h"
22 #include "qgsquickutils.h"
23 
24 QgsQuickUtils::QgsQuickUtils( QObject *parent )
25  : QObject( parent )
26  , mScreenDensity( calculateScreenDensity() )
27 {
28 }
29 
31 {
32  return mScreenDensity;
33 }
34 
35 qreal QgsQuickUtils::calculateScreenDensity()
36 {
37  // calculate screen density for calculation of real pixel sizes from density-independent pixels
38  // take the first top level window
39  double dpi = 96.0;
40  const QWindowList windows = QGuiApplication::topLevelWindows();
41  if ( !windows.isEmpty() )
42  {
43  QScreen *screen = windows.at( 0 )->screen();
44  double dpiX = screen->physicalDotsPerInchX();
45  double dpiY = screen->physicalDotsPerInchY();
46  dpi = dpiX < dpiY ? dpiX : dpiY; // In case of asymmetrical DPI. Improbable
47  }
48  return dpi / 160.; // 160 DPI is baseline for density-independent pixels in Android
49 }
qreal screenDensity() const
"dp" is useful for building building components that work well with different screen densities.
QgsQuickUtils(QObject *parent=nullptr)
Create new utilities.