QGIS API Documentation  2.14.11-Essen
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  QWidget* container = new QWidget( parent );
63  QGridLayout* layout = new QGridLayout( container );
64  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
65  QWebView* webView = new QWebView( parent );
66  webView->setObjectName( "EditorWebView" );
67  QPushButton* pb = new QPushButton( tr( "..." ), container );
68  pb->setObjectName( "FileChooserButton" );
69 
70  layout->addWidget( webView, 0, 0, 1, 2 );
71  layout->addWidget( le, 1, 0 );
72  layout->addWidget( pb, 1, 1 );
73  layout->setMargin( 0 );
74  layout->setContentsMargins( 0, 0, 0, 0 );
75 
76  container->setLayout( layout );
77 
78  return container;
79 }
80 
82 {
83  QWidget* container;
84 
85  mLineEdit = qobject_cast<QLineEdit*>( editor );
86 
87  if ( mLineEdit )
88  {
89 
90  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
91  if ( fle )
92  {
93  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
94  }
95 
96  container = qobject_cast<QWidget*>( mLineEdit->parent() );
97  }
98  else
99  {
100  container = editor;
101  mLineEdit = container->findChild<QLineEdit*>();
102  }
103 
104  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
105  if ( !mButton )
106  mButton = container->findChild<QPushButton*>();
107 
108  mWebView = container->findChild<QWebView*>( "EditorWebView" );
109  if ( !mWebView )
110  mWebView = container->findChild<QWebView*>();
111 
112  if ( mWebView )
113  {
114  mWebView->page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
115  mWebView->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
116  mWebView->settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
117  mWebView->settings()->setAttribute( QWebSettings::PluginsEnabled, true );
118 #ifdef QGISDEBUG
119  mWebView->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
120 #endif
121  }
122 
123  if ( mButton )
124  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
125 
126  if ( mLineEdit )
127  {
128  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadUrl( QString ) ) );
129  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
130  }
131 }
132 
134 {
135  return mWebView || mButton || mLineEdit;
136 }
137 
139 {
140  if ( mLineEdit )
141  {
142  if ( value.isNull() )
143  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
144  else
145  mLineEdit->setText( value.toString() );
146  }
147 
148  loadUrl( value.toString() );
149 }
150 
152 {
153  if ( mLineEdit )
154  mLineEdit->setEnabled( enabled );
155 
156  if ( mButton )
157  mButton->setEnabled( enabled );
158 }
159 
160 void QgsWebViewWidgetWrapper::selectFileName()
161 {
162  QString text;
163 
164  if ( mLineEdit )
165  text = mLineEdit->text();
166 
167  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
168 
169  if ( fileName.isNull() )
170  return;
171 
172  QString projPath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
173  QString filePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
174 
175  if ( filePath.startsWith( projPath ) )
176  filePath = QDir( projPath ).relativeFilePath( filePath );
177 
178  if ( mLineEdit )
179  mLineEdit->setText( filePath );
180 }
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 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
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:381
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