QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsqmlwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsqmlwidgetwrapper.cpp
3 
4  ---------------------
5  begin : 25.6.2018
6  copyright : (C) 2018 by Matthias Kuhn
7  email : [email protected]
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 #include "qgsqmlwidgetwrapper.h"
17 #include "qgsmessagelog.h"
19 
20 #include <QtQuickWidgets/QQuickWidget>
21 #include <QQuickWidget>
22 #include <QQmlContext>
23 #include <QQmlEngine>
24 
25 QgsQmlWidgetWrapper::QgsQmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
26  : QgsWidgetWrapper( layer, editor, parent )
27 {
28  connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsQmlWidgetWrapper::setQmlContext );
29 }
30 
32 {
33  return true;
34 }
35 
36 QWidget *QgsQmlWidgetWrapper::createWidget( QWidget *parent )
37 {
38  return new QQuickWidget( parent );
39 }
40 
41 void QgsQmlWidgetWrapper::initWidget( QWidget *editor )
42 {
43  mWidget = qobject_cast<QQuickWidget *>( editor );
44 
45  if ( !mWidget )
46  return;
47 
48 
49  if ( !mQmlFile.open() )
50  {
51  QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
52  return;
53  }
54 
55  mWidget->setSource( QUrl::fromLocalFile( mQmlFile.fileName() ) );
56 
57  mQmlFile.close();
58 }
59 
60 
62 {
63  if ( !mWidget )
64  return;
65 
66  mWidget->engine()->clearComponentCache();
67 
68  initWidget( mWidget );
69 }
70 
71 void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
72 {
73  if ( !mQmlFile.open() )
74  {
75  QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
76  return;
77  }
78 
79  mQmlFile.resize( 0 );
80  mQmlFile.write( qmlCode.toUtf8() );
81 
82  mQmlFile.close();
83 }
84 
85 void QgsQmlWidgetWrapper::setQmlContext( )
86 {
87  if ( !mWidget )
88  return;
89 
90  QgsAttributeEditorContext attributecontext = context();
91  QgsExpressionContext expressionContext = layer()->createExpressionContext();
92  expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
93  expressionContext.setFeature( mFeature );
94 
95  QmlExpression *qmlExpression = new QmlExpression();
96  qmlExpression->setExpressionContext( expressionContext );
97 
98  mWidget->rootContext()->setContextProperty( "expression", qmlExpression );
99 }
100 
102 {
103  if ( !mWidget )
104  return;
105 
106  mFeature = feature;
107 
108  setQmlContext();
109 }
110 
112 void QmlExpression::setExpressionContext( const QgsExpressionContext &context )
113 {
114  mExpressionContext = context;
115 }
116 
117 QVariant QmlExpression::evaluate( const QString &expression ) const
118 {
119  QgsExpression exp = QgsExpression( expression );
120  exp.prepare( &mExpressionContext );
121  return exp.evaluate( &mExpressionContext );
122 }
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
qgsexpressioncontextutils.h
QgsWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgswidgetwrapper.h:53
QgsExpressionContextUtils::formScope
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...
Definition: qgsexpressioncontextutils.cpp:201
QgsQmlWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgsqmlwidgetwrapper.cpp:31
QgsWidgetWrapper::context
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
Definition: qgswidgetwrapper.cpp:86
QgsQmlWidgetWrapper::QgsQmlWidgetWrapper
QgsQmlWidgetWrapper(QgsVectorLayer *layer, QWidget *editor, QWidget *parent)
Create a qml widget wrapper.
Definition: qgsqmlwidgetwrapper.cpp:25
QgsQmlWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgsqmlwidgetwrapper.cpp:36
QgsWidgetWrapper::contextChanged
void contextChanged()
Signal when QgsAttributeEditorContext mContext changed.
qgsqmlwidgetwrapper.h
QgsWidgetWrapper::layer
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
Definition: qgswidgetwrapper.cpp:91
QgsQmlWidgetWrapper::reinitWidget
void reinitWidget()
Clears the content and makes new initialization.
Definition: qgsqmlwidgetwrapper.cpp:61
QgsQmlWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgsqmlwidgetwrapper.cpp:41
QgsExpression::prepare
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
Definition: qgsexpression.cpp:323
QgsExpression::evaluate
QVariant evaluate()
Evaluate the feature and return the result.
Definition: qgsexpression.cpp:346
QgsQmlWidgetWrapper::setQmlCode
void setQmlCode(const QString &qmlCode)
writes the qmlCode into a temporary file
Definition: qgsqmlwidgetwrapper.cpp:71
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsVectorLayer::createExpressionContext
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgsvectorlayer.cpp:4952
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsAttributeEditorContext
This class contains context information for attribute editor widgets.
Definition: qgsattributeeditorcontext.h:41
QgsQmlWidgetWrapper::setFeature
void setFeature(const QgsFeature &feature) override
Definition: qgsqmlwidgetwrapper.cpp:101
QgsAttributeEditorContext::attributeFormModeString
QString attributeFormModeString() const
Returns given attributeFormMode as string.
Definition: qgsattributeeditorcontext.h:276
qgsmessagelog.h
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:521