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