Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Add API to ease saving and restoring widget geometry. (#5615)
Usage: - QgsGui::instance()->enableAutoGeometryRestore( this ); just after setupUi in your widgets to enable. - Remove any calls to saveGeometry() and restoreGeometry() in your widgets.
- Loading branch information
Showing
10 changed files
with
205 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*************************************************************************** | ||
qgswidgetstatehelper_p.cpp - QgsWidgetStateHelper | ||
--------------------- | ||
begin : 3.12.2017 | ||
copyright : (C) 2017 by Nathan Woodrow | ||
Email : woodrow.nathan at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#include "qgswidgetstatehelper_p.h" | ||
#include <QWidget> | ||
#include <QEvent> | ||
#include <QObject> | ||
#include "qgsguiutils.h" | ||
#include "qgslogger.h" | ||
|
||
QgsWidgetStateHelper::QgsWidgetStateHelper( QObject *parent ) : QObject( parent ) | ||
{ | ||
|
||
} | ||
|
||
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 ); | ||
QgsGuiUtils::saveGeometry( widget, key ); | ||
} | ||
else if ( event->type() == QEvent::Show ) | ||
{ | ||
QString key = mKeys[object->objectName()]; | ||
QWidget *widget = qobject_cast<QWidget *>( object ); | ||
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; | ||
widget->installEventFilter( this ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/*************************************************************************** | ||
qgswidgetstatehelper_p.h - QgsWidgetStateHelper | ||
--------------------- | ||
begin : 3.12.2017 | ||
copyright : (C) 2017 by Nathan Woodrow | ||
Email : woodrow.nathan at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#ifndef QGSWIDGETSTATEHELPER_P_H | ||
#define QGSWIDGETSTATEHELPER_P_H | ||
|
||
#include <QMap> | ||
#include <QObject> | ||
|
||
#define SIP_NO_FILE | ||
|
||
/** | ||
* \ingroup gui | ||
* QgsWidgetStateHelper is a helper class to save and restore the geometry of QWidgets in the application. | ||
* This removes the need for devs to remember to call saveGeometry() and restoreGeometry() when writing new widgets. | ||
* | ||
* This helper is internal and should only be called via QgsGui::enabledAutoGeometryRestore | ||
* \since QGIS 3.0 | ||
*/ | ||
class QgsWidgetStateHelper : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
|
||
/** | ||
* QgsWidgetStateHelper | ||
* @param parent Parent object | ||
*/ | ||
explicit QgsWidgetStateHelper( QObject *parent = 0 ); | ||
|
||
/** | ||
* Event filter to catch events from registered widgets. | ||
* \param object Object getting the event. | ||
* \param event Event sent from Qt. | ||
* \return Always returns True so that widget still gets event. | ||
*/ | ||
bool eventFilter( QObject *object, QEvent *event ); | ||
|
||
/** | ||
* Register a widget to have it geometry state automatically saved and restored. | ||
* \param widget The widget to save. Must have objectName() set. | ||
* \param key The override settings key name to use if objectName() isn't to be used. | ||
* objectName() is the default if not set. | ||
*/ | ||
void registerWidget( QWidget *widget, const QString &key = QString() ); | ||
|
||
private: | ||
QMap<QString, QString> mKeys; | ||
}; | ||
|
||
#endif // QGSWIDGETSTATEHELPER_P_H |