18#include <nlohmann/json.hpp>
31#include "moc_qgstexteditwrapper.cpp"
33using namespace Qt::StringLiterals;
46 if (
config( u
"UseHtml"_s ).toBool() )
48 if ( mTextEdit->toPlainText().isEmpty() )
54 v = mTextEdit->toHtml();
59 v = mTextEdit->toPlainText();
65 v = mPlainTextEdit->toPlainText();
70 v = mLineEdit->text();
73 if ( ( v.isEmpty() && (
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate ) )
86 if (
field().type() != QMetaType::Type::QVariantMap &&
field().convertCompatible( res ) )
90 else if (
field().type() == QMetaType::Type::QString &&
field().length() > 0 )
94 return QVariant( v.left(
field().length() ) );
96 else if (
field().type() == QMetaType::Type::QVariantMap )
102 mInvalidJSON =
false;
105 if ( json::accept( v.toStdString() ) )
108 mInvalidJSON =
false;
114 if ( v.length() > 0 )
120 mInvalidJSON =
false;
133 mForm = qobject_cast<QgsAttributeForm *>( parent );
134 if (
config( u
"IsMultiline"_s ).toBool() )
136 if (
config( u
"UseHtml"_s ).toBool() )
138 return new QTextBrowser( parent );
142 return new QPlainTextEdit( parent );
153 mInvalidJSON =
false;
154 mTextBrowser = qobject_cast<QTextBrowser *>( editor );
155 mTextEdit = qobject_cast<QTextEdit *>( editor );
156 mPlainTextEdit = qobject_cast<QPlainTextEdit *>( editor );
157 mLineEdit = qobject_cast<QLineEdit *>( editor );
162 if ( mPlainTextEdit )
176 if (
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate )
178 mPlaceholderText = defVal.toString();
179 mLineEdit->setPlaceholderText( mPlaceholderText );
186 connect( mLineEdit, &QLineEdit::textChanged,
this, [
this](
const QString &
value ) {
192 connect( mLineEdit, &QLineEdit::textChanged,
this, &QgsTextEditWrapper::textChanged );
198 return mLineEdit || mTextEdit || mPlainTextEdit;
204 mTextEdit->blockSignals(
true );
205 if ( mPlainTextEdit )
206 mPlainTextEdit->blockSignals(
true );
209 mLineEdit->blockSignals(
true );
212 mLineEdit->setPlaceholderText( QString() );
216 setWidgetValue( QLatin1String(
"" ) );
219 mTextEdit->blockSignals(
false );
220 if ( mPlainTextEdit )
221 mPlainTextEdit->blockSignals(
false );
223 mLineEdit->blockSignals(
false );
230 mForm->displayWarning( tr(
"Your JSON was invalid and has been reverted back to the last valid edit or the original data" ) );
232 mInvalidJSON =
false;
238void QgsTextEditWrapper::updateValues(
const QVariant &val,
const QVariantList & )
243 mLineEdit->setPlaceholderText( mPlaceholderText );
245 setWidgetValue( val );
251 mTextEdit->setReadOnly( !enabled );
253 if ( mPlainTextEdit )
254 mPlainTextEdit->setReadOnly( !enabled );
258 mLineEdit->setReadOnly( !enabled );
259 mLineEdit->setFrame( enabled );
268void QgsTextEditWrapper::textChanged(
const QString & )
273 mLineEdit->setPlaceholderText( mPlaceholderText );
277void QgsTextEditWrapper::setWidgetValue(
const QVariant &val )
282 if ( !(
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate ) )
285 else if (
field().type() == QMetaType::Type::QVariantMap )
289 if (
field().displayString( val ).isEmpty() )
291 if ( val.userType() == QMetaType::Type::QString && val.toString() !=
"\"\""_L1 )
293 v = val.toString().append(
"\"" ).insert( 0,
"\"" );
305 else if ( val.userType() == QMetaType::Type::Double && std::isnan( val.toDouble() ) )
309 else if ( val.userType() == qMetaTypeId<QgsUnsetAttributeValue>() )
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();
340 if (
config( u
"UseHtml"_s ).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;
376 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