diff --git a/python/gui/qgsexternalresourcewidget.sip b/python/gui/qgsexternalresourcewidget.sip index 687df222497..9a81c771c3f 100644 --- a/python/gui/qgsexternalresourcewidget.sip +++ b/python/gui/qgsexternalresourcewidget.sip @@ -59,6 +59,31 @@ class QgsExternalResourceWidget : QWidget //! defines if the widget is readonly void setReadOnly( bool readOnly ); + /** + * Configures if paths are handled absolute or relative and if relative, + * which should be the base path. + */ + QgsFileWidget::RelativeStorage relativeStorage() const; + + /** + * Configures if paths are handled absolute or relative and if relative, + * which should be the base path. + */ + void setRelativeStorage( const QgsFileWidget::RelativeStorage& relativeStorage ); + + + /** + * Configures the base path which should be used if the relativeStorage property + * is set to QgsFileWidget::RelativeDefaultPath. + */ + QString defaultRoot() const; + + /** + * Configures the base path which should be used if the relativeStorage property + * is set to QgsFileWidget::RelativeDefaultPath. + */ + void setDefaultRoot(const QString& defaultRoot); + signals: //! emitteed as soon as the current document changes void valueChanged( const QString& ); diff --git a/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp b/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp index 36d7711a868..37507ebd40b 100644 --- a/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp +++ b/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp @@ -112,7 +112,7 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget* editor ) } if ( config().contains( "DefaultRoot" ) ) { - mQgsWidget->fileWidget()->setDefaultRoot( config( "DefaultRoot" ).toString() ); + mQgsWidget->setDefaultRoot( config( "DefaultRoot" ).toString() ); } if ( config().contains( "StorageMode" ) ) { @@ -120,7 +120,7 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget* editor ) } if ( config().contains( "RelativeStorage" ) ) { - mQgsWidget->fileWidget()->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() ); + mQgsWidget->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() ); } if ( config().contains( "FileWidget" ) ) { diff --git a/src/gui/qgsexternalresourcewidget.cpp b/src/gui/qgsexternalresourcewidget.cpp index 560541042fe..69ffd1d580e 100644 --- a/src/gui/qgsexternalresourcewidget.cpp +++ b/src/gui/qgsexternalresourcewidget.cpp @@ -16,7 +16,9 @@ #include "qgsexternalresourcewidget.h" #include "qgspixmaplabel.h" +#include "qgsproject.h" +#include #include #include #include @@ -31,6 +33,7 @@ QgsExternalResourceWidget::QgsExternalResourceWidget( QWidget *parent ) , mDocumentViewerContent( NoContent ) , mDocumentViewerHeight( 0 ) , mDocumentViewerWidth( 0 ) + , mRelativeStorage( QgsFileWidget::Absolute ) { setBackgroundRole( QPalette::Window ); @@ -167,8 +170,48 @@ void QgsExternalResourceWidget::updateDocumentViewer() } } +QString QgsExternalResourceWidget::resolvePath( const QString& path ) +{ + switch ( mRelativeStorage ) + { + case QgsFileWidget::Absolute: + return path; + break; + case QgsFileWidget::RelativeProject: + return QgsProject::instance()->fileInfo().dir().filePath( path ); + break; + case QgsFileWidget::RelativeDefaultPath: + return QDir( mDefaultRoot ).filePath( path ); + break; + } +} + +QString QgsExternalResourceWidget::defaultRoot() const +{ + return mDefaultRoot; +} + +void QgsExternalResourceWidget::setDefaultRoot( const QString& defaultRoot ) +{ + mFileWidget->setDefaultRoot( defaultRoot ); + mDefaultRoot = defaultRoot; +} + +QgsFileWidget::RelativeStorage QgsExternalResourceWidget::relativeStorage() const +{ + return mRelativeStorage; +} + +void QgsExternalResourceWidget::setRelativeStorage( const QgsFileWidget::RelativeStorage& relativeStorage ) +{ + mFileWidget->setRelativeStorage( relativeStorage ); + mRelativeStorage = relativeStorage; +} + void QgsExternalResourceWidget::loadDocument( const QString& path ) { + QString resolvedPath; + if ( path.isEmpty() ) { #ifdef WITH_QTWEBKIT @@ -183,20 +226,23 @@ void QgsExternalResourceWidget::loadDocument( const QString& path ) updateDocumentViewer(); } } - + else + { + resolvedPath = resolvePath( path ); #ifdef WITH_QTWEBKIT - if ( mDocumentViewerContent == Web ) - { - mWebView->setUrl( QUrl( path ) ); - } + if ( mDocumentViewerContent == Web ) + { + mWebView->setUrl( QUrl( resolvedPath ) ); + } #endif - if ( mDocumentViewerContent == Image ) - { - QPixmap pm( path ); - mPixmapLabel->setPixmap( pm ); - updateDocumentViewer(); + if ( mDocumentViewerContent == Image ) + { + QPixmap pm( resolvedPath ); + mPixmapLabel->setPixmap( pm ); + updateDocumentViewer(); + } } } diff --git a/src/gui/qgsexternalresourcewidget.h b/src/gui/qgsexternalresourcewidget.h index da679e4c73d..e5f0fe828fc 100644 --- a/src/gui/qgsexternalresourcewidget.h +++ b/src/gui/qgsexternalresourcewidget.h @@ -40,6 +40,8 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget Q_PROPERTY( DocumentViewerContent documentViewerContent READ documentViewerContent WRITE setDocumentViewerContent ) Q_PROPERTY( int documentViewerHeight READ documentViewerHeight WRITE setDocumentViewerHeight ) Q_PROPERTY( int documentViewerWidth READ documentViewerWidth WRITE setDocumentViewerWidth ) + Q_PROPERTY( QgsFileWidget::RelativeStorage relativeStorage READ relativeStorage WRITE setRelativeStorage ) + Q_PROPERTY( QString defaultRoot READ defaultRoot WRITE setDefaultRoot ) public: enum DocumentViewerContent @@ -94,6 +96,31 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget //! defines if the widget is readonly void setReadOnly( bool readOnly ); + /** + * Configures if paths are handled absolute or relative and if relative, + * which should be the base path. + */ + QgsFileWidget::RelativeStorage relativeStorage() const; + + /** + * Configures if paths are handled absolute or relative and if relative, + * which should be the base path. + */ + void setRelativeStorage( const QgsFileWidget::RelativeStorage& relativeStorage ); + + + /** + * Configures the base path which should be used if the relativeStorage property + * is set to QgsFileWidget::RelativeDefaultPath. + */ + QString defaultRoot() const; + + /** + * Configures the base path which should be used if the relativeStorage property + * is set to QgsFileWidget::RelativeDefaultPath. + */ + void setDefaultRoot( const QString& defaultRoot ); + signals: //! emitteed as soon as the current document changes void valueChanged( const QString& ); @@ -104,11 +131,15 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget private: void updateDocumentViewer(); + QString resolvePath( const QString& path ); + //! properties bool mFileWidgetVisible; DocumentViewerContent mDocumentViewerContent; int mDocumentViewerHeight; int mDocumentViewerWidth; + QgsFileWidget::RelativeStorage mRelativeStorage; + QString mDefaultRoot; // configured default root path for QgsFileWidget::RelativeStorage::RelativeDefaultPath //! UI objects QgsFileWidget* mFileWidget;