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();
89 return QVariant( type );
109 return mFileWidgetVisible;
114 mFileWidgetVisible = visible;
115 mFileWidget->setVisible( visible );
120 return mDocumentViewerContent;
125 mDocumentViewerContent = content;
126 if ( mDocumentViewerContent !=
Image )
127 updateDocumentViewer();
128 loadDocument( mFileWidget->
filePath() );
133 return mDocumentViewerHeight;
138 mDocumentViewerHeight = height;
139 updateDocumentViewer();
144 return mDocumentViewerWidth;
149 mDocumentViewerWidth = width;
150 updateDocumentViewer();
158void QgsExternalResourceWidget::updateDocumentViewer()
160 mErrorLabel->setVisible(
false );
161 mLoadingLabel->setVisible(
false );
162 mLoadingMovie->stop();
164 switch ( mDocumentViewerContent )
169 mWebView->setVisible(
true );
171 mMediaWidget->setVisible(
false );
172 mPixmapLabel->setVisible(
false );
179 mWebView->setVisible(
false );
181 mMediaWidget->setVisible(
false );
182 mPixmapLabel->setVisible(
true );
184#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
185 const QPixmap pm = mPixmapLabel->pixmap() ? *mPixmapLabel->pixmap() : QPixmap();
187 const QPixmap pm = mPixmapLabel->pixmap();
190 if ( !pm || pm.isNull() )
192 mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
196 QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
197 if ( size.width() == 0 && size.height() > 0 )
199 size.setWidth( size.height() * pm.size().width() / pm.size().height() );
201 else if ( size.width() > 0 && size.height() == 0 )
203 size.setHeight( size.width() * pm.size().height() / pm.size().width() );
206 if ( size.width() != 0 || size.height() != 0 )
208 mPixmapLabel->setMinimumSize( size );
209 mPixmapLabel->setMaximumSize( size );
219 mWebView->setVisible(
false );
221 mMediaWidget->setVisible(
true );
222 mPixmapLabel->setVisible(
false );
232 mWebView->setVisible(
false );
234 mMediaWidget->setVisible(
false );
235 mPixmapLabel->setVisible(
false );
241QString QgsExternalResourceWidget::resolvePath(
const QString &path )
243 switch ( mRelativeStorage )
252 return QDir( mDefaultRoot ).filePath( path );
271 return mRelativeStorage;
310void QgsExternalResourceWidget::updateDocumentContent(
const QString &filePath )
312 switch ( mDocumentViewerContent )
317 mWebView->load( QUrl::fromEncoded( filePath.toUtf8() ) );
318 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
325 QImageReader ir( filePath );
327 ir.setAutoTransform(
true );
328 const QPixmap pm = QPixmap::fromImage( ir.read() );
335 mPixmapLabel->
clear();
353 updateDocumentViewer();
356void QgsExternalResourceWidget::clearContent()
359 if ( mDocumentViewerContent ==
Web )
361 mWebView->load( QUrl( QStringLiteral(
"about:blank" ) ) );
364 if ( mDocumentViewerContent ==
Image )
366 mPixmapLabel->
clear();
369 updateDocumentViewer();
372void QgsExternalResourceWidget::loadDocument(
const QString &path )
384 else if ( mDocumentViewerContent !=
NoContent )
386 const QString resolvedPath = resolvePath( path );
398 mWebView->setVisible(
false );
400 mMediaWidget->setVisible(
false );
401 mPixmapLabel->setVisible(
false );
402 mErrorLabel->setVisible(
false );
403 mLoadingLabel->setVisible(
true );
404 mLoadingMovie->start();
413 updateDocumentContent( resolvedPath );
418void QgsExternalResourceWidget::onFetchFinished()
425 mWebView->setVisible(
false );
427 mPixmapLabel->setVisible(
false );
428 mLoadingLabel->setVisible(
false );
429 mLoadingMovie->stop();
430 mErrorLabel->setVisible(
true );
435 tr(
"Error while fetching external resource '%1' : %2" ).arg(
436 mFileWidget->
filePath(), mContent->errorString() ) );
441 const QString filePath = mDocumentViewerContent ==
Web
442 ? QUrl::fromLocalFile( mContent->filePath() ).toString()
443 : mContent->filePath();
445 updateDocumentContent( filePath );
448 content->deleteLater();
@ Finished
Content fetching/storing is finished and successful.
@ Failed
Content fetching/storing has failed.
static QString nullRepresentation()
This string is 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.