QGIS API Documentation 4.3.0-Master (bf28115e945)
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#include <QString>
26
27#include "moc_qgshtmlwidgetwrapper.cpp"
28
29using namespace Qt::StringLiterals;
30
32 : QgsWidgetWrapper( layer, editor, parent )
33{
34 connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsHtmlWidgetWrapper::updateHtmlCode );
35}
36
38{
39 return true;
40}
41
42QWidget *QgsHtmlWidgetWrapper::createWidget( QWidget *parent )
43{
44 QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
45
46 if ( form )
47 {
48 mFormFeature = form->feature();
49 connect( form, &QgsAttributeForm::widgetValueChanged, this, [this]( const QString &attribute, const QVariant &newValue, bool attributeChanged ) {
50 if ( attributeChanged )
51 {
52 mFeature.setAttribute( attribute, newValue );
53 if ( mRequiresFormScope )
54 {
55 mFormFeature.setAttribute( attribute, newValue );
56 }
57 updateHtmlCode();
58 }
59 } );
60 }
61
62 return new QgsWebView( parent );
63}
64
65void QgsHtmlWidgetWrapper::initWidget( QWidget *editor )
66{
67 mWidget = qobject_cast<QgsWebView *>( editor );
68
69 if ( !mWidget )
70 return;
71
72 updateHtmlCode();
73}
74
76{
77 if ( !mWidget )
78 return;
79
80 initWidget( mWidget );
81}
82
83void QgsHtmlWidgetWrapper::setHtmlCode( const QString &htmlCode )
84{
85 mHtmlCode = htmlCode;
86
87 mRequiresFormScope = false;
88 mNeedsGeometry = false;
89
91 const thread_local QRegularExpression
92 expRe( QStringLiteral( R"re(expression.evaluate\s*\‍(\s*"(.*)"\))re" ), QRegularExpression::PatternOption::MultilineOption | QRegularExpression::PatternOption::DotMatchesEverythingOption );
93 QRegularExpressionMatchIterator matchIt = expRe.globalMatch( mHtmlCode );
94 while ( matchIt.hasNext() && ( !mRequiresFormScope || !mNeedsGeometry ) )
95 {
96 const QRegularExpressionMatch match = matchIt.next();
97 QString expression = match.captured( 1 );
98 expression = expression.replace( "\\\""_L1, "\""_L1 );
99
100 QgsExpression exp = QgsExpression( expression );
102 exp.prepare( &expressionContext );
103 mNeedsGeometry |= exp.needsGeometry();
104 }
105
106 updateHtmlCode();
107}
108
109void QgsHtmlWidgetWrapper::updateHtmlCode()
110{
111 if ( !mWidget )
112 return;
113
114 QString htmlCode = mHtmlCode;
115
116 const QgsAttributeEditorContext attributecontext = context();
117 QgsExpressionContext expressionContext = layer()->createExpressionContext();
118 expressionContext << QgsExpressionContextUtils::formScope( mFormFeature, attributecontext.attributeFormModeString() );
119 if ( attributecontext.parentFormFeature().isValid() )
120 {
121 expressionContext << QgsExpressionContextUtils::parentFormScope( attributecontext.parentFormFeature() );
122 }
123 expressionContext.setFeature( mFeature );
124
125 const thread_local QRegularExpression
126 expRe( QStringLiteral( R"re(<script>\s*document\.write\‍(\s*expression.evaluate\s*\‍(\s*"(.*)"\s*\)\s*\)\s*;\s*<\/script>)re" ), QRegularExpression::PatternOption::MultilineOption | QRegularExpression::PatternOption::DotMatchesEverythingOption );
127 QRegularExpressionMatch match = expRe.match( htmlCode );
128 while ( match.hasMatch() )
129 {
130 QString expression = match.captured( 1 );
131 expression = expression.replace( "\\\""_L1, "\""_L1 );
132
133 QgsExpression exp = QgsExpression( expression );
134 exp.prepare( &expressionContext );
135 QVariant result = exp.evaluate( &expressionContext );
136
137 QString resultString;
138 switch ( static_cast<QMetaType::Type>( result.typeId() ) )
139 {
140 case QMetaType::Bool:
141 resultString = result.toBool() ? u"true"_s : u"false"_s;
142 break;
143 case QMetaType::Int:
144 case QMetaType::UInt:
145 case QMetaType::Double:
146 case QMetaType::LongLong:
147 case QMetaType::ULongLong:
148 case QMetaType::QString:
149 default:
150 resultString = result.toString();
151 break;
152 }
153 htmlCode = htmlCode.mid( 0, match.capturedStart( 0 ) ) + resultString + htmlCode.mid( match.capturedEnd( 0 ) );
154 match = expRe.match( htmlCode );
155 }
156
157 mWidget->setHtml( htmlCode );
158}
159
161{
162 if ( !mWidget )
163 return;
164
165 mFeature = feature;
166 mFormFeature = feature;
167 updateHtmlCode();
168}
169
171{
172 return mNeedsGeometry;
173}
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:60
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:39
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.