Skip to content

Commit 8285356

Browse files
committedJun 6, 2016
External resource widget relative path fix for integrated viewer
Fixes #14891 References #13283
1 parent c7631f6 commit 8285356

File tree

4 files changed

+114
-12
lines changed

4 files changed

+114
-12
lines changed
 

‎python/gui/qgsexternalresourcewidget.sip

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ class QgsExternalResourceWidget : QWidget
5959
//! defines if the widget is readonly
6060
void setReadOnly( bool readOnly );
6161

62+
/**
63+
* Configures if paths are handled absolute or relative and if relative,
64+
* which should be the base path.
65+
*/
66+
QgsFileWidget::RelativeStorage relativeStorage() const;
67+
68+
/**
69+
* Configures if paths are handled absolute or relative and if relative,
70+
* which should be the base path.
71+
*/
72+
void setRelativeStorage( const QgsFileWidget::RelativeStorage& relativeStorage );
73+
74+
75+
/**
76+
* Configures the base path which should be used if the relativeStorage property
77+
* is set to QgsFileWidget::RelativeDefaultPath.
78+
*/
79+
QString defaultRoot() const;
80+
81+
/**
82+
* Configures the base path which should be used if the relativeStorage property
83+
* is set to QgsFileWidget::RelativeDefaultPath.
84+
*/
85+
void setDefaultRoot(const QString& defaultRoot);
86+
6287
signals:
6388
//! emitteed as soon as the current document changes
6489
void valueChanged( const QString& );

‎src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget* editor )
112112
}
113113
if ( config().contains( "DefaultRoot" ) )
114114
{
115-
mQgsWidget->fileWidget()->setDefaultRoot( config( "DefaultRoot" ).toString() );
115+
mQgsWidget->setDefaultRoot( config( "DefaultRoot" ).toString() );
116116
}
117117
if ( config().contains( "StorageMode" ) )
118118
{
119119
mQgsWidget->fileWidget()->setStorageMode(( QgsFileWidget::StorageMode )config( "StorageMode" ).toInt() );
120120
}
121121
if ( config().contains( "RelativeStorage" ) )
122122
{
123-
mQgsWidget->fileWidget()->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() );
123+
mQgsWidget->setRelativeStorage(( QgsFileWidget::RelativeStorage )config( "RelativeStorage" ).toInt() );
124124
}
125125
if ( config().contains( "FileWidget" ) )
126126
{

‎src/gui/qgsexternalresourcewidget.cpp

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
#include "qgsexternalresourcewidget.h"
1818
#include "qgspixmaplabel.h"
19+
#include "qgsproject.h"
1920

21+
#include <QDir>
2022
#include <QGridLayout>
2123
#include <QVariant>
2224
#include <QSettings>
@@ -31,6 +33,7 @@ QgsExternalResourceWidget::QgsExternalResourceWidget( QWidget *parent )
3133
, mDocumentViewerContent( NoContent )
3234
, mDocumentViewerHeight( 0 )
3335
, mDocumentViewerWidth( 0 )
36+
, mRelativeStorage( QgsFileWidget::Absolute )
3437

3538
{
3639
setBackgroundRole( QPalette::Window );
@@ -167,8 +170,48 @@ void QgsExternalResourceWidget::updateDocumentViewer()
167170
}
168171
}
169172

173+
QString QgsExternalResourceWidget::resolvePath( const QString& path )
174+
{
175+
switch ( mRelativeStorage )
176+
{
177+
case QgsFileWidget::Absolute:
178+
return path;
179+
break;
180+
case QgsFileWidget::RelativeProject:
181+
return QgsProject::instance()->fileInfo().dir().filePath( path );
182+
break;
183+
case QgsFileWidget::RelativeDefaultPath:
184+
return QDir( mDefaultRoot ).filePath( path );
185+
break;
186+
}
187+
}
188+
189+
QString QgsExternalResourceWidget::defaultRoot() const
190+
{
191+
return mDefaultRoot;
192+
}
193+
194+
void QgsExternalResourceWidget::setDefaultRoot( const QString& defaultRoot )
195+
{
196+
mFileWidget->setDefaultRoot( defaultRoot );
197+
mDefaultRoot = defaultRoot;
198+
}
199+
200+
QgsFileWidget::RelativeStorage QgsExternalResourceWidget::relativeStorage() const
201+
{
202+
return mRelativeStorage;
203+
}
204+
205+
void QgsExternalResourceWidget::setRelativeStorage( const QgsFileWidget::RelativeStorage& relativeStorage )
206+
{
207+
mFileWidget->setRelativeStorage( relativeStorage );
208+
mRelativeStorage = relativeStorage;
209+
}
210+
170211
void QgsExternalResourceWidget::loadDocument( const QString& path )
171212
{
213+
QString resolvedPath;
214+
172215
if ( path.isEmpty() )
173216
{
174217
#ifdef WITH_QTWEBKIT
@@ -183,20 +226,23 @@ void QgsExternalResourceWidget::loadDocument( const QString& path )
183226
updateDocumentViewer();
184227
}
185228
}
186-
229+
else
230+
{
231+
resolvedPath = resolvePath( path );
187232

188233
#ifdef WITH_QTWEBKIT
189-
if ( mDocumentViewerContent == Web )
190-
{
191-
mWebView->setUrl( QUrl( path ) );
192-
}
234+
if ( mDocumentViewerContent == Web )
235+
{
236+
mWebView->setUrl( QUrl( resolvedPath ) );
237+
}
193238
#endif
194239

195-
if ( mDocumentViewerContent == Image )
196-
{
197-
QPixmap pm( path );
198-
mPixmapLabel->setPixmap( pm );
199-
updateDocumentViewer();
240+
if ( mDocumentViewerContent == Image )
241+
{
242+
QPixmap pm( resolvedPath );
243+
mPixmapLabel->setPixmap( pm );
244+
updateDocumentViewer();
245+
}
200246
}
201247
}
202248

‎src/gui/qgsexternalresourcewidget.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
4040
Q_PROPERTY( DocumentViewerContent documentViewerContent READ documentViewerContent WRITE setDocumentViewerContent )
4141
Q_PROPERTY( int documentViewerHeight READ documentViewerHeight WRITE setDocumentViewerHeight )
4242
Q_PROPERTY( int documentViewerWidth READ documentViewerWidth WRITE setDocumentViewerWidth )
43+
Q_PROPERTY( QgsFileWidget::RelativeStorage relativeStorage READ relativeStorage WRITE setRelativeStorage )
44+
Q_PROPERTY( QString defaultRoot READ defaultRoot WRITE setDefaultRoot )
4345

4446
public:
4547
enum DocumentViewerContent
@@ -94,6 +96,31 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
9496
//! defines if the widget is readonly
9597
void setReadOnly( bool readOnly );
9698

99+
/**
100+
* Configures if paths are handled absolute or relative and if relative,
101+
* which should be the base path.
102+
*/
103+
QgsFileWidget::RelativeStorage relativeStorage() const;
104+
105+
/**
106+
* Configures if paths are handled absolute or relative and if relative,
107+
* which should be the base path.
108+
*/
109+
void setRelativeStorage( const QgsFileWidget::RelativeStorage& relativeStorage );
110+
111+
112+
/**
113+
* Configures the base path which should be used if the relativeStorage property
114+
* is set to QgsFileWidget::RelativeDefaultPath.
115+
*/
116+
QString defaultRoot() const;
117+
118+
/**
119+
* Configures the base path which should be used if the relativeStorage property
120+
* is set to QgsFileWidget::RelativeDefaultPath.
121+
*/
122+
void setDefaultRoot( const QString& defaultRoot );
123+
97124
signals:
98125
//! emitteed as soon as the current document changes
99126
void valueChanged( const QString& );
@@ -104,11 +131,15 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
104131
private:
105132
void updateDocumentViewer();
106133

134+
QString resolvePath( const QString& path );
135+
107136
//! properties
108137
bool mFileWidgetVisible;
109138
DocumentViewerContent mDocumentViewerContent;
110139
int mDocumentViewerHeight;
111140
int mDocumentViewerWidth;
141+
QgsFileWidget::RelativeStorage mRelativeStorage;
142+
QString mDefaultRoot; // configured default root path for QgsFileWidget::RelativeStorage::RelativeDefaultPath
112143

113144
//! UI objects
114145
QgsFileWidget* mFileWidget;

2 commit comments

Comments
 (2)

mbernasocchi commented on Feb 2, 2017

@mbernasocchi
Member

@m-kuhn can this easly be backported to 2.14

m-kuhn commented on Feb 2, 2017

@m-kuhn
MemberAuthor

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