18#include <nlohmann/json.hpp>
30#include "moc_qgstexteditwrapper.cpp"
44 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
46 if ( mTextEdit->toPlainText().isEmpty() )
52 v = mTextEdit->toHtml();
57 v = mTextEdit->toPlainText();
63 v = mPlainTextEdit->toPlainText();
68 v = mLineEdit->text();
71 if ( ( v.isEmpty() && (
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
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, [
this](
const QString &
value ) {
190 connect( mLineEdit, &QLineEdit::textChanged,
this, &QgsTextEditWrapper::textChanged );
196 return mLineEdit || mTextEdit || mPlainTextEdit;
202 mTextEdit->blockSignals(
true );
203 if ( mPlainTextEdit )
204 mPlainTextEdit->blockSignals(
true );
207 mLineEdit->blockSignals(
true );
210 mLineEdit->setPlaceholderText( QString() );
214 setWidgetValue( QLatin1String(
"" ) );
217 mTextEdit->blockSignals(
false );
218 if ( mPlainTextEdit )
219 mPlainTextEdit->blockSignals(
false );
221 mLineEdit->blockSignals(
false );
228 mForm->displayWarning( tr(
"Your JSON was invalid and has been reverted back to the last valid edit or the original data" ) );
230 mInvalidJSON =
false;
236void QgsTextEditWrapper::updateValues(
const QVariant &val,
const QVariantList & )
241 mLineEdit->setPlaceholderText( mPlaceholderText );
243 setWidgetValue( val );
249 mTextEdit->setReadOnly( !enabled );
251 if ( mPlainTextEdit )
252 mPlainTextEdit->setReadOnly( !enabled );
256 mLineEdit->setReadOnly( !enabled );
257 mLineEdit->setFrame( enabled );
266void QgsTextEditWrapper::textChanged(
const QString & )
271 mLineEdit->setPlaceholderText( mPlaceholderText );
275void QgsTextEditWrapper::setWidgetValue(
const QVariant &val )
280 if ( !(
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate ) )
283 else if (
field().type() == QMetaType::Type::QVariantMap )
287 if (
field().displayString( val ).isEmpty() )
289 if ( val.userType() == QMetaType::Type::QString && val.toString() != QLatin1String(
"\"\"" ) )
291 v = val.toString().append(
"\"" ).insert( 0,
"\"" );
303 else if ( val.userType() == QMetaType::Type::Double && std::isnan( val.toDouble() ) )
307 else if ( val.userType() == qMetaTypeId<QgsUnsetAttributeValue>() )
322 bool canConvertToDouble;
323 QLocale().toDouble( v, &canConvertToDouble );
324 if ( canConvertToDouble &&
layer() &&
layer()->isEditable() && !QLocale().groupSeparator().isNull() &&
field().isNumeric() )
326 v = v.remove( QLocale().groupSeparator() );
329 const QVariant currentValue =
value();
338 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
340 mTextEdit->setHtml( v );
343 mTextBrowser->setTextInteractionFlags( Qt::LinksAccessibleByMouse );
344 mTextBrowser->setOpenExternalLinks(
true );
349 mTextEdit->setPlainText( v );
352 else if ( mPlainTextEdit )
354 mPlainTextEdit->setPlainText( v );
356 else if ( mLineEdit )
358 mLineEdit->setText( v );
365 if ( hintText.isNull() )
366 mPlaceholderText = mPlaceholderTextBackup;
369 mPlaceholderTextBackup = mPlaceholderText;
370 mPlaceholderText = hintText;
374 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.
A QValidator for validation against a QgsField's constraints and field type.
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
void setEnabled(bool enabled) override
bool isInvalidJSON() const
Returns whether the text edit widget contains Invalid JSON.
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 default, "not-specified" value for a feature attribute.
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 dataset.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH