QGIS API Documentation  2.14.0-Essen
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 
20 #include <QGridLayout>
21 #include <QVariant>
22 #include <QSettings>
23 #ifdef WITH_QTWEBKIT
24 #include <QWebView>
25 #endif
26 
27 
29  : QWidget( parent )
30  , mFileWidgetVisible( true )
31  , mDocumentViewerContent( NoContent )
32  , mDocumentViewerHeight( 0 )
33  , mDocumentViewerWidth( 0 )
34 
35 {
36  setBackgroundRole( QPalette::Window );
37  setAutoFillBackground( true );
38 
40  layout->setMargin( 0 );
41 
42  mFileWidget = new QgsFileWidget( this );
43  layout->addWidget( mFileWidget, 0, 0 );
44  mFileWidget->setVisible( mFileWidgetVisible );
45 
46  mPixmapLabel = new QgsPixmapLabel( this );
47  layout->addWidget( mPixmapLabel, 1, 0 );
48 
49 #ifdef WITH_QTWEBKIT
50  mWebView = new QWebView( this );
51  layout->addWidget( mWebView, 2, 0 );
52 #endif
53 
54  updateDocumentViewer();
55 
56  setLayout( layout );
57 
58  connect( mFileWidget, SIGNAL( fileChanged( QString ) ), this, SLOT( loadDocument( QString ) ) );
59 }
60 
62 {
63  QString path = mFileWidget->filePath();
64  if ( path.isEmpty() )
65  {
66  return QVariant( type );
67  }
68  else
69  {
70  return path;
71  }
72 }
73 
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  updateDocumentViewer();
104 }
105 
107 {
108  return mDocumentViewerHeight;
109 }
110 
112 {
113  mDocumentViewerHeight = height;
114  updateDocumentViewer();
115 }
116 
118 {
119  return mDocumentViewerWidth;
120 }
121 
123 {
124  mDocumentViewerWidth = width;
125  updateDocumentViewer();
126 }
127 
129 {
130  mFileWidget->setReadOnly( readOnly );
131 }
132 
133 void QgsExternalResourceWidget::updateDocumentViewer()
134 {
135 #ifdef WITH_QTWEBKIT
136  mWebView->setVisible( mDocumentViewerContent == Web );
137 #endif
138 
139  mPixmapLabel->setVisible( mDocumentViewerContent == Image );
140 
141  if ( mDocumentViewerContent == Image )
142  {
143  const QPixmap* pm = mPixmapLabel->pixmap();
144 
145  if ( pm )
146  {
147  QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
148  if ( size.width() == 0 && size.height() > 0 )
149  {
150  size.setWidth( size.height() * pm->size().width() / pm->size().height() );
151  }
152  else if ( size.width() > 0 && size.height() == 0 )
153  {
154  size.setHeight( size.width() * pm->size().height() / pm->size().width() );
155  }
156 
157  if ( size.width() != 0 || size.height() != 0 )
158  {
159  mPixmapLabel->setMinimumSize( size );
160  mPixmapLabel->setMaximumSize( size );
161  }
162  }
163  }
164 }
165 
166 void QgsExternalResourceWidget::loadDocument( const QString& path )
167 {
168  if ( path.isNull() )
169  {
170 #ifdef WITH_QTWEBKIT
171  if ( mDocumentViewerContent == Web )
172  {
173  mWebView->setUrl( QUrl( "about:blank" ) );
174  }
175 #endif
176  if ( mDocumentViewerContent == Image )
177  {
178  mPixmapLabel->clear();
179  }
180  }
181 
182 
183 #ifdef WITH_QTWEBKIT
184  if ( mDocumentViewerContent == Web )
185  {
186  mWebView->setUrl( QUrl( path ) );
187  }
188 #endif
189 
190  if ( mDocumentViewerContent == Image )
191  {
192  QPixmap pm( path );
193  if ( !pm.isNull() )
194  {
195  mPixmapLabel->setPixmap( pm );
196 
197  updateDocumentViewer();
198  }
199  }
200 }
201 
202 
QLayout * layout() const
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
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
int documentViewerHeight() const
returns the height of the document viewer
virtual void setVisible(bool visible)
void clear()
void setDocumentPath(const QVariant &documentPath)
QString filePath()
Returns the current file path.
bool isNull() const
int width() const
QSize size() const
void setMinimumSize(const QSize &)
void setLayout(QLayout *layout)
The QgsFileWidget class creates a widget for selecting a file or a folder.
Definition: qgsfilewidget.h:31
void setWidth(int width)
bool isEmpty() const
QgsExternalResourceWidget(QWidget *parent=0)
QgsExternalResourceWidget creates a widget with a file widget and a document viewer Both part of the ...
bool fileWidgetVisible() const
returns if the file widget is visible in the widget
void setMargin(int margin)
bool isNull() const
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
void setMaximumSize(const QSize &)
QVariant documentPath(QVariant::Type type=QVariant::String) const
documentPath returns the path of the current document in the widget
int height() const
void setAutoFillBackground(bool enabled)
QgsExternalResourceWidget::DocumentViewerContent documentViewerContent() const
returns the type of content used in the document viewer
int documentViewerWidth() const
returns the width of the document viewer
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
The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given to the widget by the ...