QGIS API Documentation 3.41.0-Master (d5b93354e9c)
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 if ( attributeChanged )
44 {
45 if ( mRequiresFormScope )
46 {
47 mFormFeature.setAttribute( attribute, newValue );
48 updateTextContext();
49 }
50 }
51 } );
52 }
53 return new QLabel( parent );
54}
55
56void QgsTextWidgetWrapper::initWidget( QWidget *editor )
57{
58 mWidget = qobject_cast<QLabel *>( editor );
59
60 if ( !mWidget )
61 return;
62
63 mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
64 mWidget->setOpenExternalLinks( true );
65
66 const thread_local QRegularExpression sRegEx { QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption };
67
68 mNeedsGeometry = false;
69 QRegularExpressionMatchIterator matchIt { sRegEx.globalMatch( mText ) };
70 while ( !mNeedsGeometry && matchIt.hasNext() )
71 {
72 const QRegularExpressionMatch match { matchIt.next() };
73 const QgsExpression exp { match.captured( 1 ) };
74 mNeedsGeometry = exp.needsGeometry();
75 }
76}
77
79{
80 if ( !mWidget )
81 return;
82
83 initWidget( mWidget );
84}
85
86
87void QgsTextWidgetWrapper::setText( const QString &text )
88{
89 mText = text;
90
91 bool ok = false;
92 const thread_local QRegularExpression sRegEx( QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption );
93 QRegularExpressionMatchIterator matchIt = sRegEx.globalMatch( mText );
94 while ( !ok && matchIt.hasNext() )
95 {
96 const QRegularExpressionMatch match = matchIt.next();
97 const QgsExpression exp = match.captured( 1 );
99 }
100 mRequiresFormScope = ok;
101
102 reinitWidget();
103}
104
106{
107 return mNeedsGeometry;
108}
109
110void QgsTextWidgetWrapper::updateTextContext()
111{
112 if ( !mWidget )
113 return;
114
115 const QgsAttributeEditorContext attributeContext = context();
116 mTextContext = layer()->createExpressionContext();
117 mTextContext << QgsExpressionContextUtils::formScope( mFormFeature, attributeContext.attributeFormModeString() );
118 if ( attributeContext.parentFormFeature().isValid() )
119 {
120 mTextContext << QgsExpressionContextUtils::parentFormScope( attributeContext.parentFormFeature() );
121 }
122 mTextContext.setFeature( mFeature );
123 mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
124}
125
127{
128 if ( !mWidget )
129 return;
130
131 mFeature = feature;
132 mFormFeature = feature;
133
134 updateTextContext();
135}
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.