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 )
168 if ( defVal.isNull() )
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;
240 void 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 );
279 void QgsTextEditWrapper::textChanged(
const QString & )
284 mLineEdit->setPlaceholderText( mPlaceholderText );
288 void 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( );
341 const bool changed { val != currentValue || val.isNull() != currentValue.isNull() };
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 );