QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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( QUrl::fromEncoded( path.toUtf8() ) );
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  mWebView->settings()->setAttribute( QWebSettings::PluginsEnabled, true );
129 #ifdef QGISDEBUG
130  mWebView->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
131 #endif
132  }
133 
134  if ( mButton )
135  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
136 
137  if ( mLineEdit )
138  {
139  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadUrl( QString ) ) );
140  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
141  }
142 }
143 
145 {
146  return mWebView || mButton || mLineEdit;
147 }
148 
150 {
151  if ( mLineEdit )
152  {
153  if ( value.isNull() )
154  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
155  else
156  mLineEdit->setText( value.toString() );
157  }
158 
159  loadUrl( value.toString() );
160 }
161 
163 {
164  if ( mLineEdit )
165  mLineEdit->setEnabled( enabled );
166 
167  if ( mButton )
168  mButton->setEnabled( enabled );
169 }
170 
171 void QgsWebViewWidgetWrapper::selectFileName()
172 {
173  QString text;
174 
175  if ( mLineEdit )
176  text = mLineEdit->text();
177 
178  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
179 
180  if ( fileName.isNull() )
181  return;
182 
183  QString projPath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
184  QString filePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
185 
186  if ( filePath.startsWith( projPath ) )
187  filePath = QDir( projPath ).relativeFilePath( filePath );
188 
189  if ( mLineEdit )
190  mLineEdit->setText( filePath );
191 }
192 
193 void QgsWebViewWidgetWrapper::updateConstraintWidgetStatus( bool constraintValid )
194 {
195  if ( mLineEdit )
196  {
197  if ( constraintValid )
198  mLineEdit->setStyleSheet( QString() );
199  else
200  {
201  mLineEdit->setStyleSheet( "QgsFilterLineEdit { background-color: #dd7777; }" );
202  }
203  }
204 }
void setStyleSheet(const QString &styleSheet)
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
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
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:333
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
QgsWebViewWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:382
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)
Sets the string representation for null values in the widget.
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
QUrl fromEncoded(const QByteArray &input)
QByteArray toUtf8() const
T findChild(const QString &name) const