QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgswidgetstatehelper_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgswidgetstatehelper_p.cpp - QgsWidgetStateHelper
3
4 ---------------------
5 begin : 3.12.2017
6 copyright : (C) 2017 by Nathan Woodrow
7 Email : woodrow.nathan at gmail dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17#include <QWindow>
18#include <QWidget>
19#include <QEvent>
20#include <QObject>
21#include <QVariant>
22#include "qgsguiutils.h"
23#include "qgslogger.h"
24
25QgsWidgetStateHelper::QgsWidgetStateHelper( QObject *parent ) : QObject( parent )
26{
27
28}
29
30bool QgsWidgetStateHelper::eventFilter( QObject *object, QEvent *event )
31{
32 if ( event->type() == QEvent::Close ||
33 event->type() == QEvent::Destroy ||
34 event->type() == QEvent::Hide )
35 {
36 QWidget *widget = qobject_cast<QWidget *>( object );
37
38 // don't save geometry for windows which were never shown
39 if ( widget->property( "widgetStateHelperWasShown" ).toBool() )
40 {
41 const QString name = widgetSafeName( widget );
42 const QString key = mKeys[name];
43 QgsGuiUtils::saveGeometry( widget, key );
44 }
45 }
46 else if ( event->type() == QEvent::Show )
47 {
48 QWidget *widget = qobject_cast<QWidget *>( object );
49 const QString name = widgetSafeName( widget );
50
51 // If window is already maximized by Window Manager,
52 // there is no need to restore its geometry as it might lead to
53 // an incorrect state of QFlags<Qt::WindowState>(WindowMinimized|WindowMaximized)
54 // thus minimizing window after it just has been restored by WM.
55 // Inability to restore minimized windows has been observed with
56 // KWin 5.19 and Qt 5.15 running under X11.
57 QWindow *win = widget->windowHandle();
58 if ( !win )
59 return QObject::eventFilter( object, event );
60
61 if ( !( win->windowStates() & Qt::WindowMaximized ) )
62 {
63 const QString key = mKeys[name];
64 QgsGuiUtils::restoreGeometry( widget, key );
65 }
66
67 widget->setProperty( "widgetStateHelperWasShown", QVariant( true ) );
68 }
69 return QObject::eventFilter( object, event );
70}
71
72void QgsWidgetStateHelper::registerWidget( QWidget *widget, const QString &key )
73{
74 const QString name = widgetSafeName( widget );
75 mKeys[name] = key;
76 widget->installEventFilter( this );
77}
78
79QString QgsWidgetStateHelper::widgetSafeName( QWidget *widget )
80{
81 if ( widget->objectName().isEmpty() )
82 {
83 return widget->metaObject()->className();
84 }
85 return widget->objectName();
86}
bool eventFilter(QObject *object, QEvent *event) override
Event filter to catch events from registered widgets.
void registerWidget(QWidget *widget, const QString &key=QString())
Register a widget to have it geometry state automatically saved and restored.
QgsWidgetStateHelper(QObject *parent=nullptr)
QgsWidgetStateHelper.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.