QGIS API Documentation  3.13.0-Master (788156190c)
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 <QWidget>
18 #include <QEvent>
19 #include <QObject>
20 #include <QVariant>
21 #include "qgsguiutils.h"
22 #include "qgslogger.h"
23 
24 QgsWidgetStateHelper::QgsWidgetStateHelper( QObject *parent ) : QObject( parent )
25 {
26 
27 }
28 
29 bool QgsWidgetStateHelper::eventFilter( QObject *object, QEvent *event )
30 {
31  if ( event->type() == QEvent::Close || event->type() == QEvent::Destroy )
32  {
33  QWidget *widget = qobject_cast<QWidget *>( object );
34 
35  // don't save geometry for windows which were never shown
36  if ( widget->property( "widgetStateHelperWasShown" ).toBool() )
37  {
38  QString name = widgetSafeName( widget );
39  QString key = mKeys[name];
40  QgsGuiUtils::saveGeometry( widget, key );
41  }
42  }
43  else if ( event->type() == QEvent::Show )
44  {
45  QWidget *widget = qobject_cast<QWidget *>( object );
46  QString name = widgetSafeName( widget );
47  QString key = mKeys[name];
48  QgsGuiUtils::restoreGeometry( widget, key );
49  widget->setProperty( "widgetStateHelperWasShown", QVariant( true ) );
50  }
51  return QObject::eventFilter( object, event );
52 }
53 
54 void QgsWidgetStateHelper::registerWidget( QWidget *widget, const QString &key )
55 {
56  QString name = widgetSafeName( widget );
57  mKeys[name] = key;
58  widget->installEventFilter( this );
59 }
60 
61 QString QgsWidgetStateHelper::widgetSafeName( QWidget *widget )
62 {
63  if ( widget->objectName().isEmpty() )
64  {
65  return widget->metaObject()->className();
66  }
67  return widget->objectName();
68 }
QgsWidgetStateHelper::eventFilter
bool eventFilter(QObject *object, QEvent *event) override
Event filter to catch events from registered widgets.
Definition: qgswidgetstatehelper_p.cpp:29
QgsGuiUtils::saveGeometry
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
Definition: qgsguiutils.cpp:224
QgsGuiUtils::restoreGeometry
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
Definition: qgsguiutils.cpp:231
qgswidgetstatehelper_p.h
QgsWidgetStateHelper::QgsWidgetStateHelper
QgsWidgetStateHelper(QObject *parent=nullptr)
QgsWidgetStateHelper.
Definition: qgswidgetstatehelper_p.cpp:24
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:54