QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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
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#include <QUrl>
25
26QgsQmlWidgetWrapper::QgsQmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
27 : QgsWidgetWrapper( layer, editor, parent )
28{
29 connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsQmlWidgetWrapper::setQmlContext );
30}
31
33{
34 return true;
35}
36
37QWidget *QgsQmlWidgetWrapper::createWidget( QWidget *parent )
38{
39 return new QQuickWidget( parent );
40}
41
42void QgsQmlWidgetWrapper::initWidget( QWidget *editor )
43{
44 mWidget = qobject_cast<QQuickWidget *>( editor );
45
46 if ( !mWidget )
47 return;
48
49
50 if ( !mQmlFile.open() )
51 {
52 QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
53 return;
54 }
55
56 mWidget->setSource( QUrl::fromLocalFile( mQmlFile.fileName() ) );
57
58 mQmlFile.close();
59}
60
61
63{
64 if ( !mWidget )
65 return;
66
67 mWidget->engine()->clearComponentCache();
68
69 initWidget( mWidget );
70}
71
72void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
73{
74 if ( !mQmlFile.open() )
75 {
76 QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
77 return;
78 }
79
80 mQmlFile.resize( 0 );
81 mQmlFile.write( qmlCode.toUtf8() );
82
83 mQmlFile.close();
84}
85
86void QgsQmlWidgetWrapper::setQmlContext( )
87{
88 if ( !mWidget )
89 return;
90
91 const QgsAttributeEditorContext attributecontext = context();
92 QgsExpressionContext expressionContext = layer()->createExpressionContext();
93 expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
94 expressionContext.setFeature( mFeature );
95
96 QmlExpression *qmlExpression = new QmlExpression();
97 qmlExpression->setExpressionContext( expressionContext );
98
99 mWidget->rootContext()->setContextProperty( "expression", qmlExpression );
100}
101
103{
104 if ( !mWidget )
105 return;
106
107 mFeature = feature;
108
109 setQmlContext();
110}
111
113void QmlExpression::setExpressionContext( const QgsExpressionContext &context )
114{
115 mExpressionContext = context;
116}
117
118QVariant QmlExpression::evaluate( const QString &expression ) const
119{
120 QgsExpression exp = QgsExpression( expression );
121 exp.prepare( &mExpressionContext );
122 return exp.evaluate( &mExpressionContext );
123}
This class contains context information for attribute editor widgets.
QString attributeFormModeString() const
Returns given attributeFormMode as string.
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.
Class for 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...
Definition qgsfeature.h:56
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
bool valid() const override
Returns true if the widget has been properly initialized.
void setFeature(const QgsFeature &feature) override
void reinitWidget()
Clears the content and makes new initialization.
void setQmlCode(const QString &qmlCode)
writes the qmlCode into a temporary file
QgsQmlWidgetWrapper(QgsVectorLayer *layer, QWidget *editor, QWidget *parent)
Create a qml widget wrapper.
Represents a vector layer which manages a vector based data sets.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Manages an editor widget Widget and wrapper share the same parent.
void contextChanged()
Signal when QgsAttributeEditorContext mContext changed.
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.