32#include <QImageReader>
35#include <QMimeDatabase>
44 setBackgroundRole( QPalette::Window );
45 setAutoFillBackground(
true );
47 QGridLayout *layout =
new QGridLayout();
48 layout->setContentsMargins( 0, 0, 0, 0 );
51 layout->addWidget( mFileWidget, 0, 0 );
52 mFileWidget->setVisible( mFileWidgetVisible );
55 layout->addWidget( mPixmapLabel, 1, 0 );
58 mWebView =
new QWebView(
this );
59 mWebView->setAcceptDrops(
false );
60 layout->addWidget( mWebView, 2, 0 );
64 layout->addWidget( mMediaWidget, 3, 0 );
66 mLoadingLabel =
new QLabel(
this );
67 layout->addWidget( mLoadingLabel, 4, 0 );
69 mLoadingMovie->setScaledSize( QSize( 32, 32 ) );
70 mLoadingLabel->setMovie( mLoadingMovie );
72 mErrorLabel =
new QLabel(
this );
73 layout->addWidget( mErrorLabel, 5, 0 );
76 updateDocumentViewer();
86 const QString path = mFileWidget->
filePath();
115 return mFileWidgetVisible;
120 mFileWidgetVisible = visible;
121 mFileWidget->setVisible( visible );
126 return mDocumentViewerContent;
131 mDocumentViewerContent = content;
132 if ( mDocumentViewerContent !=
Image )
133 updateDocumentViewer();
134 loadDocument( mFileWidget->
filePath() );
139 return mDocumentViewerHeight;
144 mDocumentViewerHeight = height;
145 updateDocumentViewer();
150 return mDocumentViewerWidth;
155 mDocumentViewerWidth = width;
156 updateDocumentViewer();
164void QgsExternalResourceWidget::updateDocumentViewer()
166 mErrorLabel->setVisible(
false );
167 mLoadingLabel->setVisible(
false );
168 mLoadingMovie->stop();
170 switch ( mDocumentViewerContent )
175 mWebView->setVisible(
true );
177 mMediaWidget->setVisible(
false );
178 mPixmapLabel->setVisible(
false );
185 mWebView->setVisible(
false );
187 mMediaWidget->setVisible(
false );
188 mPixmapLabel->setVisible(
true );
190#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
191 const QPixmap pm = mPixmapLabel->pixmap() ? *mPixmapLabel->pixmap() : QPixmap();
193 const QPixmap pm = mPixmapLabel->pixmap();
196 if ( !pm || pm.isNull() )
198 mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
202 QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
203 if ( size.width() == 0 && size.height() > 0 )
205 size.setWidth( size.height() * pm.size().width() / pm.size().height() );
207 else if ( size.width() > 0 && size.height() == 0 )
209 size.setHeight( size.width() * pm.size().height() / pm.size().width() );
212 if ( size.width() != 0 || size.height() != 0 )
214 mPixmapLabel->setMinimumSize( size );
215 mPixmapLabel->setMaximumSize( size );
225 mWebView->setVisible(
false );
227 mMediaWidget->setVisible(
true );
228 mPixmapLabel->setVisible(
false );
238 mWebView->setVisible(
false );
240 mMediaWidget->setVisible(
false );
241 mPixmapLabel->setVisible(
false );
247QString QgsExternalResourceWidget::resolvePath(
const QString &path )
249 switch ( mRelativeStorage )
258 return QDir( mDefaultRoot ).filePath( path );
277 return mRelativeStorage;
316void QgsExternalResourceWidget::updateDocumentContent(
const QString &filePath )
318 switch ( mDocumentViewerContent )
323 mWebView->load( QUrl::fromUserInput( filePath.toUtf8() ) );
324 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
331 QImageReader ir( filePath );
333 ir.setAutoTransform(
true );
334 const QPixmap pm = QPixmap::fromImage( ir.read() );
341 mPixmapLabel->
clear();
359 updateDocumentViewer();
362void QgsExternalResourceWidget::clearContent()
365 if ( mDocumentViewerContent ==
Web )
367 mWebView->load( QUrl( QStringLiteral(
"about:blank" ) ) );
370 if ( mDocumentViewerContent ==
Image )
372 mPixmapLabel->
clear();
375 updateDocumentViewer();
378void QgsExternalResourceWidget::loadDocument(
const QString &path )
390 else if ( mDocumentViewerContent !=
NoContent )
392 const QString resolvedPath = resolvePath( path );
404 mWebView->setVisible(
false );
406 mMediaWidget->setVisible(
false );
407 mPixmapLabel->setVisible(
false );
408 mErrorLabel->setVisible(
false );
409 mLoadingLabel->setVisible(
true );
410 mLoadingMovie->start();
419 updateDocumentContent( resolvedPath );
424void QgsExternalResourceWidget::onFetchFinished()
431 mWebView->setVisible(
false );
433 mPixmapLabel->setVisible(
false );
434 mLoadingLabel->setVisible(
false );
435 mLoadingMovie->stop();
436 mErrorLabel->setVisible(
true );
441 tr(
"Error while fetching external resource '%1' : %2" ).arg(
442 mFileWidget->
filePath(), mContent->errorString() ) );
447 const QString filePath = mDocumentViewerContent ==
Web
448 ? QUrl::fromLocalFile( mContent->filePath() ).toString()
449 : mContent->filePath();
451 updateDocumentContent( filePath );
454 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.