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;
199 mTextEdit->blockSignals(
true );
200 if ( mPlainTextEdit )
201 mPlainTextEdit->blockSignals(
true );
204 mLineEdit->blockSignals(
true );
207 mLineEdit->setPlaceholderText( QString() );
211 setWidgetValue( QStringLiteral(
"" ) );
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 );
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.
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.
Represents a vector layer which manages a vector based data sets.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH