QGIS API Documentation  2.12.0-Lyon
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( NULL )
29  , mLineEdit( NULL )
30  , mButton( NULL )
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 
74  container->setLayout( layout );
75 
76  return container;
77 }
78 
80 {
81  QWidget* container;
82 
83  mLineEdit = qobject_cast<QLineEdit*>( editor );
84 
85  if ( mLineEdit )
86  {
87 
88  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
89  if ( fle )
90  {
91  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
92  }
93 
94  container = qobject_cast<QWidget*>( mLineEdit->parent() );
95  }
96  else
97  {
98  container = editor;
99  mLineEdit = container->findChild<QLineEdit*>();
100  }
101 
102  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
103  if ( !mButton )
104  mButton = container->findChild<QPushButton*>();
105 
106  mWebView = container->findChild<QWebView*>( "EditorWebView" );
107  if ( !mWebView )
108  mWebView = container->findChild<QWebView*>();
109 
110  if ( mWebView )
111  {
112  mWebView->page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
113  mWebView->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
114  mWebView->settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
115 #ifdef QGISDEBUG
116  mWebView->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
117 #endif
118  }
119 
120  if ( mButton )
121  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
122 
123  if ( mLineEdit )
124  {
125  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadUrl( QString ) ) );
126  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
127  }
128 }
129 
131 {
132  return mWebView || mButton || mLineEdit;
133 }
134 
136 {
137  if ( mLineEdit )
138  {
139  if ( value.isNull() )
140  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
141  else
142  mLineEdit->setText( value.toString() );
143  }
144 
145  loadUrl( value.toString() );
146 }
147 
149 {
150  if ( mLineEdit )
151  mLineEdit->setEnabled( enabled );
152 
153  if ( mButton )
154  mButton->setEnabled( enabled );
155 }
156 
157 void QgsWebViewWidgetWrapper::selectFileName()
158 {
159  QString text;
160 
161  if ( mLineEdit )
162  text = mLineEdit->text();
163 
164  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
165 
166  if ( fileName.isNull() )
167  return;
168 
169  QString projPath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
170  QString filePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
171 
172  if ( filePath.startsWith( projPath ) )
173  filePath = QDir( projPath ).relativeFilePath( filePath );
174 
175  if ( mLineEdit )
176  mLineEdit->setText( filePath );
177 }
QString toNativeSeparators(const QString &pathName)
QString relativeFilePath(const QString &fileName) const
QgsWebViewWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=0, QWidget *parent=0)
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
QVariant value() override
Will be used to access the widget's value.
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 setEnabled(bool enabled) override
QString cleanPath(const QString &path)
QVariant value(const QString &key, const QVariant &defaultValue) const
bool valid() override
Return true if the widget has been properly initialized.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:353
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
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