QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 "moc_qgswidgetstatehelper_p.cpp"
18#include <QWindow>
19#include <QWidget>
20#include <QEvent>
21#include <QObject>
22#include <QVariant>
23#include "qgsguiutils.h"
24#include "qgslogger.h"
25
26QgsWidgetStateHelper::QgsWidgetStateHelper( QObject *parent ) : QObject( parent )
27{
28
29}
30
31bool QgsWidgetStateHelper::eventFilter( QObject *object, QEvent *event )
32{
33 if ( event->type() == QEvent::Close ||
34 event->type() == QEvent::Destroy ||
35 event->type() == QEvent::Hide )
36 {
37 QWidget *widget = qobject_cast<QWidget *>( object );
38
39 // don't save geometry for windows which were never shown
40 if ( widget->property( "widgetStateHelperWasShown" ).toBool() )
41 {
42 const QString name = widgetSafeName( widget );
43 const QString key = mKeys[name];
44 QgsGuiUtils::saveGeometry( widget, key );
45 }
46 }
47 else if ( event->type() == QEvent::Show )
48 {
49 QWidget *widget = qobject_cast<QWidget *>( object );
50 const QString name = widgetSafeName( widget );
51
52 // If window is already maximized by Window Manager,
53 // there is no need to restore its geometry as it might lead to
54 // an incorrect state of QFlags<Qt::WindowState>(WindowMinimized|WindowMaximized)
55 // thus minimizing window after it just has been restored by WM.
56 // Inability to restore minimized windows has been observed with
57 // KWin 5.19 and Qt 5.15 running under X11.
58 QWindow *win = widget->windowHandle();
59 if ( !win )
60 return QObject::eventFilter( object, event );
61
62 if ( !( win->windowStates() & Qt::WindowMaximized ) )
63 {
64 const QString key = mKeys[name];
65 QgsGuiUtils::restoreGeometry( widget, key );
66 }
67
68 widget->setProperty( "widgetStateHelperWasShown", QVariant( true ) );
69 }
70 return QObject::eventFilter( object, event );
71}
72
73void QgsWidgetStateHelper::registerWidget( QWidget *widget, const QString &key )
74{
75 const QString name = widgetSafeName( widget );
76 mKeys[name] = key;
77 widget->installEventFilter( this );
78}
79
80QString QgsWidgetStateHelper::widgetSafeName( QWidget *widget )
81{
82 if ( widget->objectName().isEmpty() )
83 {
84 return widget->metaObject()->className();
85 }
86 return widget->objectName();
87}
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.