18#include "moc_qgsexternalresourcewidget.cpp"
33#include <QImageReader>
36#include <QMimeDatabase>
45 setBackgroundRole( QPalette::Window );
46 setAutoFillBackground(
true );
48 QGridLayout *layout =
new QGridLayout();
49 layout->setContentsMargins( 0, 0, 0, 0 );
52 layout->addWidget( mFileWidget, 0, 0 );
53 mFileWidget->setVisible( mFileWidgetVisible );
56 layout->addWidget( mPixmapLabel, 1, 0 );
59 mWebView =
new QWebView(
this );
60 mWebView->setAcceptDrops(
false );
61 layout->addWidget( mWebView, 2, 0 );
65 layout->addWidget( mMediaWidget, 3, 0 );
67 mLoadingLabel =
new QLabel(
this );
68 layout->addWidget( mLoadingLabel, 4, 0 );
70 mLoadingMovie->setScaledSize( QSize( 32, 32 ) );
71 mLoadingLabel->setMovie( mLoadingMovie );
73 mErrorLabel =
new QLabel(
this );
74 layout->addWidget( mErrorLabel, 5, 0 );
77 updateDocumentViewer();
87 const QString path = mFileWidget->
filePath();
116 return mFileWidgetVisible;
121 mFileWidgetVisible = visible;
122 mFileWidget->setVisible( visible );
127 return mDocumentViewerContent;
132 mDocumentViewerContent = content;
133 if ( mDocumentViewerContent !=
Image )
134 updateDocumentViewer();
135 loadDocument( mFileWidget->
filePath() );
140 return mDocumentViewerHeight;
145 mDocumentViewerHeight = height;
146 updateDocumentViewer();
151 return mDocumentViewerWidth;
156 mDocumentViewerWidth = width;
157 updateDocumentViewer();
165void QgsExternalResourceWidget::updateDocumentViewer()
167 mErrorLabel->setVisible(
false );
168 mLoadingLabel->setVisible(
false );
169 mLoadingMovie->stop();
171 switch ( mDocumentViewerContent )
176 mWebView->setVisible(
true );
178 mMediaWidget->setVisible(
false );
179 mPixmapLabel->setVisible(
false );
186 mWebView->setVisible(
false );
188 mMediaWidget->setVisible(
false );
189 mPixmapLabel->setVisible(
true );
191#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
192 const QPixmap pm = mPixmapLabel->pixmap() ? *mPixmapLabel->pixmap() : QPixmap();
194 const QPixmap pm = mPixmapLabel->pixmap();
197 if ( !pm || pm.isNull() )
199 mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
203 QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
204 if ( size.width() == 0 && size.height() > 0 )
206 size.setWidth( size.height() * pm.size().width() / pm.size().height() );
208 else if ( size.width() > 0 && size.height() == 0 )
210 size.setHeight( size.width() * pm.size().height() / pm.size().width() );
213 if ( size.width() != 0 || size.height() != 0 )
215 mPixmapLabel->setMinimumSize( size );
216 mPixmapLabel->setMaximumSize( size );
226 mWebView->setVisible(
false );
228 mMediaWidget->setVisible(
true );
229 mPixmapLabel->setVisible(
false );
239 mWebView->setVisible(
false );
241 mMediaWidget->setVisible(
false );
242 mPixmapLabel->setVisible(
false );
248QString QgsExternalResourceWidget::resolvePath(
const QString &path )
250 switch ( mRelativeStorage )
259 return QDir( mDefaultRoot ).filePath( path );
278 return mRelativeStorage;
317void QgsExternalResourceWidget::updateDocumentContent(
const QString &filePath )
319 switch ( mDocumentViewerContent )
324 mWebView->load( QUrl::fromUserInput( filePath.toUtf8() ) );
325 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
332 QImageReader ir( filePath );
334 ir.setAutoTransform(
true );
335 const QPixmap pm = QPixmap::fromImage( ir.read() );
342 mPixmapLabel->
clear();
360 updateDocumentViewer();
363void QgsExternalResourceWidget::clearContent()
366 if ( mDocumentViewerContent ==
Web )
368 mWebView->load( QUrl( QStringLiteral(
"about:blank" ) ) );
371 if ( mDocumentViewerContent ==
Image )
373 mPixmapLabel->
clear();
376 updateDocumentViewer();
379void QgsExternalResourceWidget::loadDocument(
const QString &path )
391 else if ( mDocumentViewerContent !=
NoContent )
393 const QString resolvedPath = resolvePath( path );
405 mWebView->setVisible(
false );
407 mMediaWidget->setVisible(
false );
408 mPixmapLabel->setVisible(
false );
409 mErrorLabel->setVisible(
false );
410 mLoadingLabel->setVisible(
true );
411 mLoadingMovie->start();
420 updateDocumentContent( resolvedPath );
425void QgsExternalResourceWidget::onFetchFinished()
432 mWebView->setVisible(
false );
434 mPixmapLabel->setVisible(
false );
435 mLoadingLabel->setVisible(
false );
436 mLoadingMovie->stop();
437 mErrorLabel->setVisible(
true );
441 messageBar()->
pushWarning( tr(
"Fetching External Resource" ), tr(
"Error while fetching external resource '%1' : %2" ).arg( mFileWidget->
filePath(), mContent->errorString() ) );
446 const QString filePath = mDocumentViewerContent ==
Web
447 ? QUrl::fromLocalFile( mContent->filePath() ).toString()
448 : mContent->filePath();
450 updateDocumentContent( filePath );
453 content->deleteLater();
@ Finished
Content fetching/storing is finished and successful.
@ Failed
Content fetching/storing has failed.
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
void canceled()
The signal is emitted when content fetching/storing has been canceled.
void errorOccurred(const QString &errorString)
The signal is emitted when an error occurred.
Class for QgsExternalStorage fetched content.
void fetched()
The signal is emitted when the resource has successfully been fetched.
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.
A bar for displaying non-blocking messages to the user.
void pushWarning(const QString &title, const QString &message)
Pushes a warning message that must be manually dismissed by the user.
The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given to the widget by the ...
void clear()
Clears any label contents.
void setPixmap(const QPixmap &)
static QgsProject * instance()
Returns the QgsProject singleton instance.
static QMetaType::Type variantTypeToMetaType(QVariant::Type variantType)
Converts a QVariant::Type to a QMetaType::Type.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.