QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
59 
60 QVariant QgsExternalResourceWidget::documentPath( QVariant::Type type ) const
61 {
62  QString path = mFileWidget->filePath();
63  if ( path.isEmpty() )
64  {
65  return QVariant( type );
66  }
67  else
68  {
69  return path;
70  }
71 }
72 
73 void QgsExternalResourceWidget::setDocumentPath( const QVariant &path )
74 {
75  mFileWidget->setFilePath( path.toString() );
76 }
77 
79 {
80  return mFileWidget;
81 }
82 
84 {
85  return mFileWidgetVisible;
86 }
87 
89 {
90  mFileWidgetVisible = visible;
91  mFileWidget->setVisible( visible );
92 }
93 
95 {
96  return mDocumentViewerContent;
97 }
98 
100 {
101  mDocumentViewerContent = content;
102  updateDocumentViewer();
103 }
104 
106 {
107  return mDocumentViewerHeight;
108 }
109 
111 {
112  mDocumentViewerHeight = height;
113  updateDocumentViewer();
114 }
115 
117 {
118  return mDocumentViewerWidth;
119 }
120 
122 {
123  mDocumentViewerWidth = width;
124  updateDocumentViewer();
125 }
126 
128 {
129  mFileWidget->setReadOnly( readOnly );
130 }
131 
132 void QgsExternalResourceWidget::updateDocumentViewer()
133 {
134 #ifdef WITH_QTWEBKIT
135  mWebView->setVisible( mDocumentViewerContent == Web );
136 #endif
137 
138  mPixmapLabel->setVisible( mDocumentViewerContent == Image );
139 
140  if ( mDocumentViewerContent == Image )
141  {
142  const QPixmap *pm = mPixmapLabel->pixmap();
143 
144  if ( !pm || pm->isNull() )
145  {
146  mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
147  }
148  else
149  {
150  QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
151  if ( size.width() == 0 && size.height() > 0 )
152  {
153  size.setWidth( size.height() * pm->size().width() / pm->size().height() );
154  }
155  else if ( size.width() > 0 && size.height() == 0 )
156  {
157  size.setHeight( size.width() * pm->size().height() / pm->size().width() );
158  }
159 
160  if ( size.width() != 0 || size.height() != 0 )
161  {
162  mPixmapLabel->setMinimumSize( size );
163  mPixmapLabel->setMaximumSize( size );
164  }
165  }
166  }
167 }
168 
169 QString QgsExternalResourceWidget::resolvePath( const QString &path )
170 {
171  switch ( mRelativeStorage )
172  {
174  return path;
175  break;
177  return QFileInfo( QgsProject::instance()->absoluteFilePath() ).dir().filePath( path );
178  break;
180  return QDir( mDefaultRoot ).filePath( path );
181  break;
182  }
183  return QString(); // avoid warnings
184 }
185 
187 {
188  return mDefaultRoot;
189 }
190 
191 void QgsExternalResourceWidget::setDefaultRoot( const QString &defaultRoot )
192 {
193  mFileWidget->setDefaultRoot( defaultRoot );
194  mDefaultRoot = defaultRoot;
195 }
196 
198 {
199  return mRelativeStorage;
200 }
201 
203 {
204  mFileWidget->setRelativeStorage( relativeStorage );
205  mRelativeStorage = relativeStorage;
206 }
207 
208 void QgsExternalResourceWidget::loadDocument( const QString &path )
209 {
210  QString resolvedPath;
211 
212  if ( path.isEmpty() )
213  {
214 #ifdef WITH_QTWEBKIT
215  if ( mDocumentViewerContent == Web )
216  {
217  mWebView->setUrl( QUrl( QStringLiteral( "about:blank" ) ) );
218  }
219 #endif
220  if ( mDocumentViewerContent == Image )
221  {
222  mPixmapLabel->clear();
223  updateDocumentViewer();
224  }
225  }
226  else
227  {
228  resolvedPath = resolvePath( path );
229 
230 #ifdef WITH_QTWEBKIT
231  if ( mDocumentViewerContent == Web )
232  {
233  mWebView->load( QUrl::fromEncoded( resolvedPath.toUtf8() ) );
234  mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled, true );
235  }
236 #endif
237 
238  if ( mDocumentViewerContent == Image )
239  {
240  // use an image reader to ensure image orientation and transforms are correctly handled
241  QImageReader ir( resolvedPath );
242  ir.setAutoTransform( true );
243  QPixmap pm = QPixmap::fromImage( ir.read() );
244  if ( !pm.isNull() )
245  mPixmapLabel->setPixmap( pm );
246  else
247  mPixmapLabel->clear();
248  updateDocumentViewer();
249  }
250  }
251 }
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:93
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:191
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:458
QgsExternalResourceWidget::documentViewerContent
DocumentViewerContent documentViewerContent
Definition: qgsexternalresourcewidget.h:60
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:79
QgsExternalResourceWidget::setReadOnly
void setReadOnly(bool readOnly)
defines if the widget is readonly
Definition: qgsexternalresourcewidget.cpp:127
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:202
QgsExternalResourceWidget::setDocumentViewerWidth
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
Definition: qgsexternalresourcewidget.cpp:121
QgsExternalResourceWidget::documentPath
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
Definition: qgsexternalresourcewidget.cpp:60
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:88
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:78
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:232
qgspixmaplabel.h
QgsFileWidget::RelativeDefaultPath
@ RelativeDefaultPath
Definition: qgsfilewidget.h:80
QgsExternalResourceWidget::setDocumentViewerHeight
void setDocumentViewerHeight(int height)
setDocumentViewerWidth set the height of the document viewer.
Definition: qgsexternalresourcewidget.cpp:110
QgsFileWidget::RelativeStorage
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:76
QgsFileWidget::setReadOnly
void setReadOnly(bool readOnly)
defines if the widget is readonly
Definition: qgsfilewidget.cpp:105
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:211
QgsExternalResourceWidget::setDocumentPath
void setDocumentPath(const QVariant &documentPath)
Definition: qgsexternalresourcewidget.cpp:73
QgsExternalResourceWidget::setDocumentViewerContent
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly
Definition: qgsexternalresourcewidget.cpp:99
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:36
QgsExternalResourceWidget::fileWidget
QgsFileWidget * fileWidget()
access the file widget to allow its configuration
Definition: qgsexternalresourcewidget.cpp:78
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:75