QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsexternalresourcewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexternalresourcewidget.cpp
3 
4  ---------------------
5  begin : 16.12.2015
6  copyright : (C) 2015 by Denis Rouzaud
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
18 #include "qgspixmaplabel.h"
19 #include "qgsproject.h"
20 #include "qgsapplication.h"
21 
22 #include <QDir>
23 #include <QGridLayout>
24 #include <QVariant>
25 #include <QSettings>
26 #include <QImageReader>
27 #ifdef WITH_QTWEBKIT
28 #include <QWebView>
29 #endif
30 
31 
33  : QWidget( parent )
34 {
35  setBackgroundRole( QPalette::Window );
36  setAutoFillBackground( true );
37 
38  QGridLayout *layout = new QGridLayout();
39  layout->setContentsMargins( 0, 0, 0, 0 );
40 
41  mFileWidget = new QgsFileWidget( this );
42  layout->addWidget( mFileWidget, 0, 0 );
43  mFileWidget->setVisible( mFileWidgetVisible );
44 
45  mPixmapLabel = new QgsPixmapLabel( this );
46  layout->addWidget( mPixmapLabel, 1, 0 );
47 
48 #ifdef WITH_QTWEBKIT
49  mWebView = new QWebView( this );
50  layout->addWidget( mWebView, 2, 0 );
51 #endif
52 
53  updateDocumentViewer();
54 
55  setLayout( layout );
56 
57  connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsExternalResourceWidget::loadDocument );
59 }
60 
61 QVariant QgsExternalResourceWidget::documentPath( QVariant::Type type ) const
62 {
63  QString path = mFileWidget->filePath();
64  if ( path.isEmpty() || path == QgsApplication::nullRepresentation() )
65  {
66  return QVariant( type );
67  }
68  else
69  {
70  return path;
71  }
72 }
73 
74 void QgsExternalResourceWidget::setDocumentPath( const QVariant &path )
75 {
76  mFileWidget->setFilePath( path.toString() );
77 }
78 
80 {
81  return mFileWidget;
82 }
83 
85 {
86  return mFileWidgetVisible;
87 }
88 
90 {
91  mFileWidgetVisible = visible;
92  mFileWidget->setVisible( visible );
93 }
94 
96 {
97  return mDocumentViewerContent;
98 }
99 
101 {
102  mDocumentViewerContent = content;
103  if ( mDocumentViewerContent != Image )
104  updateDocumentViewer();
105  loadDocument( mFileWidget->filePath() );
106 }
107 
109 {
110  return mDocumentViewerHeight;
111 }
112 
114 {
115  mDocumentViewerHeight = height;
116  updateDocumentViewer();
117 }
118 
120 {
121  return mDocumentViewerWidth;
122 }
123 
125 {
126  mDocumentViewerWidth = width;
127  updateDocumentViewer();
128 }
129 
131 {
132  mFileWidget->setReadOnly( readOnly );
133 }
134 
135 void QgsExternalResourceWidget::updateDocumentViewer()
136 {
137 #ifdef WITH_QTWEBKIT
138  mWebView->setVisible( mDocumentViewerContent == Web );
139 #endif
140 
141  mPixmapLabel->setVisible( mDocumentViewerContent == Image );
142 
143  if ( mDocumentViewerContent == Image )
144  {
145  const QPixmap *pm = mPixmapLabel->pixmap();
146 
147  if ( !pm || pm->isNull() )
148  {
149  mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
150  }
151  else
152  {
153  QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
154  if ( size.width() == 0 && size.height() > 0 )
155  {
156  size.setWidth( size.height() * pm->size().width() / pm->size().height() );
157  }
158  else if ( size.width() > 0 && size.height() == 0 )
159  {
160  size.setHeight( size.width() * pm->size().height() / pm->size().width() );
161  }
162 
163  if ( size.width() != 0 || size.height() != 0 )
164  {
165  mPixmapLabel->setMinimumSize( size );
166  mPixmapLabel->setMaximumSize( size );
167  }
168  }
169  }
170 }
171 
172 QString QgsExternalResourceWidget::resolvePath( const QString &path )
173 {
174  switch ( mRelativeStorage )
175  {
177  return path;
178  break;
180  return QFileInfo( QgsProject::instance()->absoluteFilePath() ).dir().filePath( path );
181  break;
183  return QDir( mDefaultRoot ).filePath( path );
184  break;
185  }
186  return QString(); // avoid warnings
187 }
188 
190 {
191  return mDefaultRoot;
192 }
193 
194 void QgsExternalResourceWidget::setDefaultRoot( const QString &defaultRoot )
195 {
196  mFileWidget->setDefaultRoot( defaultRoot );
197  mDefaultRoot = defaultRoot;
198 }
199 
201 {
202  return mRelativeStorage;
203 }
204 
206 {
207  mFileWidget->setRelativeStorage( relativeStorage );
208  mRelativeStorage = relativeStorage;
209 }
210 
211 void QgsExternalResourceWidget::loadDocument( const QString &path )
212 {
213  QString resolvedPath;
214 
215  if ( path.isEmpty() )
216  {
217 #ifdef WITH_QTWEBKIT
218  if ( mDocumentViewerContent == Web )
219  {
220  mWebView->setUrl( QUrl( QStringLiteral( "about:blank" ) ) );
221  }
222 #endif
223  if ( mDocumentViewerContent == Image )
224  {
225  mPixmapLabel->clear();
226  updateDocumentViewer();
227  }
228  }
229  else
230  {
231  resolvedPath = resolvePath( path );
232 
233 #ifdef WITH_QTWEBKIT
234  if ( mDocumentViewerContent == Web )
235  {
236  mWebView->load( QUrl::fromEncoded( resolvedPath.toUtf8() ) );
237  mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled, true );
238  }
239 #endif
240 
241  if ( mDocumentViewerContent == Image )
242  {
243  // use an image reader to ensure image orientation and transforms are correctly handled
244  QImageReader ir( resolvedPath );
245  ir.setAutoTransform( true );
246  QPixmap pm = QPixmap::fromImage( ir.read() );
247  if ( !pm.isNull() )
248  {
249  mPixmapLabel->setPixmap( pm );
250  }
251  else
252  {
253  mPixmapLabel->clear();
254  }
255  updateDocumentViewer();
256  }
257  }
258 }
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
void setDocumentPath(const QVariant &documentPath)
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
QgsExternalResourceWidget(QWidget *parent=nullptr)
QgsExternalResourceWidget creates a widget with a file widget and a document viewer Both part of the ...
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
Configures if paths are handled absolute or relative and if relative, which should be the base path.
void setDocumentViewerHeight(int height)
setDocumentViewerWidth set the height of the document viewer.
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly
DocumentViewerContent documentViewerContent
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
QgsFileWidget * fileWidget()
access the file widget to allow its configuration
QgsFileWidget::RelativeStorage relativeStorage
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
void setFileWidgetVisible(bool visible)
Sets the visibility of the file widget in the layout.
void setReadOnly(bool readOnly)
defines if the widget is readonly
void valueChanged(const QString &)
emitteed as soon as the current document changes
The QgsFileWidget class creates a widget for selecting a file or a folder.
Definition: qgsfilewidget.h:37
QString filePath()
Returns the current file path(s) when multiple files are selected, they are quoted and separated by a...
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
determines if the relative path is with respect to the project path or the default path
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:77
void setDefaultRoot(const QString &defaultRoot)
determines the default root path used as the first shown location when picking a file and used if the...
void setReadOnly(bool readOnly)
defines if the widget is readonly
void setFilePath(QString path)
Sets the file path.
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.
Definition: qgsproject.cpp:467