22 #include <QGridLayout>
25 #include <QImageReader>
34 setBackgroundRole( QPalette::Window );
35 setAutoFillBackground(
true );
37 QGridLayout *layout =
new QGridLayout();
38 layout->setMargin( 0 );
41 layout->addWidget( mFileWidget, 0, 0 );
42 mFileWidget->setVisible( mFileWidgetVisible );
45 layout->addWidget( mPixmapLabel, 1, 0 );
48 mWebView =
new QWebView(
this );
49 layout->addWidget( mWebView, 2, 0 );
52 updateDocumentViewer();
62 QString path = mFileWidget->
filePath();
65 return QVariant( type );
85 return mFileWidgetVisible;
90 mFileWidgetVisible = visible;
91 mFileWidget->setVisible( visible );
96 return mDocumentViewerContent;
101 mDocumentViewerContent = content;
102 updateDocumentViewer();
107 return mDocumentViewerHeight;
112 mDocumentViewerHeight = height;
113 updateDocumentViewer();
118 return mDocumentViewerWidth;
123 mDocumentViewerWidth = width;
124 updateDocumentViewer();
132 void QgsExternalResourceWidget::updateDocumentViewer()
135 mWebView->setVisible( mDocumentViewerContent ==
Web );
138 mPixmapLabel->setVisible( mDocumentViewerContent ==
Image );
140 if ( mDocumentViewerContent ==
Image )
142 const QPixmap *pm = mPixmapLabel->pixmap();
144 if ( !pm || pm->isNull() )
146 mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
150 QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
151 if ( size.width() == 0 && size.height() > 0 )
153 size.setWidth( size.height() * pm->size().width() / pm->size().height() );
155 else if ( size.width() > 0 && size.height() == 0 )
157 size.setHeight( size.width() * pm->size().height() / pm->size().width() );
160 if ( size.width() != 0 || size.height() != 0 )
162 mPixmapLabel->setMinimumSize( size );
163 mPixmapLabel->setMaximumSize( size );
169 QString QgsExternalResourceWidget::resolvePath(
const QString &path )
171 switch ( mRelativeStorage )
180 return QDir( mDefaultRoot ).filePath( path );
199 return mRelativeStorage;
208 void QgsExternalResourceWidget::loadDocument(
const QString &path )
210 QString resolvedPath;
212 if ( path.isEmpty() )
215 if ( mDocumentViewerContent ==
Web )
217 mWebView->setUrl( QUrl( QStringLiteral(
"about:blank" ) ) );
220 if ( mDocumentViewerContent ==
Image )
222 mPixmapLabel->clear();
223 updateDocumentViewer();
228 resolvedPath = resolvePath( path );
231 if ( mDocumentViewerContent ==
Web )
233 mWebView->load( QUrl::fromEncoded( resolvedPath.toUtf8() ) );
234 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
238 if ( mDocumentViewerContent ==
Image )
241 QImageReader ir( resolvedPath );
242 ir.setAutoTransform(
true );
243 QPixmap pm = QPixmap::fromImage( ir.read() );
247 mPixmapLabel->clear();
248 updateDocumentViewer();