Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add ability to specity setting additional Options flags for QFileDialog
in the QgsFileWidget

For example in some cases hidding filter details is useful to prevent
expanding combobox popup to the whole screen when long filter string is
used.
  • Loading branch information
alexbruy authored and nyalldawson committed May 7, 2020
1 parent 1b41377 commit df4bf42
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
14 changes: 14 additions & 0 deletions python/gui/auto_generated/qgsfilewidget.sip.in
Expand Up @@ -96,6 +96,20 @@ returns the filters used for QDialog.getOpenFileName
setFilter sets the filter used by the model to filters. The filter is used to specify the kind of files that should be shown.

:param filter: Only files that match the given filter are shown, it may be an empty string. If you want multiple filters, separate them with ';;',
%End

QFileDialog::Options options() const;
%Docstring
Returns additional options used for QFileDialog

.. versionadded:: 3.14
%End

void setOptions( QFileDialog::Options options );
%Docstring
setOptions sets additional options used for QFileDialog. These options affect the look and feel of the QFileDialog

.. versionadded:: 3.14
%End

void setSelectedFilter( const QString &selectedFilter );
Expand Down
21 changes: 15 additions & 6 deletions src/gui/qgsfilewidget.cpp
Expand Up @@ -19,7 +19,6 @@
#include <QLineEdit>
#include <QToolButton>
#include <QLabel>
#include <QFileDialog>
#include <QGridLayout>
#include <QUrl>
#include <QDropEvent>
Expand Down Expand Up @@ -132,6 +131,16 @@ void QgsFileWidget::setFilter( const QString &filters )
mLineEdit->setFilters( filters );
}

QFileDialog::Options QgsFileWidget::options() const
{
return mOptions;
}

void QgsFileWidget::setOptions( QFileDialog::Options options )
{
mOptions = options;
}

bool QgsFileWidget::fileWidgetButtonVisible() const
{
return mButtonVisible;
Expand Down Expand Up @@ -305,26 +314,26 @@ void QgsFileWidget::openFileDialog()
{
case GetFile:
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a file" );
fileName = QFileDialog::getOpenFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter );
fileName = QFileDialog::getOpenFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, mOptions );
break;
case GetMultipleFiles:
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select one or more files" );
fileNames = QFileDialog::getOpenFileNames( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter );
fileNames = QFileDialog::getOpenFileNames( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, mOptions );
break;
case GetDirectory:
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a directory" );
fileName = QFileDialog::getExistingDirectory( this, title, QFileInfo( oldPath ).absoluteFilePath(), QFileDialog::ShowDirsOnly );
fileName = QFileDialog::getExistingDirectory( this, title, QFileInfo( oldPath ).absoluteFilePath(), mOptions | QFileDialog::ShowDirsOnly );
break;
case SaveFile:
{
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Create or select a file" );
if ( !confirmOverwrite() )
{
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, QFileDialog::DontConfirmOverwrite );
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, mOptions | QFileDialog::DontConfirmOverwrite );
}
else
{
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter );
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, mOptions );
}

// make sure filename ends with filter. This isn't automatically done by
Expand Down
18 changes: 16 additions & 2 deletions src/gui/qgsfilewidget.h
Expand Up @@ -23,6 +23,7 @@ class QVariant;
class QgsFileDropEdit;
class QHBoxLayout;
#include <QWidget>
#include <QFileDialog>

#include "qgis_gui.h"
#include "qgis_sip.h"
Expand All @@ -44,7 +45,6 @@ class GUI_EXPORT QgsFileWidget : public QWidget
SIP_END
#endif


Q_OBJECT
Q_PROPERTY( bool fileWidgetButtonVisible READ fileWidgetButtonVisible WRITE setFileWidgetButtonVisible )
Q_PROPERTY( bool useLink READ useLink WRITE setUseLink )
Expand All @@ -54,6 +54,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget
Q_PROPERTY( QString defaultRoot READ defaultRoot WRITE setDefaultRoot )
Q_PROPERTY( StorageMode storageMode READ storageMode WRITE setStorageMode )
Q_PROPERTY( RelativeStorage relativeStorage READ relativeStorage WRITE setRelativeStorage )
Q_PROPERTY( QFileDialog::Options options READ options WRITE setOptions )

public:

Expand Down Expand Up @@ -123,6 +124,18 @@ class GUI_EXPORT QgsFileWidget : public QWidget
*/
void setFilter( const QString &filter );

/**
* Returns additional options used for QFileDialog
* \since QGIS 3.14
*/
QFileDialog::Options options() const;

/**
* \brief setOptions sets additional options used for QFileDialog. These options affect the look and feel of the QFileDialog
* \since QGIS 3.14
*/
void setOptions( QFileDialog::Options options );

/**
* Sets the selected filter when the file dialog opens.
*/
Expand Down Expand Up @@ -218,6 +231,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget
bool mConfirmOverwrite = true;
StorageMode mStorageMode = GetFile;
RelativeStorage mRelativeStorage = Absolute;
QFileDialog::Options mOptions = QFileDialog::Options();

QLabel *mLinkLabel = nullptr;
QgsFileDropEdit *mLineEdit = nullptr;
Expand All @@ -234,9 +248,9 @@ class GUI_EXPORT QgsFileWidget : public QWidget
friend class TestQgsFileWidget;
};

///@cond PRIVATE


///@cond PRIVATE

#ifndef SIP_RUN

Expand Down

0 comments on commit df4bf42

Please sign in to comment.