-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also fixes context evaluation before html is updated
- Loading branch information
Showing
4 changed files
with
285 additions
and
2 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
python/gui/auto_generated/editorwidgets/qgshtmlwidgetwrapper.sip.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters