QGIS API Documentation
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  return res;
70  else
71  return QVariant( field().type() );
72 }
73 
75 {
76  if ( config( "IsMultiline" ).toBool() )
77  {
78  if ( config( "UseHtml" ).toBool() )
79  {
80  return new QTextEdit( parent );
81  }
82  else
83  {
84  return new QPlainTextEdit( parent );
85  }
86  }
87  else
88  {
89  return new QgsFilterLineEdit( parent );
90  }
91 }
92 
94 {
95  mTextEdit = qobject_cast<QTextEdit*>( editor );
96  mPlainTextEdit = qobject_cast<QPlainTextEdit*>( editor );
97  mLineEdit = qobject_cast<QLineEdit*>( editor );
98 
99  if ( mTextEdit )
100  connect( mTextEdit, SIGNAL( textChanged() ), this, SLOT( valueChanged() ) );
101 
102  if ( mPlainTextEdit )
103  connect( mPlainTextEdit, SIGNAL( textChanged() ), this, SLOT( valueChanged() ) );
104 
105  if ( mLineEdit )
106  {
107  mLineEdit->setValidator( new QgsFieldValidator( mLineEdit, field(), defaultValue().toString() ) );
108 
109  QVariant defVal = defaultValue();
110  if ( defVal.isNull() )
111  {
112  defVal = QSettings().value( "qgis/nullValue", "NULL" );
113  }
114 
115  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
116  if ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date )
117  {
118  mLineEdit->setPlaceholderText( defVal.toString() );
119  }
120  else if ( fle )
121  {
122  fle->setNullValue( defVal.toString() );
123  }
124 
125  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
126 
127  mWritablePalette = mLineEdit->palette();
128  mReadOnlyPalette = mLineEdit->palette();
129  mReadOnlyPalette.setColor( QPalette::Text, mWritablePalette.color( QPalette::Disabled, QPalette::Text ) );
130  }
131 }
132 
134 {
135  return mLineEdit || mTextEdit || mPlainTextEdit;
136 }
137 
139 {
140  //note - this is deliberately a zero length string, not a null string!
141  if ( mTextEdit )
142  mTextEdit->blockSignals( true );
143  if ( mPlainTextEdit )
144  mPlainTextEdit->blockSignals( true );
145  if ( mLineEdit )
146  mLineEdit->blockSignals( true );
147 
148  setWidgetValue( QString( "" ) );
149 
150  if ( mTextEdit )
151  mTextEdit->blockSignals( false );
152  if ( mPlainTextEdit )
153  mPlainTextEdit->blockSignals( false );
154  if ( mLineEdit )
155  mLineEdit->blockSignals( false );
156 }
157 
159 {
160  setWidgetValue( val );
161 }
162 
163 void QgsTextEditWrapper::setEnabled( bool enabled )
164 {
165  if ( mTextEdit )
166  mTextEdit->setReadOnly( !enabled );
167 
168  if ( mPlainTextEdit )
169  mPlainTextEdit->setReadOnly( !enabled );
170 
171  if ( mLineEdit )
172  {
173  mLineEdit->setReadOnly( !enabled );
174  if ( enabled )
175  mLineEdit->setPalette( mWritablePalette );
176  else
177  mLineEdit->setPalette( mReadOnlyPalette );
178  }
179 }
180 
181 void QgsTextEditWrapper::setWidgetValue( const QVariant& val )
182 {
183  QString v;
184  if ( val.isNull() )
185  {
186  if ( !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
187  v = QSettings().value( "qgis/nullValue", "NULL" ).toString();
188  }
189  else
190  v = val.toString();
191 
192  if ( mTextEdit )
193  {
194  if ( val != value() )
195  {
196  if ( config( "UseHtml" ).toBool() )
197  mTextEdit->setHtml( v );
198  else
199  mTextEdit->setPlainText( v );
200  }
201  }
202 
203  if ( mPlainTextEdit )
204  {
205  if ( val != value() )
206  mPlainTextEdit->setPlainText( v );
207  }
208 
209  if ( mLineEdit )
210  mLineEdit->setText( v );
211 }
void setEnabled(bool enabled) override
void setPalette(const QPalette &)
void valueChanged()
Will call the value() method to determine the emitted value.
void setColor(ColorGroup group, ColorRole role, const QColor &color)
Manages an editor widget Widget and wrapper share the same parent.
QString toHtml() const
QgsField field() const
Access the field.
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)
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
bool isNull() const
bool isEmpty() const
Lineedit with builtin clear button.
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 defaultValue() const
Access the default value of the field.
QVariant value(const QString &key, const QVariant &defaultValue) const
void setReadOnly(bool ro)
void setValue(const QVariant &value) override
QgsEditorWidgetConfig config() const
Returns the whole config.
void setReadOnly(bool ro)
void setNullValue(const QString &nullValue)
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.
QString toString() const
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:89