/
qgshtmlwidgetwrapper.cpp
177 lines (140 loc) · 5.02 KB
/
qgshtmlwidgetwrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/***************************************************************************
qgshtmlwidgetwrapper.h
---------------------
begin : 23.03.2019
copyright : (C) 2019 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgshtmlwidgetwrapper.h"
#include "qgsmessagelog.h"
#include "qgsexpressioncontextutils.h"
#include "qgsapplication.h"
#include "qgswebframe.h"
#include <QScreen>
QgsHtmlWidgetWrapper::QgsHtmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
: QgsWidgetWrapper( layer, editor, parent )
{
connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsHtmlWidgetWrapper::setHtmlContext );
}
bool QgsHtmlWidgetWrapper::valid() const
{
return true;
}
QWidget *QgsHtmlWidgetWrapper::createWidget( QWidget *parent )
{
return new QgsWebView( parent );
}
void QgsHtmlWidgetWrapper::initWidget( QWidget *editor )
{
mWidget = qobject_cast<QgsWebView *>( editor );
if ( !mWidget )
return;
mWidget->setHtml( mHtmlCode );
#ifdef WITH_QTWEBKIT
const int horizontalDpi = mWidget->logicalDpiX();
mWidget->setZoomFactor( horizontalDpi / 96.0 );
QWebPage *page = mWidget->page();
connect( page, &QWebPage::contentsChanged, this, &QgsHtmlWidgetWrapper::fixHeight, Qt::ConnectionType::UniqueConnection );
connect( page, &QWebPage::loadFinished, this, [ = ]( bool ) { fixHeight(); }, Qt::ConnectionType::UniqueConnection );
#endif
checkGeometryNeeds();
}
void QgsHtmlWidgetWrapper::reinitWidget( )
{
if ( !mWidget )
return;
initWidget( mWidget );
}
void QgsHtmlWidgetWrapper::checkGeometryNeeds()
{
if ( !mWidget )
return;
// initialize a temporary QgsWebView to render HTML code and check if one evaluated expression
// needs geometry
QgsWebView webView;
NeedsGeometryEvaluator evaluator;
const QgsAttributeEditorContext attributecontext = context();
QgsExpressionContext expressionContext = layer()->createExpressionContext();
evaluator.setExpressionContext( expressionContext );
auto frame = webView.page()->mainFrame();
connect( frame, &QWebFrame::javaScriptWindowObjectCleared, frame, [ frame, &evaluator ]
{
frame->addToJavaScriptWindowObject( QStringLiteral( "expression" ), &evaluator );
} );
webView.setHtml( mHtmlCode );
mNeedsGeometry = evaluator.needsGeometry();
}
void QgsHtmlWidgetWrapper::setHtmlCode( const QString &htmlCode )
{
mHtmlCode = htmlCode;
checkGeometryNeeds();
}
void QgsHtmlWidgetWrapper::setHtmlContext( )
{
if ( !mWidget )
return;
const QgsAttributeEditorContext attributecontext = context();
QgsExpressionContext expressionContext = layer()->createExpressionContext();
expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
if ( attributecontext.parentFormFeature().isValid() )
{
expressionContext << QgsExpressionContextUtils::parentFormScope( attributecontext.parentFormFeature() );
}
expressionContext.setFeature( mFeature );
HtmlExpression *htmlExpression = new HtmlExpression();
htmlExpression->setExpressionContext( expressionContext );
auto frame = mWidget->page()->mainFrame();
connect( frame, &QWebFrame::javaScriptWindowObjectCleared, frame, [ = ]
{
frame->addToJavaScriptWindowObject( QStringLiteral( "expression" ), htmlExpression );
} );
mWidget->setHtml( mHtmlCode );
}
#ifdef WITH_QTWEBKIT
void QgsHtmlWidgetWrapper::fixHeight()
{
QWebPage *page = mWidget->page();
int docHeight { page->mainFrame()->contentsSize().height() };
mWidget->setFixedHeight( docHeight );
}
#endif
void QgsHtmlWidgetWrapper::setFeature( const QgsFeature &feature )
{
if ( !mWidget )
return;
mFeature = feature;
setHtmlContext();
}
bool QgsHtmlWidgetWrapper::needsGeometry() const
{
return mNeedsGeometry;
}
///@cond PRIVATE
void HtmlExpression::setExpressionContext( const QgsExpressionContext &context )
{
mExpressionContext = context;
}
QString HtmlExpression::evaluate( const QString &expression ) const
{
QgsExpression exp = QgsExpression( expression );
exp.prepare( &mExpressionContext );
return exp.evaluate( &mExpressionContext ).toString();
}
void NeedsGeometryEvaluator::evaluate( const QString &expression )
{
QgsExpression exp = QgsExpression( expression );
exp.prepare( &mExpressionContext );
mNeedsGeometry |= exp.needsGeometry();
}
void NeedsGeometryEvaluator::setExpressionContext( const QgsExpressionContext &context )
{
mExpressionContext = context;
}
///@endcond