QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgstextwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstextwidgetwrapper.h
3
4 ---------------------
5 begin : 28.12.2022
6 copyright : (C) 2022 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
18
19#include "qgsattributeform.h"
22
23#include <QScreen>
24
25#include "moc_qgstextwidgetwrapper.cpp"
26
28 : QgsWidgetWrapper( layer, editor, parent )
29{
30 connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsTextWidgetWrapper::updateTextContext );
31}
32
34{
35 return true;
36}
37
38QWidget *QgsTextWidgetWrapper::createWidget( QWidget *parent )
39{
40 QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
41
42 if ( form )
43 {
44 mFormFeature = form->feature();
45 connect( form, &QgsAttributeForm::widgetValueChanged, this, [this]( const QString &attribute, const QVariant &newValue, bool attributeChanged ) {
46 if ( attributeChanged )
47 {
48 if ( mRequiresFormScope )
49 {
50 mFormFeature.setAttribute( attribute, newValue );
51 updateTextContext();
52 }
53 }
54 } );
55 }
56 QLabel *widget = new QLabel( parent );
57 widget->setWordWrap( widget );
58 return widget;
59}
60
61void QgsTextWidgetWrapper::initWidget( QWidget *editor )
62{
63 mWidget = qobject_cast<QLabel *>( editor );
64
65 if ( !mWidget )
66 return;
67
68 mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
69 mWidget->setOpenExternalLinks( true );
70
71 const thread_local QRegularExpression sRegEx { QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption };
72
73 mNeedsGeometry = false;
74 QRegularExpressionMatchIterator matchIt { sRegEx.globalMatch( mText ) };
75 while ( !mNeedsGeometry && matchIt.hasNext() )
76 {
77 const QRegularExpressionMatch match { matchIt.next() };
78 const QgsExpression exp { match.captured( 1 ) };
79 mNeedsGeometry = exp.needsGeometry();
80 }
81}
82
84{
85 if ( !mWidget )
86 return;
87
88 initWidget( mWidget );
89}
90
91
92void QgsTextWidgetWrapper::setText( const QString &text )
93{
94 mText = text;
95
96 bool ok = false;
97 const thread_local QRegularExpression sRegEx( QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption );
98 QRegularExpressionMatchIterator matchIt = sRegEx.globalMatch( mText );
99 while ( !ok && matchIt.hasNext() )
100 {
101 const QRegularExpressionMatch match = matchIt.next();
102 const QgsExpression exp = match.captured( 1 );
104 }
105 mRequiresFormScope = ok;
106
107 reinitWidget();
108}
109
111{
112 return mNeedsGeometry;
113}
114
115void QgsTextWidgetWrapper::updateTextContext()
116{
117 if ( !mWidget )
118 return;
119
120 const QgsAttributeEditorContext attributeContext = context();
121 mTextContext = layer()->createExpressionContext();
122 mTextContext << QgsExpressionContextUtils::formScope( mFormFeature, attributeContext.attributeFormModeString() );
123 if ( attributeContext.parentFormFeature().isValid() )
124 {
125 mTextContext << QgsExpressionContextUtils::parentFormScope( attributeContext.parentFormFeature() );
126 }
127 mTextContext.setFeature( mFeature );
128 mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
129}
130
132{
133 if ( !mWidget )
134 return;
135
136 mFeature = feature;
137 mFormFeature = feature;
138
139 updateTextContext();
140}
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.
The attribute form widget for vector layer features.
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
const QgsFeature & feature() const
Returns feature of attribute form.
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...
Handles parsing and evaluation of expressions (formerly called "search strings").
static QString replaceExpressionText(const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr)
This function replaces each expression between [% and %] in the string with the result of its evaluat...
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
bool isValid() const
Returns the validity of this feature.
bool needsGeometry() const
Returns true if the widget needs feature geometry.
bool valid() const override
Returns true if the widget has been properly initialized.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setText(const QString &text)
Sets the text code to htmlCode.
QgsTextWidgetWrapper(QgsVectorLayer *layer, QWidget *editor, QWidget *parent)
Create a text widget wrapper.
void reinitWidget()
Clears the content and makes new initialization.
void setFeature(const QgsFeature &feature) override
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
static bool expressionRequiresFormScope(const QString &expression)
Check if the expression requires a form scope (i.e.
Represents a vector layer which manages a vector based dataset.
QgsExpressionContext createExpressionContext() const final
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QWidget * widget()
Access the widget managed by this wrapper.
void contextChanged()
Signal when QgsAttributeEditorContext mContext changed.
QgsWidgetWrapper(QgsVectorLayer *vl, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
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.