QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "qgsfield.h"
18 
19 /*
20 QgsField::QgsField(QString nam, QString typ, int len, int prec, bool num,
21  QString comment)
22  :mName(nam), mType(typ), mLength(len), mPrecision(prec), mNumeric(num),
23  mComment(comment)
24 {
25  // This function used to lower case the field name since some stores
26  // use upper case (eg. shapefiles), but that caused problems with
27  // attribute actions getting confused between uppercase and
28  // lowercase versions of the attribute names, so just leave the
29  // names how they are now.
30 }*/
31 
32 QgsField::QgsField( QString name, QVariant::Type type, QString typeName, int len, int prec, QString comment )
33  : mName( name ), mType( type ), mTypeName( typeName ),
34  mLength( len ), mPrecision( prec ), mComment( comment )
35 {
36 }
37 
38 
40 {
41 }
42 
43 bool QgsField::operator==( const QgsField& other ) const
44 {
45  return (( mName == other.mName ) && ( mType == other.mType )
46  && ( mLength == other.mLength ) && ( mPrecision == other.mPrecision ) );
47 }
48 
49 bool QgsField::operator!=( const QgsField& other ) const
50 {
51  return !( *this == other );
52 }
53 
54 
55 const QString & QgsField::name() const
56 {
57  return mName;
58 }
59 
60 QVariant::Type QgsField::type() const
61 {
62  return mType;
63 }
64 
65 const QString & QgsField::typeName() const
66 {
67  return mTypeName;
68 }
69 
70 int QgsField::length() const
71 {
72  return mLength;
73 }
74 
76 {
77  return mPrecision;
78 }
79 
80 const QString & QgsField::comment() const
81 {
82  return mComment;
83 }
84 
85 void QgsField::setName( const QString & nam )
86 {
87  mName = nam;
88 }
89 
90 void QgsField::setType( QVariant::Type type )
91 {
92  mType = type;
93 }
94 
95 void QgsField::setTypeName( const QString & typeName )
96 {
98 }
99 
100 void QgsField::setLength( int len )
101 {
102  mLength = len;
103 }
104 void QgsField::setPrecision( int prec )
105 {
106  mPrecision = prec;
107 }
108 
109 void QgsField::setComment( const QString & comment )
110 {
111  mComment = comment;
112 }
113 
114 QString QgsField::displayString( const QVariant& v ) const
115 {
116  switch ( mType )
117  {
118  case QVariant::Double:
119  if ( mPrecision > 0 )
120  {
121  return QString::number( v.toDouble(), 'f', mPrecision );
122  }
123  default:
124  return v.toString();
125  }
126 }
127 
128 
130 
132 {
133  mFields.clear();
134  mNameToIndex.clear();
135 }
136 
137 bool QgsFields::append( const QgsField& field, FieldOrigin origin, int originIndex )
138 {
139  if ( mNameToIndex.contains( field.name() ) )
140  return false;
141 
142  if ( originIndex == -1 && origin == OriginProvider )
143  originIndex = mFields.count();
144  mFields.append( Field( field, origin, originIndex ) );
145 
146  mNameToIndex.insert( field.name(), mFields.count() - 1 );
147  return true;
148 }
149 
150 void QgsFields::remove( int fieldIdx )
151 {
152  mNameToIndex.remove( mFields[fieldIdx].field.name() );
153  mFields.remove( fieldIdx );
154 }
155 
156 void QgsFields::extend( const QgsFields& other )
157 {
158  for ( int i = 0; i < other.count(); ++i )
159  {
160  append( other.at( i ), other.fieldOrigin( i ), other.fieldOriginIndex( i ) );
161  }
162 }
163 
164 QList<QgsField> QgsFields::toList() const
165 {
166  QList<QgsField> lst;
167  for ( int i = 0; i < mFields.count(); ++i )
168  lst.append( mFields[i].field );
169  return lst;
170 }