QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgshtmlwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshtmlwidgetwrapper.h
3 
4  ---------------------
5  begin : 23.03.2019
6  copyright : (C) 2019 by Alessandro Pasotti
7  email : elpaso at itopen dot it
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgshtmlwidgetwrapper.h"
18 #include "qgsmessagelog.h"
20 #include "qgsapplication.h"
21 #include "qgswebframe.h"
22 #include <QScreen>
23 
24 QgsHtmlWidgetWrapper::QgsHtmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
25  : QgsWidgetWrapper( layer, editor, parent )
26 {
27  connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsHtmlWidgetWrapper::setHtmlContext );
28 }
29 
31 {
32  return true;
33 }
34 
35 QWidget *QgsHtmlWidgetWrapper::createWidget( QWidget *parent )
36 {
37  return new QgsWebView( parent );
38 }
39 
40 void QgsHtmlWidgetWrapper::initWidget( QWidget *editor )
41 {
42  mWidget = qobject_cast<QgsWebView *>( editor );
43 
44  if ( !mWidget )
45  return;
46 
47  mWidget->setHtml( mHtmlCode );
48 #ifdef WITH_QTWEBKIT
49 
50 #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
51  const int horizontalDpi = qApp->desktop()->screen()->logicalDpiX();
52 #else
53  const int horizontalDpi = mWidget->logicalDpiX();
54 #endif
55 
56  mWidget->setZoomFactor( horizontalDpi / 96.0 );
57 
58  QWebPage *page = mWidget->page();
59  connect( page, &QWebPage::contentsChanged, this, &QgsHtmlWidgetWrapper::fixHeight, Qt::ConnectionType::UniqueConnection );
60  connect( page, &QWebPage::loadFinished, this, [ = ]( bool ) { fixHeight(); }, Qt::ConnectionType::UniqueConnection );
61 
62 #endif
63 }
64 
65 
67 {
68  if ( !mWidget )
69  return;
70 
71  initWidget( mWidget );
72 }
73 
74 void QgsHtmlWidgetWrapper::setHtmlCode( const QString &htmlCode )
75 {
76  mHtmlCode = htmlCode;
77 }
78 
79 void QgsHtmlWidgetWrapper::setHtmlContext( )
80 {
81  if ( !mWidget )
82  return;
83 
84  const QgsAttributeEditorContext attributecontext = context();
85  QgsExpressionContext expressionContext = layer()->createExpressionContext();
86  expressionContext << QgsExpressionContextUtils::formScope( mFeature, attributecontext.attributeFormModeString() );
87  if ( attributecontext.parentFormFeature().isValid() )
88  {
89  expressionContext << QgsExpressionContextUtils::parentFormScope( attributecontext.parentFormFeature() );
90  }
91  expressionContext.setFeature( mFeature );
92 
93  HtmlExpression *htmlExpression = new HtmlExpression();
94  htmlExpression->setExpressionContext( expressionContext );
95  mWidget->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
96  auto frame = mWidget->page()->mainFrame();
97  connect( frame, &QWebFrame::javaScriptWindowObjectCleared, frame, [ = ]
98  {
99  frame->addToJavaScriptWindowObject( QStringLiteral( "expression" ), htmlExpression );
100  } );
101 
102  mWidget->setHtml( mHtmlCode );
103 }
104 
105 #ifdef WITH_QTWEBKIT
106 void QgsHtmlWidgetWrapper::fixHeight()
107 {
108  QWebPage *page = mWidget->page();
109  int docHeight { page->mainFrame()->contentsSize().height() };
110  mWidget->setFixedHeight( docHeight );
111 }
112 #endif
113 
115 {
116  if ( !mWidget )
117  return;
118 
119  mFeature = feature;
120  setHtmlContext();
121 }
122 
124 void HtmlExpression::setExpressionContext( const QgsExpressionContext &context )
125 {
126  mExpressionContext = context;
127 }
128 
129 QString HtmlExpression::evaluate( const QString &expression ) const
130 {
131  QgsExpression exp = QgsExpression( expression );
132  exp.prepare( &mExpressionContext );
133  return exp.evaluate( &mExpressionContext ).toString();
134 }
The QWebPage class is a collection of stubs to mimic the API of a QWebPage on systems where QtWebkit ...
Definition: qgswebpage.h:104
This class contains context information for attribute editor widgets.
QString attributeFormModeString() const
Returns given attributeFormMode as string.
QgsFeature parentFormFeature() const
Returns the feature of the currently edited parent form in its actual state.
static QgsExpressionContextScope * parentFormScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current parent attribute form/tab...
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table form...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
QVariant evaluate()
Evaluate the feature and return the result.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:190
void reinitWidget()
Clears the content and makes new initialization.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QgsHtmlWidgetWrapper(QgsVectorLayer *layer, QWidget *editor, QWidget *parent)
Create a html widget wrapper.
void setHtmlCode(const QString &htmlCode)
Sets the HTML code to htmlCode.
bool valid() const override
Returns true if the widget has been properly initialized.
void setFeature(const QgsFeature &feature) override
Represents a vector layer which manages a vector based data sets.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
The QgsWebView class is a collection of stubs to mimic the API of QWebView on systems where the real ...
Definition: qgswebview.h:66
Manages an editor widget Widget and wrapper share the same parent.
void contextChanged()
Signal when QgsAttributeEditorContext mContext changed.
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.