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 ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate ) )
82 if (
field().type() != QMetaType::Type::QVariantMap &&
field().convertCompatible( res ) )
86 else if (
field().type() == QMetaType::Type::QString &&
field().length() > 0 )
90 return QVariant( v.left(
field().length() ) );
92 else if (
field().type() == QMetaType::Type::QVariantMap )
101 if ( json::accept( v.toStdString() ) )
104 mInvalidJSON =
false;
110 if ( v.length() > 0 )
116 mInvalidJSON =
false;
129 mForm = qobject_cast<QgsAttributeForm *>( parent );
130 if (
config( QStringLiteral(
"IsMultiline" ) ).toBool() )
132 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
134 return new QTextBrowser( parent );
138 return new QPlainTextEdit( parent );
149 mInvalidJSON =
false;
150 mTextBrowser = qobject_cast<QTextBrowser *>( editor );
151 mTextEdit = qobject_cast<QTextEdit *>( editor );
152 mPlainTextEdit = qobject_cast<QPlainTextEdit *>( editor );
153 mLineEdit = qobject_cast<QLineEdit *>( editor );
158 if ( mPlainTextEdit )
172 if (
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate )
174 mPlaceholderText = defVal.toString();
175 mLineEdit->setPlaceholderText( mPlaceholderText );
182 connect( mLineEdit, &QLineEdit::textChanged,
this, [=](
const QString &
value ) {
188 connect( mLineEdit, &QLineEdit::textChanged,
this, &QgsTextEditWrapper::textChanged );
194 return mLineEdit || mTextEdit || mPlainTextEdit;
200 mTextEdit->blockSignals(
true );
201 if ( mPlainTextEdit )
202 mPlainTextEdit->blockSignals(
true );
205 mLineEdit->blockSignals(
true );
208 mLineEdit->setPlaceholderText( QString() );
212 setWidgetValue( QLatin1String(
"" ) );
215 mTextEdit->blockSignals(
false );
216 if ( mPlainTextEdit )
217 mPlainTextEdit->blockSignals(
false );
219 mLineEdit->blockSignals(
false );
226 mForm->
displayWarning( tr(
"Your JSON was invalid and has been reverted back to the last valid edit or the original data" ) );
228 mInvalidJSON =
false;
234void QgsTextEditWrapper::updateValues(
const QVariant &val,
const QVariantList & )
239 mLineEdit->setPlaceholderText( mPlaceholderText );
241 setWidgetValue( val );
247 mTextEdit->setReadOnly( !enabled );
249 if ( mPlainTextEdit )
250 mPlainTextEdit->setReadOnly( !enabled );
254 mLineEdit->setReadOnly( !enabled );
255 mLineEdit->setFrame( enabled );
264void QgsTextEditWrapper::textChanged(
const QString & )
269 mLineEdit->setPlaceholderText( mPlaceholderText );
273void QgsTextEditWrapper::setWidgetValue(
const QVariant &val )
278 if ( !(
field().type() == QMetaType::Type::Int ||
field().type() == QMetaType::Type::Double ||
field().type() == QMetaType::Type::LongLong ||
field().type() == QMetaType::Type::QDate ) )
281 else if (
field().type() == QMetaType::Type::QVariantMap )
285 if (
field().displayString( val ).isEmpty() )
287 if ( val.userType() == QMetaType::Type::QString && val.toString() != QLatin1String(
"\"\"" ) )
289 v = val.toString().append(
"\"" ).insert( 0,
"\"" );
301 else if ( val.userType() == QMetaType::Type::Double && std::isnan( val.toDouble() ) )
316 bool canConvertToDouble;
317 QLocale().toDouble( v, &canConvertToDouble );
318 if ( canConvertToDouble &&
layer() &&
layer()->isEditable() && !QLocale().groupSeparator().isNull() &&
field().isNumeric() )
320 v = v.remove( QLocale().groupSeparator() );
323 const QVariant currentValue =
value();
332 if (
config( QStringLiteral(
"UseHtml" ) ).toBool() )
334 mTextEdit->setHtml( v );
337 mTextBrowser->setTextInteractionFlags( Qt::LinksAccessibleByMouse );
338 mTextBrowser->setOpenExternalLinks(
true );
343 mTextEdit->setPlainText( v );
346 else if ( mPlainTextEdit )
348 mPlainTextEdit->setPlainText( v );
350 else if ( mLineEdit )
352 mLineEdit->setText( v );
359 if ( hintText.isNull() )
360 mPlaceholderText = mPlaceholderTextBackup;
363 mPlaceholderTextBackup = mPlaceholderText;
364 mPlaceholderText = hintText;
368 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