Skip to content
Permalink
Browse files

External resource widget relative path fix for integrated viewer

Fixes #14891
References #13283
  • Loading branch information
m-kuhn committed Jun 6, 2016
1 parent c7631f6 commit 8285356525d62e8ba6c914cfe9aae9d17c0eeba3
@@ -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& );
@@ -112,15 +112,15 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget* editor )
}
if ( config().contains( "DefaultRoot" ) )
{
mQgsWidget->fileWidget()->setDefaultRoot( config( "DefaultRoot" ).toString() );
mQgsWidget->setDefaultRoot( config( "DefaultRoot" ).toString() );
}
if ( config().contains( "StorageMode" ) )
{
mQgsWidget->fileWidget()->setStorageMode(( QgsFileWidget::StorageMode )config( "StorageMode" ).toInt() );
}
if ( config().contains( "RelativeStorage" ) )
{
mQgsWidget->fileWidget()->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() );
mQgsWidget->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() );
}
if ( config().contains( "FileWidget" ) )
{
@@ -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();
}
}
}

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

2 comments on commit 8285356

@mbernasocchi

This comment has been minimized.

Copy link
Member

@mbernasocchi mbernasocchi replied Feb 2, 2017

@m-kuhn can this easly be backported to 2.14

@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Feb 2, 2017

@mbernasocchi it applies cleanly for me on top of release-2_14

git cherry-pick 8285356525d62e8ba6c914cfe9aae9d17c0eeba3

can you verify that it compiles and fixes the problem?

Please sign in to comment.
You can’t perform that action at this time.