QGIS API Documentation  3.13.0-Master (788156190c)
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 
202 void QgsExternalResourceWidget::setDefaultRoot( const QString &defaultRoot )
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  if ( !pm.isNull() )
265  mPixmapLabel->setPixmap( pm );
266  else
267  mPixmapLabel->clear();
268  updateDocumentViewer();
269  }
270  }
271 }
QgsFileWidget::fileChanged
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
QgsFileWidget::setFilePath
void setFilePath(QString path)
Sets the file path.
Definition: qgsfilewidget.cpp:89
QgsExternalResourceWidget::relativeStorage
QgsFileWidget::RelativeStorage relativeStorage
Definition: qgsexternalresourcewidget.h:63
QgsExternalResourceWidget::setDefaultRoot
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
Definition: qgsexternalresourcewidget.cpp:202
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:457
QgsExternalResourceWidget::documentViewerContent
DocumentViewerContent documentViewerContent
Definition: qgsexternalresourcewidget.h:60
QgsFileWidget::blockEvents
void blockEvents(bool)
Emitted before and after showing the file dialog.
QgsExternalResourceWidget::DocumentViewerContent
DocumentViewerContent
Definition: qgsexternalresourcewidget.h:67
QgsExternalResourceWidget::valueChanged
void valueChanged(const QString &)
emitteed as soon as the current document changes
QgsFileWidget::RelativeProject
@ RelativeProject
Definition: qgsfilewidget.h:78
QgsExternalResourceWidget::setReadOnly
void setReadOnly(bool readOnly)
defines if the widget is readonly
Definition: qgsexternalresourcewidget.cpp:138
QgsExternalResourceWidget::setRelativeStorage
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
Configures if paths are handled absolute or relative and if relative, which should be the base path.
Definition: qgsexternalresourcewidget.cpp:222
QgsExternalResourceWidget::setDocumentViewerWidth
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
Definition: qgsexternalresourcewidget.cpp:132
QgsExternalResourceWidget::documentPath
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
Definition: qgsexternalresourcewidget.cpp:71
QgsPixmapLabel::setPixmap
void setPixmap(const QPixmap &)
Definition: qgspixmaplabel.cpp:25
QgsPixmapLabel
The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given to the widget by the ...
Definition: qgspixmaplabel.h:28
QgsExternalResourceWidget::setFileWidgetVisible
void setFileWidgetVisible(bool visible)
Sets the visibility of the file widget in the layout.
Definition: qgsexternalresourcewidget.cpp:99
QgsExternalResourceWidget::fileWidgetVisible
bool fileWidgetVisible
Definition: qgsexternalresourcewidget.h:59
QgsExternalResourceWidget::QgsExternalResourceWidget
QgsExternalResourceWidget(QWidget *parent=nullptr)
QgsExternalResourceWidget creates a widget with a file widget and a document viewer Both part of the ...
Definition: qgsexternalresourcewidget.cpp:31
qgsexternalresourcewidget.h
QgsFileWidget::Absolute
@ Absolute
Definition: qgsfilewidget.h:77
QgsFileWidget::setRelativeStorage
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
determines if the relative path is with respect to the project path or the default path
Definition: qgsfilewidget.cpp:213
qgspixmaplabel.h
QgsFileWidget::RelativeDefaultPath
@ RelativeDefaultPath
Definition: qgsfilewidget.h:79
QgsExternalResourceWidget::setDocumentViewerHeight
void setDocumentViewerHeight(int height)
setDocumentViewerWidth set the height of the document viewer.
Definition: qgsexternalresourcewidget.cpp:121
QgsFileWidget::RelativeStorage
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:75
QgsFileWidget::setReadOnly
void setReadOnly(bool readOnly)
defines if the widget is readonly
Definition: qgsfilewidget.cpp:101
QgsExternalResourceWidget::Image
@ Image
Definition: qgsexternalresourcewidget.h:70
QgsExternalResourceWidget::Web
@ Web
Definition: qgsexternalresourcewidget.h:71
QgsFileWidget::setDefaultRoot
void setDefaultRoot(const QString &defaultRoot)
determines the default root path used as the first shown location when picking a file and used if the...
Definition: qgsfilewidget.cpp:192
QgsExternalResourceWidget::setDocumentPath
void setDocumentPath(const QVariant &documentPath)
Definition: qgsexternalresourcewidget.cpp:84
QgsExternalResourceWidget::eventFilter
bool eventFilter(QObject *watched, QEvent *event) override
Definition: qgsexternalresourcewidget.cpp:208
QgsExternalResourceWidget::setDocumentViewerContent
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly
Definition: qgsexternalresourcewidget.cpp:110
QgsExternalResourceWidget::documentViewerWidth
int documentViewerWidth
Definition: qgsexternalresourcewidget.h:62
QgsExternalResourceWidget::documentViewerHeight
int documentViewerHeight
Definition: qgsexternalresourcewidget.h:61
QgsFileWidget
The QgsFileWidget class creates a widget for selecting a file or a folder.
Definition: qgsfilewidget.h:35
QgsExternalResourceWidget::fileWidget
QgsFileWidget * fileWidget()
access the file widget to allow its configuration
Definition: qgsexternalresourcewidget.cpp:89
qgsproject.h
QgsExternalResourceWidget::defaultRoot
QString defaultRoot
Definition: qgsexternalresourcewidget.h:64
QgsFileWidget::filePath
QString filePath()
Returns the current file path(s) when multiple files are selected, they are quoted and separated by a...
Definition: qgsfilewidget.cpp:71