QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 #ifdef WITH_QTWEBKIT
26 #include <QWebView>
27 #endif
28 
29 
31  : QWidget( parent )
32  , mFileWidgetVisible( true )
33  , mDocumentViewerContent( NoContent )
34  , mDocumentViewerHeight( 0 )
35  , mDocumentViewerWidth( 0 )
36  , mRelativeStorage( QgsFileWidget::Absolute )
37 
38 {
39  setBackgroundRole( QPalette::Window );
40  setAutoFillBackground( true );
41 
43  layout->setMargin( 0 );
44 
45  mFileWidget = new QgsFileWidget( this );
46  layout->addWidget( mFileWidget, 0, 0 );
47  mFileWidget->setVisible( mFileWidgetVisible );
48 
49  mPixmapLabel = new QgsPixmapLabel( this );
50  layout->addWidget( mPixmapLabel, 1, 0 );
51 
52 #ifdef WITH_QTWEBKIT
53  mWebView = new QWebView( this );
54  layout->addWidget( mWebView, 2, 0 );
55 #endif
56 
57  updateDocumentViewer();
58 
59  setLayout( layout );
60 
61  connect( mFileWidget, SIGNAL( fileChanged( QString ) ), this, SLOT( loadDocument( QString ) ) );
62  connect( mFileWidget, SIGNAL( fileChanged( QString ) ), this, SIGNAL( valueChanged( QString ) ) );
63 }
64 
66 {
67  QString path = mFileWidget->filePath();
68  if ( path.isEmpty() )
69  {
70  return QVariant( type );
71  }
72  else
73  {
74  return path;
75  }
76 }
77 
79 {
80  mFileWidget->setFilePath( path.toString() );
81 }
82 
84 {
85  return mFileWidget;
86 }
87 
89 {
90  return mFileWidgetVisible;
91 }
92 
94 {
95  mFileWidgetVisible = visible;
96  mFileWidget->setVisible( visible );
97 }
98 
100 {
101  return mDocumentViewerContent;
102 }
103 
105 {
106  mDocumentViewerContent = content;
107  updateDocumentViewer();
108 }
109 
111 {
112  return mDocumentViewerHeight;
113 }
114 
116 {
117  mDocumentViewerHeight = height;
118  updateDocumentViewer();
119 }
120 
122 {
123  return mDocumentViewerWidth;
124 }
125 
127 {
128  mDocumentViewerWidth = width;
129  updateDocumentViewer();
130 }
131 
133 {
134  mFileWidget->setReadOnly( readOnly );
135 }
136 
137 void QgsExternalResourceWidget::updateDocumentViewer()
138 {
139 #ifdef WITH_QTWEBKIT
140  mWebView->setVisible( mDocumentViewerContent == Web );
141 #endif
142 
143  mPixmapLabel->setVisible( mDocumentViewerContent == Image );
144 
145  if ( mDocumentViewerContent == Image )
146  {
147  const QPixmap* pm = mPixmapLabel->pixmap();
148 
149  if ( !pm || pm->isNull() )
150  {
151  mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
152  }
153  else
154  {
155  QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
156  if ( size.width() == 0 && size.height() > 0 )
157  {
158  size.setWidth( size.height() * pm->size().width() / pm->size().height() );
159  }
160  else if ( size.width() > 0 && size.height() == 0 )
161  {
162  size.setHeight( size.width() * pm->size().height() / pm->size().width() );
163  }
164 
165  if ( size.width() != 0 || size.height() != 0 )
166  {
167  mPixmapLabel->setMinimumSize( size );
168  mPixmapLabel->setMaximumSize( size );
169  }
170  }
171  }
172 }
173 
174 QString QgsExternalResourceWidget::resolvePath( const QString& path )
175 {
176  switch ( mRelativeStorage )
177  {
179  return path;
180  break;
182  return QgsProject::instance()->fileInfo().dir().filePath( path );
183  break;
185  return QDir( mDefaultRoot ).filePath( path );
186  break;
187  }
188  return QString(); // avoid warnings
189 }
190 
192 {
193  return mDefaultRoot;
194 }
195 
197 {
198  mFileWidget->setDefaultRoot( defaultRoot );
199  mDefaultRoot = defaultRoot;
200 }
201 
203 {
204  return mRelativeStorage;
205 }
206 
208 {
209  mFileWidget->setRelativeStorage( relativeStorage );
210  mRelativeStorage = relativeStorage;
211 }
212 
213 void QgsExternalResourceWidget::loadDocument( const QString& path )
214 {
215  QString resolvedPath;
216 
217  if ( path.isEmpty() )
218  {
219 #ifdef WITH_QTWEBKIT
220  if ( mDocumentViewerContent == Web )
221  {
222  mWebView->setUrl( QUrl( "about:blank" ) );
223  }
224 #endif
225  if ( mDocumentViewerContent == Image )
226  {
227  mPixmapLabel->clear();
228  updateDocumentViewer();
229  }
230  }
231  else
232  {
233  resolvedPath = resolvePath( path );
234 
235 #ifdef WITH_QTWEBKIT
236  if ( mDocumentViewerContent == Web )
237  {
238  mWebView->setUrl( QUrl( resolvedPath ) );
239  }
240 #endif
241 
242  if ( mDocumentViewerContent == Image )
243  {
244  QPixmap pm( resolvedPath );
245  mPixmapLabel->setPixmap( pm );
246  updateDocumentViewer();
247  }
248  }
249 }
250 
251 
QLayout * layout() const
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...
QSize size() const
void setDocumentViewerHeight(int height)
setDocumentViewerWidth set the height of the document viewer.
void setHeight(int height)
void setPixmap(const QPixmap &)
int width() const
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
void setFileWidgetVisible(bool visible)
set the visiblity of the file widget in the layout
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
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 adapated accordingly...
void setReadOnly(bool readOnly)
defines if the widget is readonly
virtual void setVisible(bool visible)
QString filePath(const QString &fileName) const
void clear()
void setDocumentPath(const QVariant &documentPath)
void setRelativeStorage(const QgsFileWidget::RelativeStorage &relativeStorage)
Configures if paths are handled absolute or relative and if relative, which should be the base path...
QString filePath()
Returns the current file path.
int width() const
QSize size() const
void setMinimumSize(const QSize &)
int documentViewerWidth() const
returns the width of the document viewer
void setLayout(QLayout *layout)
The QgsFileWidget class creates a widget for selecting a file or a folder.
Definition: qgsfilewidget.h:31
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 setWidth(int width)
bool isEmpty() const
void setDefaultRoot(const QString &defaultRoot)
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
QgsExternalResourceWidget(QWidget *parent=0)
QgsExternalResourceWidget creates a widget with a file widget and a document viewer Both part of the ...
QDir dir() const
QgsFileWidget::RelativeStorage relativeStorage() const
Configures if paths are handled absolute or relative and if relative, which should be the base path...
void setMargin(int margin)
bool isNull() const
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
void setMaximumSize(const QSize &)
QFileInfo fileInfo() const
Returns QFileInfo object for the project&#39;s associated file.
Definition: qgsproject.cpp:442
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:382
QString defaultRoot() const
Configures the base path which should be used if the relativeStorage property is set to QgsFileWidget...
int height() const
QgsExternalResourceWidget::DocumentViewerContent documentViewerContent() const
returns the type of content used in the document viewer
void setAutoFillBackground(bool enabled)
bool fileWidgetVisible() const
returns if the file widget is visible in the widget
void setBackgroundRole(QPalette::ColorRole role)
void setReadOnly(bool readOnly)
defines if the widget is readonly
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const
void setFilePath(QString path)
Sets the file path.
int height() const
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:56
The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given to the widget by the ...