QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsfield.h"
19 #include "qgsfieldvalidator.h"
20 #include "qgsfilterlineedit.h"
21 
22 #include <QSettings>
23 
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26  , mTextEdit( nullptr )
27  , mPlainTextEdit( nullptr )
28  , mLineEdit( nullptr )
29 {
30 }
31 
33 {
34  QString v;
35 
36  if ( mTextEdit )
37  {
38  if ( config( "UseHtml" ).toBool() )
39  {
40  v = mTextEdit->toHtml();
41  }
42  else
43  {
44  v = mTextEdit->toPlainText();
45  }
46  }
47 
48  if ( mPlainTextEdit )
49  {
50  v = mPlainTextEdit->toPlainText();
51  }
52 
53  if ( mLineEdit )
54  {
55  v = mLineEdit->text();
56  }
57 
58  if (( v.isEmpty() && ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) ) ||
59  v == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
60  return QVariant( field().type() );
61 
62  if ( !defaultValue().isNull() && v == defaultValue().toString() )
63  {
64  return defaultValue();
65  }
66 
67  QVariant res( v );
68  if ( field().convertCompatible( res ) )
69  {
70  return res;
71  }
72  else if ( field().type() == QVariant::String && field().length() > 0 )
73  {
74  // for string fields convertCompatible may return false due to field length limit - in this case just truncate
75  // input rather then discarding it entirely
76  return QVariant( v.left( field().length() ) );
77  }
78  else
79  {
80  return QVariant( field().type() );
81  }
82 }
83 
85 {
86  if ( config( "IsMultiline" ).toBool() )
87  {
88  if ( config( "UseHtml" ).toBool() )
89  {
90  return new QTextEdit( parent );
91  }
92  else
93  {
94  return new QPlainTextEdit( parent );
95  }
96  }
97  else
98  {
99  return new QgsFilterLineEdit( parent );
100  }
101 }
102 
104 {
105  mTextEdit = qobject_cast<QTextEdit*>( editor );
106  mPlainTextEdit = qobject_cast<QPlainTextEdit*>( editor );
107  mLineEdit = qobject_cast<QLineEdit*>( editor );
108 
109  if ( mTextEdit )
110  connect( mTextEdit, SIGNAL( textChanged() ), this, SLOT( valueChanged() ) );
111 
112  if ( mPlainTextEdit )
113  connect( mPlainTextEdit, SIGNAL( textChanged() ), this, SLOT( valueChanged() ) );
114 
115  if ( mLineEdit )
116  {
117  mLineEdit->setValidator( new QgsFieldValidator( mLineEdit, field(), defaultValue().toString() ) );
118 
119  QVariant defVal = defaultValue();
120  if ( defVal.isNull() )
121  {
122  defVal = QSettings().value( "qgis/nullValue", "NULL" );
123  }
124 
125  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
126  if ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date )
127  {
128  mPlaceholderText = defVal.toString();
129  mLineEdit->setPlaceholderText( mPlaceholderText );
130  }
131  else if ( fle )
132  {
133  fle->setNullValue( defVal.toString() );
134  }
135 
136  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
137  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( textChanged( QString ) ) );
138 
139  mWritablePalette = mLineEdit->palette();
140  mReadOnlyPalette = mLineEdit->palette();
141  mReadOnlyPalette.setColor( QPalette::Text, mWritablePalette.color( QPalette::Disabled, QPalette::Text ) );
142  }
143 }
144 
146 {
147  return mLineEdit || mTextEdit || mPlainTextEdit;
148 }
149 
151 {
152  //note - this is deliberately a zero length string, not a null string!
153  if ( mTextEdit )
154  mTextEdit->blockSignals( true );
155  if ( mPlainTextEdit )
156  mPlainTextEdit->blockSignals( true );
157  if ( mLineEdit )
158  {
159  mLineEdit->blockSignals( true );
160  // for interdeminate state we need to clear the placeholder text - we want an empty line edit, not
161  // one showing the default value (eg "NULL")
162  mLineEdit->setPlaceholderText( QString() );
163  }
164 
165  setWidgetValue( QString( "" ) );
166 
167  if ( mTextEdit )
168  mTextEdit->blockSignals( false );
169  if ( mPlainTextEdit )
170  mPlainTextEdit->blockSignals( false );
171  if ( mLineEdit )
172  mLineEdit->blockSignals( false );
173 }
174 
176 {
177  if ( mLineEdit )
178  {
179  //restore placeholder text, which may have been removed by showIndeterminateState()
180  mLineEdit->setPlaceholderText( mPlaceholderText );
181  }
182  setWidgetValue( val );
183 }
184 
185 void QgsTextEditWrapper::setEnabled( bool enabled )
186 {
187  if ( mTextEdit )
188  mTextEdit->setReadOnly( !enabled );
189 
190  if ( mPlainTextEdit )
191  mPlainTextEdit->setReadOnly( !enabled );
192 
193  if ( mLineEdit )
194  {
195  mLineEdit->setReadOnly( !enabled );
196  if ( enabled )
197  mLineEdit->setPalette( mWritablePalette );
198  else
199  mLineEdit->setPalette( mReadOnlyPalette );
200  }
201 }
202 
203 void QgsTextEditWrapper::textChanged( const QString& )
204 {
205  if ( mLineEdit )
206  {
207  //restore placeholder text, which may have been removed by showIndeterminateState()
208  mLineEdit->setPlaceholderText( mPlaceholderText );
209  }
210 }
211 
212 void QgsTextEditWrapper::setWidgetValue( const QVariant& val )
213 {
214  QString v;
215  if ( val.isNull() )
216  {
217  if ( !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
218  v = QSettings().value( "qgis/nullValue", "NULL" ).toString();
219  }
220  else
221  v = val.toString();
222 
223  if ( mTextEdit )
224  {
225  if ( val != value() )
226  {
227  if ( config( "UseHtml" ).toBool() )
228  mTextEdit->setHtml( v );
229  else
230  mTextEdit->setPlainText( v );
231  }
232  }
233 
234  if ( mPlainTextEdit )
235  {
236  if ( val != value() )
237  mPlainTextEdit->setPlainText( v );
238  }
239 
240  if ( mLineEdit )
241  mLineEdit->setText( v );
242 }
void setEnabled(bool enabled) override
void setPalette(const QPalette &)
void valueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
void setColor(ColorGroup group, ColorRole role, const QColor &color)
Manages an editor widget Widget and wrapper share the same parent.
QString toHtml() const
const QColor & color(ColorGroup group, ColorRole role) const
QString toPlainText() const
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.
void setReadOnly(bool)
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.
bool isNull() const
bool isEmpty() const
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
void setPlaceholderText(const QString &)
bool blockSignals(bool block)
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.
QVariant value(const QString &key, const QVariant &defaultValue) const
void setReadOnly(bool ro)
void setValue(const QVariant &value) override
QString left(int n) const
void setReadOnly(bool ro)
QgsEditorWidgetConfig config() const
Returns the whole config.
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toPlainText() const
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:97
QString toString() const