QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgstextwidgetwrapper.cpp"
20#include "qgsattributeform.h"
22#include <QScreen>
23
24QgsTextWidgetWrapper::QgsTextWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
25 : QgsWidgetWrapper( layer, editor, parent )
26{
27 connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsTextWidgetWrapper::updateTextContext );
28}
29
31{
32 return true;
33}
34
35QWidget *QgsTextWidgetWrapper::createWidget( QWidget *parent )
36{
37 QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
38
39 if ( form )
40 {
41 mFormFeature = form->feature();
42 connect( form, &QgsAttributeForm::widgetValueChanged, this, [ = ]( const QString & attribute, const QVariant & newValue, bool attributeChanged )
43 {
44 if ( attributeChanged )
45 {
46 if ( mRequiresFormScope )
47 {
48 mFormFeature.setAttribute( attribute, newValue );
49 updateTextContext();
50 }
51
52 }
53 } );
54 }
55 return new QLabel( parent );
56}
57
58void QgsTextWidgetWrapper::initWidget( QWidget *editor )
59{
60 mWidget = qobject_cast<QLabel *>( editor );
61
62 if ( !mWidget )
63 return;
64
65 mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
66 mWidget->setOpenExternalLinks( true );
67
68 const thread_local QRegularExpression sRegEx{ QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption };
69
70 mNeedsGeometry = false;
71 QRegularExpressionMatchIterator matchIt { sRegEx.globalMatch( mText ) };
72 while ( !mNeedsGeometry && matchIt.hasNext() )
73 {
74 const QRegularExpressionMatch match { matchIt.next() };
75 const QgsExpression exp { match.captured( 1 ) };
76 mNeedsGeometry = exp.needsGeometry();
77 }
78
79}
80
82{
83 if ( !mWidget )
84 return;
85
86 initWidget( mWidget );
87}
88
89
90void QgsTextWidgetWrapper::setText( const QString &text )
91{
92 mText = text;
93
94 bool ok = false;
95 const thread_local QRegularExpression sRegEx( QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption );
96 QRegularExpressionMatchIterator matchIt = sRegEx.globalMatch( mText );
97 while ( !ok && matchIt.hasNext() )
98 {
99 const QRegularExpressionMatch match = matchIt.next();
100 const QgsExpression exp = match.captured( 1 );
102 }
103 mRequiresFormScope = ok;
104
105 reinitWidget();
106}
107
109{
110 return mNeedsGeometry;
111}
112
113void QgsTextWidgetWrapper::updateTextContext( )
114{
115 if ( !mWidget )
116 return;
117
118 const QgsAttributeEditorContext attributeContext = context();
119 mTextContext = layer()->createExpressionContext();
120 mTextContext << QgsExpressionContextUtils::formScope( mFormFeature, attributeContext.attributeFormModeString() );
121 if ( attributeContext.parentFormFeature().isValid() )
122 {
123 mTextContext << QgsExpressionContextUtils::parentFormScope( attributeContext.parentFormFeature() );
124 }
125 mTextContext.setFeature( mFeature );
126 mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
127}
128
130{
131 if ( !mWidget )
132 return;
133
134 mFeature = feature;
135 mFormFeature = feature;
136
137 updateTextContext();
138}
139
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.