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
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& );

View File

@ -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" ) )
{

View File

@ -16,7 +16,9 @@
#include "qgsexternalresourcewidget.h"
#include "qgspixmaplabel.h"
#include "qgsproject.h"
#include <QDir>
#include <QGridLayout>
#include <QVariant>
#include <QSettings>
@ -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();
}
}
}

View File

@ -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;