Skip to content
Permalink
Browse files

allow definition of dialog title, display relative url in label

  • Loading branch information
3nids committed Jan 11, 2016
1 parent 29752a3 commit a88e23827658ff21a2e9811e64581191d49697c2
@@ -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 );

@@ -34,6 +34,7 @@ QgsFilePickerWidget::QgsFilePickerWidget( QWidget *parent )
, mButtonVisible( true )
, mUseLink( false )
, mFullUrl( false )
, mDialogTitle( QString() )
, mDefaultRoot( QString() )
, mStorageMode( File )
{
@@ -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;
@@ -147,7 +158,7 @@ void QgsFilePickerWidget::setStorageMode( QgsFilePickerWidget::StorageMode stora
mStorageMode = storageMode;
}

QgsFilePickerWidget::RelativeStorage QgsFilePickerWidget::relativeStorage()
QgsFilePickerWidget::RelativeStorage QgsFilePickerWidget::relativeStorage() const
{
return mRelativeStorage;
}
@@ -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() )
@@ -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 );
@@ -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 )
@@ -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;
@@ -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 )
@@ -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
@@ -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:
@@ -99,6 +108,7 @@ class GUI_EXPORT QgsFilePickerWidget : public QWidget
bool mButtonVisible;
bool mUseLink;
bool mFullUrl;
QString mDialogTitle;
QString mDefaultRoot;
StorageMode mStorageMode;
RelativeStorage mRelativeStorage;
@@ -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.
You can’t perform that action at this time.