QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsscreenhelper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsscreenhelper.cpp
3 ---------------
4 Date : August 2022
5 Copyright : (C) 2022 by Nyall Dawson
6 Email : nyall dot dawson 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 "qgsscreenhelper.h"
17#include "moc_qgsscreenhelper.cpp"
18#include "qgis.h"
19#include <QWidget>
20#include <QEvent>
21#include <QWindow>
22#include <QScreen>
23
24
26 : QObject( parent )
27 , mWidget( parent )
28{
29 mWidget->installEventFilter( this );
30}
31
33{
35 {
36 return windowHandle->screen();
37 }
38 return nullptr;
39}
40
42{
43 if ( QWidget *window = mWidget->window() )
44 return window->windowHandle();
45
46 return nullptr;
47}
48
49bool QgsScreenHelper::eventFilter( QObject *watched, QEvent *event )
50{
51 if ( watched != mWidget )
52 return false;
53
54 switch ( event->type() )
55 {
56 case QEvent::Show:
57 {
58 updateDevicePixelFromScreen();
59 updateAvailableGeometryFromScreen();
60
61 // keep device pixel ratio up to date on screen or resolution change
62 if ( QWindow *handle = windowHandle() )
63 {
64 connect( handle, &QWindow::screenChanged, this, [ = ]( QScreen * )
65 {
66 disconnect( mScreenDpiChangedConnection );
67 disconnect( mAvailableGeometryChangedConnection );
68
69 if ( QWindow *windowHandleInLambda = windowHandle() )
70 {
71 mScreenDpiChangedConnection = connect( windowHandleInLambda->screen(), &QScreen::physicalDotsPerInchChanged, this, &QgsScreenHelper::updateDevicePixelFromScreen );
72 updateDevicePixelFromScreen();
73
74 mAvailableGeometryChangedConnection = connect( windowHandleInLambda->screen(), &QScreen::availableGeometryChanged, this, &QgsScreenHelper::updateAvailableGeometryFromScreen );
75 updateAvailableGeometryFromScreen();
76 }
77 } );
78
79 mScreenDpiChangedConnection = connect( handle->screen(), &QScreen::physicalDotsPerInchChanged, this, &QgsScreenHelper::updateDevicePixelFromScreen );
80 mAvailableGeometryChangedConnection = connect( handle->screen(), &QScreen::availableGeometryChanged, this, &QgsScreenHelper::updateAvailableGeometryFromScreen );
81 }
82 break;
83 }
84
85 default:
86 break;
87 }
88
89 return false;
90}
91
92void QgsScreenHelper::updateDevicePixelFromScreen()
93{
94 if ( QScreen *screen = QgsScreenHelper::screen() )
95 {
96 const double newDpi = screen->physicalDotsPerInch();
97 if ( !qgsDoubleNear( newDpi, mScreenDpi ) )
98 {
99 mScreenDpi = newDpi;
100 emit screenDpiChanged( mScreenDpi );
101 }
102 }
103}
104
105void QgsScreenHelper::updateAvailableGeometryFromScreen()
106{
107 if ( QScreen *screen = QgsScreenHelper::screen() )
108 {
109 const QRect newGeometry = screen->availableGeometry();
110 if ( newGeometry != mAvailableGeometry )
111 {
112 mAvailableGeometry = newGeometry;
113 emit availableGeometryChanged( mAvailableGeometry );
114 }
115 }
116}
QWindow * windowHandle()
Returns the window handle for the window the parent widget is associated with, or nullptr.
QScreen * screen()
Returns the screen that the parent widget appears on, or nullptr.
bool eventFilter(QObject *watched, QEvent *event) override
QgsScreenHelper(QWidget *parent)
Constructor for QgsScreenHelper for the specified parent widget.
void availableGeometryChanged(const QRect &geometry)
Emitted whenever the available geometry of the screen associated with the widget is changed.
void screenDpiChanged(double dpi)
Emitted whenever the screen dpi associated with the widget is changed.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5917