23 #include <QGridLayout>
26 #include <QImageReader>
35 setBackgroundRole( QPalette::Window );
36 setAutoFillBackground(
true );
38 QGridLayout *layout =
new QGridLayout();
39 layout->setContentsMargins( 0, 0, 0, 0 );
42 layout->addWidget( mFileWidget, 0, 0 );
43 mFileWidget->setVisible( mFileWidgetVisible );
46 layout->addWidget( mPixmapLabel, 1, 0 );
49 mWebView =
new QWebView(
this );
50 layout->addWidget( mWebView, 2, 0 );
53 updateDocumentViewer();
63 QString path = mFileWidget->
filePath();
66 return QVariant( type );
86 return mFileWidgetVisible;
91 mFileWidgetVisible = visible;
92 mFileWidget->setVisible( visible );
97 return mDocumentViewerContent;
102 mDocumentViewerContent = content;
103 if ( mDocumentViewerContent !=
Image )
104 updateDocumentViewer();
105 loadDocument( mFileWidget->
filePath() );
110 return mDocumentViewerHeight;
115 mDocumentViewerHeight = height;
116 updateDocumentViewer();
121 return mDocumentViewerWidth;
126 mDocumentViewerWidth = width;
127 updateDocumentViewer();
135 void QgsExternalResourceWidget::updateDocumentViewer()
138 mWebView->setVisible( mDocumentViewerContent ==
Web );
141 mPixmapLabel->setVisible( mDocumentViewerContent ==
Image );
143 if ( mDocumentViewerContent ==
Image )
145 const QPixmap *pm = mPixmapLabel->pixmap();
147 if ( !pm || pm->isNull() )
149 mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
153 QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
154 if ( size.width() == 0 && size.height() > 0 )
156 size.setWidth( size.height() * pm->size().width() / pm->size().height() );
158 else if ( size.width() > 0 && size.height() == 0 )
160 size.setHeight( size.width() * pm->size().height() / pm->size().width() );
163 if ( size.width() != 0 || size.height() != 0 )
165 mPixmapLabel->setMinimumSize( size );
166 mPixmapLabel->setMaximumSize( size );
172 QString QgsExternalResourceWidget::resolvePath(
const QString &path )
174 switch ( mRelativeStorage )
183 return QDir( mDefaultRoot ).filePath( path );
202 return mRelativeStorage;
211 void QgsExternalResourceWidget::loadDocument(
const QString &path )
213 QString resolvedPath;
215 if ( path.isEmpty() )
218 if ( mDocumentViewerContent ==
Web )
220 mWebView->setUrl( QUrl( QStringLiteral(
"about:blank" ) ) );
223 if ( mDocumentViewerContent ==
Image )
225 mPixmapLabel->
clear();
226 updateDocumentViewer();
231 resolvedPath = resolvePath( path );
234 if ( mDocumentViewerContent ==
Web )
236 mWebView->load( QUrl::fromEncoded( resolvedPath.toUtf8() ) );
237 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
241 if ( mDocumentViewerContent ==
Image )
244 QImageReader ir( resolvedPath );
245 ir.setAutoTransform(
true );
246 QPixmap pm = QPixmap::fromImage( ir.read() );
253 mPixmapLabel->
clear();
255 updateDocumentViewer();
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
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.