QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgstexteditwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstexteditwrapper.cpp
3 --------------------------------------
4 Date : 5.1.2014
5 Copyright : (C) 2014 Matthias Kuhn
6 Email : matthias at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgstexteditwrapper.h"
17
18#include "qgsfields.h"
19#include "qgsfieldvalidator.h"
20#include "qgsfilterlineedit.h"
21#include "qgsapplication.h"
22#include "qgsjsonutils.h"
23#include "qgsmessagebar.h"
24#include "qgslogger.h"
25
26#include <QSettings>
27#include <nlohmann/json.hpp>
28
29QgsTextEditWrapper::QgsTextEditWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
30 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
31
32{
33}
34
36{
37 QString v;
38
39 if ( mTextEdit )
40 {
41 if ( config( QStringLiteral( "UseHtml" ) ).toBool() )
42 {
43 if ( mTextEdit->toPlainText().isEmpty() )
44 {
45 v = QString();
46 }
47 else
48 {
49 v = mTextEdit->toHtml();
50 }
51 }
52 else
53 {
54 v = mTextEdit->toPlainText();
55 }
56 }
57
58 if ( mPlainTextEdit )
59 {
60 v = mPlainTextEdit->toPlainText();
61 }
62
63 if ( mLineEdit )
64 {
65 v = mLineEdit->text();
66 }
67
68 if ( ( v.isEmpty() && ( field().type() == QVariant::Int
69 || field().type() == QVariant::Double
70 || field().type() == QVariant::LongLong
71 || field().type() == QVariant::Date ) )
73 {
74 return QVariant( field().type() );
75 }
76
77 if ( !QgsVariantUtils::isNull( defaultValue() ) && v == defaultValue().toString() )
78 {
79 return defaultValue();
80 }
81
82 QVariant res( v );
83 // treat VariantMap fields including JSON differently
84 if ( field().type() != QVariant::Map && field().convertCompatible( res ) )
85 {
86 return res;
87 }
88 else if ( field().type() == QVariant::String && field().length() > 0 )
89 {
90 // for string fields convertCompatible may return false due to field length limit - in this case just truncate
91 // input rather then discarding it entirely
92 return QVariant( v.left( field().length() ) );
93 }
94 else if ( field().type() == QVariant::Map )
95 {
96 // replace empty string (invalid) with quoted empty string
97 if ( v.isEmpty() )
98 {
99 QVariant qjson = QgsJsonUtils::parseJson( std::string( "\"\"" ) );
100 mInvalidJSON = false;
101 return qjson;
102 }
103 if ( json::accept( v.toStdString() ) )
104 {
105 QVariant qjson = QgsJsonUtils::parseJson( v.toStdString() );
106 mInvalidJSON = false;
107 return qjson;
108 }
109 else
110 // return null value if json is invalid
111 {
112 if ( v.length() > 0 )
113 {
114 mInvalidJSON = true;
115 }
116 else
117 {
118 mInvalidJSON = false;
119 }
120 return QVariant();
121 }
122 }
123 else
124 {
125 return QVariant( field().type() );
126 }
127}
128
129QWidget *QgsTextEditWrapper::createWidget( QWidget *parent )
130{
131 mForm = qobject_cast<QgsAttributeForm *>( parent );
132 if ( config( QStringLiteral( "IsMultiline" ) ).toBool() )
133 {
134 if ( config( QStringLiteral( "UseHtml" ) ).toBool() )
135 {
136 return new QTextBrowser( parent );
137 }
138 else
139 {
140 return new QPlainTextEdit( parent );
141 }
142 }
143 else
144 {
145 return new QgsFilterLineEdit( parent );
146 }
147}
148
149void QgsTextEditWrapper::initWidget( QWidget *editor )
150{
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 );
156
157 if ( mTextEdit )
158 connect( mTextEdit, &QTextEdit::textChanged, this, &QgsEditorWidgetWrapper::emitValueChanged );
159
160 if ( mPlainTextEdit )
161 connect( mPlainTextEdit, &QPlainTextEdit::textChanged, this, &QgsEditorWidgetWrapper::emitValueChanged );
162
163 if ( mLineEdit )
164 {
165 mLineEdit->setValidator( new QgsFieldValidator( mLineEdit, field(), defaultValue().toString() ) );
166
167 QVariant defVal = defaultValue();
168 if ( QgsVariantUtils::isNull( defVal ) )
169 {
171 }
172
173 QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( mLineEdit );
174 if ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date )
175 {
176 mPlaceholderText = defVal.toString();
177 mLineEdit->setPlaceholderText( mPlaceholderText );
178 }
179 else if ( fle )
180 {
181 fle->setNullValue( defVal.toString() );
182 }
183
184 connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value )
185 {
187 emit valueChanged( value );
189 emit valuesChanged( value );
190 } );
191 connect( mLineEdit, &QLineEdit::textChanged, this, &QgsTextEditWrapper::textChanged );
192 }
193}
194
196{
197 return mLineEdit || mTextEdit || mPlainTextEdit;
198}
199
201{
202 if ( mTextEdit )
203 mTextEdit->blockSignals( true );
204 if ( mPlainTextEdit )
205 mPlainTextEdit->blockSignals( true );
206 if ( mLineEdit )
207 {
208 mLineEdit->blockSignals( true );
209 // for indeterminate state we need to clear the placeholder text - we want an empty line edit, not
210 // one showing the default value (e.g., "NULL")
211 mLineEdit->setPlaceholderText( QString() );
212 }
213
214 //note - this is deliberately a zero length string, not a null string!
215 setWidgetValue( QLatin1String( "" ) ); // skip-keyword-check
216
217 if ( mTextEdit )
218 mTextEdit->blockSignals( false );
219 if ( mPlainTextEdit )
220 mPlainTextEdit->blockSignals( false );
221 if ( mLineEdit )
222 mLineEdit->blockSignals( false );
223}
224
226{
227 // Do nothing if the value has not changed
228 if ( mInvalidJSON )
229 mForm->displayWarning( tr( "Your JSON was invalid and has been reverted back to the last valid edit or the original data" ) );
230 {
231 mInvalidJSON = false;
232 }
233 setFormFeature( feature );
234 setValue( feature.attribute( fieldIdx() ) );
235}
236
237void QgsTextEditWrapper::updateValues( const QVariant &val, const QVariantList & )
238{
239 if ( mLineEdit )
240 {
241 //restore placeholder text, which may have been removed by showIndeterminateState()
242 mLineEdit->setPlaceholderText( mPlaceholderText );
243 }
244 setWidgetValue( val );
245}
246
248{
249 if ( mTextEdit )
250 mTextEdit->setReadOnly( !enabled );
251
252 if ( mPlainTextEdit )
253 mPlainTextEdit->setReadOnly( !enabled );
254
255 if ( mLineEdit )
256 {
257 mLineEdit->setReadOnly( !enabled );
258 mLineEdit->setFrame( enabled );
259 }
260}
261
263{
264 return mInvalidJSON;
265}
266
267void QgsTextEditWrapper::textChanged( const QString & )
268{
269 if ( mLineEdit )
270 {
271 //restore placeholder text, which may have been removed by showIndeterminateState()
272 mLineEdit->setPlaceholderText( mPlaceholderText );
273 }
274}
275
276void QgsTextEditWrapper::setWidgetValue( const QVariant &val )
277{
278 QString v;
279 if ( QgsVariantUtils::isNull( val ) )
280 {
281 if ( !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
283 }
284 else if ( field().type() == QVariant::Map )
285 {
286 // this has to be overridden for json which has only values (i.e. no objects or arrays), as qgsfield.cpp displayString()
287 // uses QJsonDocument which doesn't recognise this as valid JSON although it technically is
288 if ( field().displayString( val ).isEmpty() )
289 {
290 if ( val.type() == QVariant::String && val.toString() != QLatin1String( "\"\"" ) )
291 {
292 v = val.toString().append( "\"" ).insert( 0, "\"" );
293 }
294 else
295 {
296 v = val.toString();
297 }
298 }
299 else
300 {
301 v = field().displayString( val );
302 }
303 }
304 else if ( val.type() == QVariant::Double && std::isnan( val.toDouble() ) )
305 {
307 }
308 else
309 {
310 v = field().displayString( val );
311 }
312 // For numbers, remove the group separator that might cause validation errors
313 // when the user is editing the field value.
314 // We are checking for editable layer because in the form field context we do not
315 // want to strip the separator unless the layer is editable.
316 // Also check that we have something like a number in the value to avoid
317 // stripping out dots from nextval when we have a schema: see https://github.com/qgis/QGIS/issues/28021
318 // "Wrong sequence detection with Postgres"
319 bool canConvertToDouble;
320 QLocale().toDouble( v, &canConvertToDouble );
321 if ( canConvertToDouble && layer() && layer()->isEditable() && ! QLocale().groupSeparator().isNull() && field().isNumeric() )
322 {
323 v = v.remove( QLocale().groupSeparator() );
324 }
325
326 const QVariant currentValue = value( );
327 // Note: comparing QVariants leads to funny (and wrong) results:
328 // QVariant(0.0) == QVariant(QVariant.Double) -> True
329 const bool changed { val != currentValue || QgsVariantUtils::isNull( val ) != QgsVariantUtils::isNull( currentValue ) };
330
331 if ( changed )
332 {
333 if ( mTextEdit )
334 {
335 if ( config( QStringLiteral( "UseHtml" ) ).toBool() )
336 {
337 mTextEdit->setHtml( v );
338 if ( mTextBrowser )
339 {
340 mTextBrowser->setTextInteractionFlags( Qt::LinksAccessibleByMouse );
341 mTextBrowser->setOpenExternalLinks( true );
342 }
343 }
344 else
345 {
346 mTextEdit->setPlainText( v );
347 }
348 }
349 else if ( mPlainTextEdit )
350 {
351 mPlainTextEdit->setPlainText( v );
352 }
353 else if ( mLineEdit )
354 {
355 mLineEdit->setText( v );
356 }
357 }
358}
359
360void QgsTextEditWrapper::setHint( const QString &hintText )
361{
362 if ( hintText.isNull() )
363 mPlaceholderText = mPlaceholderTextBackup;
364 else
365 {
366 mPlaceholderTextBackup = mPlaceholderText;
367 mPlaceholderText = hintText;
368 }
369
370 if ( mLineEdit )
371 mLineEdit->setPlaceholderText( mPlaceholderText );
372}
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
void displayWarning(const QString &message)
Displays a warning message in the form message bar.
Manages an editor widget Widget and wrapper share the same parent.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
void setFormFeature(const QgsFeature &feature)
Set the feature currently being edited to feature.
int fieldIdx() const
Access the field index.
QVariant defaultValue() const
Access the default value of the field.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
void emitValueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
virtual void setValue(const QVariant &value)
Is called when the value of the widget needs to be changed.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
QString displayString(const QVariant &v) const
Formats string for display.
Definition qgsfield.cpp:296
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)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based data sets.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QVariantMap config() const
Returns the whole config.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:4916
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:4915