QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
24 QgsWebViewWidgetWrapper::QgsWebViewWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26 {
27 }
28 
29 void QgsWebViewWidgetWrapper::loadUrl( const QString &url )
30 {
31  if ( mWebView )
32  mWebView->load( url );
33 }
34 
36 {
37  QVariant v;
38 
39  if ( mLineEdit )
40  v = mLineEdit->text();
41 
42  return v;
43 }
44 
45 QWidget* QgsWebViewWidgetWrapper::createWidget( QWidget* parent )
46 {
47  QWidget* container = new QWidget( parent );
48  QGridLayout* layout = new QGridLayout( container );
49  QgsFilterLineEdit* le = new QgsFilterLineEdit( container );
50  QWebView* webView = new QWebView( parent );
51  webView->setObjectName( "EditorWebView" );
52  QPushButton* pb = new QPushButton( tr( "..." ), container );
53  pb->setObjectName( "FileChooserButton" );
54 
55  layout->addWidget( webView, 0, 0, 1, 2 );
56  layout->addWidget( le, 1, 0 );
57  layout->addWidget( pb, 1, 1 );
58 
59  container->setLayout( layout );
60 
61  return container;
62 }
63 
64 void QgsWebViewWidgetWrapper::initWidget( QWidget* editor )
65 {
66  QWidget* container;
67 
68  mLineEdit = qobject_cast<QLineEdit*>( editor );
69 
70  if ( mLineEdit )
71  {
72  container = qobject_cast<QWidget*>( mLineEdit->parent() );
73  }
74  else
75  {
76  container = editor;
77  mLineEdit = container->findChild<QLineEdit*>();
78  }
79 
80  mButton = container->findChild<QPushButton*>( "FileChooserButton" );
81  if ( !mButton )
82  mButton = container->findChild<QPushButton*>();
83 
84  mWebView = container->findChild<QWebView*>( "EditorWebView" );
85  if ( !mWebView )
86  mWebView = container->findChild<QWebView*>();
87 
88  if ( mWebView )
89  {
90  mWebView->page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
91  mWebView->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
92  mWebView->settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
93 #ifdef QGISDEBUG
94  mWebView->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
95 #endif
96  }
97 
98  if ( mButton )
99  connect( mButton, SIGNAL( clicked() ), this, SLOT( selectFileName() ) );
100 
101  if ( mLineEdit )
102  {
103  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( loadUrl( QString ) ) );
104  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
105  }
106 }
107 
108 void QgsWebViewWidgetWrapper::setValue( const QVariant& value )
109 {
110  if ( mLineEdit )
111  mLineEdit->setText( value.toString() );
112 
113  loadUrl( value.toString() );
114 }
115 
117 {
118  if ( mLineEdit )
119  mLineEdit->setEnabled( enabled );
120 
121  if ( mButton )
122  mButton->setEnabled( enabled );
123 }
124 
126 {
127  QString text;
128 
129  if ( mLineEdit )
130  text = mLineEdit->text();
131 
132  QString fileName = QFileDialog::getOpenFileName( mLineEdit, tr( "Select a file" ), QFileInfo( text ).absolutePath() );
133 
134  if ( fileName.isNull() )
135  return;
136 
137  if ( mLineEdit )
138  mLineEdit->setText( QDir::toNativeSeparators( fileName ) );
139 }
QgsWebViewWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=0, QWidget *parent=0)
void valueChanged()
Will call the value() method to determine the emitted value.
Manages an editor widget Widget and wrapper share the same parent.
QPushButton * mButton
The button to open the file chooser dialog.
void initWidget(QWidget *editor)
This method should initialize the editor widget with runtime data.
Lineedit with builtin clear button.
QVariant value()
Will be used to access the widget's value.
void loadUrl(const QString &url)
QLineEdit * mLineEdit
The line edit containing the path to the picture.
void setValue(const QVariant &value)
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
QWidget * createWidget(QWidget *parent)
This method should create a new widget with the provided parent.
QWebView * mWebView
This label is used as a container to display the picture.
Represents a vector layer which manages a vector based data sets.
#define tr(sourceText)