QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsexternalresourcewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexternalresourcewidget.cpp
3 
4  ---------------------
5  begin : 16.12.2015
6  copyright : (C) 2015 by Denis Rouzaud
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
18 #include "qgspixmaplabel.h"
19 #include "qgsproject.h"
20 #include "qgsapplication.h"
22 #include "qgstaskmanager.h"
23 #include "qgsexternalstorage.h"
24 #include "qgsmessagebar.h"
26 
27 #include <QDir>
28 #include <QGridLayout>
29 #include <QVariant>
30 #include <QSettings>
31 #include <QImageReader>
32 #include <QToolButton>
33 #include <QMimeType>
34 #include <QMimeDatabase>
35 #include <QMovie>
36 #ifdef WITH_QTWEBKIT
37 #include <QWebView>
38 #endif
39 
41  : QWidget( parent )
42 {
43  setBackgroundRole( QPalette::Window );
44  setAutoFillBackground( true );
45 
46  QGridLayout *layout = new QGridLayout();
47  layout->setContentsMargins( 0, 0, 0, 0 );
48 
49  mFileWidget = new QgsExternalStorageFileWidget( this );
50  layout->addWidget( mFileWidget, 0, 0 );
51  mFileWidget->setVisible( mFileWidgetVisible );
52 
53  mPixmapLabel = new QgsPixmapLabel( this );
54  layout->addWidget( mPixmapLabel, 1, 0 );
55 
56 #ifdef WITH_QTWEBKIT
57  mWebView = new QWebView( this );
58  mWebView->setAcceptDrops( false );
59  layout->addWidget( mWebView, 2, 0 );
60 #endif
61 
62  mLoadingLabel = new QLabel( this );
63  layout->addWidget( mLoadingLabel, 3, 0 );
64  mLoadingMovie = new QMovie( QgsApplication::iconPath( QStringLiteral( "/mIconLoading.gif" ) ), QByteArray(), this );
65  mLoadingMovie->setScaledSize( QSize( 32, 32 ) );
66  mLoadingLabel->setMovie( mLoadingMovie );
67 
68  mErrorLabel = new QLabel( this );
69  layout->addWidget( mErrorLabel, 4, 0 );
70  mErrorLabel->setPixmap( QPixmap( QgsApplication::iconPath( QStringLiteral( "/mIconWarning.svg" ) ) ) );
71 
72  updateDocumentViewer();
73 
74  setLayout( layout );
75 
76  connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsExternalResourceWidget::loadDocument );
78 }
79 
80 QVariant QgsExternalResourceWidget::documentPath( QVariant::Type type ) const
81 {
82  const QString path = mFileWidget->filePath();
83  if ( path.isEmpty() || path == QgsApplication::nullRepresentation() )
84  {
85  return QVariant( type );
86  }
87  else
88  {
89  return path;
90  }
91 }
92 
93 void QgsExternalResourceWidget::setDocumentPath( const QVariant &path )
94 {
95  mFileWidget->setFilePath( path.toString() );
96 }
97 
99 {
100  return mFileWidget;
101 }
102 
104 {
105  return mFileWidgetVisible;
106 }
107 
109 {
110  mFileWidgetVisible = visible;
111  mFileWidget->setVisible( visible );
112 }
113 
115 {
116  return mDocumentViewerContent;
117 }
118 
120 {
121  mDocumentViewerContent = content;
122  if ( mDocumentViewerContent != Image )
123  updateDocumentViewer();
124  loadDocument( mFileWidget->filePath() );
125 }
126 
128 {
129  return mDocumentViewerHeight;
130 }
131 
133 {
134  mDocumentViewerHeight = height;
135  updateDocumentViewer();
136 }
137 
139 {
140  return mDocumentViewerWidth;
141 }
142 
144 {
145  mDocumentViewerWidth = width;
146  updateDocumentViewer();
147 }
148 
150 {
151  mFileWidget->setReadOnly( readOnly );
152 }
153 
154 void QgsExternalResourceWidget::updateDocumentViewer()
155 {
156  mErrorLabel->setVisible( false );
157  mLoadingLabel->setVisible( false );
158  mLoadingMovie->stop();
159 
160 #ifdef WITH_QTWEBKIT
161  mWebView->setVisible( mDocumentViewerContent == Web );
162 #endif
163 
164  mPixmapLabel->setVisible( mDocumentViewerContent == Image );
165 
166  if ( mDocumentViewerContent == Image )
167  {
168 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
169  const QPixmap pm = mPixmapLabel->pixmap() ? *mPixmapLabel->pixmap() : QPixmap();
170 #else
171  const QPixmap pm = mPixmapLabel->pixmap();
172 #endif
173 
174  if ( !pm || pm.isNull() )
175  {
176  mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
177  }
178  else
179  {
180  QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
181  if ( size.width() == 0 && size.height() > 0 )
182  {
183  size.setWidth( size.height() * pm.size().width() / pm.size().height() );
184  }
185  else if ( size.width() > 0 && size.height() == 0 )
186  {
187  size.setHeight( size.width() * pm.size().height() / pm.size().width() );
188  }
189 
190  if ( size.width() != 0 || size.height() != 0 )
191  {
192  mPixmapLabel->setMinimumSize( size );
193  mPixmapLabel->setMaximumSize( size );
194  }
195  }
196  }
197 }
198 
199 QString QgsExternalResourceWidget::resolvePath( const QString &path )
200 {
201  switch ( mRelativeStorage )
202  {
204  return path;
205  break;
207  return QFileInfo( QgsProject::instance()->absoluteFilePath() ).dir().filePath( path );
208  break;
210  return QDir( mDefaultRoot ).filePath( path );
211  break;
212  }
213  return QString(); // avoid warnings
214 }
215 
217 {
218  return mDefaultRoot;
219 }
220 
221 void QgsExternalResourceWidget::setDefaultRoot( const QString &defaultRoot )
222 {
223  mFileWidget->setDefaultRoot( defaultRoot );
224  mDefaultRoot = defaultRoot;
225 }
226 
228 {
229  return mRelativeStorage;
230 }
231 
233 {
234  mFileWidget->setRelativeStorage( relativeStorage );
235  mRelativeStorage = relativeStorage;
236 }
237 
238 void QgsExternalResourceWidget::setStorageType( const QString &storageType )
239 {
240  mFileWidget->setStorageType( storageType );
241 }
242 
244 {
245  return mFileWidget->storageType();
246 }
247 
249 {
250  mFileWidget->setStorageAuthConfigId( authCfg );
251 }
252 
254 {
255  return mFileWidget->storageAuthConfigId();
256 }
257 
259 {
260  mFileWidget->setMessageBar( messageBar );
261 }
262 
264 {
265  return mFileWidget->messageBar();
266 }
267 
268 void QgsExternalResourceWidget::updateDocumentContent( const QString &filePath )
269 {
270 #ifdef WITH_QTWEBKIT
271  if ( mDocumentViewerContent == Web )
272  {
273  mWebView->load( QUrl::fromEncoded( filePath.toUtf8() ) );
274  mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled, true );
275  }
276 #endif
277 
278  if ( mDocumentViewerContent == Image )
279  {
280  // use an image reader to ensure image orientation and transforms are correctly handled
281  QImageReader ir( filePath );
282  ir.setAutoTransform( true );
283  const QPixmap pm = QPixmap::fromImage( ir.read() );
284  if ( !pm.isNull() )
285  mPixmapLabel->setPixmap( pm );
286  else
287  mPixmapLabel->clear();
288  }
289 
290  updateDocumentViewer();
291 }
292 
293 void QgsExternalResourceWidget::clearContent()
294 {
295 #ifdef WITH_QTWEBKIT
296  if ( mDocumentViewerContent == Web )
297  {
298  mWebView->load( QUrl( QStringLiteral( "about:blank" ) ) );
299  }
300 #endif
301  if ( mDocumentViewerContent == Image )
302  {
303  mPixmapLabel->clear();
304  }
305 
306  updateDocumentViewer();
307 }
308 
309 void QgsExternalResourceWidget::loadDocument( const QString &path )
310 {
311  if ( path.isEmpty() || path == QgsApplication::nullRepresentation() )
312  {
313  if ( mFileWidget->externalStorage() && mContent )
314  {
315  mContent->cancel();
316  mContent.clear();
317  }
318 
319  clearContent();
320  }
321  else if ( mDocumentViewerContent != NoContent )
322  {
323  const QString resolvedPath = resolvePath( path );
324 
325  if ( mFileWidget->externalStorage() )
326  {
327  if ( mContent )
328  {
329  mContent->cancel();
330  }
331 
332  mContent = mFileWidget->externalStorage()->fetch( resolvedPath, storageAuthConfigId() );
333 
334 #ifdef WITH_QTWEBKIT
335  mWebView->setVisible( false );
336 #endif
337  mPixmapLabel->setVisible( false );
338  mErrorLabel->setVisible( false );
339  mLoadingLabel->setVisible( true );
340  mLoadingMovie->start();
341  connect( mContent, &QgsExternalStorageFetchedContent::fetched, this, &QgsExternalResourceWidget::onFetchFinished );
342  connect( mContent, &QgsExternalStorageFetchedContent::errorOccurred, this, &QgsExternalResourceWidget::onFetchFinished );
343  connect( mContent, &QgsExternalStorageFetchedContent::canceled, this, &QgsExternalResourceWidget::onFetchFinished );
344 
345  mContent->fetch();
346  }
347  else
348  {
349  updateDocumentContent( resolvedPath );
350  }
351  }
352 }
353 
354 void QgsExternalResourceWidget::onFetchFinished()
355 {
356  QgsExternalStorageFetchedContent *content = qobject_cast<QgsExternalStorageFetchedContent *>( sender() );
357 
358  if ( content == mContent && mContent->status() == Qgis::ContentStatus::Failed )
359  {
360 #ifdef WITH_QTWEBKIT
361  mWebView->setVisible( false );
362 #endif
363  mPixmapLabel->setVisible( false );
364  mLoadingLabel->setVisible( false );
365  mLoadingMovie->stop();
366  mErrorLabel->setVisible( true );
367 
368  if ( messageBar() )
369  {
370  messageBar()->pushWarning( tr( "Fetching External Resource" ),
371  tr( "Error while fetching external resource '%1' : %2" ).arg(
372  mFileWidget->filePath(), mContent->errorString() ) );
373  }
374  }
375  else if ( content == mContent && mContent->status() == Qgis::ContentStatus::Finished )
376  {
377  const QString filePath = mDocumentViewerContent == Web
378  ? QUrl::fromLocalFile( mContent->filePath() ).toString()
379  : mContent->filePath();
380 
381  updateDocumentContent( filePath );
382  }
383 
384  content->deleteLater();
385 }
QgsFileWidget::fileChanged
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
QgsExternalResourceWidget::fileWidget
QgsExternalStorageFileWidget * fileWidget()
Returns file widget to allow its configuration.
Definition: qgsexternalresourcewidget.cpp:98
QgsExternalResourceWidget::relativeStorage
QgsFileWidget::RelativeStorage relativeStorage
Definition: qgsexternalresourcewidget.h:69
QgsExternalStorageFetchedContent
Class for QgsExternalStorage fetched content.
Definition: qgsexternalstorage.h:170
Qgis::ContentStatus::Failed
@ Failed
Content fetching/storing has failed.
qgstaskmanager.h
QgsExternalResourceWidget::setDefaultRoot
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
Definition: qgsexternalresourcewidget.cpp:221
QgsPixmapLabel::clear
void clear()
Clears any label contents.
Definition: qgspixmaplabel.cpp:69
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
QgsExternalResourceWidget::documentViewerContent
DocumentViewerContent documentViewerContent
Definition: qgsexternalresourcewidget.h:66
QgsApplication::iconPath
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
Definition: qgsapplication.cpp:682
QgsExternalResourceWidget::setMessageBar
void setMessageBar(QgsMessageBar *messageBar)
Set messageBar to report messages.
Definition: qgsexternalresourcewidget.cpp:258
QgsExternalResourceWidget::storageType
QString storageType() const
Returns storage type unique identifier as defined in QgsExternalStorageRegistry.
Definition: qgsexternalresourcewidget.cpp:243
QgsExternalResourceWidget::NoContent
@ NoContent
Definition: qgsexternalresourcewidget.h:75
QgsExternalResourceWidget::DocumentViewerContent
DocumentViewerContent
Definition: qgsexternalresourcewidget.h:73
QgsExternalResourceWidget::valueChanged
void valueChanged(const QString &)
emitteed as soon as the current document changes
QgsFileWidget::RelativeProject
@ RelativeProject
Definition: qgsfilewidget.h:81
qgsapplication.h
QgsExternalResourceWidget::messageBar
QgsMessageBar * messageBar() const
Returns message bar used to report messages.
Definition: qgsexternalresourcewidget.cpp:263
QgsExternalStorageFileWidget
The QgsExternalStorageFileWidget class creates a widget for selecting a file or a folder and stores i...
Definition: qgsexternalstoragefilewidget.h:39
QgsExternalResourceWidget::setReadOnly
void setReadOnly(bool readOnly)
defines if the widget is readonly
Definition: qgsexternalresourcewidget.cpp:149
QgsExternalResourceWidget::setRelativeStorage
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
Configures if paths are handled absolute or relative and if relative, which should be the base path.
Definition: qgsexternalresourcewidget.cpp:232
QgsExternalResourceWidget::setDocumentViewerWidth
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
Definition: qgsexternalresourcewidget.cpp:143
QgsExternalResourceWidget::documentPath
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
Definition: qgsexternalresourcewidget.cpp:80
QgsPixmapLabel::setPixmap
void setPixmap(const QPixmap &)
Definition: qgspixmaplabel.cpp:24
QgsExternalStorageFileWidget::externalStorage
QgsExternalStorage * externalStorage() const
Returns external storage used to store selected file names, nullptr if none have been defined.
Definition: qgsexternalstoragefilewidget.cpp:97
QgsExternalResourceWidget::setStorageAuthConfigId
void setStorageAuthConfigId(const QString &authCfg)
Sets the authentication configuration ID to be used for the current external storage (if defined)
Definition: qgsexternalresourcewidget.cpp:248
QgsExternalStorageFileWidget::storageType
QString storageType
Definition: qgsexternalstoragefilewidget.h:52
QgsExternalStorageFetchedContent::fetched
void fetched()
The signal is emitted when the resource has successfully been fetched.
QgsPixmapLabel
The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given to the widget by the ...
Definition: qgspixmaplabel.h:28
QgsExternalResourceWidget::setFileWidgetVisible
void setFileWidgetVisible(bool visible)
Sets the visibility of the file widget in the layout.
Definition: qgsexternalresourcewidget.cpp:108
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:2018
QgsExternalResourceWidget::fileWidgetVisible
bool fileWidgetVisible
Definition: qgsexternalresourcewidget.h:65
QgsExternalStorageContent::canceled
void canceled()
The signal is emitted when content fetching/storing has been canceled.
QgsExternalResourceWidget::QgsExternalResourceWidget
QgsExternalResourceWidget(QWidget *parent=nullptr)
QgsExternalResourceWidget creates a widget with a file widget and a document viewer Both part of the ...
Definition: qgsexternalresourcewidget.cpp:40
qgsnetworkaccessmanager.h
qgsexternalstorage.h
QgsExternalStorageFileWidget::setReadOnly
void setReadOnly(bool readOnly) override
Sets whether the widget should be read only.
Definition: qgsexternalstoragefilewidget.cpp:81
qgsexternalresourcewidget.h
QgsExternalResourceWidget::storageAuthConfigId
QString storageAuthConfigId() const
Returns the authentication configuration ID used for the current external storage (if defined)
Definition: qgsexternalresourcewidget.cpp:253
QgsFileWidget::Absolute
@ Absolute
Definition: qgsfilewidget.h:80
QgsFileWidget::setRelativeStorage
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
Sets whether the relative path is with respect to the project path or the default path.
Definition: qgsfilewidget.cpp:248
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:60
qgspixmaplabel.h
QgsFileWidget::RelativeDefaultPath
@ RelativeDefaultPath
Definition: qgsfilewidget.h:82
qgsmessagebar.h
qgsexternalstoragefilewidget.h
QgsExternalResourceWidget::setDocumentViewerHeight
void setDocumentViewerHeight(int height)
setDocumentViewerWidth set the height of the document viewer.
Definition: qgsexternalresourcewidget.cpp:132
QgsExternalStorageFileWidget::setMessageBar
void setMessageBar(QgsMessageBar *messageBar)
Set messageBar to report messages.
Definition: qgsexternalstoragefilewidget.cpp:159
QgsFileWidget::setFilePath
void setFilePath(const QString &path)
Sets the current file path.
Definition: qgsfilewidget.cpp:103
QgsFileWidget::RelativeStorage
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:78
QgsExternalResourceWidget::Image
@ Image
Definition: qgsexternalresourcewidget.h:76
QgsExternalStorageFileWidget::messageBar
QgsMessageBar * messageBar() const
Returns message bar used to report messages.
Definition: qgsexternalstoragefilewidget.cpp:164
QgsExternalStorageContent::errorOccurred
void errorOccurred(const QString &errorString)
The signal is emitted when an error occurred.
QgsExternalResourceWidget::Web
@ Web
Definition: qgsexternalresourcewidget.h:77
QgsExternalStorage::fetch
QgsExternalStorageFetchedContent * fetch(const QString &url, const QString &authCfg=QString(), Qgis::ActionStart fetchingMode=Qgis::ActionStart::Deferred) const
Fetches file from url for this project external storage.
Definition: qgsexternalstorage.cpp:44
QgsFileWidget::setDefaultRoot
void setDefaultRoot(const QString &defaultRoot)
Returns the default root path used as the first shown location when picking a file and used if the Re...
Definition: qgsfilewidget.cpp:227
QgsMessageBar::pushWarning
void pushWarning(const QString &title, const QString &message)
Pushes a warning message that must be manually dismissed by the user.
Definition: qgsmessagebar.cpp:205
QgsExternalStorageFileWidget::setStorageAuthConfigId
void setStorageAuthConfigId(const QString &authCfg)
Sets the authentication configuration ID to be used for the current external storage (if defined)
Definition: qgsexternalstoragefilewidget.cpp:102
QgsExternalResourceWidget::setDocumentPath
void setDocumentPath(const QVariant &documentPath)
Definition: qgsexternalresourcewidget.cpp:93
QgsExternalResourceWidget::setDocumentViewerContent
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly
Definition: qgsexternalresourcewidget.cpp:119
Qgis::ContentStatus::Finished
@ Finished
Content fetching/storing is finished and successful.
QgsExternalResourceWidget::documentViewerWidth
int documentViewerWidth
Definition: qgsexternalresourcewidget.h:68
QgsExternalResourceWidget::documentViewerHeight
int documentViewerHeight
Definition: qgsexternalresourcewidget.h:67
QgsExternalResourceWidget::setStorageType
void setStorageType(const QString &storageType)
Set storageType storage type unique identifier as defined in QgsExternalStorageRegistry or null QStri...
Definition: qgsexternalresourcewidget.cpp:238
qgsproject.h
QgsExternalStorageFileWidget::storageAuthConfigId
const QString & storageAuthConfigId() const
Returns the authentication configuration ID used for the current external storage (if defined)
Definition: qgsexternalstoragefilewidget.cpp:107
QgsExternalResourceWidget::defaultRoot
QString defaultRoot
Definition: qgsexternalresourcewidget.h:70
QgsFileWidget::filePath
QString filePath()
Returns the current file path(s).
Definition: qgsfilewidget.cpp:77
QgsExternalStorageFileWidget::setStorageType
void setStorageType(const QString &storageType)
Set storageType storage type unique identifier as defined in QgsExternalStorageRegistry or null QStri...
Definition: qgsexternalstoragefilewidget.cpp:60