Skip to content

Commit

Permalink
Merge pull request #44132 from m-kuhn/respect_screen_dpi
Browse files Browse the repository at this point in the history
Allow using physical DPI for map canvas
  • Loading branch information
m-kuhn committed Jul 14, 2021
2 parents c09443a + 598e2bd commit 071e4b5
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 10 deletions.
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsgui.sip.in
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions python/gui/auto_generated/qgsmapcanvas.sip.in
Expand Up @@ -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 );




Expand Down
42 changes: 42 additions & 0 deletions python/gui/auto_generated/settings/qgssettingsregistrygui.sip.in
@@ -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 *
************************************************************************/
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/app/options/qgsoptions.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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
//
Expand Down Expand Up @@ -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() );

Expand Down
17 changes: 17 additions & 0 deletions src/core/settings/qgssettingsregistry.cpp
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/core/settings/qgssettingsregistry.h
Expand Up @@ -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.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/gui/CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/gui/qgsgui.cpp
Expand Up @@ -61,6 +61,7 @@
#include "qgssubsetstringeditorproviderregistry.h"
#include "qgsprovidersourcewidgetproviderregistry.h"
#include "qgsrelationwidgetregistry.h"
#include "qgssettingsregistrygui.h"

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

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

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

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

mSettingsRegistryGui = new QgsSettingsRegistryGui();

mCodeEditorColorSchemeRegistry = new QgsCodeEditorColorSchemeRegistry();

// provider gui registry initialize QgsProviderRegistry too
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsgui.h
Expand Up @@ -23,6 +23,7 @@
#include <QWidget>
#include <memory>

class QgsSettingsRegistryGui;
class QgsEditorWidgetRegistry;
class QgsShortcutsManager;
class QgsLayerTreeEmbeddedWidgetRegistry;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -261,6 +268,7 @@ class GUI_EXPORT QgsGui : public QObject

QgsGui();

QgsSettingsRegistryGui *mSettingsRegistryGui = nullptr;
QgsProviderGuiRegistry *mProviderGuiRegistry = nullptr;
QgsWidgetStateHelper *mWidgetStateHelper = nullptr;
QgsNative *mNative = nullptr;
Expand Down
45 changes: 36 additions & 9 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -91,6 +91,7 @@ email : sherman at mrcc.com
#include "qgscoordinatereferencesystemregistry.h"
#include "qgslabelingresults.h"
#include "qgsmaplayerutils.h"
#include "qgssettingsregistrygui.h"

/**
* \ingroup gui
Expand Down Expand Up @@ -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 );

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

setTemporalRange( mSettings.temporalRange() );

refresh();
}

Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -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:

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/gui/settings/qgssettingsregistrygui.cpp
@@ -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.