Skip to content
Permalink
Browse files
Readd utils
  • Loading branch information
m-kuhn committed Jun 5, 2021
1 parent 5cd4617 commit 9427b380300fd5a72b716699b40f75cec23dfd17
@@ -5,13 +5,15 @@ set(QGIS_QUICK_GUI_MOC_HDRS
qgsquickmapcanvasmap.h
qgsquickmapsettings.h
qgsquickmaptransform.h
qgsquickutils.h
)

set(QGIS_QUICK_GUI_SRC
qgsquickcoordinatetransformer.cpp
qgsquickmapcanvasmap.cpp
qgsquickmapsettings.cpp
qgsquickmaptransform.cpp
qgsquickutils.cpp
)

include_directories(
@@ -27,13 +27,22 @@
#include "qgsproject.h"
#include "qgsrelationmanager.h"
#include "qgscoordinatetransformcontext.h"
#include "qgscoordinateformatter.h"
#include "qgsvectorlayer.h"
#include "qgsunittypes.h"

#include "qgsquickmapcanvasmap.h"
#include "qgsquickmapsettings.h"
#include "qgsquickmaptransform.h"
#include "qgsquickplugin.h"
#include "qgsquickutils.h"

static QObject *_utilsProvider( QQmlEngine *engine, QJSEngine *scriptEngine )
{
Q_UNUSED( engine )
Q_UNUSED( scriptEngine )
return new QgsQuickUtils(); // the object will be owned by QML engine and destroyed by the engine on exit
}

void QgsQuickPlugin::registerTypes( const char *uri )
{
@@ -57,5 +66,7 @@ void QgsQuickPlugin::registerTypes( const char *uri )
qmlRegisterType< QgsQuickMapSettings >( uri, 0, 1, "MapSettings" );
qmlRegisterType< QgsQuickMapTransform >( uri, 0, 1, "MapTransform" );
qmlRegisterType< QgsVectorLayer >( uri, 0, 1, "VectorLayer" );

qmlRegisterSingletonType< QgsQuickUtils >( uri, 0, 1, "Utils", _utilsProvider );
}

@@ -0,0 +1,49 @@
/***************************************************************************
qgsquickutils.cpp
--------------------------------------
Date : Nov 2017
Copyright : (C) 2017 by Peter Petrik
Email : zilolv at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QGuiApplication>
#include <QScreen>
#include <QString>
#include <QWindow>

#include "qgsquickmapsettings.h"
#include "qgsquickutils.h"

QgsQuickUtils::QgsQuickUtils( QObject *parent )
: QObject( parent )
, mScreenDensity( calculateScreenDensity() )
{
}

qreal QgsQuickUtils::screenDensity() const
{
return mScreenDensity;
}

qreal QgsQuickUtils::calculateScreenDensity()
{
// calculate screen density for calculation of real pixel sizes from density-independent pixels
// take the first top level window
double dpi = 96.0;
const QWindowList windows = QGuiApplication::topLevelWindows();
if ( !windows.isEmpty() )
{
QScreen *screen = windows.at( 0 )->screen();
double dpiX = screen->physicalDotsPerInchX();
double dpiY = screen->physicalDotsPerInchY();
dpi = dpiX < dpiY ? dpiX : dpiY; // In case of asymmetrical DPI. Improbable
}
return dpi / 160.; // 160 DPI is baseline for density-independent pixels in Android
}
@@ -0,0 +1,84 @@
/***************************************************************************
qgsquickutils.h
--------------------------------------
Date : Nov 2017
Copyright : (C) 2017 by Peter Petrik
Email : zilolv at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSQUICKUTILS_H
#define QGSQUICKUTILS_H


#include <QObject>
#include <QString>
#include <QUrl>
#include <QtPositioning/QGeoCoordinate>
#include <QModelIndex>

#include <limits>

#include "qgis.h"
#include "qgsexpressioncontextutils.h"
#include "qgsmessagelog.h"
#include "qgspoint.h"
#include "qgspointxy.h"
#include "qgsunittypes.h"
#include "qgsquickmapsettings.h"
#include "qgis_quick.h"
#include "qgscoordinateformatter.h"


class QgsFeature;
class QgsVectorLayer;
class QgsCoordinateReferenceSystem;

/**
* \ingroup quick
*
* \brief Encapsulating the common utilities for QgsQuick library.
*
* \note QML Type: Utils (Singleton)
*
* \since QGIS 3.2
*/
class QUICK_EXPORT QgsQuickUtils: public QObject
{
Q_OBJECT

/**
* "dp" is useful for building building components that work well with different screen densities.
* It stands for density-independent pixels. A width of 10dp is going to be the same physical size
* on all screens regardless their density. In QML code, all values are specified in screen pixels,
* so in order to set a width of 10dp, one would use the following code: "width: 10 * QgsQuick.Utils.dp"
*
* 1dp is approximately 0.16mm. When screen has 160 DPI (baseline), the value of "dp" is 1.
* On high DPI screen the value will be greater, e.g. 1.5.
*
* This is a readonly property.
*/
Q_PROPERTY( qreal dp READ screenDensity CONSTANT )

public:
//! Create new utilities
QgsQuickUtils( QObject *parent = nullptr );
//! Destructor
~QgsQuickUtils() = default;

//! \copydoc QgsQuickUtils::dp
qreal screenDensity() const;

private:
static qreal calculateScreenDensity();

qreal mScreenDensity;
};

#endif // QGSQUICKUTILS_H
@@ -57,6 +57,10 @@ macro (ADD_QGIS_TEST testname testsrc)
add_test(qgis_${testname} ${CMAKE_CURRENT_BINARY_DIR}/../../../output/bin/qgis_${testname} -maxwarnings 10000)
endmacro (ADD_QGIS_TEST)

#############################################################
# Tests:

ADD_QGIS_TEST(qgsquickutils testqgsquickutils.cpp)

#############################################################
# Add also test application
@@ -0,0 +1,49 @@
/***************************************************************************
testqgsquickutils.cpp
--------------------------------------
Date : Nov 2017
Copyright : (C) 2017 by Peter Petrik
Email : zilolv at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QObject>
#include <QApplication>
#include <QDesktopWidget>

#include "qgsapplication.h"
#include "qgscoordinatereferencesystem.h"
#include "qgscoordinatetransformcontext.h"
#include "qgspoint.h"
#include "qgspointxy.h"
#include "qgstest.h"
#include "qgis.h"
#include "qgsunittypes.h"

#include "qgsquickutils.h"

class TestQgsQuickUtils: public QObject
{
Q_OBJECT
private slots:
void init() {} // will be called before each testfunction is executed.
void cleanup() {} // will be called after every testfunction.

void screen_density();
private:
QgsQuickUtils utils;
};

void TestQgsQuickUtils::screen_density()
{
qreal dp = utils.screenDensity();
QVERIFY( ( dp > 0 ) && ( dp < 1000 ) );
}

QGSTEST_MAIN( TestQgsQuickUtils )
#include "testqgsquickutils.moc"

0 comments on commit 9427b38

Please sign in to comment.