Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
510 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" ); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
Oops, something went wrong.