QGIS API Documentation  2.15.0-Master (13f053b)
qgswebviewwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgswebviewwidgetwrapper.cpp
3  --------------------------------------
4  Date : 5.1.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias at opengis dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgsfilterlineedit.h"
20 #include "qgsproject.h"
21 
22 #include <QGridLayout>
23 #include <QFileDialog>
24 #include <QSettings>
25 
27  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
28  , mWebView( nullptr )
29  , mLineEdit( nullptr )
30  , mButton( nullptr )
31 {
32 }
33 
34 void QgsWebViewWidgetWrapper::loadUrl( const QString &url )
35 {
36  QString path = url;
37 
38  if ( QUrl( url ).isRelative() )
39  path = QDir( QgsProject::instance()->fileInfo().absolutePath() ).filePath( url );
40 
41  if ( mWebView )
42  mWebView->load( path );
43 }
44 
46 {
47  QVariant v;
48 
49  if ( mLineEdit )
50  {
51  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
52  v = QVariant( QVariant::String );
53  else
54  v = mLineEdit->text();
55  }
56 
57  return v;
58 }
59 
61 {
62  if ( mLineEdit )
63  {
64  whileBlocking( mLineEdit )->clear();
65  }
66 
67  if ( mWebView )
68  mWebView->load( QString() );
69 }
70 
72 {
73  QWidget* container = new QWidget( parent );
74  QGridLayout* layout = new QGridLayout();
76  QWebView* webView = new QWebView();
77  webView->setObjectName( "EditorWebView" );
78  QPushButton* pb = new QPushButton( tr( "..." ) );
79  pb->setObjectName( "FileChooserButton" );
80 
81  layout->addWidget( webView, 0, 0, 1, 2 );
82  layout->addWidget( le, 1, 0 );
83  layout->addWidget( pb, 1, 1 );
84  layout->setMargin( 0 );
85  layout->setContentsMargins( 0, 0, 0, 0 );
86 
87  container->setLayout( layout );
88 
89  return container;
90 }
91 
93 {
94  QWidget* container;
95 
96  mLineEdit = qobject_cast<QLineEdit*>( editor );
97 
98  if ( mLineEdit )
99  {
100 
101  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
102  if ( fle )
103  {
104  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
105  }
106 
107  container = qobject_cast<QWidget*>( mLineEdit->parent() );
108  }
109  else
110  {
111  container = editor;
112  mLineEdit = container->findChild<QLineEdit*>();
113  }
114 
115  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
116  if ( !mButton )
117  mButton = container->findChild<QPushButton*>();
118 
119  mWebView = container->findChild<QWebView*>( "EditorWebView" );
120  if ( !mWebView )
121  mWebView = container->findChild<QWebView*>();
122 
123  if ( mWebView )
124  {
125  mWebView->page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
126  mWebView->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
127  mWebView->settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
128 #ifdef QGISDEBUG
129  mWebView->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
130 #endif
131  }
132 
133  if ( mButton )
134  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
135 
136  if ( mLineEdit )
137  {
138  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadUrl( QString ) ) );
139  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
140  }
141 }
142 
144 {
145  return mWebView || mButton || mLineEdit;
146 }
147 
149 {
150  if ( mLineEdit )
151  {
152  if ( value.isNull() )
153  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
154  else
155  mLineEdit->setText( value.toString() );
156  }
157 
158  loadUrl( value.toString() );
159 }
160 
162 {
163  if ( mLineEdit )
164  mLineEdit->setEnabled( enabled );
165 
166  if ( mButton )
167  mButton->setEnabled( enabled );
168 }
169 
170 void QgsWebViewWidgetWrapper::selectFileName()
171 {
172  QString text;
173 
174  if ( mLineEdit )
175  text = mLineEdit->text();
176 
177  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
178 
179  if ( fileName.isNull() )
180  return;
181 
182  QString projPath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
183  QString filePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
184 
185  if ( filePath.startsWith( projPath ) )
186  filePath = QDir( projPath ).relativeFilePath( filePath );
187 
188  if ( mLineEdit )
189  mLineEdit->setText( filePath );
190 }
QString toNativeSeparators(const QString &pathName)
QString relativeFilePath(const QString &fileName) const
void setContentsMargins(int left, int top, int right, int bottom)
void load(const QUrl &url)
void valueChanged()
Will call the value() method to determine the emitted value.
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Manages an editor widget Widget and wrapper share the same parent.
QString filePath(const QString &fileName) const
void setAttribute(Qt::WidgetAttribute attribute, bool on)
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isNull() const
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
void setEnabled(bool)
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setLayout(QLayout *layout)
bool isNull() const
void setObjectName(const QString &name)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
Lineedit with builtin clear button.
void setMargin(int margin)
void setEnabled(bool enabled) override
QVariant value() const override
Will be used to access the widget&#39;s value.
QString cleanPath(const QString &path)
QVariant value(const QString &key, const QVariant &defaultValue) const
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:332
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
QgsWebViewWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:388
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
bool valid() const override
Return true if the widget has been properly initialized.
void setNullValue(const QString &nullValue)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QString toString() const
void setValue(const QVariant &value) override
T findChild(const QString &name) const