Skip to content
Permalink
Browse files

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 ea5f3c69134d8eb31ac1b5542e3cac1cca153f4b
Showing with 27 additions and 4 deletions.
  1. +5 −0 src/gui/qgsgui.cpp
  2. +15 −4 src/gui/qgswidgetstatehelper_p.cpp
  3. +7 −0 src/gui/qgswidgetstatehelper_p.h
@@ -30,6 +30,7 @@
#endif
#include "qgsshortcutsmanager.h"
#include "qgswidgetstatehelper_p.h"
#include "qgslogger.h"

QgsGui *QgsGui::instance()
{
@@ -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 );
}

@@ -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();
}
@@ -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.
You can’t perform that action at this time.