Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[ui] Properly fix UI state restoration on QGIS launch
  • Loading branch information
nirvn committed Oct 10, 2021
1 parent 8498c55 commit 6c9f0e6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
63 changes: 40 additions & 23 deletions src/app/qgisapp.cpp
Expand Up @@ -148,6 +148,8 @@
#include "qgsopenclutils.h"
#endif

#include "ui_defaults.h"

#include <QNetworkReply>
#include <QNetworkProxy>
#include <QAuthenticator>
Expand Down Expand Up @@ -2213,9 +2215,45 @@ void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst )
onActiveLayerChanged( activeLayer() );
}

void QgisApp::resizeEvent( QResizeEvent *resizeEvent )
{
QMainWindow::resizeEvent( resizeEvent );
if ( mRestoreStateOnResize )
{
QgsSettings settings;
restoreState( settings.value( QStringLiteral( "UI/state" ), QByteArray::fromRawData( reinterpret_cast< const char * >( defaultUIstate ), sizeof defaultUIstate ) ).toByteArray() );
}
}

bool QgisApp::event( QEvent *event )
{
bool done = false;

if ( mRestoreStateOnResize )
{
switch ( event->type() )
{
case QEvent::Enter:
case QEvent::DragEnter:
case QEvent::DragLeave:
case QEvent::DragMove:
case QEvent::Drop:
case QEvent::KeyPress:
case QEvent::KeyRelease:
case QEvent::MouseButtonDblClick:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseMove:
case QEvent::NativeGesture:
// The user did something, end state restores
mRestoreStateOnResize = false;
break;

default:
break;
}
}

if ( event->type() == QEvent::FileOpen )
{
// handle FileOpen event (double clicking a file icon in Mac OS X Finder)
Expand Down Expand Up @@ -5337,21 +5375,14 @@ void QgisApp::saveWindowState()
QgsPluginRegistry::instance()->unloadAll();
}

#include "ui_defaults.h"

void QgisApp::restoreWindowState()
{
// restore the toolbar and dock widgets positions using Qt4 settings API
QgsSettings settings;
#if 0
// because of Qt regression: https://bugreports.qt.io/browse/QTBUG-89034
// we have to wait till dialog is first shown to try to restore dock geometry or it's not correctly restored
// so this code was moved to showEvent for now...
if ( !restoreState( settings.value( QStringLiteral( "UI/state" ), QByteArray::fromRawData( reinterpret_cast< const char * >( defaultUIstate ), sizeof defaultUIstate ) ).toByteArray() ) )
{
QgsDebugMsg( QStringLiteral( "restore of UI state failed" ) );
}
#endif

if ( settings.value( QStringLiteral( "UI/hidebrowser" ), false ).toBool() )
{
Expand All @@ -5370,6 +5401,8 @@ void QgisApp::restoreWindowState()
move( pos.width(), pos.height() );
}

// restore state on resize need to work around Qt regression: https://bugreports.qt.io/browse/QTBUG-89034
mRestoreStateOnResize = true;
}
///////////// END OF GUI SETUP ROUTINES ///////////////
void QgisApp::sponsors()
Expand Down Expand Up @@ -17543,19 +17576,3 @@ QgsAttributeEditorContext QgisApp::createAttributeEditorContext()
context.setMainMessageBar( messageBar() );
return context;
}

void QgisApp::showEvent( QShowEvent *event )
{
QMainWindow::showEvent( event );
// because of Qt regression: https://bugreports.qt.io/browse/QTBUG-89034
// we have to wait till dialog is first shown to try to restore dock geometry or it's not correctly restored
static std::once_flag firstShow;
std::call_once( firstShow, [this]
{
QgsSettings settings;
if ( !restoreState( settings.value( QStringLiteral( "UI/state" ), QByteArray::fromRawData( reinterpret_cast< const char * >( defaultUIstate ), sizeof defaultUIstate ) ).toByteArray() ) )
{
QgsDebugMsg( QStringLiteral( "restore of UI state failed" ) );
}
} );
}
8 changes: 6 additions & 2 deletions src/app/qgisapp.h
Expand Up @@ -1070,7 +1070,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
*/
void legendLayerStretchUsingCurrentExtent();

//! Watch for QFileOpenEvent.
//! Watches for QFileOpenEvent.
bool event( QEvent *event ) override;

//! Sets the CRS of the current legend group
Expand Down Expand Up @@ -1276,7 +1276,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QgsAttributeEditorContext createAttributeEditorContext();

protected:
void showEvent( QShowEvent *event ) override;

//! Listens to resize event in order to properly restore UI upon re-launching QGIS
void resizeEvent( QResizeEvent *resizeEvent ) override;

//! Handle state changes (WindowTitleChange)
void changeEvent( QEvent *event ) override;
Expand Down Expand Up @@ -2732,6 +2734,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
int mFreezeCount = 0;
friend class QgsCanvasRefreshBlocker;

bool mRestoreStateOnResize = false;

friend class TestQgisAppPython;
friend class QgisAppInterface;
friend class QgsAppScreenShots;
Expand Down

0 comments on commit 6c9f0e6

Please sign in to comment.