Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Generate safe widget name in QgsWidgetStateHelper
Assert is a bit rough here so just warn devs
  • Loading branch information
NathanW2 committed Dec 18, 2017
1 parent 7f474a0 commit ea5f3c6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/gui/qgsgui.cpp
Expand Up @@ -30,6 +30,7 @@
#endif
#include "qgsshortcutsmanager.h"
#include "qgswidgetstatehelper_p.h"
#include "qgslogger.h"

QgsGui *QgsGui::instance()
{
Expand Down Expand Up @@ -74,6 +75,10 @@ QgsLayoutItemGuiRegistry *QgsGui::layoutItemGuiRegistry()

void QgsGui::enableAutoGeometryRestore( QWidget *widget, const QString &key )
{
if ( widget->objectName().isEmpty() )
{
QgsDebugMsg( "WARNING: No object name set. Best for it to be set objectName when using QgsGui::enableAutoGeometryRestore" );
}
instance()->mWidgetStateHelper->registerWidget( widget, key );
}

Expand Down
19 changes: 15 additions & 4 deletions src/gui/qgswidgetstatehelper_p.cpp
Expand Up @@ -29,22 +29,33 @@ bool QgsWidgetStateHelper::eventFilter( QObject *object, QEvent *event )
{
if ( event->type() == QEvent::Close || event->type() == QEvent::Destroy )
{
QString key = mKeys[object->objectName()];
QWidget *widget = qobject_cast<QWidget *>( object );
QString name = widgetSafeName( widget );
QString key = mKeys[name];
QgsGuiUtils::saveGeometry( widget, key );
}
else if ( event->type() == QEvent::Show )
{
QString key = mKeys[object->objectName()];
QWidget *widget = qobject_cast<QWidget *>( object );
QString name = widgetSafeName( widget );
QString key = mKeys[name];
QgsGuiUtils::restoreGeometry( widget, key );
}
return QObject::eventFilter( object, event );
}

void QgsWidgetStateHelper::registerWidget( QWidget *widget, const QString &key )
{
Q_ASSERT( !widget->objectName().isEmpty() );
mKeys[widget->objectName()] = key;
QString name = widgetSafeName( widget );
mKeys[name] = key;
widget->installEventFilter( this );
}

QString QgsWidgetStateHelper::widgetSafeName( QWidget *widget )
{
if ( widget->objectName().isEmpty() )
{
return widget->metaObject()->className();
}
return widget->objectName();
}
7 changes: 7 additions & 0 deletions src/gui/qgswidgetstatehelper_p.h
Expand Up @@ -58,6 +58,13 @@ class QgsWidgetStateHelper : public QObject

private:
QMap<QString, QString> mKeys;

/**
* Return a non null safe name for the widget.
* @param widget The widget.
* @return A non null safe name for the widget.
*/
QString widgetSafeName( QWidget *widget );
};

#endif // QGSWIDGETSTATEHELPER_P_H

0 comments on commit ea5f3c6

Please sign in to comment.