Skip to content

Commit

Permalink
expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Sep 5, 2018
1 parent 3d8d3ac commit abbe5c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
Expand Up @@ -54,6 +54,7 @@ the Free Software Foundation; either version 2 of the License, or *

};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
34 changes: 16 additions & 18 deletions src/app/qgsattributesformproperties.cpp
Expand Up @@ -1163,45 +1163,43 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
}
} );

//attributes to select
QgsFieldComboBox *attributeFieldCombo = new QgsFieldComboBox();
attributeFieldCombo->setLayer( mLayer );
connect( attributeFieldCombo, &QgsFieldComboBox::fieldChanged, this, [ = ]( QString attributeName )
{
qmlCode->insertPlainText( QStringLiteral( "feature.attribute(\"%1\")" ).arg( attributeName ) );
} );

QgsFieldExpressionWidget *expressionWidget = new QgsFieldExpressionWidget;
expressionWidget->setLayer( mLayer );
expressionWidget->setExpressionDialogTitle( tr( "Expression" ) );
QToolButton *addExpressionButton = new QToolButton();
addExpressionButton->setIcon( QgsApplication::getThemeIcon( "/symbologyAdd.svg" ) );

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


QgsQmlWidgetWrapper *qmlWrapper = new QgsQmlWidgetWrapper( mLayer, nullptr, this );
qmlWrapper->setQmlCode( qmlCode->toPlainText() );
connect( qmlCode, &QPlainTextEdit::textChanged, this, [ = ]
{
qmlWrapper->setQmlCode( qmlCode->toPlainText() );
qmlWrapper->reinitWidget();
QgsFeature f;
mLayer->getFeatures().nextFeature(f);
qmlWrapper->setFeature( f );
} );

layout->addRow( tr( "Title" ), title );
QGroupBox *qmlCodeBox = new QGroupBox( tr( "QML Code" ) );
QScrollArea *qmlCodeBox = new QScrollArea();
qmlCodeBox->setLayout( new QGridLayout );
qmlCodeBox->layout()->addWidget( qmlObjectTemplate );
qmlCodeBox->layout()->addWidget( attributeFieldCombo );
qmlCodeBox->layout()->addWidget( expressionWidget );
QGroupBox *expressionWidgetBox = new QGroupBox();
qmlCodeBox->layout()->addWidget( expressionWidgetBox );
expressionWidgetBox->setLayout( new QGridLayout );
expressionWidgetBox->layout()->addWidget( expressionWidget );
expressionWidgetBox->layout()->addWidget( addExpressionButton );
qmlCodeBox->layout()->addWidget( qmlCode );
layout->addRow( qmlCodeBox );
QGroupBox *qmlPreviewBox = new QGroupBox( tr( "Preview") );
QScrollArea *qmlPreviewBox = new QScrollArea();
qmlPreviewBox->setLayout( new QGridLayout );
qmlPreviewBox->setMinimumWidth( 400 );
qmlPreviewBox->layout()->addWidget(qmlWrapper->widget());
qmlPreviewBox->layout()->addWidget( qmlWrapper->widget() );
qmlLayout->addWidget( qmlPreviewBox );

QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
Expand Down
23 changes: 22 additions & 1 deletion src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp
Expand Up @@ -91,5 +91,26 @@ void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
void QgsQmlWidgetWrapper::setFeature( const QgsFeature &feature )
{
if ( mWidget )
mWidget->rootContext()->setContextProperty( "feature", feature );
{
QgsExpressionContext context = layer()->createExpressionContext();

context.setFeature( feature );

QmlExpression *qmlExpression = new QmlExpression();
qmlExpression->setExpressionContext( context );

mWidget->rootContext()->setContextProperty( "expression", qmlExpression );
}
}

void QmlExpression::setExpressionContext( const QgsExpressionContext &context )
{
mExpressionContext = context;
}

QVariant QmlExpression::evaluate( const QString& expression ) const
{
QgsExpression exp = QgsExpression( expression );
exp.prepare( &mExpressionContext );
return exp.evaluate(&mExpressionContext);
}
18 changes: 9 additions & 9 deletions src/gui/editorwidgets/qgsqmlwidgetwrapper.h
Expand Up @@ -21,7 +21,6 @@
#include "qgis_gui.h"
#include <QtQuickWidgets/QQuickWidget>


class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper
{
Q_OBJECT
Expand Down Expand Up @@ -51,17 +50,18 @@ class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper
QgsFeature mFeature;
};

/*
class GUI_EXPORT QmlExpression

class GUI_EXPORT QmlExpression : public QObject
{
Q_GADGET
Q_OBJECT

public:
QgsExpressionContext expressionContext();
void setExpressionContext( QgsExpressionContext expressionContext );
void setExpressionContext( const QgsExpressionContext &context );

Q_INVOKABLE QVariant evaluate( const QString &expression) const;

Q_INVOKABLE evaluate();
}
*/
private:
QgsExpressionContext mExpressionContext;
};

#endif // QGSQMLWIDGETWRAPPER_H

0 comments on commit abbe5c5

Please sign in to comment.