QGIS API Documentation  3.2.0-Bonn (bc43194)
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 QTextBrowser( 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  mTextBrowser = qobject_cast<QTextBrowser *>( editor );
104  mTextEdit = qobject_cast<QTextEdit *>( editor );
105  mPlainTextEdit = qobject_cast<QPlainTextEdit *>( editor );
106  mLineEdit = qobject_cast<QLineEdit *>( editor );
107 
108  if ( mTextEdit )
109  connect( mTextEdit, &QTextEdit::textChanged, this, &QgsEditorWidgetWrapper::emitValueChanged );
110 
111  if ( mPlainTextEdit )
112  connect( mPlainTextEdit, &QPlainTextEdit::textChanged, this, &QgsEditorWidgetWrapper::emitValueChanged );
113 
114  if ( mLineEdit )
115  {
116  mLineEdit->setValidator( new QgsFieldValidator( mLineEdit, field(), defaultValue().toString() ) );
117 
118  QVariant defVal = defaultValue();
119  if ( defVal.isNull() )
120  {
122  }
123 
124  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( mLineEdit );
125  if ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date )
126  {
127  mPlaceholderText = defVal.toString();
128  mLineEdit->setPlaceholderText( mPlaceholderText );
129  }
130  else if ( fle )
131  {
132  fle->setNullValue( defVal.toString() );
133  }
134 
135  connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value ) { emit valueChanged( value ); } );
136  connect( mLineEdit, &QLineEdit::textChanged, this, &QgsTextEditWrapper::textChanged );
137 
138  mWritablePalette = mLineEdit->palette();
139  mReadOnlyPalette = mLineEdit->palette();
140  mReadOnlyPalette.setColor( QPalette::Text, mWritablePalette.color( QPalette::Disabled, QPalette::Text ) );
141  }
142 }
143 
145 {
146  return mLineEdit || mTextEdit || mPlainTextEdit;
147 }
148 
150 {
151  //note - this is deliberately a zero length string, not a null string!
152  if ( mTextEdit )
153  mTextEdit->blockSignals( true );
154  if ( mPlainTextEdit )
155  mPlainTextEdit->blockSignals( true );
156  if ( mLineEdit )
157  {
158  mLineEdit->blockSignals( true );
159  // for interdeminate state we need to clear the placeholder text - we want an empty line edit, not
160  // one showing the default value (e.g., "NULL")
161  mLineEdit->setPlaceholderText( QString() );
162  }
163 
164  setWidgetValue( QLatin1String( "" ) );
165 
166  if ( mTextEdit )
167  mTextEdit->blockSignals( false );
168  if ( mPlainTextEdit )
169  mPlainTextEdit->blockSignals( false );
170  if ( mLineEdit )
171  mLineEdit->blockSignals( false );
172 }
173 
174 void QgsTextEditWrapper::setValue( const QVariant &val )
175 {
176  if ( mLineEdit )
177  {
178  //restore placeholder text, which may have been removed by showIndeterminateState()
179  mLineEdit->setPlaceholderText( mPlaceholderText );
180  }
181  setWidgetValue( val );
182 }
183 
184 void QgsTextEditWrapper::setEnabled( bool enabled )
185 {
186  if ( mTextEdit )
187  mTextEdit->setReadOnly( !enabled );
188 
189  if ( mPlainTextEdit )
190  mPlainTextEdit->setReadOnly( !enabled );
191 
192  if ( mLineEdit )
193  {
194  mLineEdit->setReadOnly( !enabled );
195  if ( enabled )
196  mLineEdit->setPalette( mWritablePalette );
197  else
198  mLineEdit->setPalette( mReadOnlyPalette );
199  }
200 }
201 
202 void QgsTextEditWrapper::textChanged( const QString & )
203 {
204  if ( mLineEdit )
205  {
206  //restore placeholder text, which may have been removed by showIndeterminateState()
207  mLineEdit->setPlaceholderText( mPlaceholderText );
208  }
209 }
210 
211 void QgsTextEditWrapper::setWidgetValue( const QVariant &val )
212 {
213  QString v;
214  if ( val.isNull() )
215  {
216  if ( !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
218  }
219  else
220  {
221  v = field().displayString( val );
222  }
223 
224  if ( mTextEdit )
225  {
226  if ( val != value() )
227  {
228  if ( config( QStringLiteral( "UseHtml" ) ).toBool() )
229  {
230  mTextEdit->setHtml( v );
231  if ( mTextBrowser )
232  {
233  mTextBrowser->setTextInteractionFlags( Qt::LinksAccessibleByMouse );
234  mTextBrowser->setOpenExternalLinks( true );
235  }
236  }
237  else
238  mTextEdit->setPlainText( v );
239  }
240  }
241 
242  if ( mPlainTextEdit )
243  {
244  if ( val != value() )
245  mPlainTextEdit->setPlainText( v );
246  }
247 
248  if ( mLineEdit )
249  mLineEdit->setText( v );
250 }
251 
252 void QgsTextEditWrapper::setHint( const QString &hintText )
253 {
254  if ( hintText.isNull() )
255  mPlaceholderText = mPlaceholderTextBackup;
256  else
257  {
258  mPlaceholderTextBackup = mPlaceholderText;
259  mPlaceholderText = hintText;
260  }
261 
262  mLineEdit->setPlaceholderText( mPlaceholderText );
263 }
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.
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:205
QgsTextEditWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
bool valid() const override
Returns 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.