Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added missing SIP files and headers
Also fixes context evaluation before html is updated
  • Loading branch information
elpaso committed Mar 23, 2019
1 parent b165258 commit b06103f
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 2 deletions.
@@ -0,0 +1,65 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/editorwidgets/qgshtmlwidgetwrapper.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/


class QgsHtmlWidgetWrapper : QgsWidgetWrapper
{
%Docstring
Wraps a QQuickWidget to display HTML code

.. versionadded:: 3.4
%End

%TypeHeaderCode
#include "qgshtmlwidgetwrapper.h"
%End
public:

QgsHtmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent );
%Docstring
Create a html widget wrapper

:param layer: The layer on which the feature is
:param editor: An editor widget. Can be ``None`` if one should be autogenerated.
:param parent: A parent widget
%End

virtual bool valid() const;


virtual QWidget *createWidget( QWidget *parent );


virtual void initWidget( QWidget *editor );


void reinitWidget();
%Docstring
Clears the content and makes new initialization
%End

void setHtmlCode( const QString &htmlCode );
%Docstring
Sets the HTML code to ``htmlCode``
%End

public slots:
virtual void setFeature( const QgsFeature &feature );


};



/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/editorwidgets/qgshtmlwidgetwrapper.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
121 changes: 121 additions & 0 deletions src/gui/editorwidgets/qgshtmlwidgetwrapper.cpp
@@ -0,0 +1,121 @@
/***************************************************************************
qgshtmlwidgetwrapper.h
---------------------
begin : 23.03.2019
copyright : (C) 2019 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgshtmlwidgetwrapper.h"
#include "qgsmessagelog.h"
#include "qgsexpressioncontextutils.h"
#include "qgsapplication.h"

#include <QWebFrame>

QgsHtmlWidgetWrapper::QgsHtmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
: QgsWidgetWrapper( layer, editor, parent )
{
connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsHtmlWidgetWrapper::setHtmlContext );
}

bool QgsHtmlWidgetWrapper::valid() const
{
return true;
}

QWidget *QgsHtmlWidgetWrapper::createWidget( QWidget *parent )
{
return new QgsWebView( parent );
}

void QgsHtmlWidgetWrapper::initWidget( QWidget *editor )
{
mWidget = qobject_cast<QgsWebView *>( editor );

if ( !mWidget )
return;

mWidget->setHtml( mHtmlCode );
const int horizontalDpi = qApp->desktop()->screen()->logicalDpiX();
mWidget->setZoomFactor( horizontalDpi / 96.0 );

auto page = mWidget->page();
connect( page, &QWebPage::contentsChanged, this, [ = ]
{
auto docHeight { page->mainFrame()->contentsSize().height() };
mWidget->setFixedHeight( docHeight );
}, Qt::ConnectionType::UniqueConnection );

}


void QgsHtmlWidgetWrapper::reinitWidget( )
{
if ( !mWidget )
return;

initWidget( mWidget );
}

void QgsHtmlWidgetWrapper::setHtmlCode( const QString &htmlCode )
{
mHtmlCode = htmlCode;
}

void QgsHtmlWidgetWrapper::setHtmlContext( )
{
if ( !mWidget )
return;

QgsAttributeEditorContext attributecontext = context();
QgsExpressionContext expressionContext = layer()->createExpressionContext();
expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
expressionContext.setFeature( mFeature );

HtmlExpression *htmlExpression = new HtmlExpression();
htmlExpression->setExpressionContext( expressionContext );

#ifdef QGISDEBUG
mWidget->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
#endif

auto frame = mWidget->page()->mainFrame();
connect( frame, &QWebFrame::javaScriptWindowObjectCleared, [ = ]
{
frame->addToJavaScriptWindowObject( QStringLiteral( "expression" ), htmlExpression );
} );

mWidget->setHtml( mHtmlCode );
}

void QgsHtmlWidgetWrapper::setFeature( const QgsFeature &feature )
{
if ( !mWidget )
return;

mFeature = feature;
setHtmlContext();
}

///@cond PRIVATE
void HtmlExpression::setExpressionContext( const QgsExpressionContext &context )
{
mExpressionContext = context;
}

QString HtmlExpression::evaluate( const QString &expression ) const
{
QgsExpression exp = QgsExpression( expression );
exp.prepare( &mExpressionContext );
return exp.evaluate( &mExpressionContext ).toString();
}
///@endcond
97 changes: 97 additions & 0 deletions src/gui/editorwidgets/qgshtmlwidgetwrapper.h
@@ -0,0 +1,97 @@
/***************************************************************************
qgshtmlwidgetwrapper.h
---------------------
begin : 23.03.2019
copyright : (C) 2019 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSHTMLWIDGETWRAPPER_H
#define QGSHTMLWIDGETWRAPPER_H

#include "qgswidgetwrapper.h"
#include "qgswebview.h"
#include "qgis_sip.h"
#include "qgis_gui.h"

/**
* \ingroup gui
* Wraps a QQuickWidget to display HTML code
* \since QGIS 3.4
*/
class GUI_EXPORT QgsHtmlWidgetWrapper : public QgsWidgetWrapper
{
Q_OBJECT

public:

/**
* Create a html widget wrapper
*
* \param layer The layer on which the feature is
* \param editor An editor widget. Can be NULLPTR if one should be autogenerated.
* \param parent A parent widget
*/
QgsHtmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent );

bool valid() const override;

QWidget *createWidget( QWidget *parent ) override;

void initWidget( QWidget *editor ) override;

//! Clears the content and makes new initialization
void reinitWidget();

//! Sets the HTML code to \a htmlCode
void setHtmlCode( const QString &htmlCode );

public slots:
void setFeature( const QgsFeature &feature ) override;

private slots:
//! sets the html context with the current values
void setHtmlContext();

private:
QString mHtmlCode;
QgsWebView *mWidget = nullptr;
QgsFeature mFeature;
};


#ifndef SIP_RUN
///@cond PRIVATE

/**
* \ingroup gui
* To pass the QgsExpression functionality and it's context to the context of the QWebView
* \since QGIS 3.10
*/
class HtmlExpression : public QObject
{
Q_OBJECT

public:
//! set the \a context of the expression
void setExpressionContext( const QgsExpressionContext &context );

public:

//! evaluates the value regarding the \a expression and the context
Q_INVOKABLE QString evaluate( const QString &expression ) const;

private:
QgsExpressionContext mExpressionContext;
};
///@endcond
#endif //SIP_RUN

#endif // QGSHTMLWIDGETWRAPPER_H
4 changes: 2 additions & 2 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -1847,10 +1847,10 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
const QgsAttributeEditorHtmlElement *elementDef = static_cast<const QgsAttributeEditorHtmlElement *>( widgetDef );

QgsHtmlWidgetWrapper *htmlWrapper = new QgsHtmlWidgetWrapper( mLayer, nullptr, this );
context.setAttributeFormMode( mMode );
htmlWrapper->setHtmlCode( elementDef->htmlCode() );
htmlWrapper->reinitWidget();
htmlWrapper->setConfig( mLayer->editFormConfig().widgetConfig( elementDef->name() ) );
context.setAttributeFormMode( mMode );
htmlWrapper->setContext( context );

mWidgets.append( htmlWrapper );

Expand Down

0 comments on commit b06103f

Please sign in to comment.