Skip to content

Commit f39486f

Browse files
committed
WebView attribute editor fixes:
- allow browsing in readonly mode - add option to open page in default browser - add tooltips to editor push buttons
1 parent 640f345 commit f39486f

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

src/gui/qgsattributedialog.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <QVBoxLayout>
4141
#include <QLineEdit>
4242
#include <QWebView>
43+
#include <QPushButton>
4344

4445
int QgsAttributeDialog::smFormCounter = 0;
4546

@@ -216,7 +217,12 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
216217
{
217218
foreach ( QWidget *w, myWidget->findChildren<QWidget *>() )
218219
{
219-
w->setEnabled( qobject_cast<QWebView *>( w ) ? true : false );
220+
if ( qobject_cast<QWebView *>( w ) )
221+
w->setEnabled( true );
222+
else if ( qobject_cast<QPushButton *>( w ) && w->objectName() == "openUrl" )
223+
w->setEnabled( true );
224+
else
225+
w->setEnabled( false );
220226
}
221227
}
222228
else

src/gui/qgsattributeeditor.cpp

+61-13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <QGroupBox>
5050
#include <QLabel>
5151
#include <QWebView>
52+
#include <QDesktopServices>
5253

5354
void QgsAttributeEditor::selectFileName()
5455
{
@@ -187,6 +188,23 @@ void QgsAttributeEditor::updateUrl()
187188
le->blockSignals( false );
188189
}
189190

191+
void QgsAttributeEditor::openUrl()
192+
{
193+
QPushButton *pb = qobject_cast<QPushButton *>( sender() );
194+
if ( !pb )
195+
return;
196+
197+
QWidget *hbox = qobject_cast<QWidget *>( pb->parent() );
198+
if ( !hbox )
199+
return;
200+
201+
QWebView *ww = hbox->findChild<QWebView *>();
202+
if ( !ww )
203+
return;
204+
205+
QDesktopServices::openUrl( ww->url().toString() );
206+
}
207+
190208
void QgsAttributeEditor::updateColor()
191209
{
192210
QString color;
@@ -706,8 +724,9 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
706724
if ( ww )
707725
{
708726
ww->page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
709-
ww->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
727+
// ww->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
710728
ww->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
729+
ww->settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
711730
#ifdef QGISDEBUG
712731
ww->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
713732
#endif
@@ -729,7 +748,8 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
729748
break;
730749
}
731750

732-
QPushButton *pb = 0;
751+
QPushButton *pb0 = 0;
752+
QPushButton *pb1 = 0;
733753
QLineEdit *le = qobject_cast<QLineEdit *>( editor );
734754
if ( le )
735755
{
@@ -738,7 +758,7 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
738758

739759
if ( editor->parent() )
740760
{
741-
pb = editor->parent()->findChild<QPushButton *>();
761+
pb0 = editor->parent()->findChild<QPushButton *>();
742762
}
743763
}
744764
else
@@ -753,15 +773,18 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
753773
case QgsVectorLayer::FileName:
754774
case QgsVectorLayer::Photo:
755775
case QgsVectorLayer::Calendar:
756-
pb = new QPushButton( tr( "..." ), myWidget );
776+
pb0 = new QPushButton( tr( "..." ), myWidget );
757777
break;
758778

759779
case QgsVectorLayer::WebView:
760-
pb = new QPushButton( tr( "<" ), myWidget );
780+
pb0 = new QPushButton( tr( "<" ), myWidget );
781+
pb0->setObjectName( "saveUrl" );
782+
pb1 = new QPushButton( tr( "..." ), myWidget );
783+
pb1->setObjectName( "openUrl" );
761784
break;
762785

763786
case QgsVectorLayer::Color:
764-
pb = new QgsColorButton( myWidget );
787+
pb0 = new QgsColorButton( myWidget );
765788
break;
766789

767790
default:
@@ -780,9 +803,11 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
780803
else if ( editType == QgsVectorLayer::WebView )
781804
{
782805
ww = new QWebView( myWidget );
806+
ww->setObjectName( "webview" );
783807
ww->page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
784-
ww->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
808+
// ww->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
785809
ww->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
810+
ww->settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
786811
#ifdef QGISDEBUG
787812
ww->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
788813
#endif
@@ -791,7 +816,9 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
791816
}
792817

793818
layout->addWidget( le, row, 0 );
794-
layout->addWidget( pb, row, 1 );
819+
layout->addWidget( pb0, row, 1 );
820+
if ( pb1 )
821+
layout->addWidget( pb1, row, 2 );
795822

796823
myWidget->setLayout( layout );
797824
}
@@ -808,16 +835,37 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
808835
connect( le, SIGNAL( textChanged( const QString & ) ), new QgsAttributeEditor( le ), SLOT( updateColor() ) );
809836
}
810837

811-
if ( pb )
838+
if ( pb0 )
812839
{
813840
if ( editType == QgsVectorLayer::FileName || editType == QgsVectorLayer::Photo )
814-
connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectFileName() ) );
841+
{
842+
connect( pb0, SIGNAL( clicked() ), new QgsAttributeEditor( pb0 ), SLOT( selectFileName() ) );
843+
pb0->setToolTip( tr( "Select filename..." ) );
844+
}
815845
if ( editType == QgsVectorLayer::WebView )
816-
connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( updateUrl() ) );
846+
{
847+
connect( pb0, SIGNAL( clicked() ), new QgsAttributeEditor( pb0 ), SLOT( updateUrl() ) );
848+
pb0->setToolTip( tr( "Save current page url in attribute" ) );
849+
}
817850
if ( editType == QgsVectorLayer::Calendar )
818-
connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectDate() ) );
851+
{
852+
connect( pb0, SIGNAL( clicked() ), new QgsAttributeEditor( pb0 ), SLOT( selectDate() ) );
853+
pb0->setToolTip( tr( "Select date in calendar" ) );
854+
}
819855
if ( editType == QgsVectorLayer::Color )
820-
connect( pb, SIGNAL( colorChanged( const QColor & ) ), new QgsAttributeEditor( pb ), SLOT( updateColor() ) );
856+
{
857+
connect( pb0, SIGNAL( colorChanged( const QColor & ) ), new QgsAttributeEditor( pb0 ), SLOT( updateColor() ) );
858+
pb0->setToolTip( tr( "Select color in browser" ) );
859+
}
860+
}
861+
862+
if ( pb1 )
863+
{
864+
if ( editType == QgsVectorLayer::WebView )
865+
{
866+
connect( pb1, SIGNAL( clicked() ), new QgsAttributeEditor( pb1 ), SLOT( openUrl() ) );
867+
pb1->setToolTip( tr( "Open current page in default browser" ) );
868+
}
821869
}
822870
}
823871
break;

src/gui/qgsattributeeditor.h

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class GUI_EXPORT QgsAttributeEditor : public QObject
8585
void loadUrl( const QString & );
8686
void loadPixmap( const QString & );
8787
void updateUrl();
88+
void openUrl();
8889
void updateColor();
8990

9091
private:

0 commit comments

Comments
 (0)