Block the events while showing a dialog

When showing a file dialog, Qt can choose to use the "system" file
dialog, which will make QgsExternalResourceWidget to loose the focus.
This patch blocks all the events that are sent to
QgsExternalResourceWidget while a dialog is shown, this way it will keep
the focus until the dialog is closed.

Sponsored by the QGIS project (qgis.org)

Fixes: #26948
This commit is contained in:
BogDan Vatra 2019-09-20 14:34:29 +03:00 committed by BogDan Vatra
parent 240bfb2a3f
commit f96c4a2308
7 changed files with 40 additions and 4 deletions

View File

@ -141,6 +141,10 @@ is set to QgsFileWidget.RelativeDefaultPath.
emitteed as soon as the current document changes
%End
protected:
virtual bool eventFilter( QObject *watched, QEvent *event );
};
/************************************************************************

View File

@ -189,6 +189,7 @@ the appearance and behavior of the line edit portion of the widget.
emitted as soon as the current file or directory is changed
%End
};

View File

@ -175,8 +175,7 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget *editor )
}
if ( cfg.contains( QStringLiteral( "FileWidgetButton" ) ) )
{
// Prevent from showing the button in the attribute table, see https://github.com/qgis/QGIS/issues/26948
mQgsWidget->fileWidget()->setFileWidgetButtonVisible( cfg.value( QStringLiteral( "FileWidgetButton" ) ).toBool() && context().formMode() != QgsAttributeEditorContext::Popup );
mQgsWidget->fileWidget()->setFileWidgetButtonVisible( cfg.value( QStringLiteral( "FileWidgetButton" ) ).toBool() );
}
if ( cfg.contains( QStringLiteral( "DocumentViewer" ) ) )
{

View File

@ -55,6 +55,17 @@ QgsExternalResourceWidget::QgsExternalResourceWidget( QWidget *parent )
connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsExternalResourceWidget::loadDocument );
connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsExternalResourceWidget::valueChanged );
connect( mFileWidget, &QgsFileWidget::blockEvents, this, [this]( bool block )
{
if ( block )
{
installEventFilter( this );
}
else
{
removeEventFilter( this );
}
} );
}
QVariant QgsExternalResourceWidget::documentPath( QVariant::Type type ) const
@ -194,6 +205,15 @@ void QgsExternalResourceWidget::setDefaultRoot( const QString &defaultRoot )
mDefaultRoot = defaultRoot;
}
bool QgsExternalResourceWidget::eventFilter( QObject *watched, QEvent *event )
{
if ( watched == this && event && ( event->type() == QEvent::FocusOut || event->type() == QEvent::FocusAboutToChange ) )
{
return true;
}
return QWidget::eventFilter( watched, event );
}
QgsFileWidget::RelativeStorage QgsExternalResourceWidget::relativeStorage() const
{
return mRelativeStorage;
@ -246,5 +266,3 @@ void QgsExternalResourceWidget::loadDocument( const QString &path )
}
}
}

View File

@ -147,6 +147,10 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
//! emitteed as soon as the current document changes
void valueChanged( const QString & );
protected:
// Needed to block events when showing a dialog
bool eventFilter( QObject *watched, QEvent *event ) override;
private slots:
void loadDocument( const QString &path );

View File

@ -255,6 +255,7 @@ void QgsFileWidget::openFileDialog()
QStringList fileNames;
QString title;
emit blockEvents( true );
switch ( mStorageMode )
{
case GetFile:
@ -287,6 +288,7 @@ void QgsFileWidget::openFileDialog()
}
break;
}
emit blockEvents( false );
if ( fileName.isEmpty() && fileNames.isEmpty( ) )
return;

View File

@ -184,6 +184,14 @@ class GUI_EXPORT QgsFileWidget : public QWidget
//! emitted as soon as the current file or directory is changed
void fileChanged( const QString & );
/**
* Emitted before and after showing the file dialog.
*
* \note not available in Python bindings
* \since QGIS 3.10
*/
void blockEvents( bool ) SIP_SKIP;
private slots:
void openFileDialog();
void textEdited( const QString &path );