External resource widget relative path fix for integrated viewer

Fixes #14891
References #13283
This commit is contained in:
Matthias Kuhn 2016-06-06 19:18:42 +02:00
parent c7631f68b1
commit 8285356525
4 changed files with 114 additions and 12 deletions

View File

@ -59,6 +59,31 @@ class QgsExternalResourceWidget : QWidget
//! defines if the widget is readonly //! defines if the widget is readonly
void setReadOnly( bool 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: signals:
//! emitteed as soon as the current document changes //! emitteed as soon as the current document changes
void valueChanged( const QString& ); void valueChanged( const QString& );

View File

@ -112,7 +112,7 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget* editor )
} }
if ( config().contains( "DefaultRoot" ) ) if ( config().contains( "DefaultRoot" ) )
{ {
mQgsWidget->fileWidget()->setDefaultRoot( config( "DefaultRoot" ).toString() ); mQgsWidget->setDefaultRoot( config( "DefaultRoot" ).toString() );
} }
if ( config().contains( "StorageMode" ) ) if ( config().contains( "StorageMode" ) )
{ {
@ -120,7 +120,7 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget* editor )
} }
if ( config().contains( "RelativeStorage" ) ) if ( config().contains( "RelativeStorage" ) )
{ {
mQgsWidget->fileWidget()->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() ); mQgsWidget->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() );
} }
if ( config().contains( "FileWidget" ) ) if ( config().contains( "FileWidget" ) )
{ {

View File

@ -16,7 +16,9 @@
#include "qgsexternalresourcewidget.h" #include "qgsexternalresourcewidget.h"
#include "qgspixmaplabel.h" #include "qgspixmaplabel.h"
#include "qgsproject.h"
#include <QDir>
#include <QGridLayout> #include <QGridLayout>
#include <QVariant> #include <QVariant>
#include <QSettings> #include <QSettings>
@ -31,6 +33,7 @@ QgsExternalResourceWidget::QgsExternalResourceWidget( QWidget *parent )
, mDocumentViewerContent( NoContent ) , mDocumentViewerContent( NoContent )
, mDocumentViewerHeight( 0 ) , mDocumentViewerHeight( 0 )
, mDocumentViewerWidth( 0 ) , mDocumentViewerWidth( 0 )
, mRelativeStorage( QgsFileWidget::Absolute )
{ {
setBackgroundRole( QPalette::Window ); 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 ) void QgsExternalResourceWidget::loadDocument( const QString& path )
{ {
QString resolvedPath;
if ( path.isEmpty() ) if ( path.isEmpty() )
{ {
#ifdef WITH_QTWEBKIT #ifdef WITH_QTWEBKIT
@ -183,20 +226,23 @@ void QgsExternalResourceWidget::loadDocument( const QString& path )
updateDocumentViewer(); updateDocumentViewer();
} }
} }
else
{
resolvedPath = resolvePath( path );
#ifdef WITH_QTWEBKIT #ifdef WITH_QTWEBKIT
if ( mDocumentViewerContent == Web ) if ( mDocumentViewerContent == Web )
{ {
mWebView->setUrl( QUrl( path ) ); mWebView->setUrl( QUrl( resolvedPath ) );
} }
#endif #endif
if ( mDocumentViewerContent == Image ) if ( mDocumentViewerContent == Image )
{ {
QPixmap pm( path ); QPixmap pm( resolvedPath );
mPixmapLabel->setPixmap( pm ); mPixmapLabel->setPixmap( pm );
updateDocumentViewer(); updateDocumentViewer();
}
} }
} }

View File

@ -40,6 +40,8 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
Q_PROPERTY( DocumentViewerContent documentViewerContent READ documentViewerContent WRITE setDocumentViewerContent ) Q_PROPERTY( DocumentViewerContent documentViewerContent READ documentViewerContent WRITE setDocumentViewerContent )
Q_PROPERTY( int documentViewerHeight READ documentViewerHeight WRITE setDocumentViewerHeight ) Q_PROPERTY( int documentViewerHeight READ documentViewerHeight WRITE setDocumentViewerHeight )
Q_PROPERTY( int documentViewerWidth READ documentViewerWidth WRITE setDocumentViewerWidth ) 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: public:
enum DocumentViewerContent enum DocumentViewerContent
@ -94,6 +96,31 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
//! defines if the widget is readonly //! defines if the widget is readonly
void setReadOnly( bool 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: signals:
//! emitteed as soon as the current document changes //! emitteed as soon as the current document changes
void valueChanged( const QString& ); void valueChanged( const QString& );
@ -104,11 +131,15 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
private: private:
void updateDocumentViewer(); void updateDocumentViewer();
QString resolvePath( const QString& path );
//! properties //! properties
bool mFileWidgetVisible; bool mFileWidgetVisible;
DocumentViewerContent mDocumentViewerContent; DocumentViewerContent mDocumentViewerContent;
int mDocumentViewerHeight; int mDocumentViewerHeight;
int mDocumentViewerWidth; int mDocumentViewerWidth;
QgsFileWidget::RelativeStorage mRelativeStorage;
QString mDefaultRoot; // configured default root path for QgsFileWidget::RelativeStorage::RelativeDefaultPath
//! UI objects //! UI objects
QgsFileWidget* mFileWidget; QgsFileWidget* mFileWidget;