QGIS API Documentation  2.99.0-Master (7705179)
qgsfield.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfield.cpp - Describes a field in a layer or table
3  --------------------------------------
4  Date : 01-Jan-2004
5  Copyright : (C) 2004 by Gary E.Sherman
6  email : sherman at mrcc.com
7 
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgsfields.h"
18 #include "qgsfield_p.h"
19 #include "qgis.h"
20 #include "qgsapplication.h"
21 #include "qgssettings.h"
22 
23 #include <QDataStream>
24 #include <QIcon>
25 
26 /***************************************************************************
27  * This class is considered CRITICAL and any change MUST be accompanied with
28  * full unit tests in testqgsfield.cpp.
29  * See details in QEP #17
30  ****************************************************************************/
31 
32 #if 0
33 QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
34  QString comment )
35  : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
36  , mComment( comment )
37 {
38  // This function used to lower case the field name since some stores
39  // use upper case (e.g., shapefiles), but that caused problems with
40  // attribute actions getting confused between uppercase and
41  // lowercase versions of the attribute names, so just leave the
42  // names how they are now.
43 }
44 #endif
45 QgsField::QgsField( const QString &name, QVariant::Type type,
46  const QString &typeName, int len, int prec, const QString &comment,
47  QVariant::Type subType )
48 {
49  d = new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
50 }
51 
52 QgsField::QgsField( const QgsField &other ) //NOLINT
53  : d( other.d )
54 {
55 
56 }
57 
58 /***************************************************************************
59  * This class is considered CRITICAL and any change MUST be accompanied with
60  * full unit tests in testqgsfield.cpp.
61  * See details in QEP #17
62  ****************************************************************************/
63 
64 QgsField &QgsField::operator =( const QgsField &other ) //NOLINT
65 {
66  d = other.d;
67  return *this;
68 }
69 
70 bool QgsField::operator==( const QgsField &other ) const
71 {
72  return *( other.d ) == *d;
73 }
74 
75 bool QgsField::operator!=( const QgsField &other ) const
76 {
77  return !( *this == other );
78 }
79 
80 QString QgsField::name() const
81 {
82  return d->name;
83 }
84 
85 QString QgsField::displayName() const
86 {
87  if ( !d->alias.isEmpty() )
88  return d->alias;
89  else
90  return d->name;
91 }
92 
93 QVariant::Type QgsField::type() const
94 {
95  return d->type;
96 }
97 
98 QVariant::Type QgsField::subType() const
99 {
100  return d->subType;
101 }
102 
103 QString QgsField::typeName() const
104 {
105  return d->typeName;
106 }
107 
108 int QgsField::length() const
109 {
110  return d->length;
111 }
112 
113 int QgsField::precision() const
114 {
115  return d->precision;
116 }
117 
118 QString QgsField::comment() const
119 {
120  return d->comment;
121 }
122 
123 bool QgsField::isNumeric() const
124 {
125  return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
126 }
127 
128 /***************************************************************************
129  * This class is considered CRITICAL and any change MUST be accompanied with
130  * full unit tests in testqgsfield.cpp.
131  * See details in QEP #17
132  ****************************************************************************/
133 
134 void QgsField::setName( const QString &name )
135 {
136  d->name = name;
137 }
138 
139 void QgsField::setType( QVariant::Type type )
140 {
141  d->type = type;
142 }
143 
144 void QgsField::setSubType( QVariant::Type subType )
145 {
146  d->subType = subType;
147 }
148 
149 void QgsField::setTypeName( const QString &typeName )
150 {
151  d->typeName = typeName;
152 }
153 
154 void QgsField::setLength( int len )
155 {
156  d->length = len;
157 }
159 {
160  d->precision = precision;
161 }
162 
163 void QgsField::setComment( const QString &comment )
164 {
165  d->comment = comment;
166 }
167 
168 QString QgsField::defaultValueExpression() const
169 {
170  return d->defaultValueExpression;
171 }
172 
173 void QgsField::setDefaultValueExpression( const QString &expression )
174 {
175  d->defaultValueExpression = expression;
176 }
177 
179 {
180  d->constraints = constraints;
181 }
182 
184 {
185  return d->constraints;
186 }
187 
188 QString QgsField::alias() const
189 {
190  return d->alias;
191 }
192 
193 void QgsField::setAlias( const QString &alias )
194 {
195  d->alias = alias;
196 }
197 
198 /***************************************************************************
199  * This class is considered CRITICAL and any change MUST be accompanied with
200  * full unit tests in testqgsfield.cpp.
201  * See details in QEP #17
202  ****************************************************************************/
203 
204 QString QgsField::displayString( const QVariant &v ) const
205 {
206  if ( v.isNull() )
207  {
208  QgsSettings settings;
210  }
211 
212  if ( d->type == QVariant::Double && d->precision > 0 )
213  return QString::number( v.toDouble(), 'f', d->precision );
214 
215  return v.toString();
216 }
217 
218 /***************************************************************************
219  * This class is considered CRITICAL and any change MUST be accompanied with
220  * full unit tests in testqgsfield.cpp.
221  * See details in QEP #17
222  ****************************************************************************/
223 
224 bool QgsField::convertCompatible( QVariant &v ) const
225 {
226  if ( v.isNull() )
227  {
228  v.convert( d->type );
229  return true;
230  }
231 
232  if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
233  {
234  v = QVariant( d->type );
235  return false;
236  }
237 
238  //String representations of doubles in QVariant will return false to convert( QVariant::Int )
239  //work around this by first converting to double, and then checking whether the double is convertible to int
240  if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
241  {
242  bool ok = false;
243  double dbl = v.toDouble( &ok );
244  if ( !ok )
245  {
246  //couldn't convert to number
247  v = QVariant( d->type );
248  return false;
249  }
250 
251  double round = std::round( dbl );
252  if ( round > INT_MAX || round < -INT_MAX )
253  {
254  //double too large to fit in int
255  v = QVariant( d->type );
256  return false;
257  }
258  v = QVariant( static_cast< int >( std::round( dbl ) ) );
259  return true;
260  }
261 
262  if ( !v.convert( d->type ) )
263  {
264  v = QVariant( d->type );
265  return false;
266  }
267 
268  if ( d->type == QVariant::Double && d->precision > 0 )
269  {
270  double s = std::pow( 10, d->precision );
271  double d = v.toDouble() * s;
272  v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
273  return true;
274  }
275 
276  if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
277  {
278  v = v.toString().left( d->length );
279  return false;
280  }
281 
282  return true;
283 }
284 
286 {
287  d->editorWidgetSetup = v;
288 }
289 
291 {
292  return d->editorWidgetSetup;
293 }
294 
295 /***************************************************************************
296  * This class is considered CRITICAL and any change MUST be accompanied with
297  * full unit tests in testqgsfield.cpp.
298  * See details in QEP #17
299  ****************************************************************************/
300 
301 QDataStream &operator<<( QDataStream &out, const QgsField &field )
302 {
303  out << field.name();
304  out << static_cast< quint32 >( field.type() );
305  out << field.typeName();
306  out << field.length();
307  out << field.precision();
308  out << field.comment();
309  out << field.alias();
310  out << field.defaultValueExpression();
311  out << field.constraints().constraints();
312  out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) );
313  out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ) );
314  out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintExpression ) );
315  out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
316  out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
317  out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
318  out << field.constraints().constraintExpression();
319  out << field.constraints().constraintDescription();
320  out << static_cast< quint32 >( field.subType() );
321  return out;
322 }
323 
324 QDataStream &operator>>( QDataStream &in, QgsField &field )
325 {
326  quint32 type, subType, length, precision, constraints, originNotNull, originUnique, originExpression, strengthNotNull, strengthUnique, strengthExpression;
327  QString name, typeName, comment, alias, defaultValueExpression, constraintExpression, constraintDescription;
328  in >> name >> type >> typeName >> length >> precision >> comment >> alias
329  >> defaultValueExpression >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
330  constraintExpression >> constraintDescription >> subType;
331  field.setName( name );
332  field.setType( static_cast< QVariant::Type >( type ) );
333  field.setTypeName( typeName );
334  field.setLength( static_cast< int >( length ) );
335  field.setPrecision( static_cast< int >( precision ) );
336  field.setComment( comment );
337  field.setAlias( alias );
338  field.setDefaultValueExpression( defaultValueExpression );
339  QgsFieldConstraints fieldConstraints;
340  if ( constraints & QgsFieldConstraints::ConstraintNotNull )
341  {
342  fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
343  fieldConstraints.setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthNotNull ) );
344  }
345  else
346  fieldConstraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull );
347  if ( constraints & QgsFieldConstraints::ConstraintUnique )
348  {
349  fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
350  fieldConstraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthUnique ) );
351  }
352  else
353  fieldConstraints.removeConstraint( QgsFieldConstraints::ConstraintUnique );
354  if ( constraints & QgsFieldConstraints::ConstraintExpression )
355  {
356  fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
357  fieldConstraints.setConstraintStrength( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthExpression ) );
358  }
359  else
360  fieldConstraints.removeConstraint( QgsFieldConstraints::ConstraintExpression );
361  fieldConstraints.setConstraintExpression( constraintExpression, constraintDescription );
362  field.setConstraints( fieldConstraints );
363  field.setSubType( static_cast< QVariant::Type >( subType ) );
364  return in;
365 }
bool isNumeric() const
Returns if this field is numeric.
QgsField & operator=(const QgsField &other)
Assignment operator.
Definition: qgsfield.cpp:64
void setConstraintStrength(Constraint constraint, ConstraintStrength strength)
Sets the strength of a constraint.
QgsField(const QString &name=QString(), QVariant::Type type=QVariant::Invalid, const QString &typeName=QString(), int len=0, int prec=0, const QString &comment=QString(), QVariant::Type subType=QVariant::Invalid)
Constructor.
Definition: qgsfield.cpp:45
QString comment() const
Returns the field comment.
ConstraintStrength constraintStrength(Constraint constraint) const
Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint is not pres...
QString name
Definition: qgsfield.h:54
int precision
Definition: qgsfield.h:52
QString alias() const
Returns the alias for the field (the friendly displayed name of the field ), or an empty string if th...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:54
QString alias
Definition: qgsfield.h:55
void setPrecision(int precision)
Set the field precision.
Definition: qgsfield.cpp:158
QString comment
Definition: qgsfield.h:53
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field. QGIS version compatibility is not guaranteed.
Definition: qgsfield.cpp:324
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:224
void setName(const QString &name)
Set the field name.
Definition: qgsfield.cpp:134
QgsEditorWidgetSetup editorWidgetSetup() const
Get the editor widget setup for the field.
Definition: qgsfield.cpp:290
int precision() const
Gets the precision of the field.
Stores information about constraints which may be present on a field.
QString name() const
Returns the name of the field.
Field has an expression constraint set. See constraintExpression().
int length
Definition: qgsfield.h:51
void setLength(int len)
Set the field length.
Definition: qgsfield.cpp:154
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:103
QString defaultValueExpression
Definition: qgsfield.h:56
QString displayName() const
Returns the name to use when displaying this field.
Definition: qgsfield.cpp:85
void setTypeName(const QString &typeName)
Set the field type.
Definition: qgsfield.cpp:149
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
bool operator!=(const QgsField &other) const
Definition: qgsfield.cpp:75
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:204
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:46
void setDefaultValueExpression(const QString &expression)
Sets an expression to use when calculating the default value for the field.
Definition: qgsfield.cpp:173
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field.
void setSubType(QVariant::Type subType)
If the field is a collection, set its element&#39;s type.
Definition: qgsfield.cpp:144
QgsFieldConstraints constraints
Definition: qgsfield.h:57
void setType(QVariant::Type type)
Set variant type.
Definition: qgsfield.cpp:139
bool operator==(const QgsField &other) const
Definition: qgsfield.cpp:70
QString defaultValueExpression() const
Returns the expression used when calculating the default value for the field.
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition: qgsfield.cpp:193
void setConstraintExpression(const QString &expression, const QString &description=QString())
Set the constraint expression for the field.
Holder for the widget type and its configuration for a field.
int length() const
Gets the length of the field.
QVariant::Type subType() const
If the field is a collection, gets its element&#39;s type.
Definition: qgsfield.cpp:98
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
const QgsFieldConstraints & constraints() const
Returns constraints which are present for the field.
QString constraintExpression() const
Returns the constraint expression for the field, if set.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:93
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field.
Definition: qgsfield.cpp:178
void setComment(const QString &comment)
Set the field comment.
Definition: qgsfield.cpp:163
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfield.cpp:301
void removeConstraint(Constraint constraint)
Removes a constraint from the field.
Field must have a unique value.
void setEditorWidgetSetup(const QgsEditorWidgetSetup &v)
Set the editor widget setup for the field.
Definition: qgsfield.cpp:285