Navigation Menu

Skip to content

Commit

Permalink
[layouts] Ensures that 1:1 zoom level on layout designer accounts
Browse files Browse the repository at this point in the history
for actual screen dpi, and responds correctly when window is dragged
between screens
  • Loading branch information
nyalldawson committed Jul 20, 2021
1 parent d8461a1 commit 9328953
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
2 changes: 2 additions & 0 deletions python/gui/auto_generated/layout/qgslayoutview.sip.in
Expand Up @@ -579,6 +579,8 @@ but is still in a perfectly valid state.

virtual void paintEvent( QPaintEvent *event );

virtual void showEvent( QShowEvent *event );


};

Expand Down
30 changes: 29 additions & 1 deletion src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -96,6 +96,8 @@
#include <QClipboard>
#include <QRegularExpression>
#include <QUrl>
#include <QWindow>
#include <QScreen>

#ifdef Q_OS_MACX
#include <ApplicationServices/ApplicationServices.h>
Expand Down Expand Up @@ -1617,6 +1619,25 @@ void QgsLayoutDesignerDialog::dragEnterEvent( QDragEnterEvent *event )
}
}

void QgsLayoutDesignerDialog::showEvent( QShowEvent *event )
{
QMainWindow::showEvent( 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, &QgsLayoutDesignerDialog::updateDevicePixelFromScreen );
updateDevicePixelFromScreen();
} );

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

void QgsLayoutDesignerDialog::setTitle( const QString &title )
{
mTitle = title;
Expand Down Expand Up @@ -1801,7 +1822,7 @@ void QgsLayoutDesignerDialog::updateStatusZoom()
}
else
{
double dpi = QgsApplication::desktop()->logicalDpiX();
double dpi = mScreenDpi;
//monitor dpi is not always correct - so make sure the value is sane
if ( ( dpi < 60 ) || ( dpi > 1200 ) )
dpi = 72;
Expand Down Expand Up @@ -4924,6 +4945,13 @@ void QgsLayoutDesignerDialog::onItemAdded( QgsLayoutItem *item )
}
}

void QgsLayoutDesignerDialog::updateDevicePixelFromScreen()
{
if ( window()->windowHandle() )
mScreenDpi = window()->windowHandle()->screen()->physicalDotsPerInch();
updateStatusZoom();
}

void QgsLayoutDesignerDialog::storeExportResults( QgsLayoutExporter::ExportResult result, QgsLayoutExporter *exporter )
{
mLastExportResults = std::make_unique< QgsLayoutDesignerInterface::ExportResults >();
Expand Down
5 changes: 5 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.h
Expand Up @@ -354,6 +354,7 @@ class QgsLayoutDesignerDialog: public QMainWindow, public Ui::QgsLayoutDesignerB
void closeEvent( QCloseEvent * ) override;
void dropEvent( QDropEvent *event ) override;
void dragEnterEvent( QDragEnterEvent *event ) override;
void showEvent( QShowEvent *event ) override;

private slots:

Expand Down Expand Up @@ -417,6 +418,7 @@ class QgsLayoutDesignerDialog: public QMainWindow, public Ui::QgsLayoutDesignerB
void backgroundTaskCountChanged( int total );
void onMapPreviewRefreshed();
void onItemAdded( QgsLayoutItem *item );
void updateDevicePixelFromScreen();

private:

Expand Down Expand Up @@ -516,6 +518,9 @@ class QgsLayoutDesignerDialog: public QMainWindow, public Ui::QgsLayoutDesignerB
std::unique_ptr< QgsLayoutDesignerInterface::ExportResults> mLastExportResults;
QMap< QString, QgsLabelingResults *> mLastExportLabelingResults;

double mScreenDpi = 96.0;
QMetaObject::Connection mScreenDpiChangedConnection;

//! Save window state
void saveWindowState();

Expand Down
29 changes: 28 additions & 1 deletion src/gui/layout/qgslayoutview.cpp
Expand Up @@ -42,6 +42,8 @@
#include <QMenu>
#include <QClipboard>
#include <QMimeData>
#include <QWindow>
#include <QScreen>

#define MIN_VIEW_SCALE 0.05
#define MAX_VIEW_SCALE 1000.0
Expand Down Expand Up @@ -207,7 +209,7 @@ void QgsLayoutView::setZoomLevel( double level )
}
else
{
double dpi = QgsApplication::desktop()->logicalDpiX();
double dpi = mScreenDpi;
//monitor dpi is not always correct - so make sure the value is sane
if ( ( dpi < 60 ) || ( dpi > 1200 ) )
dpi = 72;
Expand Down Expand Up @@ -1154,6 +1156,25 @@ void QgsLayoutView::paintEvent( QPaintEvent *event )
}
}

void QgsLayoutView::showEvent( QShowEvent *event )
{
QGraphicsView::showEvent( 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, &QgsLayoutView::updateDevicePixelFromScreen );
updateDevicePixelFromScreen();
} );

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

void QgsLayoutView::invalidateCachedRenders()
{
if ( !currentLayout() )
Expand All @@ -1169,6 +1190,12 @@ void QgsLayoutView::invalidateCachedRenders()
}
}

void QgsLayoutView::updateDevicePixelFromScreen()
{
if ( window()->windowHandle() )
mScreenDpi = window()->windowHandle()->screen()->physicalDotsPerInch();
}

void QgsLayoutView::viewChanged()
{
if ( mHorizontalRuler )
Expand Down
5 changes: 5 additions & 0 deletions src/gui/layout/qgslayoutview.h
Expand Up @@ -544,10 +544,12 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
void scrollContentsBy( int dx, int dy ) override;
void dragEnterEvent( QDragEnterEvent *e ) override;
void paintEvent( QPaintEvent *event ) override;
void showEvent( QShowEvent *event ) override;

private slots:

void invalidateCachedRenders();
void updateDevicePixelFromScreen();

private:

Expand Down Expand Up @@ -578,6 +580,9 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView

bool mPaintingEnabled = true;

double mScreenDpi = 96.0;
QMetaObject::Connection mScreenDpiChangedConnection;

friend class TestQgsLayoutView;
friend class QgsLayoutMouseHandles;

Expand Down

0 comments on commit 9328953

Please sign in to comment.