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 installEventFilter(
this );
66 removeEventFilter(
this );
73 QString path = mFileWidget->
filePath();
76 return QVariant( type );
96 return mFileWidgetVisible;
101 mFileWidgetVisible = visible;
102 mFileWidget->setVisible( visible );
107 return mDocumentViewerContent;
112 mDocumentViewerContent = content;
113 updateDocumentViewer();
118 return mDocumentViewerHeight;
123 mDocumentViewerHeight = height;
124 updateDocumentViewer();
129 return mDocumentViewerWidth;
134 mDocumentViewerWidth = width;
135 updateDocumentViewer();
143 void QgsExternalResourceWidget::updateDocumentViewer()
146 mWebView->setVisible( mDocumentViewerContent ==
Web );
149 mPixmapLabel->setVisible( mDocumentViewerContent ==
Image );
151 if ( mDocumentViewerContent ==
Image )
153 const QPixmap *pm = mPixmapLabel->pixmap();
155 if ( !pm || pm->isNull() )
157 mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
161 QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
162 if ( size.width() == 0 && size.height() > 0 )
164 size.setWidth( size.height() * pm->size().width() / pm->size().height() );
166 else if ( size.width() > 0 && size.height() == 0 )
168 size.setHeight( size.width() * pm->size().height() / pm->size().width() );
171 if ( size.width() != 0 || size.height() != 0 )
173 mPixmapLabel->setMinimumSize( size );
174 mPixmapLabel->setMaximumSize( size );
180 QString QgsExternalResourceWidget::resolvePath(
const QString &path )
182 switch ( mRelativeStorage )
191 return QDir( mDefaultRoot ).filePath( path );
210 if ( watched ==
this && event && ( event->type() == QEvent::FocusOut ||
event->type() == QEvent::FocusAboutToChange ) )
214 return QWidget::eventFilter( watched, event );
219 return mRelativeStorage;
228 void QgsExternalResourceWidget::loadDocument(
const QString &path )
230 QString resolvedPath;
232 if ( path.isEmpty() )
235 if ( mDocumentViewerContent ==
Web )
237 mWebView->setUrl( QUrl( QStringLiteral(
"about:blank" ) ) );
240 if ( mDocumentViewerContent ==
Image )
242 mPixmapLabel->clear();
243 updateDocumentViewer();
248 resolvedPath = resolvePath( path );
251 if ( mDocumentViewerContent ==
Web )
253 mWebView->load( QUrl::fromEncoded( resolvedPath.toUtf8() ) );
254 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
258 if ( mDocumentViewerContent ==
Image )
261 QImageReader ir( resolvedPath );
262 ir.setAutoTransform(
true );
263 QPixmap pm = QPixmap::fromImage( ir.read() );
265 updateDocumentViewer();
void setPixmap(const QPixmap &)
static QgsProject * instance()
Returns the QgsProject singleton instance.
The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given to the widget by the ...