27#include <nlohmann/json.hpp>
41 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
43 if ( mTextEdit->toPlainText().isEmpty() )
49 v = mTextEdit->toHtml();
54 v = mTextEdit->toPlainText();
60 v = mPlainTextEdit->toPlainText();
65 v = mLineEdit->text();
68 if ( ( v.isEmpty() && (
field().type() == QMetaType::Type::Int
69 ||
field().type() == QMetaType::Type::Double
70 ||
field().type() == QMetaType::Type::LongLong
71 ||
field().type() == QMetaType::Type::QDate ) )
84 if (
field().type() != QMetaType::Type::QVariantMap &&
field().convertCompatible( res ) )
88 else if (
field().type() == QMetaType::Type::QString &&
field().length() > 0 )
92 return QVariant( v.left(
field().length() ) );
94 else if (
field().type() == QMetaType::Type::QVariantMap )
100 mInvalidJSON =
false;
103 if ( json::accept( v.toStdString() ) )
106 mInvalidJSON =
false;
112 if ( v.length() > 0 )
118 mInvalidJSON =
false;
131 mForm = qobject_cast<QgsAttributeForm *>( parent );
132 if (
config( QStringLiteral(
"IsMultiline" ) ).toBool() )
134 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
136 return new QTextBrowser( parent );
140 return new QPlainTextEdit( parent );
151 mInvalidJSON =
false;
152 mTextBrowser = qobject_cast<QTextBrowser *>( editor );
153 mTextEdit = qobject_cast<QTextEdit *>( editor );
154 mPlainTextEdit = qobject_cast<QPlainTextEdit *>( editor );
155 mLineEdit = qobject_cast<QLineEdit *>( editor );
160 if ( mPlainTextEdit )
174 if (
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate )
176 mPlaceholderText = defVal.toString();
177 mLineEdit->setPlaceholderText( mPlaceholderText );
184 connect( mLineEdit, &QLineEdit::textChanged,
this, [ = ](
const QString &
value )
191 connect( mLineEdit, &QLineEdit::textChanged,
this, &QgsTextEditWrapper::textChanged );
197 return mLineEdit || mTextEdit || mPlainTextEdit;
203 mTextEdit->blockSignals(
true );
204 if ( mPlainTextEdit )
205 mPlainTextEdit->blockSignals(
true );
208 mLineEdit->blockSignals(
true );
211 mLineEdit->setPlaceholderText( QString() );
215 setWidgetValue( QLatin1String(
"" ) );
218 mTextEdit->blockSignals(
false );
219 if ( mPlainTextEdit )
220 mPlainTextEdit->blockSignals(
false );
222 mLineEdit->blockSignals(
false );
229 mForm->
displayWarning( tr(
"Your JSON was invalid and has been reverted back to the last valid edit or the original data" ) );
231 mInvalidJSON =
false;
237void QgsTextEditWrapper::updateValues(
const QVariant &val,
const QVariantList & )
242 mLineEdit->setPlaceholderText( mPlaceholderText );
244 setWidgetValue( val );
250 mTextEdit->setReadOnly( !enabled );
252 if ( mPlainTextEdit )
253 mPlainTextEdit->setReadOnly( !enabled );
257 mLineEdit->setReadOnly( !enabled );
258 mLineEdit->setFrame( enabled );
267void QgsTextEditWrapper::textChanged(
const QString & )
272 mLineEdit->setPlaceholderText( mPlaceholderText );
276void QgsTextEditWrapper::setWidgetValue(
const QVariant &val )
281 if ( !(
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate ) )
284 else if (
field().type() == QMetaType::Type::QVariantMap )
288 if (
field().displayString( val ).isEmpty() )
290 if ( val.userType() == QMetaType::Type::QString && val.toString() != QLatin1String(
"\"\"" ) )
292 v = val.toString().append(
"\"" ).insert( 0,
"\"" );
304 else if ( val.userType() == QMetaType::Type::Double && std::isnan( val.toDouble() ) )
319 bool canConvertToDouble;
320 QLocale().toDouble( v, &canConvertToDouble );
321 if ( canConvertToDouble &&
layer() &&
layer()->isEditable() && ! QLocale().groupSeparator().isNull() &&
field().isNumeric() )
323 v = v.remove( QLocale().groupSeparator() );
326 const QVariant currentValue =
value( );
335 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
337 mTextEdit->setHtml( v );
340 mTextBrowser->setTextInteractionFlags( Qt::LinksAccessibleByMouse );
341 mTextBrowser->setOpenExternalLinks(
true );
346 mTextEdit->setPlainText( v );
349 else if ( mPlainTextEdit )
351 mPlainTextEdit->setPlainText( v );
353 else if ( mLineEdit )
355 mLineEdit->setText( v );
362 if ( hintText.isNull() )
363 mPlaceholderText = mPlaceholderTextBackup;
366 mPlaceholderTextBackup = mPlaceholderText;
367 mPlaceholderText = hintText;
371 mLineEdit->setPlaceholderText( mPlaceholderText );
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
QString displayString(const QVariant &v) const
Formats string for display.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
static QVariant parseJson(const std::string &jsonString)
Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and ...
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
void setHint(const QString &hintText) override
Add a hint text on the widget.
QgsTextEditWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsTextEditWrapper.
void setFeature(const QgsFeature &feature) override
bool isInvalidJSON()
Returns whether the text edit widget contains Invalid JSON.
void setEnabled(bool enabled) override
bool valid() const override
Returns true if the widget has been properly initialized.
QVariant value() const override
Will be used to access the widget's value.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based data sets.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH