QGIS API Documentation  2.7.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 {
28 }
29 
30 void QgsWebViewWidgetWrapper::loadUrl( const QString &url )
31 {
32  if ( mWebView )
33  mWebView->load( url );
34 }
35 
37 {
38  QVariant v;
39 
40  if ( mLineEdit )
41  {
42  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
43  v = QVariant( QVariant::String );
44  else
45  v = mLineEdit->text();
46  }
47 
48  return v;
49 }
50 
51 QWidget* QgsWebViewWidgetWrapper::createWidget( QWidget* parent )
52 {
53  QWidget* container = new QWidget( parent );
54  QGridLayout* layout = new QGridLayout( container );
55  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
56  QWebView* webView = new QWebView( parent );
57  webView->setObjectName( "EditorWebView" );
58  QPushButton* pb = new QPushButton( tr( "..." ), container );
59  pb->setObjectName( "FileChooserButton" );
60 
61  layout->addWidget( webView, 0, 0, 1, 2 );
62  layout->addWidget( le, 1, 0 );
63  layout->addWidget( pb, 1, 1 );
64 
65  container->setLayout( layout );
66 
67  return container;
68 }
69 
70 void QgsWebViewWidgetWrapper::initWidget( QWidget* editor )
71 {
72  QWidget* container;
73 
74  mLineEdit = qobject_cast<QLineEdit*>( editor );
75 
76  if ( mLineEdit )
77  {
78 
79  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
80  if ( fle )
81  {
82  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
83  }
84 
85  container = qobject_cast<QWidget*>( mLineEdit->parent() );
86  }
87  else
88  {
89  container = editor;
90  mLineEdit = container->findChild<QLineEdit*>();
91  }
92 
93  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
94  if ( !mButton )
95  mButton = container->findChild<QPushButton*>();
96 
97  mWebView = container->findChild<QWebView*>( "EditorWebView" );
98  if ( !mWebView )
99  mWebView = container->findChild<QWebView*>();
100 
101  if ( mWebView )
102  {
103  mWebView->page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
104  mWebView->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
105  mWebView->settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
106 #ifdef QGISDEBUG
107  mWebView->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
108 #endif
109  }
110 
111  if ( mButton )
112  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
113 
114  if ( mLineEdit )
115  {
116  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadUrl( QString ) ) );
117  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
118  }
119 }
120 
121 void QgsWebViewWidgetWrapper::setValue( const QVariant& value )
122 {
123  if ( mLineEdit )
124  {
125  if ( value.isNull() )
126  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
127  else
128  mLineEdit->setText( value.toString() );
129  }
130 
131  loadUrl( value.toString() );
132 }
133 
135 {
136  if ( mLineEdit )
137  mLineEdit->setEnabled( enabled );
138 
139  if ( mButton )
140  mButton->setEnabled( enabled );
141 }
142 
143 void QgsWebViewWidgetWrapper::selectFileName()
144 {
145  QString text;
146 
147  if ( mLineEdit )
148  text = mLineEdit->text();
149 
150  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
151 
152  if ( fileName.isNull() )
153  return;
154 
155  if ( mLineEdit )
156  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
157 }
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)