29#include "moc_qgsqmlwidgetwrapper.cpp"
31using namespace Qt::StringLiterals;
36 mQmlFile.setFileTemplate( u
"%1%2QgsQmlWidgetWrapper-XXXXXX.qml"_s.arg( QDir::tempPath(), QDir::separator() ) );
53 if ( attributeChanged )
55 if ( mRequiresFormScope )
57 mFormFeature.setAttribute( attribute, newValue );
64 QQuickWidget *
widget =
new QQuickWidget( parent );
71 mWidget = qobject_cast<QQuickWidget *>( editor );
78 if ( !mQmlFile.open() )
86 mWidgetBridge->deleteLater();
87 mWidgetBridge =
nullptr;
91 QUrl sourceUrl = QUrl::fromLocalFile( mQmlFile.fileName() );
92 sourceUrl.setQuery( u
"t=%1"_s.arg( QDateTime::currentSecsSinceEpoch() ) );
93 mWidget->setSource( sourceUrl );
106 mWidget->engine()->clearComponentCache();
113 if ( mQmlCode == qmlCode )
121 const thread_local QRegularExpression
122 expRe( QStringLiteral( R
"re(expression.evaluate\s*\(\s*"(.*)"\s*\))re" ), QRegularExpression::PatternOption::MultilineOption | QRegularExpression::PatternOption::DotMatchesEverythingOption | QRegularExpression::InvertedGreedinessOption );
123 QRegularExpressionMatchIterator matchIt = expRe.globalMatch( mQmlCode );
124 while ( !ok && matchIt.hasNext() )
126 const QRegularExpressionMatch match = matchIt.next();
130 mRequiresFormScope = ok;
132 if ( !mQmlFile.open() )
138 mQmlFile.resize( 0 );
139 mQmlFile.write( mQmlCode.toUtf8() );
143void QgsQmlWidgetWrapper::setQmlContext()
150 const QgsAttributeEditorContext attributeEditorContext =
context();
152 if ( !mWidgetBridge )
154 QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent() );
155 mWidgetBridge =
new QgsQmlAttributeFormBridge( form,
this );
156 mWidget->rootContext()->setContextProperty(
"attributeFormBridge", mWidgetBridge );
158 mWidgetBridge->setContext( attributeEditorContext );
168 QgsQmlExpression *qmlExpression =
new QgsQmlExpression();
169 qmlExpression->setExpressionContext( expressionContext );
171 mWidget->rootContext()->setContextProperty(
"expression", qmlExpression );
182 mFormFeature = feature;
184 emit mWidgetBridge->currentFormFeatureChanged();
192 mExpressionContext = context;
195QVariant QgsQmlExpression::evaluate(
const QString &expression )
const
198 exp.
prepare( &mExpressionContext );
199 return exp.
evaluate( &mExpressionContext );
203QgsQmlAttributeFormBridge::QgsQmlAttributeFormBridge(
QgsAttributeForm *form, QObject *parent )
210 if ( attributeChanged )
212 emit currentFormFeatureChanged();
220bool QgsQmlAttributeFormBridge::editable()
const
222 return mForm ? mForm->editable() :
false;
233 emit contextChanged();
238 return mForm ? mForm->layer() :
nullptr;
241QgsFeature QgsQmlAttributeFormBridge::currentFormFeature()
const
243 return mForm ? mForm->currentFormFeature() :
QgsFeature();
246void QgsQmlAttributeFormBridge::changeGeometry(
const QgsGeometry &geometry )
250 mForm->changeGeometry( geometry );
251 emit currentFormFeatureChanged();
255void QgsQmlAttributeFormBridge::changeAttribute(
const QString &field,
const QVariant &value )
259 mForm->changeAttribute( field, value );
263QVariant QgsQmlAttributeFormBridge::attribute(
const QString &field )
265 return mForm ? mForm->feature().attribute( field ) : QVariant();
Contains context information for attribute editor widgets.
QgsFeature parentFormFeature
QString attributeFormModeString() const
Returns given attributeFormMode as string.
static std::unique_ptr< 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...
static std::unique_ptr< 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...
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.
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.
A geometry is the spatial representation of a feature.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
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...