QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  mPixmapLabel->setPixmap( pm );
249  else
250  mPixmapLabel->clear();
251  updateDocumentViewer();
252  }
253  }
254 }
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:194
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:468
QgsExternalResourceWidget::DocumentViewerContent
DocumentViewerContent
Definition: qgsexternalresourcewidget.h:68
QgsExternalResourceWidget::valueChanged
void valueChanged(const QString &)
emitteed as soon as the current document changes
QgsFileWidget::RelativeProject
@ RelativeProject
Definition: qgsfilewidget.h:79
qgsapplication.h
QgsExternalResourceWidget::setReadOnly
void setReadOnly(bool readOnly)
defines if the widget is readonly
Definition: qgsexternalresourcewidget.cpp:130
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:205
QgsExternalResourceWidget::setDocumentViewerWidth
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
Definition: qgsexternalresourcewidget.cpp:124
QgsExternalResourceWidget::documentPath
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
Definition: qgsexternalresourcewidget.cpp:61
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:29
QgsExternalResourceWidget::setFileWidgetVisible
void setFileWidgetVisible(bool visible)
Sets the visibility of the file widget in the layout.
Definition: qgsexternalresourcewidget.cpp:89
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:1851
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:32
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:226
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:113
QgsFileWidget::RelativeStorage
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:77
QgsFileWidget::setReadOnly
void setReadOnly(bool readOnly)
defines if the widget is readonly
Definition: qgsfilewidget.cpp:99
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:205
QgsExternalResourceWidget::setDocumentPath
void setDocumentPath(const QVariant &documentPath)
Definition: qgsexternalresourcewidget.cpp:74
QgsExternalResourceWidget::setDocumentViewerContent
void setDocumentViewerContent(QgsExternalResourceWidget::DocumentViewerContent content)
setDocumentViewerContent defines the type of content to be shown. Widget will be adapted accordingly
Definition: qgsexternalresourcewidget.cpp:100
QgsExternalResourceWidget::documentViewerWidth
int documentViewerWidth
Definition: qgsexternalresourcewidget.h:62
QgsExternalResourceWidget::documentViewerHeight
int documentViewerHeight
Definition: qgsexternalresourcewidget.h:61
QgsExternalResourceWidget::documentViewerContent
DocumentViewerContent documentViewerContent
Definition: qgsexternalresourcewidget.h:60
QgsFileWidget
The QgsFileWidget class creates a widget for selecting a file or a folder.
Definition: qgsfilewidget.h:37
QgsExternalResourceWidget::fileWidget
QgsFileWidget * fileWidget()
access the file widget to allow its configuration
Definition: qgsexternalresourcewidget.cpp:79
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