Skip to content
Permalink
Browse files
Merge pull request #44132 from m-kuhn/respect_screen_dpi
Allow using physical DPI for map canvas
  • Loading branch information
m-kuhn committed Jul 14, 2021
2 parents c09443a + 598e2bd commit 071e4b585dbd56351636e4b22198084e46cb0a71
@@ -46,6 +46,11 @@ The ``searchChildRegistries`` parameter specifies if child registries should be
void addSubRegistry( const QgsSettingsRegistry *settingsRegistry );
%Docstring
Append a child ``settingsRegistry`` to the register.
%End

void removeSubRegistry( const QgsSettingsRegistry *settingsRegistry );
%Docstring
Remove a child ``settingsRegistry`` from the register.
%End

QList<const QgsSettingsRegistry *> subRegistries() const;
@@ -38,6 +38,13 @@ Returns a pointer to the singleton instance.
%End


static QgsSettingsRegistryGui *settingsRegistryGui() /KeepReference/;
%Docstring
Returns the gui's settings registry, used for managing gui settings.

.. versionadded:: 3.22
%End

static QgsEditorWidgetRegistry *editorWidgetRegistry() /KeepReference/;
%Docstring
Returns the global editor widget registry, used for managing all known edit widget factories.
@@ -1314,6 +1314,8 @@ called when panning is in action, reset indicates end of panning
virtual void dropEvent( QDropEvent *event );


virtual void showEvent( QShowEvent *event );




@@ -0,0 +1,42 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/settings/qgssettingsregistrygui.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsSettingsRegistryGui : QgsSettingsRegistry
{
%Docstring(signature="appended")
:py:class:`QgsSettingsRegistryGui` is used for settings introspection and collects all
:py:class:`QgsSettingsEntry` instances of gui.

.. versionadded:: 3.22
%End

%TypeHeaderCode
#include "qgssettingsregistrygui.h"
%End
public:

QgsSettingsRegistryGui();
%Docstring
Constructor for QgsSettingsRegistryGui.
%End

virtual ~QgsSettingsRegistryGui();


};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/settings/qgssettingsregistrygui.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -425,6 +425,7 @@
%Include auto_generated/symbology/qgssymbolslistwidget.sip
%Include auto_generated/symbology/qgssymbolwidgetcontext.sip
%Include auto_generated/symbology/qgsvectorfieldsymbollayerwidget.sip
%Include auto_generated/settings/qgssettingsregistrygui.sip
%Include auto_generated/tableeditor/qgstableeditordialog.sip
%Include auto_generated/tableeditor/qgstableeditorwidget.sip
%Include auto_generated/editorwidgets/qgsqmlwidgetwrapper.sip
@@ -55,6 +55,7 @@
#include "qgsclipboard.h"
#include "qgssettings.h"
#include "qgssettingsregistrycore.h"
#include "qgssettingsregistrygui.h"
#include "qgsoptionswidgetfactory.h"
#include "qgslocatorwidget.h"
#include "qgslocatoroptionswidget.h"
@@ -740,6 +741,8 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
mMapTipsDelaySpinBox->setValue( mSettings->value( QStringLiteral( "qgis/mapTipsDelay" ), 850 ).toInt() );
mMapTipsDelaySpinBox->setClearValue( 850 );

mRespectScreenDpiCheckBox->setChecked( QgsSettingsRegistryGui::settingsRespectScreenDPI.value() );

//
// Raster properties
//
@@ -1606,6 +1609,8 @@ void QgsOptions::saveOptions()

mSettings->setValue( QStringLiteral( "/qgis/defaultLegendGraphicResolution" ), mLegendGraphicResolutionSpinBox->value() );
mSettings->setValue( QStringLiteral( "/qgis/mapTipsDelay" ), mMapTipsDelaySpinBox->value() );
QgsSettingsRegistryGui::settingsRespectScreenDPI.setValue( mRespectScreenDpiCheckBox->isChecked() );

mSettings->setEnumValue( QStringLiteral( "/qgis/copyFeatureFormat" ), ( QgsClipboard::CopyFormat )mComboCopyFeatureFormat->currentData().toInt() );
QgisApp::instance()->setMapTipsDelay( mMapTipsDelaySpinBox->value() );

@@ -98,6 +98,23 @@ void QgsSettingsRegistry::addSubRegistry( const QgsSettingsRegistry *settingsReg
mSettingsRegistryChildList.append( settingsRegistry );
}

void QgsSettingsRegistry::removeSubRegistry( const QgsSettingsRegistry *settingsRegistry )
{
if ( !settingsRegistry )
{
QgsDebugMsg( QStringLiteral( "Trying to unregister a nullptr child settings registry." ) );
return;
}

if ( mSettingsRegistryChildList.contains( settingsRegistry ) )
{
QgsDebugMsg( QStringLiteral( "Child register is not registered." ) );
return;
}

mSettingsRegistryChildList.removeAll( settingsRegistry );
}

QList<const QgsSettingsRegistry *> QgsSettingsRegistry::subRegistries() const
{
return mSettingsRegistryChildList;
@@ -62,6 +62,11 @@ class CORE_EXPORT QgsSettingsRegistry
*/
void addSubRegistry( const QgsSettingsRegistry *settingsRegistry );

/**
* Remove a child \a settingsRegistry from the register.
*/
void removeSubRegistry( const QgsSettingsRegistry *settingsRegistry );

/**
* Returns the list of registered child QgsSettingsRegistry.
*/
@@ -368,6 +368,8 @@ set(QGIS_GUI_SRCS
providers/ogr/qgsogritemguiprovider.cpp
providers/ogr/qgsgeopackageprojectstorageguiprovider.cpp

settings/qgssettingsregistrygui.cpp

tableeditor/qgstableeditordialog.cpp
tableeditor/qgstableeditorformattingwidget.cpp
tableeditor/qgstableeditorwidget.cpp
@@ -1241,6 +1243,8 @@ set(QGIS_GUI_HDRS
symbology/qgssymbolwidgetcontext.h
symbology/qgsvectorfieldsymbollayerwidget.h

settings/qgssettingsregistrygui.h

tableeditor/qgstableeditordialog.h
tableeditor/qgstableeditorformattingwidget.h
tableeditor/qgstableeditorwidget.h
@@ -1423,9 +1427,10 @@ target_include_directories(qgis_gui PUBLIC
${CMAKE_SOURCE_DIR}/src/gui/providers/ogr
${CMAKE_SOURCE_DIR}/src/gui/pointcloud
${CMAKE_SOURCE_DIR}/src/gui/raster
${CMAKE_SOURCE_DIR}/src/gui/settings
${CMAKE_SOURCE_DIR}/src/gui/tableeditor
${CMAKE_SOURCE_DIR}/src/gui/vector
${CMAKE_SOURCE_DIR}/src/gui/vectortile
${CMAKE_SOURCE_DIR}/src/gui/tableeditor

${CMAKE_BINARY_DIR}/src/gui
${CMAKE_BINARY_DIR}/src/ui
@@ -61,6 +61,7 @@
#include "qgssubsetstringeditorproviderregistry.h"
#include "qgsprovidersourcewidgetproviderregistry.h"
#include "qgsrelationwidgetregistry.h"
#include "qgssettingsregistrygui.h"

QgsGui *QgsGui::instance()
{
@@ -73,6 +74,11 @@ QgsNative *QgsGui::nativePlatformInterface()
return instance()->mNative;
}

QgsSettingsRegistryGui *QgsGui::settingsRegistryGui()
{
return instance()->mSettingsRegistryGui;
}

QgsEditorWidgetRegistry *QgsGui::editorWidgetRegistry()
{
return instance()->mEditorWidgetRegistry;
@@ -204,6 +210,7 @@ QgsGui::~QgsGui()
delete mSubsetStringEditorProviderRegistry;
delete mProviderSourceWidgetProviderRegistry;
delete mRelationEditorRegistry;
delete mSettingsRegistryGui;
}

QColor QgsGui::sampleColor( QPoint point )
@@ -249,6 +256,8 @@ QgsGui::QgsGui()
mNative = new QgsNative();
#endif

mSettingsRegistryGui = new QgsSettingsRegistryGui();

mCodeEditorColorSchemeRegistry = new QgsCodeEditorColorSchemeRegistry();

// provider gui registry initialize QgsProviderRegistry too
@@ -23,6 +23,7 @@
#include <QWidget>
#include <memory>

class QgsSettingsRegistryGui;
class QgsEditorWidgetRegistry;
class QgsShortcutsManager;
class QgsLayerTreeEmbeddedWidgetRegistry;
@@ -84,6 +85,12 @@ class GUI_EXPORT QgsGui : public QObject
*/
SIP_SKIP static QgsNative *nativePlatformInterface();

/**
* Returns the gui's settings registry, used for managing gui settings.
* \since QGIS 3.22
*/
static QgsSettingsRegistryGui *settingsRegistryGui() SIP_KEEPREFERENCE;

/**
* Returns the global editor widget registry, used for managing all known edit widget factories.
*/
@@ -261,6 +268,7 @@ class GUI_EXPORT QgsGui : public QObject

QgsGui();

QgsSettingsRegistryGui *mSettingsRegistryGui = nullptr;
QgsProviderGuiRegistry *mProviderGuiRegistry = nullptr;
QgsWidgetStateHelper *mWidgetStateHelper = nullptr;
QgsNative *mNative = nullptr;
@@ -91,6 +91,7 @@ email : sherman at mrcc.com
#include "qgscoordinatereferencesystemregistry.h"
#include "qgslabelingresults.h"
#include "qgsmaplayerutils.h"
#include "qgssettingsregistrygui.h"

/**
* \ingroup gui
@@ -205,19 +206,12 @@ QgsMapCanvas::QgsMapCanvas( QWidget *parent )

QSize s = viewport()->size();
mSettings.setOutputSize( s );
mSettings.setDevicePixelRatio( devicePixelRatio() );

setSceneRect( 0, 0, s.width(), s.height() );
mScene->setSceneRect( QRectF( 0, 0, s.width(), s.height() ) );

moveCanvasContents( true );

// keep device pixel ratio up to date on screen or resolution change
if ( window()->windowHandle() )
{
connect( window()->windowHandle(), &QWindow::screenChanged, this, [ = ]( QScreen * ) {mSettings.setDevicePixelRatio( devicePixelRatio() );} );
connect( window()->windowHandle()->screen(), &QScreen::physicalDotsPerInchChanged, this, [ = ]( qreal ) {mSettings.setDevicePixelRatio( devicePixelRatio() );} );
}

connect( &mMapUpdateTimer, &QTimer::timeout, this, &QgsMapCanvas::mapUpdateTimeout );
mMapUpdateTimer.setInterval( 250 );

@@ -245,7 +239,6 @@ QgsMapCanvas::QgsMapCanvas( QWidget *parent )
setCanvasColor( mSettings.backgroundColor() );

setTemporalRange( mSettings.temporalRange() );

refresh();
}

@@ -968,6 +961,22 @@ void QgsMapCanvas::showContextMenu( QgsMapMouseEvent *event )
menu.exec( event->globalPos() );
}

void QgsMapCanvas::updateDevicePixelFromScreen()
{
mSettings.setDevicePixelRatio( devicePixelRatio() );
// TODO: QGIS 4 -> always respect screen dpi
if ( QgsSettingsRegistryGui::settingsRespectScreenDPI.value() )
{
if ( window()->windowHandle() )
mSettings.setOutputDpi( window()->windowHandle()->screen()->physicalDotsPerInch() );
}
else
{
// Fallback: compatibility with QGIS <= 3.20; always assume low dpi screens
mSettings.setOutputDpi( window()->windowHandle()->screen()->logicalDotsPerInch() );
}
}

void QgsMapCanvas::setTemporalRange( const QgsDateTimeRange &dateTimeRange )
{
if ( temporalRange() == dateTimeRange )
@@ -2569,6 +2578,24 @@ void QgsMapCanvas::dropEvent( QDropEvent *event )
}
}

void QgsMapCanvas::showEvent( QShowEvent *event )
{
Q_UNUSED( event )
updateDevicePixelFromScreen();
// keep device pixel ratio up to date on screen or resolution change
if ( window()->windowHandle() )
{
connect( window()->windowHandle(), &QWindow::screenChanged, this, [ = ]( QScreen * )
{
disconnect( mScreenDpiChangedConnection );
mScreenDpiChangedConnection = connect( window()->windowHandle()->screen(), &QScreen::physicalDotsPerInchChanged, this, &QgsMapCanvas::updateDevicePixelFromScreen );
updateDevicePixelFromScreen();
} );

mScreenDpiChangedConnection = connect( window()->windowHandle()->screen(), &QScreen::physicalDotsPerInchChanged, this, &QgsMapCanvas::updateDevicePixelFromScreen );
}
}

QPoint QgsMapCanvas::mouseLastXY()
{
return mCanvasProperties->mouseLastXY;
@@ -981,6 +981,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView, public QgsExpressionContex
//! Renames the active map theme called \a theme to \a newTheme
void mapThemeRenamed( const QString &theme, const QString &newTheme );

void updateDevicePixelFromScreen();

signals:

/**
@@ -1181,6 +1183,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView, public QgsExpressionContex

void dropEvent( QDropEvent *event ) override;

void showEvent( QShowEvent *event ) override;

/// implementation struct
class CanvasProperties;
@@ -1358,6 +1361,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView, public QgsExpressionContex

int mBlockItemPositionUpdates = 0;

QMetaObject::Connection mScreenDpiChangedConnection;

/**
* Returns the last cursor position on the canvas in geographical coordinates
* \since QGIS 3.4
@@ -0,0 +1,33 @@
/***************************************************************************
qgssettingsregistrygui.cpp
--------------------------------------
Date : July 2021
Copyright : (C) 2021 by Damiano Lombardi
Email : damiano at opengis dot ch
***************************************************************************
* *
* 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 "qgssettingsregistrygui.h"

#include "qgsapplication.h"
#include "qgssettingsregistrycore.h"

QgsSettingsRegistryGui::QgsSettingsRegistryGui()
: QgsSettingsRegistry()
{
addSettingsEntry( &settingsRespectScreenDPI );

QgsApplication::settingsRegistryCore()->addSubRegistry( this );
}

QgsSettingsRegistryGui::~QgsSettingsRegistryGui()
{
QgsApplication::settingsRegistryCore()->removeSubRegistry( this );
}

0 comments on commit 071e4b5

Please sign in to comment.