31#include <QImageReader>
32#include <QMimeDatabase>
39#include "moc_qgsexternalresourcewidget.cpp"
48 setBackgroundRole( QPalette::Window );
49 setAutoFillBackground(
true );
51 QGridLayout *layout =
new QGridLayout();
52 layout->setContentsMargins( 0, 0, 0, 0 );
55 layout->addWidget( mFileWidget, 0, 0 );
56 mFileWidget->setVisible( mFileWidgetVisible );
59 layout->addWidget( mPixmapLabel, 1, 0 );
62 mWebView =
new QWebView(
this );
63 mWebView->setAcceptDrops(
false );
64 layout->addWidget( mWebView, 2, 0 );
68 layout->addWidget( mMediaWidget, 3, 0 );
70 mLoadingLabel =
new QLabel(
this );
71 layout->addWidget( mLoadingLabel, 4, 0 );
73 mLoadingMovie->setScaledSize( QSize( 32, 32 ) );
74 mLoadingLabel->setMovie( mLoadingMovie );
76 mErrorLabel =
new QLabel(
this );
77 layout->addWidget( mErrorLabel, 5, 0 );
80 updateDocumentViewer();
90 const QString path = mFileWidget->filePath();
109 mFileWidget->setFilePath( path.toString() );
119 return mFileWidgetVisible;
124 mFileWidgetVisible = visible;
125 mFileWidget->setVisible( visible );
130 return mDocumentViewerContent;
135 mDocumentViewerContent = content;
136 if ( mDocumentViewerContent !=
Image )
137 updateDocumentViewer();
138 loadDocument( mFileWidget->filePath() );
143 return mDocumentViewerHeight;
148 mDocumentViewerHeight = height;
149 updateDocumentViewer();
154 return mDocumentViewerWidth;
159 mDocumentViewerWidth = width;
160 updateDocumentViewer();
165 mFileWidget->setReadOnly( readOnly );
168void QgsExternalResourceWidget::updateDocumentViewer()
170 mErrorLabel->setVisible(
false );
171 mLoadingLabel->setVisible(
false );
172 mLoadingMovie->stop();
174 switch ( mDocumentViewerContent )
179 mWebView->setVisible(
true );
181 mMediaWidget->setVisible(
false );
182 mPixmapLabel->setVisible(
false );
189 mWebView->setVisible(
false );
191 mMediaWidget->setVisible(
false );
192 mPixmapLabel->setVisible(
true );
194#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
195 const QPixmap pm = mPixmapLabel->pixmap() ? *mPixmapLabel->pixmap() : QPixmap();
197 const QPixmap pm = mPixmapLabel->pixmap();
200 if ( !pm || pm.isNull() )
202 mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
206 QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
207 if ( size.width() == 0 && size.height() > 0 )
209 size.setWidth( size.height() * pm.size().width() / pm.size().height() );
211 else if ( size.width() > 0 && size.height() == 0 )
213 size.setHeight( size.width() * pm.size().height() / pm.size().width() );
216 if ( size.width() != 0 || size.height() != 0 )
218 mPixmapLabel->setMinimumSize( size );
219 mPixmapLabel->setMaximumSize( size );
229 mWebView->setVisible(
false );
231 mMediaWidget->setVisible(
true );
232 mPixmapLabel->setVisible(
false );
235 mMediaWidget->setVideoHeight( mDocumentViewerHeight );
242 mWebView->setVisible(
false );
244 mMediaWidget->setVisible(
false );
245 mPixmapLabel->setVisible(
false );
251QString QgsExternalResourceWidget::resolvePath(
const QString &path )
253 switch ( mRelativeStorage )
262 return QDir( mDefaultRoot ).filePath( path );
281 return mRelativeStorage;
297 return mFileWidget->storageType();
302 mFileWidget->setStorageAuthConfigId( authCfg );
307 return mFileWidget->storageAuthConfigId();
317 return mFileWidget->messageBar();
320void QgsExternalResourceWidget::updateDocumentContent(
const QString &filePath )
322 switch ( mDocumentViewerContent )
327 mWebView->load( QUrl::fromUserInput( filePath.toUtf8() ) );
328 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
335 QImageReader ir( filePath );
337 ir.setAutoTransform(
true );
338 const QPixmap pm = QPixmap::fromImage( ir.read() );
341 mPixmapLabel->setPixmap( pm );
345 mPixmapLabel->clear();
353 mMediaWidget->setMediaPath( filePath );
363 updateDocumentViewer();
366void QgsExternalResourceWidget::clearContent()
369 if ( mDocumentViewerContent ==
Web )
371 mWebView->load( QUrl( QStringLiteral(
"about:blank" ) ) );
374 if ( mDocumentViewerContent ==
Image )
376 mPixmapLabel->clear();
379 updateDocumentViewer();
382void QgsExternalResourceWidget::loadDocument(
const QString &path )
386 if ( mFileWidget->externalStorage() && mContent )
394 else if ( mDocumentViewerContent !=
NoContent )
396 const QString resolvedPath = resolvePath( path );
398 if ( mFileWidget->externalStorage() )
408 mWebView->setVisible(
false );
410 mMediaWidget->setVisible(
false );
411 mPixmapLabel->setVisible(
false );
412 mErrorLabel->setVisible(
false );
413 mLoadingLabel->setVisible(
true );
414 mLoadingMovie->start();
423 updateDocumentContent( resolvedPath );
428void QgsExternalResourceWidget::onFetchFinished()
430 QgsExternalStorageFetchedContent *content = qobject_cast<QgsExternalStorageFetchedContent *>( sender() );
435 mWebView->setVisible(
false );
437 mPixmapLabel->setVisible(
false );
438 mLoadingLabel->setVisible(
false );
439 mLoadingMovie->stop();
440 mErrorLabel->setVisible(
true );
444 messageBar()->
pushWarning( tr(
"Fetching External Resource" ), tr(
"Error while fetching external resource '%1' : %2" ).arg( mFileWidget->filePath(), mContent->errorString() ) );
449 const QString filePath = mDocumentViewerContent ==
Web
450 ? QUrl::fromLocalFile( mContent->filePath() ).toString()
451 : mContent->filePath();
453 updateDocumentContent( filePath );
456 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.
void fetched()
The signal is emitted when the resource has successfully been fetched.
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.
Shows a pixmap and adjusts its size to the space given to the widget by the layout and keeping its as...
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.