Skip to content
Permalink
Browse files

Add filters to file picker and external widget

  • Loading branch information
3nids committed Jan 15, 2016
1 parent 4adae28 commit 36157d43acf73c3a8582abfe79895ad97db66613
@@ -9,8 +9,8 @@ class QgsFilePickerWidget : QWidget

enum StorageMode
{
File,
Directory
GetFile,
GetDirectory
};

enum RelativeStorage
@@ -41,6 +41,14 @@ class QgsFilePickerWidget : QWidget
*/
void setDialogTitle( QString title );

//! returns the filter used for QDialog::getOpenFileName
QString filter() const;
/**
* @brief 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 ';;',
*/
void setFilter( const QString &filter );

//! determines if the tool button is shown
bool filePickerButtonVisible() const;
//! determines if the tool button is shown
@@ -43,8 +43,8 @@ QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg( QgsVectorLayer* vl,
connect( mRelativeGroupBox, SIGNAL( toggled( bool ) ), this, SLOT( enableRelative( bool ) ) );

// set ids for StorageTypeButtons
mStorageButtonGroup->setId( mStoreFilesButton, QgsFilePickerWidget::File );
mStorageButtonGroup->setId( mStoreDirsButton, QgsFilePickerWidget::Directory );
mStorageButtonGroup->setId( mStoreFilesButton, QgsFilePickerWidget::GetFile );
mStorageButtonGroup->setId( mStoreDirsButton, QgsFilePickerWidget::GetDirectory );
mStoreFilesButton->setChecked( true );

// set ids for RelativeButtons
@@ -110,7 +110,8 @@ QgsEditorWidgetConfig QgsExternalResourceConfigDlg::config()
QgsEditorWidgetConfig cfg;

cfg.insert( "FilePicker", mFilePickerGroupBox->isChecked() );
cfg.insert( "FilePickerButton", mFilePickerButton->isChecked() );
cfg.insert( "FilePickerButton", mFilePickerButtonGroupBox->isChecked() );
cfg.insert( "FilePickerFilter", mFilePickerFilterLineEdit->text() );

if ( mUseLink->isChecked() )
{
@@ -160,7 +161,11 @@ void QgsExternalResourceConfigDlg::setConfig( const QgsEditorWidgetConfig& confi
}
if ( config.contains( "FilePicker" ) )
{
mFilePickerButton->setChecked( config.value( "FilePickerButton" ).toBool() );
mFilePickerButtonGroupBox->setChecked( config.value( "FilePickerButton" ).toBool() );
}
if ( config.contains( "FilePickerFilter" ) )
{
mFilePickerFilterLineEdit->setText( config.value( "Filter" ).toString() );
}

if ( config.contains( "UseLink" ) )
@@ -57,6 +57,8 @@ void QgsExternalResourceWidgetFactory::writeConfig( const QgsEditorWidgetConfig&
if ( config.contains( "DocumentViewer" ) )
configElement.setAttribute( "DocumentViewer", config.value( "DocumentViewer" ).toInt() );

if ( config.contains( "FilePickerFilter" ) )
configElement.setAttribute( "FilePickerFilter", config.value( "FilePickerFilter" ).toString() );

configElement.setAttribute( "StorageMode", config.value( "StorageMode" ).toString() );
}
@@ -93,6 +95,9 @@ QgsEditorWidgetConfig QgsExternalResourceWidgetFactory::readConfig( const QDomEl
if ( configElement.hasAttribute( "DocumentViewer" ) )
cfg.insert( "DocumentViewer", configElement.attribute( "DocumentViewer" ) );

if ( configElement.hasAttribute( "FilePickerFilter" ) )
cfg.insert( "FilePickerFilter", configElement.attribute( "FilePickerFilter" ) );


cfg.insert( "StorageMode", configElement.attribute( "StorageMode", "Files" ) );

@@ -82,7 +82,7 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget* editor )

if ( mQgsWidget )
{
mQgsWidget->filePickerwidget()->setStorageMode( QgsFilePickerWidget::File );
mQgsWidget->filePickerwidget()->setStorageMode( QgsFilePickerWidget::GetFile );
if ( config().contains( "UseLink" ) )
{
mQgsWidget->filePickerwidget()->setUseLink( config( "UseLink" ).toBool() );
@@ -115,6 +115,10 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget* editor )
{
mQgsWidget->setDocumentViewerContent(( QgsExternalResourceWidget::DocumentViewerContent )config( "DocumentViewer" ).toInt() );
}
if ( config().contains( "FilePickerFilter" ) )
{
mQgsWidget->filePickerwidget()->setFilter( config( "FilePickerFilter" ).toString() );
}
}
}

@@ -35,8 +35,9 @@ QgsFilePickerWidget::QgsFilePickerWidget( QWidget *parent )
, mUseLink( false )
, mFullUrl( false )
, mDialogTitle( QString() )
, mFilter( QString() )
, mDefaultRoot( QString() )
, mStorageMode( File )
, mStorageMode( GetFile )
{
setBackgroundRole( QPalette::Window );
setAutoFillBackground( true );
@@ -106,6 +107,16 @@ void QgsFilePickerWidget::setDialogTitle( QString title )
mDialogTitle = title;
}

QString QgsFilePickerWidget::filter() const
{
return mFilter;
}

void QgsFilePickerWidget::setFilter( const QString& filters )
{
mFilter = filters;
}

bool QgsFilePickerWidget::filePickerButtonVisible() const
{
return mButtonVisible;
@@ -207,12 +218,12 @@ void QgsFilePickerWidget::openFileDialog()
// Handle Storage
QString fileName;
QString title;
if ( mStorageMode == File )
if ( mStorageMode == GetFile )
{
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a file" );
fileName = QFileDialog::getOpenFileName( this, title, QFileInfo( oldPath ).absoluteFilePath() );
fileName = QFileDialog::getOpenFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter );
}
else if ( mStorageMode == Directory )
else if ( mStorageMode == GetDirectory )
{
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a directory" );
fileName = QFileDialog::getExistingDirectory( this, title, QFileInfo( oldPath ).absoluteFilePath(), QFileDialog::ShowDirsOnly );
@@ -225,11 +236,11 @@ void QgsFilePickerWidget::openFileDialog()
fileName = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
// Store the last used path:

if ( mStorageMode == File )
if ( mStorageMode == GetFile )
{
settings.setValue( "/UI/lastFileNameWidgetDir", QFileInfo( fileName ).absolutePath() );
}
else if ( mStorageMode == Directory )
else if ( mStorageMode == GetDirectory )
{
settings.setValue( "/UI/lastFileNameWidgetDir", fileName );
}
@@ -35,6 +35,7 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
Q_PROPERTY( bool useLink READ useLink WRITE setUseLink )
Q_PROPERTY( bool fullUrl READ fullUrl WRITE setFullUrl )
Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
Q_PROPERTY( QString filter READ filter WRITE setFilter )
Q_PROPERTY( QString defaultRoot READ defaultRoot WRITE setDefaultRoot )
Q_PROPERTY( StorageMode storageMode READ storageMode WRITE setStorageMode )
Q_PROPERTY( RelativeStorage relativeStorage READ relativeStorage WRITE setRelativeStorage )
@@ -45,8 +46,8 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
*/
enum StorageMode
{
File,
Directory
GetFile,
GetDirectory
};

/**
@@ -83,6 +84,14 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
*/
void setDialogTitle( QString title );

//! returns the filters used for QDialog::getOpenFileName
QString filter() const;
/**
* @brief 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 ';;',
*/
void setFilter( const QString &filter );

//! determines if the tool button is shown
bool filePickerButtonVisible() const;
//! determines if the tool button is shown
@@ -127,6 +136,7 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
bool mUseLink;
bool mFullUrl;
QString mDialogTitle;
QString mFilter;
QString mDefaultRoot;
StorageMode mStorageMode;
RelativeStorage mRelativeStorage;
@@ -235,13 +235,25 @@
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="mFilePickerButton">
<property name="text">
<string>Display button to open file picker</string>
<widget class="QGroupBox" name="mFilePickerButtonGroupBox">
<property name="title">
<string>Display button to open file dialog</string>
</property>
<property name="checked">
<property name="checkable">
<bool>true</bool>
</property>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Filter</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="mFilePickerFilterLineEdit"/>
</item>
</layout>
</widget>
</item>
</layout>
@@ -331,7 +343,7 @@
<resources/>
<connections/>
<buttongroups>
<buttongroup name="mStorageButtonGroup"/>
<buttongroup name="mRelativeButtonGroup"/>
<buttongroup name="mStorageButtonGroup"/>
</buttongroups>
</ui>

1 comment on commit 36157d4

@nyalldawson

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson commented on 36157d4 Jan 15, 2016

Great!

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