QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  ***************************************************************************/
16 #include "qgswidgetstatehelper_p.h"
17 #include <QWindow>
18 #include <QWidget>
19 #include <QEvent>
20 #include <QObject>
21 #include <QVariant>
22 #include "qgsguiutils.h"
23 #include "qgslogger.h"
24 
25 QgsWidgetStateHelper::QgsWidgetStateHelper( QObject *parent ) : QObject( parent )
26 {
27 
28 }
29 
30 bool 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  QString name = widgetSafeName( widget );
42  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  QString name = widgetSafeName( widget );
50 
51 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
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 minimzed 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  QString key = mKeys[name];
65  QgsGuiUtils::restoreGeometry( widget, key );
66  }
67 #else
68  QString key = mKeys[name];
69  QgsGuiUtils::restoreGeometry( widget, key );
70 #endif
71 
72  widget->setProperty( "widgetStateHelperWasShown", QVariant( true ) );
73  }
74  return QObject::eventFilter( object, event );
75 }
76 
77 void QgsWidgetStateHelper::registerWidget( QWidget *widget, const QString &key )
78 {
79  QString name = widgetSafeName( widget );
80  mKeys[name] = key;
81  widget->installEventFilter( this );
82 }
83 
84 QString QgsWidgetStateHelper::widgetSafeName( QWidget *widget )
85 {
86  if ( widget->objectName().isEmpty() )
87  {
88  return widget->metaObject()->className();
89  }
90  return widget->objectName();
91 }
QgsWidgetStateHelper::eventFilter
bool eventFilter(QObject *object, QEvent *event) override
Event filter to catch events from registered widgets.
Definition: qgswidgetstatehelper_p.cpp:30
QgsGuiUtils::saveGeometry
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
Definition: qgsguiutils.cpp:212
QgsGuiUtils::restoreGeometry
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
Definition: qgsguiutils.cpp:219
qgswidgetstatehelper_p.h
QgsWidgetStateHelper::QgsWidgetStateHelper
QgsWidgetStateHelper(QObject *parent=nullptr)
QgsWidgetStateHelper.
Definition: qgswidgetstatehelper_p.cpp:25
qgslogger.h
qgsguiutils.h
QgsWidgetStateHelper::registerWidget
void registerWidget(QWidget *widget, const QString &key=QString())
Register a widget to have it geometry state automatically saved and restored.
Definition: qgswidgetstatehelper_p.cpp:77