Skip to content

Commit

Permalink
Merge pull request #51316 from elpaso/htmlwidget-get_current_value
Browse files Browse the repository at this point in the history
Support current_value in HTML widget
  • Loading branch information
troopa81 committed Jan 3, 2023
2 parents a3fc7e8 + cc37164 commit 9593a56
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
29 changes: 26 additions & 3 deletions src/gui/editorwidgets/qgshtmlwidgetwrapper.cpp
Expand Up @@ -15,10 +15,10 @@
***************************************************************************/

#include "qgshtmlwidgetwrapper.h"
#include "qgsmessagelog.h"
#include "qgsexpressioncontextutils.h"
#include "qgsapplication.h"
#include "qgswebframe.h"
#include "qgsvaluerelationfieldformatter.h"
#include "qgsattributeform.h"
#include <QScreen>

QgsHtmlWidgetWrapper::QgsHtmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
Expand All @@ -34,6 +34,27 @@ bool QgsHtmlWidgetWrapper::valid() const

QWidget *QgsHtmlWidgetWrapper::createWidget( QWidget *parent )
{

QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );

if ( form )
{
mFormFeature = form->feature();
connect( form, &QgsAttributeForm::widgetValueChanged, this, [ = ]( const QString & attribute, const QVariant & newValue, bool attributeChanged )
{
if ( attributeChanged )
{
const QRegularExpression expRe { QStringLiteral( R"re(expression.evaluate\s*\(\s*"(.*)"\))re" ), QRegularExpression::PatternOption::MultilineOption | QRegularExpression::PatternOption::DotMatchesEverythingOption };
const QRegularExpressionMatch match { expRe.match( mHtmlCode ) };
if ( match.hasMatch() && QgsValueRelationFieldFormatter::expressionRequiresFormScope( match.captured( 1 ) ) )
{
mFormFeature.setAttribute( attribute, newValue );
setHtmlContext();
}
}
} );
}

return new QgsWebView( parent );
}

Expand Down Expand Up @@ -110,11 +131,12 @@ void QgsHtmlWidgetWrapper::setHtmlContext( )

const QgsAttributeEditorContext attributecontext = context();
QgsExpressionContext expressionContext = layer()->createExpressionContext();
expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
expressionContext << QgsExpressionContextUtils::formScope( mFormFeature, attributecontext.attributeFormModeString() );
if ( attributecontext.parentFormFeature().isValid() )
{
expressionContext << QgsExpressionContextUtils::parentFormScope( attributecontext.parentFormFeature() );
}

expressionContext.setFeature( mFeature );

HtmlExpression *htmlExpression = new HtmlExpression();
Expand Down Expand Up @@ -143,6 +165,7 @@ void QgsHtmlWidgetWrapper::setFeature( const QgsFeature &feature )
return;

mFeature = feature;
mFormFeature = feature;
setHtmlContext();
}

Expand Down
1 change: 1 addition & 0 deletions src/gui/editorwidgets/qgshtmlwidgetwrapper.h
Expand Up @@ -78,6 +78,7 @@ class GUI_EXPORT QgsHtmlWidgetWrapper : public QgsWidgetWrapper
QgsWebView *mWidget = nullptr;
QgsFeature mFeature;
bool mNeedsGeometry = false;
QgsFeature mFormFeature;

friend class TestQgsHtmlWidgetWrapper;
};
Expand Down
14 changes: 14 additions & 0 deletions src/gui/vector/qgsattributesformproperties.cpp
Expand Up @@ -1418,6 +1418,7 @@ void QgsAttributesDnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int colum
} );

QgsFieldExpressionWidget *expressionWidget = new QgsFieldExpressionWidget;
expressionWidget->registerExpressionContextGenerator( this );
expressionWidget->setLayer( mLayer );
QToolButton *addExpressionButton = new QToolButton();
addExpressionButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/symbologyAdd.svg" ) ) );
Expand Down Expand Up @@ -1466,6 +1467,19 @@ void QgsAttributesDnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int colum
}
}

QgsExpressionContext QgsAttributesDnDTree::createExpressionContext() const
{
QgsExpressionContext expContext;
expContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() );

if ( mLayer )
expContext << QgsExpressionContextUtils::layerScope( mLayer );

expContext.appendScope( QgsExpressionContextUtils::formScope( ) );
return expContext;
}

QgsAttributesDnDTree::Type QgsAttributesDnDTree::type() const
{
return mType;
Expand Down
6 changes: 5 additions & 1 deletion src/gui/vector/qgsattributesformproperties.h
Expand Up @@ -342,7 +342,7 @@ QDataStream &operator>> ( QDataStream &stream, QgsAttributesFormProperties::DnDT
*
* Graphical representation for the attribute editor drag and drop editor
*/
class GUI_EXPORT QgsAttributesDnDTree : public QTreeWidget
class GUI_EXPORT QgsAttributesDnDTree : public QTreeWidget, private QgsExpressionContextGenerator
{
Q_OBJECT

Expand Down Expand Up @@ -391,6 +391,10 @@ class GUI_EXPORT QgsAttributesDnDTree : public QTreeWidget
private:
QgsVectorLayer *mLayer = nullptr;
Type mType = QgsAttributesDnDTree::Type::Drag;

// QgsExpressionContextGenerator interface
public:
QgsExpressionContext createExpressionContext() const override;
};


Expand Down
1 change: 1 addition & 0 deletions tests/src/gui/testqgshtmlwidgetwrapper.cpp
Expand Up @@ -74,6 +74,7 @@ void TestQgsHtmlWidgetWrapper::testExpressionEvaluate_data()
QTest::newRow( "with-geometry" ) << "geom_to_wkt($geometry)" << true << "The text is 'Point (0.5 0.5)'";
QTest::newRow( "without-geometry" ) << "2+pk" << false << "The text is '3'";
QTest::newRow( "aggregate newline" ) << "concat('a', \n'b')" << false << "The text is 'ab'";
QTest::newRow( "form value" ) << "current_value('pk') + 2" << false << "The text is '3'";
}

void TestQgsHtmlWidgetWrapper::testExpressionEvaluate()
Expand Down

0 comments on commit 9593a56

Please sign in to comment.