QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
21 #include <QDir>
22 #include <QGridLayout>
23 #include <QVariant>
24 #include <QSettings>
25 #include <QImageReader>
26 #ifdef WITH_QTWEBKIT
27 #include <QWebView>
28 #endif
29 
30 
32  : QWidget( parent )
33 {
34  setBackgroundRole( QPalette::Window );
35  setAutoFillBackground( true );
36 
37  QGridLayout *layout = new QGridLayout();
38  layout->setMargin( 0 );
39 
40  mFileWidget = new QgsFileWidget( this );
41  layout->addWidget( mFileWidget, 0, 0 );
42  mFileWidget->setVisible( mFileWidgetVisible );
43 
44  mPixmapLabel = new QgsPixmapLabel( this );
45  layout->addWidget( mPixmapLabel, 1, 0 );
46 
47 #ifdef WITH_QTWEBKIT
48  mWebView = new QWebView( this );
49  layout->addWidget( mWebView, 2, 0 );
50 #endif
51 
52  updateDocumentViewer();
53 
54  setLayout( layout );
55 
56  connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsExternalResourceWidget::loadDocument );
58  connect( mFileWidget, &QgsFileWidget::blockEvents, this, [this]( bool block )
59  {
60  if ( block )
61  {
62  installEventFilter( this );
63  }
64  else
65  {
66  removeEventFilter( this );
67  }
68  } );
69 }
70 
71 QVariant QgsExternalResourceWidget::documentPath( QVariant::Type type ) const
72 {
73  QString path = mFileWidget->filePath();
74  if ( path.isEmpty() )
75  {
76  return QVariant( type );
77  }
78  else
79  {
80  return path;
81  }
82 }
83 
84 void QgsExternalResourceWidget::setDocumentPath( const QVariant &path )
85 {
86  mFileWidget->setFilePath( path.toString() );
87 }
88 
90 {
91  return mFileWidget;
92 }
93 
95 {
96  return mFileWidgetVisible;
97 }
98 
100 {
101  mFileWidgetVisible = visible;
102  mFileWidget->setVisible( visible );
103 }
104 
106 {
107  return mDocumentViewerContent;
108 }
109 
111 {
112  mDocumentViewerContent = content;
113  updateDocumentViewer();
114 }
115 
117 {
118  return mDocumentViewerHeight;
119 }
120 
122 {
123  mDocumentViewerHeight = height;
124  updateDocumentViewer();
125 }
126 
128 {
129  return mDocumentViewerWidth;
130 }
131 
133 {
134  mDocumentViewerWidth = width;
135  updateDocumentViewer();
136 }
137 
139 {
140  mFileWidget->setReadOnly( readOnly );
141 }
142 
143 void QgsExternalResourceWidget::updateDocumentViewer()
144 {
145 #ifdef WITH_QTWEBKIT
146  mWebView->setVisible( mDocumentViewerContent == Web );
147 #endif
148 
149  mPixmapLabel->setVisible( mDocumentViewerContent == Image );
150 
151  if ( mDocumentViewerContent == Image )
152  {
153  const QPixmap *pm = mPixmapLabel->pixmap();
154 
155  if ( !pm || pm->isNull() )
156  {
157  mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
158  }
159  else
160  {
161  QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
162  if ( size.width() == 0 && size.height() > 0 )
163  {
164  size.setWidth( size.height() * pm->size().width() / pm->size().height() );
165  }
166  else if ( size.width() > 0 && size.height() == 0 )
167  {
168  size.setHeight( size.width() * pm->size().height() / pm->size().width() );
169  }
170 
171  if ( size.width() != 0 || size.height() != 0 )
172  {
173  mPixmapLabel->setMinimumSize( size );
174  mPixmapLabel->setMaximumSize( size );
175  }
176  }
177  }
178 }
179 
180 QString QgsExternalResourceWidget::resolvePath( const QString &path )
181 {
182  switch ( mRelativeStorage )
183  {
185  return path;
186  break;
188  return QFileInfo( QgsProject::instance()->absoluteFilePath() ).dir().filePath( path );
189  break;
191  return QDir( mDefaultRoot ).filePath( path );
192  break;
193  }
194  return QString(); // avoid warnings
195 }
196 
198 {
199  return mDefaultRoot;
200 }
201 
203 {
204  mFileWidget->setDefaultRoot( defaultRoot );
205  mDefaultRoot = defaultRoot;
206 }
207 
208 bool QgsExternalResourceWidget::eventFilter( QObject *watched, QEvent *event )
209 {
210  if ( watched == this && event && ( event->type() == QEvent::FocusOut || event->type() == QEvent::FocusAboutToChange ) )
211  {
212  return true;
213  }
214  return QWidget::eventFilter( watched, event );
215 }
216 
218 {
219  return mRelativeStorage;
220 }
221 
223 {
224  mFileWidget->setRelativeStorage( relativeStorage );
225  mRelativeStorage = relativeStorage;
226 }
227 
228 void QgsExternalResourceWidget::loadDocument( const QString &path )
229 {
230  QString resolvedPath;
231 
232  if ( path.isEmpty() )
233  {
234 #ifdef WITH_QTWEBKIT
235  if ( mDocumentViewerContent == Web )
236  {
237  mWebView->setUrl( QUrl( QStringLiteral( "about:blank" ) ) );
238  }
239 #endif
240  if ( mDocumentViewerContent == Image )
241  {
242  mPixmapLabel->clear();
243  updateDocumentViewer();
244  }
245  }
246  else
247  {
248  resolvedPath = resolvePath( path );
249 
250 #ifdef WITH_QTWEBKIT
251  if ( mDocumentViewerContent == Web )
252  {
253  mWebView->load( QUrl::fromEncoded( resolvedPath.toUtf8() ) );
254  mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled, true );
255  }
256 #endif
257 
258  if ( mDocumentViewerContent == Image )
259  {
260  // use an image reader to ensure image orientation and transforms are correctly handled
261  QImageReader ir( resolvedPath );
262  ir.setAutoTransform( true );
263  QPixmap pm = QPixmap::fromImage( ir.read() );
264  mPixmapLabel->setPixmap( pm );
265  updateDocumentViewer();
266  }
267  }
268 }
void valueChanged(const QString &)
emitteed as soon as the current document changes
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 setDocumentViewerHeight(int height)
setDocumentViewerWidth set the height of the document viewer.
bool eventFilter(QObject *watched, QEvent *event) override
void setPixmap(const QPixmap &)
void fileChanged(const QString &)
emitted as soon as the current file or directory is changed
void setFileWidgetVisible(bool visible)
Sets the visiblity of the file widget in the layout.
QgsFileWidget * fileWidget()
access the file widget to allow its configuration
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly ...
void setReadOnly(bool readOnly)
defines if the widget is readonly
int documentViewerHeight() const
returns the height of the document viewer
QgsExternalResourceWidget(QWidget *parent=nullptr)
QgsExternalResourceWidget creates a widget with a file widget and a document viewer Both part of the ...
void setDocumentPath(const QVariant &documentPath)
QString filePath()
Returns the current file path(s) when multiple files are selected, they are quoted and separated by a...
The QgsFileWidget class creates a widget for selecting a file or a folder.
Definition: qgsfilewidget.h:35
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
determines if the relative path is with respect to the project path or the default path ...
QgsFileWidget::RelativeStorage relativeStorage() const
Configures if paths are handled absolute or relative and if relative, which should be the base path...
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
bool fileWidgetVisible() const
returns if the file widget is visible in the widget
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
QString defaultRoot() const
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:411
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
Configures if paths are handled absolute or relative and if relative, which should be the base path...
QgsExternalResourceWidget::DocumentViewerContent documentViewerContent() const
returns the type of content used in the document viewer
int documentViewerWidth() const
returns the width of the document viewer
void blockEvents(bool)
Emitted before and after showing the file dialog.
void setReadOnly(bool readOnly)
defines if the widget is readonly
void setFilePath(QString path)
Sets the file path.
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:74
The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given to the widget by the ...