Skip to content

Commit abbe5c5

Browse files
committed
expressions
1 parent 3d8d3ac commit abbe5c5

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

python/gui/auto_generated/editorwidgets/qgsqmlwidgetwrapper.sip.in

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ the Free Software Foundation; either version 2 of the License, or *
5454

5555
};
5656

57+
5758
/************************************************************************
5859
* This file has been generated automatically from *
5960
* *

src/app/qgsattributesformproperties.cpp

+16-18
Original file line numberDiff line numberDiff line change
@@ -1163,45 +1163,43 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
11631163
}
11641164
} );
11651165

1166-
//attributes to select
1167-
QgsFieldComboBox *attributeFieldCombo = new QgsFieldComboBox();
1168-
attributeFieldCombo->setLayer( mLayer );
1169-
connect( attributeFieldCombo, &QgsFieldComboBox::fieldChanged, this, [ = ]( QString attributeName )
1170-
{
1171-
qmlCode->insertPlainText( QStringLiteral( "feature.attribute(\"%1\")" ).arg( attributeName ) );
1172-
} );
1173-
11741166
QgsFieldExpressionWidget *expressionWidget = new QgsFieldExpressionWidget;
11751167
expressionWidget->setLayer( mLayer );
1176-
expressionWidget->setExpressionDialogTitle( tr( "Expression" ) );
1168+
QToolButton *addExpressionButton = new QToolButton();
1169+
addExpressionButton->setIcon( QgsApplication::getThemeIcon( "/symbologyAdd.svg" ) );
11771170

1178-
connect( expressionWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString & ) > ( &QgsFieldExpressionWidget::fieldChanged ), this, [ = ]( const QString & expressionText )
1171+
connect( addExpressionButton, &QAbstractButton::clicked, this, [ = ]
11791172
{
1180-
qmlCode->insertPlainText( QStringLiteral( "expression(%1)" ).arg( expressionText ) );
1181-
QgsLogger::warning( QStringLiteral( "Do it..." ) );
1182-
//expression needs to be added here...
1173+
qmlCode->insertPlainText( QStringLiteral( "expression.evaluate(\"(%1)\")" ).arg( expressionWidget->currentText()) );
11831174
} );
11841175

1176+
11851177
QgsQmlWidgetWrapper *qmlWrapper = new QgsQmlWidgetWrapper( mLayer, nullptr, this );
11861178
qmlWrapper->setQmlCode( qmlCode->toPlainText() );
11871179
connect( qmlCode, &QPlainTextEdit::textChanged, this, [ = ]
11881180
{
11891181
qmlWrapper->setQmlCode( qmlCode->toPlainText() );
11901182
qmlWrapper->reinitWidget();
1183+
QgsFeature f;
1184+
mLayer->getFeatures().nextFeature(f);
1185+
qmlWrapper->setFeature( f );
11911186
} );
11921187

11931188
layout->addRow( tr( "Title" ), title );
1194-
QGroupBox *qmlCodeBox = new QGroupBox( tr( "QML Code" ) );
1189+
QScrollArea *qmlCodeBox = new QScrollArea();
11951190
qmlCodeBox->setLayout( new QGridLayout );
11961191
qmlCodeBox->layout()->addWidget( qmlObjectTemplate );
1197-
qmlCodeBox->layout()->addWidget( attributeFieldCombo );
1198-
qmlCodeBox->layout()->addWidget( expressionWidget );
1192+
QGroupBox *expressionWidgetBox = new QGroupBox();
1193+
qmlCodeBox->layout()->addWidget( expressionWidgetBox );
1194+
expressionWidgetBox->setLayout( new QGridLayout );
1195+
expressionWidgetBox->layout()->addWidget( expressionWidget );
1196+
expressionWidgetBox->layout()->addWidget( addExpressionButton );
11991197
qmlCodeBox->layout()->addWidget( qmlCode );
12001198
layout->addRow( qmlCodeBox );
1201-
QGroupBox *qmlPreviewBox = new QGroupBox( tr( "Preview") );
1199+
QScrollArea *qmlPreviewBox = new QScrollArea();
12021200
qmlPreviewBox->setLayout( new QGridLayout );
12031201
qmlPreviewBox->setMinimumWidth( 400 );
1204-
qmlPreviewBox->layout()->addWidget(qmlWrapper->widget());
1202+
qmlPreviewBox->layout()->addWidget( qmlWrapper->widget() );
12051203
qmlLayout->addWidget( qmlPreviewBox );
12061204

12071205
QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );

src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,26 @@ void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
9191
void QgsQmlWidgetWrapper::setFeature( const QgsFeature &feature )
9292
{
9393
if ( mWidget )
94-
mWidget->rootContext()->setContextProperty( "feature", feature );
94+
{
95+
QgsExpressionContext context = layer()->createExpressionContext();
96+
97+
context.setFeature( feature );
98+
99+
QmlExpression *qmlExpression = new QmlExpression();
100+
qmlExpression->setExpressionContext( context );
101+
102+
mWidget->rootContext()->setContextProperty( "expression", qmlExpression );
103+
}
104+
}
105+
106+
void QmlExpression::setExpressionContext( const QgsExpressionContext &context )
107+
{
108+
mExpressionContext = context;
109+
}
110+
111+
QVariant QmlExpression::evaluate( const QString& expression ) const
112+
{
113+
QgsExpression exp = QgsExpression( expression );
114+
exp.prepare( &mExpressionContext );
115+
return exp.evaluate(&mExpressionContext);
95116
}

src/gui/editorwidgets/qgsqmlwidgetwrapper.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "qgis_gui.h"
2222
#include <QtQuickWidgets/QQuickWidget>
2323

24-
2524
class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper
2625
{
2726
Q_OBJECT
@@ -51,17 +50,18 @@ class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper
5150
QgsFeature mFeature;
5251
};
5352

54-
/*
55-
class GUI_EXPORT QmlExpression
53+
54+
class GUI_EXPORT QmlExpression : public QObject
5655
{
57-
Q_GADGET
56+
Q_OBJECT
5857

5958
public:
60-
QgsExpressionContext expressionContext();
61-
void setExpressionContext( QgsExpressionContext expressionContext );
59+
void setExpressionContext( const QgsExpressionContext &context );
60+
61+
Q_INVOKABLE QVariant evaluate( const QString &expression) const;
6262

63-
Q_INVOKABLE evaluate();
64-
}
65-
*/
63+
private:
64+
QgsExpressionContext mExpressionContext;
65+
};
6666

6767
#endif // QGSQMLWIDGETWRAPPER_H

0 commit comments

Comments
 (0)