QGIS API Documentation  3.4.3-Madeira (2f64a3c)
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 : denis.rouzaud@gmail.com
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 
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->setUrl( QUrl::fromEncoded( resolvedPath.toUtf8() ) );
234  }
235 #endif
236 
237  if ( mDocumentViewerContent == Image )
238  {
239  // use an image reader to ensure image orientation and transforms are correctly handled
240  QImageReader ir( resolvedPath );
241  ir.setAutoTransform( true );
242  QPixmap pm = QPixmap::fromImage( ir.read() );
243  mPixmapLabel->setPixmap( pm );
244  updateDocumentViewer();
245  }
246  }
247 }
248 
249 
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.
void setPixmap(const QPixmap &)
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
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
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...
int documentViewerWidth() const
returns the width of the document viewer
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 ...
int documentViewerHeight() const
returns the height of the document viewer
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
QgsFileWidget::RelativeStorage relativeStorage() const
Configures if paths are handled absolute or relative and if relative, which should be the base path...
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:411
QString defaultRoot() const
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
QgsExternalResourceWidget::DocumentViewerContent documentViewerContent() const
returns the type of content used in the document viewer
void setRelativeStorage(QgsFileWidget::RelativeStorage relativeStorage)
Configures if paths are handled absolute or relative and if relative, which should be the base path...
bool fileWidgetVisible() const
returns if the file widget is visible in the widget
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 ...