Skip to content

Commit

Permalink
allow definition of dialog title, display relative url in label
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Jan 13, 2016
1 parent 29752a3 commit a88e238
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/gui/editorwidgets/qgsexternalresourceconfigdlg.cpp
Expand Up @@ -60,9 +60,13 @@ void QgsExternalResourceConfigDlg::chooseDefaultPath()
{
QString dir;
if ( !mRootPath->text().isEmpty() )
{
dir = mRootPath->text();
}
else
{
dir = QSettings().value( "/UI/lastExternalResourceWidgetDir", QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) ) ).toString();
}

QString rootName = QFileDialog::getExistingDirectory( this, tr( "Select a directory" ), dir, QFileDialog::ShowDirsOnly );

Expand Down
46 changes: 32 additions & 14 deletions src/gui/qgsfilepickerwidget.cpp
Expand Up @@ -34,6 +34,7 @@ QgsFilePickerWidget::QgsFilePickerWidget( QWidget *parent )
, mButtonVisible( true )
, mUseLink( false )
, mFullUrl( false )
, mDialogTitle( QString() )
, mDefaultRoot( QString() )
, mStorageMode( File )
{
Expand Down Expand Up @@ -87,6 +88,16 @@ void QgsFilePickerWidget::setReadOnly( bool readOnly )
mLineEdit->setEnabled( !readOnly );
}

QString QgsFilePickerWidget::dialogTitle() const
{
return mDialogTitle;
}

void QgsFilePickerWidget::setDialogTitle( QString title )
{
mDialogTitle = title;
}

bool QgsFilePickerWidget::filePickerButtonVisible() const
{
return mButtonVisible;
Expand Down Expand Up @@ -147,7 +158,7 @@ void QgsFilePickerWidget::setStorageMode( QgsFilePickerWidget::StorageMode stora
mStorageMode = storageMode;
}

QgsFilePickerWidget::RelativeStorage QgsFilePickerWidget::relativeStorage()
QgsFilePickerWidget::RelativeStorage QgsFilePickerWidget::relativeStorage() const
{
return mRelativeStorage;
}
Expand Down Expand Up @@ -182,13 +193,16 @@ void QgsFilePickerWidget::openFileDialog()

// Handle Storage
QString fileName;
QString title;
if ( mStorageMode == File )
{
fileName = QFileDialog::getOpenFileName( this, tr( "Select a file" ), QFileInfo( oldPath ).absoluteFilePath() );
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a file" );
fileName = QFileDialog::getOpenFileName( this, title, QFileInfo( oldPath ).absoluteFilePath() );
}
else if ( mStorageMode == Directory )
{
fileName = QFileDialog::getExistingDirectory( this, tr( "Select a directory" ), QFileInfo( oldPath ).absoluteFilePath(), QFileDialog::ShowDirsOnly );
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a directory" );
fileName = QFileDialog::getExistingDirectory( this, title, QFileInfo( oldPath ).absoluteFilePath(), QFileDialog::ShowDirsOnly );
}

if ( fileName.isEmpty() )
Expand All @@ -199,9 +213,13 @@ void QgsFilePickerWidget::openFileDialog()
// Store the last used path:

if ( mStorageMode == File )
{
settings.setValue( "/UI/lastFileNameWidgetDir", QFileInfo( fileName ).absolutePath() );
}
else if ( mStorageMode == Directory )
{
settings.setValue( "/UI/lastFileNameWidgetDir", fileName );
}

// Handle relative Path storage
fileName = relativePath( fileName, true );
Expand All @@ -211,7 +229,7 @@ void QgsFilePickerWidget::openFileDialog()
}


QString QgsFilePickerWidget::relativePath( QString filePath, bool removeRelative )
QString QgsFilePickerWidget::relativePath( QString filePath, bool removeRelative ) const
{
QString RelativePath;
if ( mRelativeStorage == RelativeProject )
Expand All @@ -234,31 +252,31 @@ QString QgsFilePickerWidget::relativePath( QString filePath, bool removeRelative
}


QString QgsFilePickerWidget::toUrl( const QString& value )
QString QgsFilePickerWidget::toUrl(const QString& path ) const
{
QString rep;
if ( value.isEmpty() )
if ( path.isEmpty() )
{
rep = QSettings().value( "qgis/nullValue", "NULL" ).toString();
}

QString urlStr = relativePath( value, false );
QUrl theUrl = QUrl::fromUserInput( urlStr );
if ( !theUrl.isValid() or !theUrl.isLocalFile() )
QString urlStr = relativePath( path, false );
QUrl url = QUrl::fromUserInput( urlStr );
if ( !url.isValid() or !url.isLocalFile() )
{
QgsDebugMsg( QString( "URL: %1 is not valid or not a local file !" ).arg( value ) );
rep = value;
QgsDebugMsg( QString( "URL: %1 is not valid or not a local file !" ).arg( path ) );
rep = path;
}

QString filePath = theUrl.toString();
QString pathStr = url.toString();
if ( mFullUrl )
{
rep = QString( "<a href=\"%1\">%2</a>" ).arg( filePath, urlStr );
rep = QString( "<a href=\"%1\">%2</a>" ).arg( pathStr, path );
}
else
{
QString fileName = QFileInfo( urlStr ).fileName();
rep = QString( "<a href=\"%1\">%2</a>" ).arg( filePath, fileName );
rep = QString( "<a href=\"%1\">%2</a>" ).arg( pathStr, fileName );
}

return rep;
Expand Down
16 changes: 13 additions & 3 deletions src/gui/qgsfilepickerwidget.h
Expand Up @@ -31,6 +31,7 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
Q_PROPERTY( bool filePickerButtonVisible READ filePickerButtonVisible WRITE setFilePickerButtonVisible )
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 defaultRoot READ defaultRoot WRITE setDefaultRoot )
Q_PROPERTY( StorageMode storageMode READ storageMode WRITE setStorageMode )
Q_PROPERTY( RelativeStorage relativeStorage READ relativeStorage WRITE setRelativeStorage )
Expand Down Expand Up @@ -61,6 +62,14 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
//! defines if the widget is readonly
void setReadOnly( bool readOnly );

//! returns the open file dialog title
QString dialogTitle() const;
/**
* @brief setDialogTitle defines the open file dialog title
* @note if not defined, the title is "Select a file" or "Select a directory" depending on the configuration.
*/
void setDialogTitle( QString title );

//! determines if the tool button is shown
bool filePickerButtonVisible() const;
//! determines if the tool button is shown
Expand All @@ -84,7 +93,7 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
void setStorageMode( QgsFilePickerWidget::StorageMode storageMode );

//! determines if the relative path is with respect to the project path or the default path
QgsFilePickerWidget::RelativeStorage relativeStorage();
QgsFilePickerWidget::RelativeStorage relativeStorage() const;
void setRelativeStorage( QgsFilePickerWidget::RelativeStorage relativeStorage );

signals:
Expand All @@ -99,6 +108,7 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
bool mButtonVisible;
bool mUseLink;
bool mFullUrl;
QString mDialogTitle;
QString mDefaultRoot;
StorageMode mStorageMode;
RelativeStorage mRelativeStorage;
Expand All @@ -108,10 +118,10 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
QToolButton* mFilePickerButton;

//! returns a HTML code with a link to the given file path
QString toUrl( const QString& value );
QString toUrl( const QString& path ) const;

//! Returns a filePath with relative path options applied (or not) !
QString relativePath( QString filePath, bool removeRelative );
QString relativePath( QString filePath, bool removeRelative ) const;
};

#endif // QGSFILEPICKERWIDGET_H

0 comments on commit a88e238

Please sign in to comment.