QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgshtmlwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgshtmlwidgetwrapper.h
3
4 ---------------------
5 begin : 23.03.2019
6 copyright : (C) 2019 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include "qgsattributeform.h"
22#include "qgswebframe.h"
23
24#include <QScreen>
25
26#include "moc_qgshtmlwidgetwrapper.cpp"
27
29 : QgsWidgetWrapper( layer, editor, parent )
30{
31 connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsHtmlWidgetWrapper::setHtmlContext );
32}
33
35{
36 return true;
37}
38
39QWidget *QgsHtmlWidgetWrapper::createWidget( QWidget *parent )
40{
41 QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
42
43 if ( form )
44 {
45 mFormFeature = form->feature();
46 connect( form, &QgsAttributeForm::widgetValueChanged, this, [this]( const QString &attribute, const QVariant &newValue, bool attributeChanged ) {
47 if ( attributeChanged )
48 {
49 if ( mRequiresFormScope )
50 {
51 mFormFeature.setAttribute( attribute, newValue );
52 setHtmlContext();
53 }
54 }
55 } );
56 }
57
58 return new QgsWebView( parent );
59}
60
61void QgsHtmlWidgetWrapper::initWidget( QWidget *editor )
62{
63 mWidget = qobject_cast<QgsWebView *>( editor );
64
65 if ( !mWidget )
66 return;
67
68 mWidget->setHtml( mHtmlCode.replace( "\n", " " ) );
69
70#ifdef WITH_QTWEBKIT
71
72 const int horizontalDpi = mWidget->logicalDpiX();
73
74 mWidget->setZoomFactor( horizontalDpi / 96.0 );
75
76 QWebPage *page = mWidget->page();
77 connect( page, &QWebPage::contentsChanged, this, &QgsHtmlWidgetWrapper::fixHeight, Qt::ConnectionType::UniqueConnection );
78 connect( page, &QWebPage::loadFinished, this, &QgsHtmlWidgetWrapper::fixHeight, Qt::ConnectionType::UniqueConnection );
79
80#endif
81
82 checkGeometryNeeds();
83}
84
86{
87 if ( !mWidget )
88 return;
89
90 initWidget( mWidget );
91}
92
93void QgsHtmlWidgetWrapper::checkGeometryNeeds()
94{
95 if ( !mWidget )
96 return;
97
98 // initialize a temporary QgsWebView to render HTML code and check if one evaluated expression
99 // needs geometry
100 QgsWebView webView;
101 NeedsGeometryEvaluator evaluator;
102
103 const QgsAttributeEditorContext attributecontext = context();
104 if ( QgsVectorLayer *vl = layer() )
105 {
106 const QgsExpressionContext expressionContext = vl->createExpressionContext();
107 evaluator.setExpressionContext( expressionContext );
108 }
109
110 auto frame = webView.page()->mainFrame();
111 connect( frame, &QWebFrame::javaScriptWindowObjectCleared, frame, [frame, &evaluator] {
112 frame->addToJavaScriptWindowObject( QStringLiteral( "expression" ), &evaluator );
113 } );
114
115 webView.setHtml( mHtmlCode );
116
117 mNeedsGeometry = evaluator.needsGeometry();
118}
119
120void QgsHtmlWidgetWrapper::setHtmlCode( const QString &htmlCode )
121{
122 mHtmlCode = htmlCode;
123
124 bool ok = false;
125 const thread_local QRegularExpression expRe( QStringLiteral( R"re(expression.evaluate\s*\‍(\s*"(.*)"\))re" ), QRegularExpression::PatternOption::MultilineOption | QRegularExpression::PatternOption::DotMatchesEverythingOption );
126 QRegularExpressionMatchIterator matchIt = expRe.globalMatch( mHtmlCode );
127 while ( !ok && matchIt.hasNext() )
128 {
129 const QRegularExpressionMatch match = matchIt.next();
130 const QgsExpression exp = match.captured( 1 );
132 }
133 mRequiresFormScope = ok;
134
135 checkGeometryNeeds();
136}
137
138void QgsHtmlWidgetWrapper::setHtmlContext()
139{
140 if ( !mWidget )
141 return;
142
143 const QgsAttributeEditorContext attributecontext = context();
144 QgsExpressionContext expressionContext = layer()->createExpressionContext();
145 expressionContext << QgsExpressionContextUtils::formScope( mFormFeature, attributecontext.attributeFormModeString() );
146 if ( attributecontext.parentFormFeature().isValid() )
147 {
148 expressionContext << QgsExpressionContextUtils::parentFormScope( attributecontext.parentFormFeature() );
149 }
150
151 expressionContext.setFeature( mFeature );
152
153 HtmlExpression *htmlExpression = new HtmlExpression();
154 htmlExpression->setExpressionContext( expressionContext );
155 auto frame = mWidget->page()->mainFrame();
156 connect( frame, &QWebFrame::javaScriptWindowObjectCleared, frame, [frame, htmlExpression] {
157 frame->addToJavaScriptWindowObject( QStringLiteral( "expression" ), htmlExpression );
158 } );
159
160 mWidget->setHtml( mHtmlCode );
161}
162
163#ifdef WITH_QTWEBKIT
164void QgsHtmlWidgetWrapper::fixHeight()
165{
166 QWebPage *page = mWidget->page();
167 const int docHeight { page->mainFrame()->contentsSize().height() };
168 mWidget->setFixedHeight( docHeight );
169}
170#endif
171
173{
174 if ( !mWidget )
175 return;
176
177 mFeature = feature;
178 mFormFeature = feature;
179 setHtmlContext();
180}
181
183{
184 return mNeedsGeometry;
185}
186
187
189void HtmlExpression::setExpressionContext( const QgsExpressionContext &context )
190{
191 mExpressionContext = context;
192}
193
194QString HtmlExpression::evaluate( const QString &expression ) const
195{
196 QgsExpression exp { expression };
197 exp.prepare( &mExpressionContext );
198 return exp.evaluate( &mExpressionContext ).toString();
199}
200
201void NeedsGeometryEvaluator::evaluate( const QString &expression )
202{
203 QgsExpression exp { expression };
204 exp.prepare( &mExpressionContext );
205 mNeedsGeometry |= exp.needsGeometry();
206}
207
208void NeedsGeometryEvaluator::setExpressionContext( const QgsExpressionContext &context )
209{
210 mExpressionContext = context;
211}
212
213
A collection of stubs to mimic the API of a QWebPage on systems where QtWebkit is not available.
Definition qgswebpage.h:105
Contains context information for attribute editor widgets.
QString attributeFormModeString() const
Returns given attributeFormMode as string.
QgsFeature parentFormFeature() const
Returns the feature of the currently edited parent form in its actual state.
The attribute form widget for vector layer features.
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
const QgsFeature & feature() const
Returns feature of attribute form.
static QgsExpressionContextScope * parentFormScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current parent attribute form/tab...
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table form...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Handles parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
QVariant evaluate()
Evaluate the feature and return the result.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
bool isValid() const
Returns the validity of this feature.
void reinitWidget()
Clears the content and makes new initialization.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QgsHtmlWidgetWrapper(QgsVectorLayer *layer, QWidget *editor, QWidget *parent)
Create a html widget wrapper.
void setHtmlCode(const QString &htmlCode)
Sets the HTML code to htmlCode.
bool valid() const override
Returns true if the widget has been properly initialized.
void setFeature(const QgsFeature &feature) override
bool needsGeometry() const
Returns true if the widget needs feature geometry.
static bool expressionRequiresFormScope(const QString &expression)
Check if the expression requires a form scope (i.e.
Represents a vector layer which manages a vector based dataset.
QgsExpressionContext createExpressionContext() const final
This method needs to be reimplemented in all classes which implement this interface and return an exp...
A collection of stubs to mimic the API of QWebView on systems where the real library is not available...
Definition qgswebview.h:66
void contextChanged()
Signal when QgsAttributeEditorContext mContext changed.
QgsWidgetWrapper(QgsVectorLayer *vl, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.