Skip to content

Commit

Permalink
Add filters to file picker and external widget
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Jan 15, 2016
1 parent 4adae28 commit 36157d4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 20 deletions.
12 changes: 10 additions & 2 deletions python/gui/qgsfilepickerwidget.sip
Expand Up @@ -9,8 +9,8 @@ class QgsFilePickerWidget : QWidget

enum StorageMode
{
File,
Directory
GetFile,
GetDirectory
};

enum RelativeStorage
Expand Down Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/gui/editorwidgets/qgsexternalresourceconfigdlg.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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" ) )
Expand Down
5 changes: 5 additions & 0 deletions src/gui/editorwidgets/qgsexternalresourcewidgetfactory.cpp
Expand Up @@ -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() );
}
Expand Down Expand Up @@ -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" ) );

Expand Down
6 changes: 5 additions & 1 deletion src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp
Expand Up @@ -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() );
Expand Down Expand Up @@ -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() );
}
}
}

Expand Down
23 changes: 17 additions & 6 deletions src/gui/qgsfilepickerwidget.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
}
Expand Down
14 changes: 12 additions & 2 deletions src/gui/qgsfilepickerwidget.h
Expand Up @@ -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 )
Expand All @@ -45,8 +46,8 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
*/
enum StorageMode
{
File,
Directory
GetFile,
GetDirectory
};

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -127,6 +136,7 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
bool mUseLink;
bool mFullUrl;
QString mDialogTitle;
QString mFilter;
QString mDefaultRoot;
StorageMode mStorageMode;
RelativeStorage mRelativeStorage;
Expand Down
22 changes: 17 additions & 5 deletions src/ui/editorwidgets/qgsexternalresourceconfigdlg.ui
Expand Up @@ -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>
Expand Down Expand Up @@ -331,7 +343,7 @@
<resources/>
<connections/>
<buttongroups>
<buttongroup name="mStorageButtonGroup"/>
<buttongroup name="mRelativeButtonGroup"/>
<buttongroup name="mStorageButtonGroup"/>
</buttongroups>
</ui>

1 comment on commit 36157d4

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Please sign in to comment.