18#include <nlohmann/json.hpp>
31#include "moc_qgstexteditwrapper.cpp"
33using namespace Qt::StringLiterals;
47 if (
config( u
"UseHtml"_s ).toBool() )
49 if ( mTextEdit->toPlainText().isEmpty() )
55 v = mTextEdit->toHtml();
60 v = mTextEdit->toPlainText();
66 v = mPlainTextEdit->toPlainText();
71 v = mLineEdit->text();
74 if ( ( v.isEmpty() && (
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate ) )
87 if (
field().type() != QMetaType::Type::QVariantMap &&
field().convertCompatible( res ) )
91 else if (
field().type() == QMetaType::Type::QString &&
field().length() > 0 )
95 return QVariant( v.left(
field().length() ) );
97 else if (
field().type() == QMetaType::Type::QVariantMap )
103 mInvalidJSON =
false;
106 if ( json::accept( v.toStdString() ) )
109 mInvalidJSON =
false;
115 if ( v.length() > 0 )
121 mInvalidJSON =
false;
134 mForm = qobject_cast<QgsAttributeForm *>( parent );
135 if (
config( u
"IsMultiline"_s ).toBool() )
137 if (
config( u
"UseHtml"_s ).toBool() )
139 return new QTextBrowser( parent );
143 return new QPlainTextEdit( parent );
154 mInvalidJSON =
false;
155 mTextBrowser = qobject_cast<QTextBrowser *>( editor );
156 mTextEdit = qobject_cast<QTextEdit *>( editor );
157 mPlainTextEdit = qobject_cast<QPlainTextEdit *>( editor );
158 mLineEdit = qobject_cast<QLineEdit *>( editor );
163 if ( mPlainTextEdit )
177 if (
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate )
179 mPlaceholderText = defVal.toString();
180 mLineEdit->setPlaceholderText( mPlaceholderText );
187 connect( mLineEdit, &QLineEdit::textChanged,
this, [
this](
const QString &
value ) {
193 connect( mLineEdit, &QLineEdit::textChanged,
this, &QgsTextEditWrapper::textChanged );
199 return mLineEdit || mTextEdit || mPlainTextEdit;
205 mTextEdit->blockSignals(
true );
206 if ( mPlainTextEdit )
207 mPlainTextEdit->blockSignals(
true );
210 mLineEdit->blockSignals(
true );
213 mLineEdit->setPlaceholderText( QString() );
217 setWidgetValue( QLatin1String(
"" ) );
220 mTextEdit->blockSignals(
false );
221 if ( mPlainTextEdit )
222 mPlainTextEdit->blockSignals(
false );
224 mLineEdit->blockSignals(
false );
231 mForm->displayWarning( tr(
"Your JSON was invalid and has been reverted back to the last valid edit or the original data" ) );
233 mInvalidJSON =
false;
239void QgsTextEditWrapper::updateValues(
const QVariant &val,
const QVariantList & )
244 mLineEdit->setPlaceholderText( mPlaceholderText );
246 setWidgetValue( val );
252 mTextEdit->setReadOnly( !enabled );
254 if ( mPlainTextEdit )
255 mPlainTextEdit->setReadOnly( !enabled );
259 mLineEdit->setReadOnly( !enabled );
260 mLineEdit->setFrame( enabled );
269void QgsTextEditWrapper::textChanged(
const QString & )
274 mLineEdit->setPlaceholderText( mPlaceholderText );
278void QgsTextEditWrapper::setWidgetValue(
const QVariant &val )
283 if ( !(
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate ) )
286 else if (
field().type() == QMetaType::Type::QVariantMap )
290 if (
field().displayString( val ).isEmpty() )
292 if ( val.userType() == QMetaType::Type::QString && val.toString() !=
"\"\""_L1 )
294 v = val.toString().append(
"\"" ).insert( 0,
"\"" );
306 else if ( val.userType() == QMetaType::Type::Double && std::isnan( val.toDouble() ) )
310 else if ( val.userType() == qMetaTypeId<QgsUnsetAttributeValue>() )
325 bool canConvertToDouble;
326 QLocale().toDouble( v, &canConvertToDouble );
327 if ( canConvertToDouble &&
layer() &&
layer()->isEditable() && !QLocale().groupSeparator().isNull() &&
field().isNumeric() )
329 v = v.remove( QLocale().groupSeparator() );
332 const QVariant currentValue =
value();
341 if (
config( u
"UseHtml"_s ).toBool() )
343 mTextEdit->setHtml( v );
346 mTextBrowser->setTextInteractionFlags( Qt::LinksAccessibleByMouse );
347 mTextBrowser->setOpenExternalLinks(
true );
352 mTextEdit->setPlainText( v );
355 else if ( mPlainTextEdit )
357 mPlainTextEdit->setPlainText( v );
359 else if ( mLineEdit )
361 mLineEdit->setText( v );
368 if ( hintText.isNull() )
369 mPlaceholderText = mPlaceholderTextBackup;
372 mPlaceholderTextBackup = mPlaceholderText;
373 mPlaceholderText = hintText;
377 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