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