Skip to content
Permalink
Browse files

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 df4bf42ae5095d5168fbb828f4b714ce49526efd
Showing with 45 additions and 8 deletions.
  1. +14 −0 python/gui/auto_generated/qgsfilewidget.sip.in
  2. +15 −6 src/gui/qgsfilewidget.cpp
  3. +16 −2 src/gui/qgsfilewidget.h
@@ -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 );
@@ -19,7 +19,6 @@
#include <QLineEdit>
#include <QToolButton>
#include <QLabel>
#include <QFileDialog>
#include <QGridLayout>
#include <QUrl>
#include <QDropEvent>
@@ -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;
@@ -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
@@ -23,6 +23,7 @@ class QVariant;
class QgsFileDropEdit;
class QHBoxLayout;
#include <QWidget>
#include <QFileDialog>

#include "qgis_gui.h"
#include "qgis_sip.h"
@@ -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 )
@@ -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:

@@ -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.
*/
@@ -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;
@@ -234,9 +248,9 @@ class GUI_EXPORT QgsFileWidget : public QWidget
friend class TestQgsFileWidget;
};

///@cond PRIVATE


///@cond PRIVATE

#ifndef SIP_RUN

0 comments on commit df4bf42

Please sign in to comment.
You can’t perform that action at this time.