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() == QVariant::Int
69 ||
field().type() == QVariant::Double
70 ||
field().type() == QVariant::LongLong
71 ||
field().type() == QVariant::Date ) )
74 return QVariant(
field().type() );
84 if (
field().type() != QVariant::Map &&
field().convertCompatible( res ) )
88 else if (
field().type() == QVariant::String &&
field().length() > 0 )
92 return QVariant( v.left(
field().length() ) );
94 else if (
field().type() == QVariant::Map )
100 mInvalidJSON =
false;
103 if ( json::accept( v.toUtf8() ) )
106 mInvalidJSON =
false;
112 if ( v.length() > 0 )
118 mInvalidJSON =
false;
125 return QVariant(
field().type() );
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() == QVariant::Int ||
field().type() == QVariant::Double ||
field().type() == QVariant::LongLong ||
field().type() == QVariant::Date )
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 );
193 mWritablePalette = mLineEdit->palette();
194 mReadOnlyPalette = mLineEdit->palette();
200 return mLineEdit || mTextEdit || mPlainTextEdit;
206 mTextEdit->blockSignals(
true );
207 if ( mPlainTextEdit )
208 mPlainTextEdit->blockSignals(
true );
211 mLineEdit->blockSignals(
true );
214 mLineEdit->setPlaceholderText( QString() );
218 setWidgetValue( QLatin1String(
"" ) );
221 mTextEdit->blockSignals(
false );
222 if ( mPlainTextEdit )
223 mPlainTextEdit->blockSignals(
false );
225 mLineEdit->blockSignals(
false );
232 mForm->
displayWarning( tr(
"Your JSON was invalid and has been reverted back to the last valid edit or the original data" ) );
234 mInvalidJSON =
false;
240void QgsTextEditWrapper::updateValues(
const QVariant &val,
const QVariantList & )
245 mLineEdit->setPlaceholderText( mPlaceholderText );
247 setWidgetValue( val );
253 mTextEdit->setReadOnly( !enabled );
255 if ( mPlainTextEdit )
256 mPlainTextEdit->setReadOnly( !enabled );
260 mLineEdit->setReadOnly( !enabled );
262 mLineEdit->setPalette( mWritablePalette );
265 mLineEdit->setPalette( mReadOnlyPalette );
268 mLineEdit->setStyleSheet( QStringLiteral(
"QLineEdit { background-color: rgba(255, 255, 255, 75%); }" ) );
270 mLineEdit->setFrame( enabled );
279void QgsTextEditWrapper::textChanged(
const QString & )
284 mLineEdit->setPlaceholderText( mPlaceholderText );
288void QgsTextEditWrapper::setWidgetValue(
const QVariant &val )
293 if ( !(
field().type() == QVariant::Int ||
field().type() == QVariant::Double ||
field().type() == QVariant::LongLong ||
field().type() == QVariant::Date ) )
296 else if (
field().type() == QVariant::Map )
300 if (
field().displayString( val ).isEmpty() )
302 if ( val.type() == QVariant::String && val.toString() != QLatin1String(
"\"\"" ) )
304 v = val.toString().append(
"\"" ).insert( 0,
"\"" );
316 else if ( val.type() == QVariant::Double && std::isnan( val.toDouble() ) )
331 bool canConvertToDouble;
332 QLocale().toDouble( v, &canConvertToDouble );
333 if ( canConvertToDouble &&
layer() &&
layer()->isEditable() && ! QLocale().groupSeparator().isNull() &&
field().isNumeric() )
335 v = v.remove( QLocale().groupSeparator() );
338 const QVariant currentValue =
value( );
347 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
349 mTextEdit->setHtml( v );
352 mTextBrowser->setTextInteractionFlags( Qt::LinksAccessibleByMouse );
353 mTextBrowser->setOpenExternalLinks(
true );
358 mTextEdit->setPlainText( v );
361 else if ( mPlainTextEdit )
363 mPlainTextEdit->setPlainText( v );
365 else if ( mLineEdit )
367 mLineEdit->setText( v );
374 if ( hintText.isNull() )
375 mPlaceholderText = mPlaceholderTextBackup;
378 mPlaceholderTextBackup = mPlaceholderText;
379 mPlaceholderText = hintText;
383 mLineEdit->setPlaceholderText( mPlaceholderText );
static QString nullRepresentation()
This string is 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...
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)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based data sets.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH