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