31#include <QImageReader>
32#include <QMimeDatabase>
40#include "moc_qgsexternalresourcewidget.cpp"
42using namespace Qt::StringLiterals;
47 setBackgroundRole( QPalette::Window );
48 setAutoFillBackground(
true );
50 QGridLayout *layout =
new QGridLayout();
51 layout->setContentsMargins( 0, 0, 0, 0 );
54 layout->addWidget( mFileWidget, 0, 0 );
55 mFileWidget->setVisible( mFileWidgetVisible );
58 layout->addWidget( mPixmapLabel, 1, 0 );
61 layout->addWidget( mMediaWidget, 3, 0 );
63 mLoadingLabel =
new QLabel(
this );
64 layout->addWidget( mLoadingLabel, 4, 0 );
66 mLoadingMovie->setScaledSize( QSize( 32, 32 ) );
67 mLoadingLabel->setMovie( mLoadingMovie );
69 mErrorLabel =
new QLabel(
this );
70 layout->addWidget( mErrorLabel, 5, 0 );
73 updateDocumentViewer();
83 const QString path = mFileWidget->filePath();
102 mFileWidget->setFilePath( path.toString() );
112 return mFileWidgetVisible;
117 mFileWidgetVisible = visible;
118 mFileWidget->setVisible( visible );
123 return mDocumentViewerContent;
128 mDocumentViewerContent = content;
129 if ( mDocumentViewerContent !=
Image )
130 updateDocumentViewer();
131 loadDocument( mFileWidget->filePath() );
136 return mDocumentViewerHeight;
141 mDocumentViewerHeight = height;
142 updateDocumentViewer();
147 return mDocumentViewerWidth;
152 mDocumentViewerWidth = width;
153 updateDocumentViewer();
158 mFileWidget->setReadOnly( readOnly );
161void QgsExternalResourceWidget::updateDocumentViewer()
163 mErrorLabel->setVisible(
false );
164 mLoadingLabel->setVisible(
false );
165 mLoadingMovie->stop();
167 switch ( mDocumentViewerContent )
171 mMediaWidget->setVisible(
false );
172 mPixmapLabel->setVisible(
false );
178 mMediaWidget->setVisible(
false );
179 mPixmapLabel->setVisible(
true );
181 const QPixmap pm = mPixmapLabel->pixmap();
183 if ( !pm || pm.isNull() )
185 mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
189 QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
190 if ( size.width() == 0 && size.height() > 0 )
192 size.setWidth( size.height() * pm.size().width() / pm.size().height() );
194 else if ( size.width() > 0 && size.height() == 0 )
196 size.setHeight( size.width() * pm.size().height() / pm.size().width() );
199 if ( size.width() != 0 || size.height() != 0 )
201 mPixmapLabel->setMinimumSize( size );
202 mPixmapLabel->setMaximumSize( size );
211 mMediaWidget->setVisible(
true );
212 mPixmapLabel->setVisible(
false );
215 mMediaWidget->setVideoHeight( mDocumentViewerHeight );
221 mMediaWidget->setVisible(
false );
222 mPixmapLabel->setVisible(
false );
228QString QgsExternalResourceWidget::resolvePath(
const QString &path )
230 switch ( mRelativeStorage )
239 return QDir( mDefaultRoot ).filePath( path );
258 return mRelativeStorage;
274 return mFileWidget->storageType();
279 mFileWidget->setStorageAuthConfigId( authCfg );
284 return mFileWidget->storageAuthConfigId();
294 return mFileWidget->messageBar();
297void QgsExternalResourceWidget::updateDocumentContent(
const QString &filePath )
299 switch ( mDocumentViewerContent )
303 QImageReader ir( filePath );
305 ir.setAutoTransform(
true );
306 const QPixmap pm = QPixmap::fromImage( ir.read() );
313 mPixmapLabel->clear();
321 mMediaWidget->setMediaPath( filePath );
332 updateDocumentViewer();
335void QgsExternalResourceWidget::clearContent()
337 if ( mDocumentViewerContent ==
Image )
339 mPixmapLabel->clear();
342 updateDocumentViewer();
345void QgsExternalResourceWidget::loadDocument(
const QString &path )
349 if ( mFileWidget->externalStorage() && mContent )
357 else if ( mDocumentViewerContent !=
NoContent )
359 const QString resolvedPath = resolvePath( path );
361 if ( mFileWidget->externalStorage() )
370 mMediaWidget->setVisible(
false );
371 mPixmapLabel->setVisible(
false );
372 mErrorLabel->setVisible(
false );
373 mLoadingLabel->setVisible(
true );
374 mLoadingMovie->start();
383 updateDocumentContent( resolvedPath );
388void QgsExternalResourceWidget::onFetchFinished()
390 QgsExternalStorageFetchedContent *content = qobject_cast<QgsExternalStorageFetchedContent *>( sender() );
394 mPixmapLabel->setVisible(
false );
395 mLoadingLabel->setVisible(
false );
396 mLoadingMovie->stop();
397 mErrorLabel->setVisible(
true );
401 messageBar()->
pushWarning( tr(
"Fetching External Resource" ), tr(
"Error while fetching external resource '%1' : %2" ).arg( mFileWidget->filePath(), mContent->errorString() ) );
406 const QString filePath = mDocumentViewerContent ==
Web
407 ? QUrl::fromLocalFile( mContent->filePath() ).toString()
408 : mContent->filePath();
410 updateDocumentContent( filePath );
413 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...
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.