QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#include "moc_qgshtmlwidgetwrapper.cpp"
20#include "qgswebframe.h"
22#include "qgsattributeform.h"
23
24#include <QScreen>
25
26QgsHtmlWidgetWrapper::QgsHtmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
27 : QgsWidgetWrapper( layer, editor, parent )
28{
29 connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsHtmlWidgetWrapper::setHtmlContext );
30}
31
33{
34 return true;
35}
36
37QWidget *QgsHtmlWidgetWrapper::createWidget( QWidget *parent )
38{
39 QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
40
41 if ( form )
42 {
43 mFormFeature = form->feature();
44 connect( form, &QgsAttributeForm::widgetValueChanged, this, [=]( const QString &attribute, const QVariant &newValue, bool attributeChanged ) {
45 if ( attributeChanged )
46 {
47 if ( mRequiresFormScope )
48 {
49 mFormFeature.setAttribute( attribute, newValue );
50 setHtmlContext();
51 }
52 }
53 } );
54 }
55
56 return new QgsWebView( parent );
57}
58
59void QgsHtmlWidgetWrapper::initWidget( QWidget *editor )
60{
61 mWidget = qobject_cast<QgsWebView *>( editor );
62
63 if ( !mWidget )
64 return;
65
66 mWidget->setHtml( mHtmlCode.replace( "\n", " " ) );
67
68#ifdef WITH_QTWEBKIT
69
70 const int horizontalDpi = mWidget->logicalDpiX();
71
72 mWidget->setZoomFactor( horizontalDpi / 96.0 );
73
74 QWebPage *page = mWidget->page();
75 connect( page, &QWebPage::contentsChanged, this, &QgsHtmlWidgetWrapper::fixHeight, Qt::ConnectionType::UniqueConnection );
76 connect( page, &QWebPage::loadFinished, this, &QgsHtmlWidgetWrapper::fixHeight, Qt::ConnectionType::UniqueConnection );
77
78#endif
79
80 checkGeometryNeeds();
81}
82
84{
85 if ( !mWidget )
86 return;
87
88 initWidget( mWidget );
89}
90
91void QgsHtmlWidgetWrapper::checkGeometryNeeds()
92{
93 if ( !mWidget )
94 return;
95
96 // initialize a temporary QgsWebView to render HTML code and check if one evaluated expression
97 // needs geometry
98 QgsWebView webView;
99 NeedsGeometryEvaluator evaluator;
100
101 const QgsAttributeEditorContext attributecontext = context();
102 if ( QgsVectorLayer *vl = layer() )
103 {
104 const QgsExpressionContext expressionContext = vl->createExpressionContext();
105 evaluator.setExpressionContext( expressionContext );
106 }
107
108 auto frame = webView.page()->mainFrame();
109 connect( frame, &QWebFrame::javaScriptWindowObjectCleared, frame, [frame, &evaluator] {
110 frame->addToJavaScriptWindowObject( QStringLiteral( "expression" ), &evaluator );
111 } );
112
113 webView.setHtml( mHtmlCode );
114
115 mNeedsGeometry = evaluator.needsGeometry();
116}
117
118void QgsHtmlWidgetWrapper::setHtmlCode( const QString &htmlCode )
119{
120 mHtmlCode = htmlCode;
121
122 bool ok = false;
123 const thread_local QRegularExpression expRe( QStringLiteral( R"re(expression.evaluate\s*\‍(\s*"(.*)"\))re" ), QRegularExpression::PatternOption::MultilineOption | QRegularExpression::PatternOption::DotMatchesEverythingOption );
124 QRegularExpressionMatchIterator matchIt = expRe.globalMatch( mHtmlCode );
125 while ( !ok && matchIt.hasNext() )
126 {
127 const QRegularExpressionMatch match = matchIt.next();
128 const QgsExpression exp = match.captured( 1 );
130 }
131 mRequiresFormScope = ok;
132
133 checkGeometryNeeds();
134}
135
136void QgsHtmlWidgetWrapper::setHtmlContext()
137{
138 if ( !mWidget )
139 return;
140
141 const QgsAttributeEditorContext attributecontext = context();
142 QgsExpressionContext expressionContext = layer()->createExpressionContext();
143 expressionContext << QgsExpressionContextUtils::formScope( mFormFeature, attributecontext.attributeFormModeString() );
144 if ( attributecontext.parentFormFeature().isValid() )
145 {
146 expressionContext << QgsExpressionContextUtils::parentFormScope( attributecontext.parentFormFeature() );
147 }
148
149 expressionContext.setFeature( mFeature );
150
151 HtmlExpression *htmlExpression = new HtmlExpression();
152 htmlExpression->setExpressionContext( expressionContext );
153 auto frame = mWidget->page()->mainFrame();
154 connect( frame, &QWebFrame::javaScriptWindowObjectCleared, frame, [=] {
155 frame->addToJavaScriptWindowObject( QStringLiteral( "expression" ), htmlExpression );
156 } );
157
158 mWidget->setHtml( mHtmlCode );
159}
160
161#ifdef WITH_QTWEBKIT
162void QgsHtmlWidgetWrapper::fixHeight()
163{
164 QWebPage *page = mWidget->page();
165 const int docHeight { page->mainFrame()->contentsSize().height() };
166 mWidget->setFixedHeight( docHeight );
167}
168#endif
169
171{
172 if ( !mWidget )
173 return;
174
175 mFeature = feature;
176 mFormFeature = feature;
177 setHtmlContext();
178}
179
181{
182 return mNeedsGeometry;
183}
184
185
187void HtmlExpression::setExpressionContext( const QgsExpressionContext &context )
188{
189 mExpressionContext = context;
190}
191
192QString HtmlExpression::evaluate( const QString &expression ) const
193{
194 QgsExpression exp { expression };
195 exp.prepare( &mExpressionContext );
196 return exp.evaluate( &mExpressionContext ).toString();
197}
198
199void NeedsGeometryEvaluator::evaluate( const QString &expression )
200{
201 QgsExpression exp { expression };
202 exp.prepare( &mExpressionContext );
203 mNeedsGeometry |= exp.needsGeometry();
204}
205
206void NeedsGeometryEvaluator::setExpressionContext( const QgsExpressionContext &context )
207{
208 mExpressionContext = context;
209}
210
211
The QWebPage class is a collection of stubs to mimic the API of a QWebPage on systems where QtWebkit ...
Definition qgswebpage.h:104
This class 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.
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
const QgsFeature & feature()
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.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
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 data sets.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
The QgsWebView class is a collection of stubs to mimic the API of QWebView on systems where the real ...
Definition qgswebview.h:66
Manages an editor widget Widget and wrapper share the same parent.
void contextChanged()
Signal when QgsAttributeEditorContext mContext changed.
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.