QGIS API Documentation 3.38.0-Grenoble (exported)
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
19#include "qgsattributeform.h"
21#include <QScreen>
22
23QgsTextWidgetWrapper::QgsTextWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
24 : QgsWidgetWrapper( layer, editor, parent )
25{
26 connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsTextWidgetWrapper::updateTextContext );
27}
28
30{
31 return true;
32}
33
34QWidget *QgsTextWidgetWrapper::createWidget( QWidget *parent )
35{
36 QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
37
38 if ( form )
39 {
40 mFormFeature = form->feature();
41 connect( form, &QgsAttributeForm::widgetValueChanged, this, [ = ]( const QString & attribute, const QVariant & newValue, bool attributeChanged )
42 {
43 if ( attributeChanged )
44 {
45 if ( mRequiresFormScope )
46 {
47 mFormFeature.setAttribute( attribute, newValue );
48 updateTextContext();
49 }
50
51 }
52 } );
53 }
54 return new QLabel( parent );
55}
56
57void QgsTextWidgetWrapper::initWidget( QWidget *editor )
58{
59 mWidget = qobject_cast<QLabel *>( editor );
60
61 if ( !mWidget )
62 return;
63
64 mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
65 mWidget->setOpenExternalLinks( true );
66
67 const thread_local QRegularExpression sRegEx{ QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption };
68
69 mNeedsGeometry = false;
70 QRegularExpressionMatchIterator matchIt { sRegEx.globalMatch( mText ) };
71 while ( !mNeedsGeometry && matchIt.hasNext() )
72 {
73 const QRegularExpressionMatch match { matchIt.next() };
74 const QgsExpression exp { match.captured( 1 ) };
75 mNeedsGeometry = exp.needsGeometry();
76 }
77
78}
79
81{
82 if ( !mWidget )
83 return;
84
85 initWidget( mWidget );
86}
87
88
89void QgsTextWidgetWrapper::setText( const QString &text )
90{
91 mText = text;
92
93 bool ok = false;
94 const thread_local QRegularExpression sRegEx( QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption );
95 QRegularExpressionMatchIterator matchIt = sRegEx.globalMatch( mText );
96 while ( !ok && matchIt.hasNext() )
97 {
98 const QRegularExpressionMatch match = matchIt.next();
99 const QgsExpression exp = match.captured( 1 );
101 }
102 mRequiresFormScope = ok;
103
104 reinitWidget();
105}
106
108{
109 return mNeedsGeometry;
110}
111
112void QgsTextWidgetWrapper::updateTextContext( )
113{
114 if ( !mWidget )
115 return;
116
117 const QgsAttributeEditorContext attributeContext = context();
118 mTextContext = layer()->createExpressionContext();
119 mTextContext << QgsExpressionContextUtils::formScope( mFormFeature, attributeContext.attributeFormModeString() );
120 if ( attributeContext.parentFormFeature().isValid() )
121 {
122 mTextContext << QgsExpressionContextUtils::parentFormScope( attributeContext.parentFormFeature() );
123 }
124 mTextContext.setFeature( mFeature );
125 mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
126}
127
129{
130 if ( !mWidget )
131 return;
132
133 mFeature = feature;
134 mFormFeature = feature;
135
136 updateTextContext();
137}
138
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.
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
const QgsFeature & feature()
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...
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").
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...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
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 data sets.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
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.