Skip to content

Commit

Permalink
Support get_current_value in HTML widget
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Dec 28, 2022
1 parent 2712aed commit 882919a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/gui/editorwidgets/qgshtmlwidgetwrapper.cpp
Expand Up @@ -15,10 +15,9 @@
***************************************************************************/

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

QgsHtmlWidgetWrapper::QgsHtmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
Expand All @@ -34,6 +33,22 @@ 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 )
{
mFormFeature.setAttribute( attribute, newValue );
setHtmlContext();
}
} );
}

return new QgsWebView( parent );
}

Expand Down Expand Up @@ -110,11 +125,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 +159,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

0 comments on commit 882919a

Please sign in to comment.