QGIS API Documentation  2.14.11-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 : 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 
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 || pm->isNull() )
146  {
147  mPixmapLabel->setMinimumSize( QSize( 0, 0 ) );
148  }
149  else
150  {
151  QSize size( mDocumentViewerWidth, mDocumentViewerHeight );
152  if ( size.width() == 0 && size.height() > 0 )
153  {
154  size.setWidth( size.height() * pm->size().width() / pm->size().height() );
155  }
156  else if ( size.width() > 0 && size.height() == 0 )
157  {
158  size.setHeight( size.width() * pm->size().height() / pm->size().width() );
159  }
160 
161  if ( size.width() != 0 || size.height() != 0 )
162  {
163  mPixmapLabel->setMinimumSize( size );
164  mPixmapLabel->setMaximumSize( size );
165  }
166  }
167  }
168 }
169 
170 void QgsExternalResourceWidget::loadDocument( const QString& path )
171 {
172  if ( path.isEmpty() )
173  {
174 #ifdef WITH_QTWEBKIT
175  if ( mDocumentViewerContent == Web )
176  {
177  mWebView->setUrl( QUrl( "about:blank" ) );
178  }
179 #endif
180  if ( mDocumentViewerContent == Image )
181  {
182  mPixmapLabel->clear();
183  updateDocumentViewer();
184  }
185  }
186 
187 
188 #ifdef WITH_QTWEBKIT
189  if ( mDocumentViewerContent == Web )
190  {
191  mWebView->setUrl( QUrl( path ) );
192  }
193 #endif
194 
195  if ( mDocumentViewerContent == Image )
196  {
197  QPixmap pm( path );
198  mPixmapLabel->setPixmap( pm );
199  updateDocumentViewer();
200  }
201 }
202 
203 
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
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)
void clear()
void setDocumentPath(const QVariant &documentPath)
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
int documentViewerHeight() const
returns the height of the document viewer
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 ...
void setMargin(int margin)
bool isNull() const
void setDocumentViewerWidth(int width)
setDocumentViewerWidth set the width of the document viewer.
void setMaximumSize(const QSize &)
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
The QgsPixmapLabel class shows a pixmap and adjusts its size to the space given to the widget by the ...