Skip to content

Commit

Permalink
New text widget
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Dec 28, 2022
1 parent 2712aed commit 908c95f
Show file tree
Hide file tree
Showing 13 changed files with 510 additions and 4 deletions.
Expand Up @@ -69,7 +69,8 @@ layer.
AeTypeInvalid,
AeTypeQmlElement,
AeTypeHtmlElement,
AeTypeAction
AeTypeAction,
AeTypeTextElement,
};

QgsAttributeEditorElement( AttributeEditorType type, const QString &name, QgsAttributeEditorElement *parent = 0 );
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -139,6 +139,7 @@ set(QGIS_CORE_SRCS
editform/qgsattributeeditorfield.cpp
editform/qgsattributeeditorrelation.cpp
editform/qgsattributeeditorhtmlelement.cpp
editform/qgsattributeeditortextelement.cpp
editform/qgsattributeeditorqmlelement.cpp

effects/qgsblureffect.cpp
Expand Down Expand Up @@ -1316,6 +1317,7 @@ set(QGIS_CORE_HDRS
editform/qgsattributeeditorfield.h
editform/qgsattributeeditorrelation.h
editform/qgsattributeeditorhtmlelement.h
editform/qgsattributeeditortextelement.h
editform/qgsattributeeditorqmlelement.h

elevation/qgsabstractprofilegenerator.h
Expand Down
5 changes: 5 additions & 0 deletions src/core/editform/qgsattributeeditorelement.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsattributeeditorhtmlelement.h"
#include "qgsattributeeditorqmlelement.h"
#include "qgsattributeeditorrelation.h"
#include "qgsattributeeditortextelement.h"
#include "qgssymbollayerutils.h"
#include "qgsfontutils.h"

Expand Down Expand Up @@ -85,6 +86,10 @@ QgsAttributeEditorElement *QgsAttributeEditorElement::create( const QDomElement
{
newElement = new QgsAttributeEditorHtmlElement( element.attribute( QStringLiteral( "name" ) ), parent );
}
else if ( element.tagName() == QLatin1String( "attributeEditorTextElement" ) )
{
newElement = new QgsAttributeEditorTextElement( element.attribute( QStringLiteral( "name" ) ), parent );
}
else if ( element.tagName() == QLatin1String( "attributeEditorAction" ) )
{
newElement = new QgsAttributeEditorAction( QUuid( element.attribute( QStringLiteral( "name" ) ) ), parent );
Expand Down
3 changes: 2 additions & 1 deletion src/core/editform/qgsattributeeditorelement.h
Expand Up @@ -108,7 +108,8 @@ class CORE_EXPORT QgsAttributeEditorElement SIP_ABSTRACT
AeTypeInvalid, //!< Invalid
AeTypeQmlElement, //!< A QML element
AeTypeHtmlElement, //!< A HTML element
AeTypeAction //!< A layer action element (since QGIS 3.22)
AeTypeAction, //!< A layer action element (since QGIS 3.22)
AeTypeTextElement, //!< A text element (since QGIS 3.30)
};

/**
Expand Down
55 changes: 55 additions & 0 deletions src/core/editform/qgsattributeeditortextelement.cpp
@@ -0,0 +1,55 @@
/***************************************************************************
qgsattributeeditortextelement.cpp - QgsAttributeEditorTextelement
---------------------
begin : 28.12.2022
copyright : (C) 2022 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsattributeeditortextelement.h"


QgsAttributeEditorElement *QgsAttributeEditorTextElement::clone( QgsAttributeEditorElement *parent ) const
{
QgsAttributeEditorTextElement *element = new QgsAttributeEditorTextElement( name(), parent );
element->setText( mText );

return element;
}

QString QgsAttributeEditorTextElement::text() const
{
return mText;
}

void QgsAttributeEditorTextElement::setText( const QString &text )
{
mText = text;
}

void QgsAttributeEditorTextElement::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
{
const QDomText textElem = doc.createTextNode( mText );
elem.appendChild( textElem );
}

void QgsAttributeEditorTextElement::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
{
Q_UNUSED( layerId )
Q_UNUSED( context )
Q_UNUSED( fields )
setText( element.text() );
}

QString QgsAttributeEditorTextElement::typeIdentifier() const
{
return QStringLiteral( "attributeEditorTextElement" );
}

64 changes: 64 additions & 0 deletions src/core/editform/qgsattributeeditortextelement.h
@@ -0,0 +1,64 @@
/***************************************************************************
qgsattributeeditortextelement.h - QgsAttributeEditorTextElement
---------------------
begin : 28.12.2022
copyright : (C) 2022 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSATTRIBUTEEDITORTEXTELEMENT_H
#define QGSATTRIBUTEEDITORTEXTELEMENT_H

#include "qgis_core.h"
#include "qgsattributeeditorelement.h"



/**
* \ingroup core
* \brief An attribute editor widget that will represent arbitrary text code.
*
* \since QGIS 3.30
*/
class CORE_EXPORT QgsAttributeEditorTextElement : public QgsAttributeEditorElement
{
public:

/**
* Creates a new element which can display text
*
* \param name The name of the widget
* \param parent The parent (used as container)
*/
QgsAttributeEditorTextElement( const QString &name, QgsAttributeEditorElement *parent )
: QgsAttributeEditorElement( AeTypeTextElement, name, parent )
{}

QgsAttributeEditorElement *clone( QgsAttributeEditorElement *parent ) const override SIP_FACTORY;

/**
* The Text that will be represented within this widget.
*/
QString text() const;

/**
* Sets the text that will be represented within this widget to \a text
*/
void setText( const QString &text );

private:
void saveConfiguration( QDomElement &elem, QDomDocument &doc ) const override;
void loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields ) override;
QString typeIdentifier() const override;
QString mText;
};


#endif // QGSATTRIBUTEEDITORTEXTELEMENT_H
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -214,6 +214,7 @@ set(QGIS_GUI_SRCS
editorwidgets/qgstexteditsearchwidgetwrapper.cpp
editorwidgets/qgstexteditwrapper.cpp
editorwidgets/qgstexteditwidgetfactory.cpp
editorwidgets/qgstextwidgetwrapper.cpp
editorwidgets/qgsuniquevaluesconfigdlg.cpp
editorwidgets/qgsuniquevaluewidgetwrapper.cpp
editorwidgets/qgsuniquevaluewidgetfactory.cpp
Expand Down Expand Up @@ -1084,6 +1085,7 @@ set(QGIS_GUI_HDRS
editorwidgets/qgstexteditsearchwidgetwrapper.h
editorwidgets/qgstexteditwidgetfactory.h
editorwidgets/qgstexteditwrapper.h
editorwidgets/qgstextwidgetwrapper.h
editorwidgets/qgsuniquevaluesconfigdlg.h
editorwidgets/qgsuniquevaluewidgetfactory.h
editorwidgets/qgsuniquevaluewidgetwrapper.h
Expand Down
2 changes: 2 additions & 0 deletions src/gui/attributeformconfig/qgsattributewidgetedit.cpp
Expand Up @@ -52,6 +52,7 @@ QgsAttributeWidgetEdit::QgsAttributeWidgetEdit( QTreeWidgetItem *item, QWidget *
case QgsAttributesFormProperties::DnDTreeItemData::Container:
case QgsAttributesFormProperties::DnDTreeItemData::QmlWidget:
case QgsAttributesFormProperties::DnDTreeItemData::HtmlWidget:
case QgsAttributesFormProperties::DnDTreeItemData::TextWidget:
case QgsAttributesFormProperties::DnDTreeItemData::WidgetType:
mWidgetSpecificConfigGroupBox->hide();
break;
Expand Down Expand Up @@ -85,6 +86,7 @@ void QgsAttributeWidgetEdit::updateItemData()
case QgsAttributesFormProperties::DnDTreeItemData::Container:
case QgsAttributesFormProperties::DnDTreeItemData::QmlWidget:
case QgsAttributesFormProperties::DnDTreeItemData::HtmlWidget:
case QgsAttributesFormProperties::DnDTreeItemData::TextWidget:
case QgsAttributesFormProperties::DnDTreeItemData::WidgetType:
break;
}
Expand Down
136 changes: 136 additions & 0 deletions src/gui/editorwidgets/qgstextwidgetwrapper.cpp
@@ -0,0 +1,136 @@
/***************************************************************************
qgstextwidgetwrapper.h
---------------------
begin : 28.12.2022
copyright : (C) 2022 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgstextwidgetwrapper.h"
#include "qgsexpressioncontextutils.h"
#include "qgsattributeform.h"
#include "qgsvaluerelationfieldformatter.h"
#include <QScreen>

QgsTextWidgetWrapper::QgsTextWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
: QgsWidgetWrapper( layer, editor, parent )
{
connect( this, &QgsWidgetWrapper::contextChanged, this, &QgsTextWidgetWrapper::updateTextContext );
}

bool QgsTextWidgetWrapper::valid() const
{
return true;
}

QWidget *QgsTextWidgetWrapper::createWidget( QWidget *parent )
{
QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );

if ( form )
{
mFormFeature = form->feature();
connect( form, &QgsAttributeForm::widgetValueChanged, this, [ = ]( const QString & attribute, const QVariant & newValue, bool attributeChanged )
{
if ( attributeChanged )
{
bool ok { false };
const thread_local QRegularExpression sRegEx{ QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption };
QRegularExpressionMatchIterator matchIt { sRegEx.globalMatch( mText ) };
while ( !ok && matchIt.hasNext() )
{
const QRegularExpressionMatch match { matchIt.next() };
const QgsExpression exp { match.captured( 1 ) };
ok = QgsValueRelationFieldFormatter::expressionRequiresFormScope( exp );
}

if ( ok )
{
mFormFeature.setAttribute( attribute, newValue );
updateTextContext();
}

}
} );
}
return new QLabel( parent );
}

void QgsTextWidgetWrapper::initWidget( QWidget *editor )
{
mWidget = qobject_cast<QLabel *>( editor );

if ( !mWidget )
return;

mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
mWidget->setOpenExternalLinks( true );

const thread_local QRegularExpression sRegEx{ QStringLiteral( "\\[%(.*?)%\\]" ), QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption };

mNeedsGeometry = false;
QRegularExpressionMatchIterator matchIt { sRegEx.globalMatch( mText ) };
while ( !mNeedsGeometry && matchIt.hasNext() )
{
const QRegularExpressionMatch match { matchIt.next() };
const QgsExpression exp { match.captured( 1 ) };
mNeedsGeometry = exp.needsGeometry();
}

}

void QgsTextWidgetWrapper::reinitWidget( )
{
if ( !mWidget )
return;

initWidget( mWidget );
}


void QgsTextWidgetWrapper::setText( const QString &text )
{
mText = text;
reinitWidget();
}

bool QgsTextWidgetWrapper::needsGeometry() const
{
return mNeedsGeometry;
}

void QgsTextWidgetWrapper::updateTextContext( )
{
if ( !mWidget )
return;

const QgsAttributeEditorContext attributeContext = context();
mTextContext = layer()->createExpressionContext();
mTextContext << QgsExpressionContextUtils::formScope( mFormFeature, attributeContext.attributeFormModeString() );
if ( attributeContext.parentFormFeature().isValid() )
{
mTextContext << QgsExpressionContextUtils::parentFormScope( attributeContext.parentFormFeature() );
}
mTextContext.setFeature( mFeature );
mWidget->setText( QgsExpression::replaceExpressionText( mText, &mTextContext ) );
}

void QgsTextWidgetWrapper::setFeature( const QgsFeature &feature )
{
if ( !mWidget )
return;

mFeature = feature;
mFormFeature = feature;

updateTextContext();
}

0 comments on commit 908c95f

Please sign in to comment.