28 #include <QGridLayout>
31 #include <QImageReader>
32 #include <QToolButton>
34 #include <QMimeDatabase>
43 setBackgroundRole( QPalette::Window );
44 setAutoFillBackground(
true );
46 QGridLayout *layout =
new QGridLayout();
47 layout->setContentsMargins( 0, 0, 0, 0 );
50 layout->addWidget( mFileWidget, 0, 0 );
51 mFileWidget->setVisible( mFileWidgetVisible );
54 layout->addWidget( mPixmapLabel, 1, 0 );
57 mWebView =
new QWebView(
this );
58 mWebView->setAcceptDrops(
false );
59 layout->addWidget( mWebView, 2, 0 );
62 mLoadingLabel =
new QLabel(
this );
63 layout->addWidget( mLoadingLabel, 3, 0 );
65 mLoadingMovie->setScaledSize( QSize( 32, 32 ) );
66 mLoadingLabel->setMovie( mLoadingMovie );
68 mErrorLabel =
new QLabel(
this );
69 layout->addWidget( mErrorLabel, 4, 0 );
72 updateDocumentViewer();
82 const QString path = mFileWidget->
filePath();
85 return QVariant( type );
105 return mFileWidgetVisible;
110 mFileWidgetVisible = visible;
111 mFileWidget->setVisible( visible );
116 return mDocumentViewerContent;
121 mDocumentViewerContent = content;
122 if ( mDocumentViewerContent !=
Image )
123 updateDocumentViewer();
124 loadDocument( mFileWidget->
filePath() );
129 return mDocumentViewerHeight;
134 mDocumentViewerHeight = height;
135 updateDocumentViewer();
140 return mDocumentViewerWidth;
145 mDocumentViewerWidth = width;
146 updateDocumentViewer();
154 void QgsExternalResourceWidget::updateDocumentViewer()
156 mErrorLabel->setVisible(
false );
157 mLoadingLabel->setVisible(
false );
158 mLoadingMovie->stop();
161 mWebView->setVisible( mDocumentViewerContent ==
Web );
164 mPixmapLabel->setVisible( mDocumentViewerContent ==
Image );
166 if ( mDocumentViewerContent ==
Image )
168 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
169 const QPixmap pm = mPixmapLabel->pixmap() ? *mPixmapLabel->pixmap() : QPixmap();
171 const QPixmap pm = mPixmapLabel->pixmap();
174 if ( !pm || pm.isNull() )
176 mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
180 QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
181 if ( size.width() == 0 && size.height() > 0 )
183 size.setWidth( size.height() * pm.size().width() / pm.size().height() );
185 else if ( size.width() > 0 && size.height() == 0 )
187 size.setHeight( size.width() * pm.size().height() / pm.size().width() );
190 if ( size.width() != 0 || size.height() != 0 )
192 mPixmapLabel->setMinimumSize( size );
193 mPixmapLabel->setMaximumSize( size );
199 QString QgsExternalResourceWidget::resolvePath(
const QString &path )
201 switch ( mRelativeStorage )
210 return QDir( mDefaultRoot ).filePath( path );
229 return mRelativeStorage;
268 void QgsExternalResourceWidget::updateDocumentContent(
const QString &filePath )
271 if ( mDocumentViewerContent ==
Web )
273 mWebView->load( QUrl::fromEncoded( filePath.toUtf8() ) );
274 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
278 if ( mDocumentViewerContent ==
Image )
281 QImageReader ir( filePath );
282 ir.setAutoTransform(
true );
283 const QPixmap pm = QPixmap::fromImage( ir.read() );
287 mPixmapLabel->
clear();
290 updateDocumentViewer();
293 void QgsExternalResourceWidget::clearContent()
296 if ( mDocumentViewerContent ==
Web )
298 mWebView->load( QUrl( QStringLiteral(
"about:blank" ) ) );
301 if ( mDocumentViewerContent ==
Image )
303 mPixmapLabel->
clear();
306 updateDocumentViewer();
309 void QgsExternalResourceWidget::loadDocument(
const QString &path )
321 else if ( mDocumentViewerContent !=
NoContent )
323 const QString resolvedPath = resolvePath( path );
335 mWebView->setVisible(
false );
337 mPixmapLabel->setVisible(
false );
338 mErrorLabel->setVisible(
false );
339 mLoadingLabel->setVisible(
true );
340 mLoadingMovie->start();
349 updateDocumentContent( resolvedPath );
354 void QgsExternalResourceWidget::onFetchFinished()
361 mWebView->setVisible(
false );
363 mPixmapLabel->setVisible(
false );
364 mLoadingLabel->setVisible(
false );
365 mLoadingMovie->stop();
366 mErrorLabel->setVisible(
true );
371 tr(
"Error while fetching external resource '%1' : %2" ).arg(
372 mFileWidget->
filePath(), mContent->errorString() ) );
377 const QString filePath = mDocumentViewerContent ==
Web
378 ? QUrl::fromLocalFile( mContent->filePath() ).toString()
379 : mContent->filePath();
381 updateDocumentContent( filePath );
384 content->deleteLater();