QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "qgsguiutils.h"
21 #include "qgslogger.h"
22 
23 QgsWidgetStateHelper::QgsWidgetStateHelper( QObject *parent ) : QObject( parent )
24 {
25 
26 }
27 
28 bool QgsWidgetStateHelper::eventFilter( QObject *object, QEvent *event )
29 {
30  if ( event->type() == QEvent::Close || event->type() == QEvent::Destroy )
31  {
32  QWidget *widget = qobject_cast<QWidget *>( object );
33  QString name = widgetSafeName( widget );
34  QString key = mKeys[name];
35  QgsGuiUtils::saveGeometry( widget, key );
36  }
37  else if ( event->type() == QEvent::Show )
38  {
39  QWidget *widget = qobject_cast<QWidget *>( object );
40  QString name = widgetSafeName( widget );
41  QString key = mKeys[name];
42  QgsGuiUtils::restoreGeometry( widget, key );
43  }
44  return QObject::eventFilter( object, event );
45 }
46 
47 void QgsWidgetStateHelper::registerWidget( QWidget *widget, const QString &key )
48 {
49  QString name = widgetSafeName( widget );
50  mKeys[name] = key;
51  widget->installEventFilter( this );
52 }
53 
54 QString QgsWidgetStateHelper::widgetSafeName( QWidget *widget )
55 {
56  if ( widget->objectName().isEmpty() )
57  {
58  return widget->metaObject()->className();
59  }
60  return widget->objectName();
61 }
QgsWidgetStateHelper(QObject *parent=nullptr)
QgsWidgetStateHelper.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
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.