Skip to content

Commit

Permalink
External resource widget relative path fix for integrated viewer
Browse files Browse the repository at this point in the history
Fixes #14891
References #13283
  • Loading branch information
m-kuhn committed Jun 6, 2016
1 parent c7631f6 commit 8285356
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 12 deletions.
25 changes: 25 additions & 0 deletions python/gui/qgsexternalresourcewidget.sip
Expand Up @@ -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& );
Expand Down
4 changes: 2 additions & 2 deletions src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp
Expand Up @@ -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" ) )
{
Expand Down
66 changes: 56 additions & 10 deletions src/gui/qgsexternalresourcewidget.cpp
Expand Up @@ -16,7 +16,9 @@

#include "qgsexternalresourcewidget.h"
#include "qgspixmaplabel.h"
#include "qgsproject.h"

#include <QDir>
#include <QGridLayout>
#include <QVariant>
#include <QSettings>
Expand All @@ -31,6 +33,7 @@ QgsExternalResourceWidget::QgsExternalResourceWidget( QWidget *parent )
, mDocumentViewerContent( NoContent )
, mDocumentViewerHeight( 0 )
, mDocumentViewerWidth( 0 )
, mRelativeStorage( QgsFileWidget::Absolute )

{
setBackgroundRole( QPalette::Window );
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/gui/qgsexternalresourcewidget.h
Expand Up @@ -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
Expand Down Expand Up @@ -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& );
Expand All @@ -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;
Expand Down

2 comments on commit 8285356

@mbernasocchi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-kuhn can this easly be backported to 2.14

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on 8285356 Feb 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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