17#include "moc_qgstexteditwrapper.cpp"
28#include <nlohmann/json.hpp>
42 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
44 if ( mTextEdit->toPlainText().isEmpty() )
50 v = mTextEdit->toHtml();
55 v = mTextEdit->toPlainText();
61 v = mPlainTextEdit->toPlainText();
66 v = mLineEdit->text();
69 if ( ( v.isEmpty() && (
field().type() == QMetaType::Type::Int
70 ||
field().type() == QMetaType::Type::Double
71 ||
field().type() == QMetaType::Type::LongLong
72 ||
field().type() == QMetaType::Type::QDate ) )
85 if (
field().type() != QMetaType::Type::QVariantMap &&
field().convertCompatible( res ) )
89 else if (
field().type() == QMetaType::Type::QString &&
field().length() > 0 )
93 return QVariant( v.left(
field().length() ) );
95 else if (
field().type() == QMetaType::Type::QVariantMap )
101 mInvalidJSON =
false;
104 if ( json::accept( v.toStdString() ) )
107 mInvalidJSON =
false;
113 if ( v.length() > 0 )
119 mInvalidJSON =
false;
132 mForm = qobject_cast<QgsAttributeForm *>( parent );
133 if (
config( QStringLiteral(
"IsMultiline" ) ).toBool() )
135 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
137 return new QTextBrowser( parent );
141 return new QPlainTextEdit( parent );
152 mInvalidJSON =
false;
153 mTextBrowser = qobject_cast<QTextBrowser *>( editor );
154 mTextEdit = qobject_cast<QTextEdit *>( editor );
155 mPlainTextEdit = qobject_cast<QPlainTextEdit *>( editor );
156 mLineEdit = qobject_cast<QLineEdit *>( editor );
161 if ( mPlainTextEdit )
175 if (
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate )
177 mPlaceholderText = defVal.toString();
178 mLineEdit->setPlaceholderText( mPlaceholderText );
185 connect( mLineEdit, &QLineEdit::textChanged,
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() != QLatin1String(
"\"\"" ) )
293 v = val.toString().append(
"\"" ).insert( 0,
"\"" );
305 else if ( val.userType() == QMetaType::Type::Double && std::isnan( val.toDouble() ) )
320 bool canConvertToDouble;
321 QLocale().toDouble( v, &canConvertToDouble );
322 if ( canConvertToDouble &&
layer() &&
layer()->isEditable() && ! QLocale().groupSeparator().isNull() &&
field().isNumeric() )
324 v = v.remove( QLocale().groupSeparator() );
327 const QVariant currentValue =
value( );
336 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
338 mTextEdit->setHtml( v );
341 mTextBrowser->setTextInteractionFlags( Qt::LinksAccessibleByMouse );
342 mTextBrowser->setOpenExternalLinks(
true );
347 mTextEdit->setPlainText( v );
350 else if ( mPlainTextEdit )
352 mPlainTextEdit->setPlainText( v );
354 else if ( mLineEdit )
356 mLineEdit->setText( v );
363 if ( hintText.isNull() )
364 mPlaceholderText = mPlaceholderTextBackup;
367 mPlaceholderTextBackup = mPlaceholderText;
368 mPlaceholderText = hintText;
372 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.
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
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.
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 data sets.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH