27#include "moc_qgshtmlwidgetwrapper.cpp"
29using namespace Qt::StringLiterals;
50 if ( attributeChanged )
52 mFeature.setAttribute( attribute, newValue );
53 if ( mRequiresFormScope )
55 mFormFeature.setAttribute( attribute, newValue );
67 mWidget = qobject_cast<QgsWebView *>( editor );
87 mRequiresFormScope =
false;
88 mNeedsGeometry =
false;
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 ) )
96 const QRegularExpressionMatch match = matchIt.next();
97 QString expression = match.captured( 1 );
98 expression = expression.replace(
"\\\""_L1,
"\""_L1 );
102 exp.
prepare( &expressionContext );
109void QgsHtmlWidgetWrapper::updateHtmlCode()
114 QString htmlCode = mHtmlCode;
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() )
130 QString expression = match.captured( 1 );
131 expression = expression.replace(
"\\\""_L1,
"\""_L1 );
133 QgsExpression exp = QgsExpression( expression );
134 exp.
prepare( &expressionContext );
135 QVariant result = exp.
evaluate( &expressionContext );
137 QString resultString;
138 switch (
static_cast<QMetaType::Type
>( result.typeId() ) )
140 case QMetaType::Bool:
141 resultString = result.toBool() ? u
"true"_s : u
"false"_s;
144 case QMetaType::UInt:
145 case QMetaType::Double:
146 case QMetaType::LongLong:
147 case QMetaType::ULongLong:
148 case QMetaType::QString:
150 resultString = result.toString();
153 htmlCode = htmlCode.mid( 0, match.capturedStart( 0 ) ) + resultString + htmlCode.mid( match.capturedEnd( 0 ) );
154 match = expRe.match( htmlCode );
157 mWidget->setHtml( htmlCode );
166 mFormFeature = feature;
172 return mNeedsGeometry;
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.
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...
bool isValid() const
Returns the validity of this feature.
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...