QGIS API Documentation  2.14.5-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 #ifdef QGISDEBUG
118  mWebView->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
119 #endif
120  }
121 
122  if ( mButton )
123  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
124 
125  if ( mLineEdit )
126  {
127  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadUrl( QString ) ) );
128  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
129  }
130 }
131 
133 {
134  return mWebView || mButton || mLineEdit;
135 }
136 
138 {
139  if ( mLineEdit )
140  {
141  if ( value.isNull() )
142  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
143  else
144  mLineEdit->setText( value.toString() );
145  }
146 
147  loadUrl( value.toString() );
148 }
149 
151 {
152  if ( mLineEdit )
153  mLineEdit->setEnabled( enabled );
154 
155  if ( mButton )
156  mButton->setEnabled( enabled );
157 }
158 
159 void QgsWebViewWidgetWrapper::selectFileName()
160 {
161  QString text;
162 
163  if ( mLineEdit )
164  text = mLineEdit->text();
165 
166  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
167 
168  if ( fileName.isNull() )
169  return;
170 
171  QString projPath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
172  QString filePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
173 
174  if ( filePath.startsWith( projPath ) )
175  filePath = QDir( projPath ).relativeFilePath( filePath );
176 
177  if ( mLineEdit )
178  mLineEdit->setText( filePath );
179 }
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