QGIS API Documentation  2.99.0-Master (8ec3eaf)
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 
26 QgsWebViewWidgetWrapper::QgsWebViewWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
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( QStringLiteral( "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 
71 QWidget* QgsWebViewWidgetWrapper::createWidget( QWidget* parent )
72 {
73  QWidget* container = new QWidget( parent );
74  QGridLayout* layout = new QGridLayout();
76  QWebView* webView = new QWebView();
77  webView->setObjectName( QStringLiteral( "EditorWebView" ) );
78  QPushButton* pb = new QPushButton( tr( "..." ) );
79  pb->setObjectName( QStringLiteral( "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 
92 void QgsWebViewWidgetWrapper::initWidget( QWidget* editor )
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( QStringLiteral( "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*>( QStringLiteral( "FileChooserButton" ) );
116  if ( !mButton )
117  mButton = container->findChild<QPushButton*>();
118 
119  mWebView = container->findChild<QWebView*>( QStringLiteral( "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( QStringLiteral( "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( ConstraintResult status )
194 {
195  if ( mLineEdit )
196  {
197  switch ( status )
198  {
200  mLineEdit->setStyleSheet( QString() );
201  break;
202 
204  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #dd7777; }" ) );
205  break;
206 
208  mLineEdit->setStyleSheet( QStringLiteral( "QgsFilterLineEdit { background-color: #ffd85d; }" ) );
209  break;
210  }
211  }
212 }
Widget failed at least one soft (non-enforced) constraint.
void valueChanged()
Will call the value() method to determine the emitted value.
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.
Widget failed at least one hard (enforced) constraint.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
void setEnabled(bool enabled) override
QVariant value() const override
Will be used to access the widget&#39;s value.
Widget passed constraints successfully.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:176
ConstraintResult
Result of constraint checks.
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:348
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.
Represents a vector layer which manages a vector based data sets.
void setValue(const QVariant &value) override