QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 dot kuhn at gmx 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 
21 #include <QGridLayout>
22 #include <QFileDialog>
23 #include <QSettings>
24 
25 QgsWebViewWidgetWrapper::QgsWebViewWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
26  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
27  , mWebView( NULL )
28  , mLineEdit( NULL )
29  , mButton( NULL )
30 {
31 }
32 
33 void QgsWebViewWidgetWrapper::loadUrl( const QString &url )
34 {
35  if ( mWebView )
36  mWebView->load( url );
37 }
38 
40 {
41  QVariant v;
42 
43  if ( mLineEdit )
44  {
45  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
46  v = QVariant( QVariant::String );
47  else
48  v = mLineEdit->text();
49  }
50 
51  return v;
52 }
53 
54 QWidget* QgsWebViewWidgetWrapper::createWidget( QWidget* parent )
55 {
56  QWidget* container = new QWidget( parent );
57  QGridLayout* layout = new QGridLayout( container );
58  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
59  QWebView* webView = new QWebView( parent );
60  webView->setObjectName( "EditorWebView" );
61  QPushButton* pb = new QPushButton( tr( "..." ), container );
62  pb->setObjectName( "FileChooserButton" );
63 
64  layout->addWidget( webView, 0, 0, 1, 2 );
65  layout->addWidget( le, 1, 0 );
66  layout->addWidget( pb, 1, 1 );
67 
68  container->setLayout( layout );
69 
70  return container;
71 }
72 
73 void QgsWebViewWidgetWrapper::initWidget( QWidget* editor )
74 {
75  QWidget* container;
76 
77  mLineEdit = qobject_cast<QLineEdit*>( editor );
78 
79  if ( mLineEdit )
80  {
81 
82  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
83  if ( fle )
84  {
85  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
86  }
87 
88  container = qobject_cast<QWidget*>( mLineEdit->parent() );
89  }
90  else
91  {
92  container = editor;
93  mLineEdit = container->findChild<QLineEdit*>();
94  }
95 
96  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
97  if ( !mButton )
98  mButton = container->findChild<QPushButton*>();
99 
100  mWebView = container->findChild<QWebView*>( "EditorWebView" );
101  if ( !mWebView )
102  mWebView = container->findChild<QWebView*>();
103 
104  if ( mWebView )
105  {
106  mWebView->page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
107  mWebView->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
108  mWebView->settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
109 #ifdef QGISDEBUG
110  mWebView->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
111 #endif
112  }
113 
114  if ( mButton )
115  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
116 
117  if ( mLineEdit )
118  {
119  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadUrl( QString ) ) );
120  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
121  }
122 }
123 
124 void QgsWebViewWidgetWrapper::setValue( const QVariant& value )
125 {
126  if ( mLineEdit )
127  {
128  if ( value.isNull() )
129  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
130  else
131  mLineEdit->setText( value.toString() );
132  }
133 
134  loadUrl( value.toString() );
135 }
136 
138 {
139  if ( mLineEdit )
140  mLineEdit->setEnabled( enabled );
141 
142  if ( mButton )
143  mButton->setEnabled( enabled );
144 }
145 
146 void QgsWebViewWidgetWrapper::selectFileName()
147 {
148  QString text;
149 
150  if ( mLineEdit )
151  text = mLineEdit->text();
152 
153  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
154 
155  if ( fileName.isNull() )
156  return;
157 
158  if ( mLineEdit )
159  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
160 }
QgsWebViewWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=0, QWidget *parent=0)
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.
static QgsNetworkAccessManager * instance()
Definition: qgssingleton.h:23
QVariant value() override
Will be used to access the widget's value.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Lineedit with builtin clear button.
void setNullValue(QString nullValue)
void setEnabled(bool enabled) override
Represents a vector layer which manages a vector based data sets.
void setValue(const QVariant &value) override
#define tr(sourceText)