Skip to content

Commit c7fb230

Browse files
committedDec 12, 2012
improve html viewing in identify results window:
- add print to normal context menu instead of replacing it completely - enable inspection with QGISDEBUG - allow accessing remote urls - use QgsNetworkManager (eg. to allow skip SSL errors or do authenticaton)
1 parent 51d7d56 commit c7fb230

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed
 

‎src/app/qgsidentifyresults.cpp

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "qgsmapcanvas.h"
2929
#include "qgsattributeaction.h"
3030
#include "qgsfeatureaction.h"
31+
#include "qgslogger.h"
32+
#include "qgsnetworkaccessmanager.h"
3133

3234
#include <QCloseEvent>
3335
#include <QLabel>
@@ -46,7 +48,36 @@
4648
#include <QDesktopServices>
4749
#include <QMessageBox>
4850

49-
#include "qgslogger.h"
51+
QgsWebView::QgsWebView( QWidget *parent ) : QWebView( parent )
52+
{
53+
page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
54+
page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
55+
settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
56+
#ifdef QGISDEBUG
57+
settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
58+
#endif
59+
}
60+
61+
void QgsWebView::print( void )
62+
{
63+
QPrinter printer;
64+
QPrintDialog *dialog = new QPrintDialog( &printer );
65+
if ( dialog->exec() == QDialog::Accepted )
66+
QWebView::print( &printer );
67+
}
68+
69+
void QgsWebView::contextMenuEvent( QContextMenuEvent *e )
70+
{
71+
QMenu *menu = page()->createStandardContextMenu();
72+
if ( menu )
73+
{
74+
QAction *action = new QAction( tr( "Print" ), this );
75+
connect( action, SIGNAL( triggered() ), this, SLOT( print() ) );
76+
menu->addAction( action );
77+
menu->exec( e->globalPos() );
78+
delete menu;
79+
}
80+
}
5081

5182
class QgsIdentifyResultsDock : public QDockWidget
5283
{
@@ -289,15 +320,9 @@ void QgsIdentifyResults::addFeature( QgsRasterLayer *layer,
289320
QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << attributes.begin().key() << "" );
290321
featItem->addChild( attrItem );
291322

292-
QWebView *wv = new QWebView( attrItem->treeWidget() );
323+
QgsWebView *wv = new QgsWebView( attrItem->treeWidget() );
293324
wv->setHtml( attributes.begin().value() );
294-
wv->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
295-
296-
QAction *action = new QAction( tr( "Print" ), wv );
297-
connect( action, SIGNAL( triggered() ), this, SLOT( print() ) );
298-
wv->insertAction( 0, action );
299325

300-
wv->setContextMenuPolicy( Qt::ActionsContextMenu );
301326
mPrintToolButton->setVisible( true );
302327

303328
connect( wv, SIGNAL( linkClicked( const QUrl & ) ), this, SLOT( openUrl( const QUrl & ) ) );
@@ -1040,22 +1065,6 @@ void QgsIdentifyResults::openUrl( const QUrl &url )
10401065
}
10411066
}
10421067

1043-
void QgsIdentifyResults::print()
1044-
{
1045-
QAction *action = qobject_cast<QAction*>( sender() );
1046-
if ( !action )
1047-
return;
1048-
1049-
QWebView *wv = qobject_cast<QWebView*>( action->parent() );
1050-
if ( !wv )
1051-
return;
1052-
1053-
QPrinter printer;
1054-
QPrintDialog *dialog = new QPrintDialog( &printer );
1055-
if ( dialog->exec() == QDialog::Accepted )
1056-
wv->print( &printer );
1057-
}
1058-
10591068
void QgsIdentifyResults::printCurrentItem()
10601069
{
10611070
QTreeWidgetItem *item = lstResults->currentItem();

‎src/app/qgsidentifyresults.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <QWidget>
2727
#include <QList>
28+
#include <QWebView>
2829

2930
class QCloseEvent;
3031
class QTreeWidgetItem;
@@ -119,7 +120,6 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
119120
void on_mCollapseToolButton_clicked( bool checked ) { Q_UNUSED( checked ); collapseAll(); }
120121

121122
void openUrl( const QUrl &url );
122-
void print();
123123
void printCurrentItem();
124124

125125
private:
@@ -148,4 +148,16 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
148148
QDockWidget *mDock;
149149
};
150150

151+
class QgsWebView : public QWebView
152+
{
153+
Q_OBJECT;
154+
public:
155+
QgsWebView( QWidget *parent = 0 );
156+
public slots:
157+
void print( void );
158+
protected:
159+
void contextMenuEvent( QContextMenuEvent* );
160+
};
161+
162+
151163
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.