27 #include <nlohmann/json.hpp>
41 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
43 v = mTextEdit->toHtml();
47 v = mTextEdit->toPlainText();
53 v = mPlainTextEdit->toPlainText();
58 v = mLineEdit->text();
61 if ( ( v.isEmpty() && (
field().type() == QVariant::Int
62 ||
field().type() == QVariant::Double
63 ||
field().type() == QVariant::LongLong
64 ||
field().type() == QVariant::Date ) )
67 return QVariant(
field().type() );
77 if (
field().type() != QVariant::Map &&
field().convertCompatible( res ) )
81 else if (
field().type() == QVariant::String &&
field().length() > 0 )
85 return QVariant( v.left(
field().length() ) );
87 else if (
field().type() == QVariant::Map )
96 if ( json::accept( v.toUtf8() ) )
105 if ( v.length() > 0 )
111 mInvalidJSON =
false;
118 return QVariant(
field().type() );
124 mForm = qobject_cast<QgsAttributeForm *>( parent );
125 if (
config( QStringLiteral(
"IsMultiline" ) ).toBool() )
127 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
129 return new QTextBrowser( parent );
133 return new QPlainTextEdit( parent );
144 mInvalidJSON =
false;
145 mTextBrowser = qobject_cast<QTextBrowser *>( editor );
146 mTextEdit = qobject_cast<QTextEdit *>( editor );
147 mPlainTextEdit = qobject_cast<QPlainTextEdit *>( editor );
148 mLineEdit = qobject_cast<QLineEdit *>( editor );
153 if ( mPlainTextEdit )
161 if ( defVal.isNull() )
167 if (
field().type() == QVariant::Int ||
field().type() == QVariant::Double ||
field().type() == QVariant::LongLong ||
field().type() == QVariant::Date )
169 mPlaceholderText = defVal.toString();
170 mLineEdit->setPlaceholderText( mPlaceholderText );
177 connect( mLineEdit, &QLineEdit::textChanged,
this, [ = ](
const QString &
value )
184 connect( mLineEdit, &QLineEdit::textChanged,
this, &QgsTextEditWrapper::textChanged );
186 mWritablePalette = mLineEdit->palette();
187 mReadOnlyPalette = mLineEdit->palette();
193 return mLineEdit || mTextEdit || mPlainTextEdit;
200 mTextEdit->blockSignals(
true );
201 if ( mPlainTextEdit )
202 mPlainTextEdit->blockSignals(
true );
205 mLineEdit->blockSignals(
true );
208 mLineEdit->setPlaceholderText( QString() );
211 setWidgetValue( QString() );
214 mTextEdit->blockSignals(
false );
215 if ( mPlainTextEdit )
216 mPlainTextEdit->blockSignals(
false );
218 mLineEdit->blockSignals(
false );
225 mForm->
displayWarning( tr(
"Your JSON was invalid and has been reverted back to the last valid edit or the original data" ) );
227 mInvalidJSON =
false;
233 void QgsTextEditWrapper::updateValues(
const QVariant &val,
const QVariantList & )
238 mLineEdit->setPlaceholderText( mPlaceholderText );
240 setWidgetValue( val );
246 mTextEdit->setReadOnly( !enabled );
248 if ( mPlainTextEdit )
249 mPlainTextEdit->setReadOnly( !enabled );
253 mLineEdit->setReadOnly( !enabled );
255 mLineEdit->setPalette( mWritablePalette );
258 mLineEdit->setPalette( mReadOnlyPalette );
261 mLineEdit->setStyleSheet( QStringLiteral(
"QLineEdit { background-color: rgba(255, 255, 255, 75%); }" ) );
263 mLineEdit->setFrame( enabled );
272 void QgsTextEditWrapper::textChanged(
const QString & )
277 mLineEdit->setPlaceholderText( mPlaceholderText );
281 void QgsTextEditWrapper::setWidgetValue(
const QVariant &val )
286 if ( !(
field().type() == QVariant::Int ||
field().type() == QVariant::Double ||
field().type() == QVariant::LongLong ||
field().type() == QVariant::Date ) )
289 else if (
field().type() == QVariant::Map )
293 if (
field().displayString( val ).isEmpty() )
295 if ( val.type() == QVariant::String && val.toString() != QLatin1String(
"\"\"" ) )
297 v = val.toString().append(
"\"" ).insert( 0,
"\"" );
309 else if ( val.type() == QVariant::Double && std::isnan( val.toDouble() ) )
324 bool canConvertToDouble;
325 QLocale().toDouble( v, &canConvertToDouble );
326 if ( canConvertToDouble &&
layer() &&
layer()->isEditable() && ! QLocale().groupSeparator().isNull() &&
field().isNumeric() )
328 v = v.remove( QLocale().groupSeparator() );
331 const QVariant currentValue {
value( ) };
334 const bool changed { val != currentValue || val.isNull() != currentValue.isNull() };
340 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
342 mTextEdit->setHtml( v );
345 mTextBrowser->setTextInteractionFlags( Qt::LinksAccessibleByMouse );
346 mTextBrowser->setOpenExternalLinks(
true );
351 mTextEdit->setPlainText( v );
354 else if ( mPlainTextEdit )
356 mPlainTextEdit->setPlainText( v );
358 else if ( mLineEdit )
360 mLineEdit->setText( v );
367 if ( hintText.isNull() )
368 mPlaceholderText = mPlaceholderTextBackup;
371 mPlaceholderTextBackup = mPlaceholderText;
372 mPlaceholderText = hintText;
375 mLineEdit->setPlaceholderText( mPlaceholderText );