QGIS API Documentation  3.0.2-Girona (307d082)
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 
22 #include <QSettings>
23 
24 QgsTextEditWrapper::QgsTextEditWrapper( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent )
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26 
27 {
28 }
29 
30 QVariant QgsTextEditWrapper::value() const
31 {
32  QString v;
33 
34  if ( mTextEdit )
35  {
36  if ( config( QStringLiteral( "UseHtml" ) ).toBool() )
37  {
38  v = mTextEdit->toHtml();
39  }
40  else
41  {
42  v = mTextEdit->toPlainText();
43  }
44  }
45 
46  if ( mPlainTextEdit )
47  {
48  v = mPlainTextEdit->toPlainText();
49  }
50 
51  if ( mLineEdit )
52  {
53  v = mLineEdit->text();
54  }
55 
56  if ( ( v.isEmpty() && ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) ) ||
58  return QVariant( field().type() );
59 
60  if ( !defaultValue().isNull() && v == defaultValue().toString() )
61  {
62  return defaultValue();
63  }
64 
65  QVariant res( v );
66  if ( field().convertCompatible( res ) )
67  {
68  return res;
69  }
70  else if ( field().type() == QVariant::String && field().length() > 0 )
71  {
72  // for string fields convertCompatible may return false due to field length limit - in this case just truncate
73  // input rather then discarding it entirely
74  return QVariant( v.left( field().length() ) );
75  }
76  else
77  {
78  return QVariant( field().type() );
79  }
80 }
81 
82 QWidget *QgsTextEditWrapper::createWidget( QWidget *parent )
83 {
84  if ( config( QStringLiteral( "IsMultiline" ) ).toBool() )
85  {
86  if ( config( QStringLiteral( "UseHtml" ) ).toBool() )
87  {
88  return new QTextEdit( parent );
89  }
90  else
91  {
92  return new QPlainTextEdit( parent );
93  }
94  }
95  else
96  {
97  return new QgsFilterLineEdit( parent );
98  }
99 }
100 
101 void QgsTextEditWrapper::initWidget( QWidget *editor )
102 {
103  mTextEdit = qobject_cast<QTextEdit *>( editor );
104  mPlainTextEdit = qobject_cast<QPlainTextEdit *>( editor );
105  mLineEdit = qobject_cast<QLineEdit *>( editor );
106 
107  if ( mTextEdit )
108  connect( mTextEdit, &QTextEdit::textChanged, this, &QgsEditorWidgetWrapper::emitValueChanged );
109 
110  if ( mPlainTextEdit )
111  connect( mPlainTextEdit, &QPlainTextEdit::textChanged, this, &QgsEditorWidgetWrapper::emitValueChanged );
112 
113  if ( mLineEdit )
114  {
115  mLineEdit->setValidator( new QgsFieldValidator( mLineEdit, field(), defaultValue().toString() ) );
116 
117  QVariant defVal = defaultValue();
118  if ( defVal.isNull() )
119  {
121  }
122 
123  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( mLineEdit );
124  if ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date )
125  {
126  mPlaceholderText = defVal.toString();
127  mLineEdit->setPlaceholderText( mPlaceholderText );
128  }
129  else if ( fle )
130  {
131  fle->setNullValue( defVal.toString() );
132  }
133 
134  connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value ) { emit valueChanged( value ); } );
135  connect( mLineEdit, &QLineEdit::textChanged, this, &QgsTextEditWrapper::textChanged );
136 
137  mWritablePalette = mLineEdit->palette();
138  mReadOnlyPalette = mLineEdit->palette();
139  mReadOnlyPalette.setColor( QPalette::Text, mWritablePalette.color( QPalette::Disabled, QPalette::Text ) );
140  }
141 }
142 
144 {
145  return mLineEdit || mTextEdit || mPlainTextEdit;
146 }
147 
149 {
150  //note - this is deliberately a zero length string, not a null string!
151  if ( mTextEdit )
152  mTextEdit->blockSignals( true );
153  if ( mPlainTextEdit )
154  mPlainTextEdit->blockSignals( true );
155  if ( mLineEdit )
156  {
157  mLineEdit->blockSignals( true );
158  // for interdeminate state we need to clear the placeholder text - we want an empty line edit, not
159  // one showing the default value (e.g., "NULL")
160  mLineEdit->setPlaceholderText( QString() );
161  }
162 
163  setWidgetValue( QLatin1String( "" ) );
164 
165  if ( mTextEdit )
166  mTextEdit->blockSignals( false );
167  if ( mPlainTextEdit )
168  mPlainTextEdit->blockSignals( false );
169  if ( mLineEdit )
170  mLineEdit->blockSignals( false );
171 }
172 
173 void QgsTextEditWrapper::setValue( const QVariant &val )
174 {
175  if ( mLineEdit )
176  {
177  //restore placeholder text, which may have been removed by showIndeterminateState()
178  mLineEdit->setPlaceholderText( mPlaceholderText );
179  }
180  setWidgetValue( val );
181 }
182 
183 void QgsTextEditWrapper::setEnabled( bool enabled )
184 {
185  if ( mTextEdit )
186  mTextEdit->setReadOnly( !enabled );
187 
188  if ( mPlainTextEdit )
189  mPlainTextEdit->setReadOnly( !enabled );
190 
191  if ( mLineEdit )
192  {
193  mLineEdit->setReadOnly( !enabled );
194  if ( enabled )
195  mLineEdit->setPalette( mWritablePalette );
196  else
197  mLineEdit->setPalette( mReadOnlyPalette );
198  }
199 }
200 
201 void QgsTextEditWrapper::textChanged( const QString & )
202 {
203  if ( mLineEdit )
204  {
205  //restore placeholder text, which may have been removed by showIndeterminateState()
206  mLineEdit->setPlaceholderText( mPlaceholderText );
207  }
208 }
209 
210 void QgsTextEditWrapper::setWidgetValue( const QVariant &val )
211 {
212  QString v;
213  if ( val.isNull() )
214  {
215  if ( !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
217  }
218  else
219  v = val.toString();
220 
221  if ( mTextEdit )
222  {
223  if ( val != value() )
224  {
225  if ( config( QStringLiteral( "UseHtml" ) ).toBool() )
226  mTextEdit->setHtml( v );
227  else
228  mTextEdit->setPlainText( v );
229  }
230  }
231 
232  if ( mPlainTextEdit )
233  {
234  if ( val != value() )
235  mPlainTextEdit->setPlainText( v );
236  }
237 
238  if ( mLineEdit )
239  mLineEdit->setText( v );
240 }
241 
242 void QgsTextEditWrapper::setHint( const QString &hintText )
243 {
244  if ( hintText.isNull() )
245  mPlaceholderText = mPlaceholderTextBackup;
246  else
247  {
248  mPlaceholderTextBackup = mPlaceholderText;
249  mPlaceholderText = hintText;
250  }
251 
252  mLineEdit->setPlaceholderText( mPlaceholderText );
253 }
void setEnabled(bool enabled) override
void emitValueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
Manages an editor widget Widget and wrapper share the same parent.
QVariantMap config() const
Returns the whole config.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
QVariant value() const override
Will be used to access the widget&#39;s value.
QVariant defaultValue() const
Access the default value of the field.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
QgsTextEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
bool valid() const override
Return true if the widget has been properly initialized.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setValue(const QVariant &value) override
void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
Represents a vector layer which manages a vector based data sets.
QVariant::Type type
Definition: qgsfield.h:55
void setHint(const QString &hintText) override
Add a hint text on the widget.