Skip to content

Commit 3f60d72

Browse files
author
jef
committedMay 30, 2009
- allow setting of field width and precision when adding attributes
- update vector data providers accordingly - postgres provider: - add support for more native types and setting of column comments - catch errors on retrieval of defaults values (fixes #1713) git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10863 c8812cc2-4d05-0410-92ff-de0c093fc19c

19 files changed

+342
-167
lines changed
 

‎python/core/qgsvectordataprovider.sip

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class QgsVectorDataProvider : QgsDataProvider
164164
* @param attributes map with attribute name as key and type as value
165165
* @return true in case of success and false in case of failure
166166
*/
167-
virtual bool addAttributes(const QMap<QString, QString> & attributes);
167+
virtual bool addAttributes(const QList<QgsField> & attributes);
168168

169169
/**
170170
* Deletes existing attributes
@@ -231,9 +231,12 @@ class QgsVectorDataProvider : QgsDataProvider
231231
*/
232232
QList<int> attributeIndexes();
233233

234-
/**Returns the names of the numerical types*/
235-
const QMap<QString,QVariant::Type> &supportedNativeTypes() const;
236-
234+
/**
235+
* check if provider supports type of field
236+
* @note added in 1.2
237+
*/
238+
bool supportedType( const QgsField &field ) const;
239+
237240
/**
238241
* Set whether provider should return also features that don't have
239242
* associated geometry. FALSE by default

‎python/core/qgsvectorlayer.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public:
325325

326326
/** add an attribute field (but does not commit it)
327327
returns the field index or -1 in case of failure */
328-
bool addAttribute(QString name, QString type);
328+
bool addAttribute( const QgsField &field );
329329

330330
/** delete an attribute field (but does not commit it) */
331331
bool deleteAttribute(int attr);

‎src/app/qgsaddattrdialog.cpp

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,74 @@
1717

1818
#include "qgsaddattrdialog.h"
1919
#include "qgsvectordataprovider.h"
20+
#include "qgslogger.h"
2021

2122
QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorDataProvider* provider, QWidget *parent, Qt::WFlags fl )
2223
: QDialog( parent, fl ), mDataProvider( provider )
2324
{
2425
setupUi( this );
2526

2627
//fill data types into the combo box
27-
const QgsNativeTypeMap &typelist = mDataProvider->supportedNativeTypes();
28+
const QList< QgsVectorDataProvider::NativeType > &typelist = mDataProvider->nativeTypes();
2829

29-
for ( QgsNativeTypeMap::const_iterator it = typelist.constBegin(); it != typelist.constEnd(); ++it )
30+
for ( int i = 0; i < typelist.size(); i++ )
3031
{
31-
mTypeBox->addItem( it.key() );
32+
QgsDebugMsg( QString( "name:%1 type:%2 typeName:%3 length:%4-%5 prec:%6-%7" )
33+
.arg( typelist[i].mTypeDesc )
34+
.arg( typelist[i].mType )
35+
.arg( typelist[i].mTypeName )
36+
.arg( typelist[i].mMinLen ).arg( typelist[i].mMaxLen )
37+
.arg( typelist[i].mMinPrec ).arg( typelist[i].mMaxPrec ) );
38+
39+
mTypeBox->addItem( typelist[i].mTypeDesc );
40+
mTypeBox->setItemData( i, static_cast<int>( typelist[i].mType ), Qt::UserRole );
41+
mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
42+
mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
43+
mTypeBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
44+
mTypeBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
45+
mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
3246
}
47+
48+
on_mTypeBox_currentIndexChanged( 0 );
3349
}
3450

35-
QgsAddAttrDialog::QgsAddAttrDialog( const std::list<QString>& typelist, QWidget *parent, Qt::WFlags fl )
36-
: QDialog( parent, fl ), mDataProvider( 0 )
51+
void QgsAddAttrDialog::on_mTypeBox_currentIndexChanged( int idx )
3752
{
38-
setupUi( this );
53+
mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
3954

40-
for ( std::list<QString>::const_iterator iter = typelist.begin();iter != typelist.end();++iter )
41-
{
42-
mTypeBox->addItem( *iter );
43-
}
44-
}
55+
mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
56+
mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
57+
mLength->setVisible( mLength->minimum() < mLength->maximum() );
58+
if ( mLength->value() < mLength->minimum() )
59+
mLength->setValue( mLength->minimum() );
60+
if ( mLength->value() > mLength->maximum() )
61+
mLength->setValue( mLength->maximum() );
4562

46-
QString QgsAddAttrDialog::name() const
47-
{
48-
return mNameEdit->text();
63+
mPrec->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt() );
64+
mPrec->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt() );
65+
mPrec->setVisible( mPrec->minimum() < mPrec->maximum() );
66+
if ( mPrec->value() < mPrec->minimum() )
67+
mPrec->setValue( mPrec->minimum() );
68+
if ( mPrec->value() > mPrec->maximum() )
69+
mPrec->setValue( mPrec->maximum() );
4970
}
5071

51-
QString QgsAddAttrDialog::type() const
72+
QgsField QgsAddAttrDialog::field() const
5273
{
53-
return mTypeBox->currentText();
74+
QgsDebugMsg( QString( "idx:%1 name:%2 type:%3 typeName:%4 length:%5 prec:%6 comment:%7" )
75+
.arg( mTypeBox->currentIndex() )
76+
.arg( mNameEdit->text() )
77+
.arg( mTypeBox->itemData( mTypeBox->currentIndex(), Qt::UserRole ).toInt() )
78+
.arg( mTypeBox->itemData( mTypeBox->currentIndex(), Qt::UserRole + 1 ).toString() )
79+
.arg( mLength->value() )
80+
.arg( mPrec->value() )
81+
.arg( mCommentEdit->text() ) );
82+
83+
return QgsField(
84+
mNameEdit->text(),
85+
( QVariant::Type ) mTypeBox->itemData( mTypeBox->currentIndex(), Qt::UserRole ).toInt(),
86+
mTypeBox->itemData( mTypeBox->currentIndex(), Qt::UserRole + 1 ).toString(),
87+
mLength->value(),
88+
mPrec->value(),
89+
mCommentEdit->text() );
5490
}

‎src/app/qgsaddattrdialog.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "ui_qgsaddattrdialogbase.h"
2222
#include "qgisgui.h"
23+
#include "qgsfield.h"
2324

2425
class QgsVectorDataProvider;
2526

@@ -31,8 +32,12 @@ class QgsAddAttrDialog: public QDialog, private Ui::QgsAddAttrDialogBase
3132
QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
3233
QgsAddAttrDialog( const std::list<QString>& typelist,
3334
QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
34-
QString name() const;
35-
QString type() const;
35+
36+
QgsField field() const;
37+
38+
public slots:
39+
void on_mTypeBox_currentIndexChanged( int idx );
40+
3641
protected:
3742
QgsVectorDataProvider* mDataProvider;
3843
};

‎src/app/qgsvectorlayerproperties.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,17 @@ void QgsVectorLayerProperties::addAttribute()
261261
QgsAddAttrDialog dialog( layer->dataProvider(), this );
262262
if ( dialog.exec() == QDialog::Accepted )
263263
{
264-
if ( !addAttribute( dialog.name(), dialog.type() ) )
264+
if ( !addAttribute( dialog.field() ) )
265265
{
266266
QMessageBox::information( this, tr( "Name conflict" ), tr( "The attribute could not be inserted. The name already exists in the table." ) );
267267
}
268268
}
269269
}
270270

271-
bool QgsVectorLayerProperties::addAttribute( QString name, QString type )
271+
bool QgsVectorLayerProperties::addAttribute( const QgsField &field )
272272
{
273-
QgsDebugMsg( "inserting attribute " + name + " of type " + type );
274-
return layer->addAttribute( name, type );
273+
QgsDebugMsg( "inserting attribute " + field.name() + " of type " + field.typeName() );
274+
return layer->addAttribute( field );
275275
}
276276

277277
void QgsVectorLayerProperties::deleteAttribute()

‎src/app/qgsvectorlayerproperties.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
5050
void setDisplayField( QString name );
5151

5252
/**Adds an attribute to the table (but does not commit it yet)
53-
@param name attribute name
54-
@param type attribute type
55-
@return false in case of a name conflict, true in case of success*/
56-
bool addAttribute( QString name, QString type );
53+
@param field the field to add
54+
@return false in case of a name conflict, true in case of success */
55+
bool addAttribute( const QgsField &field );
5756

5857
/**Deletes an attribute (but does not commit it)
5958
@param name attribute name

‎src/core/qgsvectordataprovider.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ bool QgsVectorDataProvider::deleteFeatures( const QgsFeatureIds & id )
7878
return false;
7979
}
8080

81-
bool QgsVectorDataProvider::addAttributes( const QgsNewAttributesMap & attributes )
81+
bool QgsVectorDataProvider::addAttributes( const QList<QgsField> & attributes )
8282
{
8383
return false;
8484
}
@@ -262,12 +262,28 @@ void QgsVectorDataProvider::enableGeometrylessFeatures( bool fetch )
262262
mFetchFeaturesWithoutGeom = fetch;
263263
}
264264

265-
const QgsNativeTypeMap &QgsVectorDataProvider::supportedNativeTypes() const
265+
const QList< QgsVectorDataProvider::NativeType > &QgsVectorDataProvider::nativeTypes() const
266266
{
267-
return mSupportedNativeTypes;
267+
return mNativeTypes;
268268
}
269269

270270

271+
bool QgsVectorDataProvider::supportedType( const QgsField &field ) const
272+
{
273+
int i;
274+
for ( i = 0; i < mNativeTypes.size(); i++ )
275+
{
276+
if ( field.type() == mNativeTypes[i].mType &&
277+
field.length() >= mNativeTypes[i].mMinLen && field.length() <= mNativeTypes[i].mMaxLen &&
278+
field.precision() >= mNativeTypes[i].mMinPrec && field.precision() <= mNativeTypes[i].mMaxPrec )
279+
{
280+
break;
281+
}
282+
}
283+
284+
return i < mNativeTypes.size();
285+
}
286+
271287
QVariant QgsVectorDataProvider::minimumValue( int index )
272288
{
273289
if ( !fields().contains( index ) )

‎src/core/qgsvectordataprovider.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class QTextCodec;
2929
#include "qgsvectorlayer.h"
3030
#include "qgsfield.h"
3131

32-
typedef QMap<QString, QString> QgsNewAttributesMap;
33-
typedef QMap<QString, QVariant::Type> QgsNativeTypeMap;
34-
3532
/** \ingroup core
3633
* This is the base class for vector data providers.
3734
*
@@ -208,21 +205,21 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
208205
* @param attributes map with attribute name as key and type as value
209206
* @return true in case of success and false in case of failure
210207
*/
211-
virtual bool addAttributes( const QgsNewAttributesMap & attributes );
208+
virtual bool addAttributes( const QList<QgsField> &attributes );
212209

213210
/**
214211
* Deletes existing attributes
215212
* @param attributes a set containing names of attributes
216213
* @return true in case of success and false in case of failure
217214
*/
218-
virtual bool deleteAttributes( const QgsAttributeIds& attributes );
215+
virtual bool deleteAttributes( const QgsAttributeIds &attributes );
219216

220217
/**
221218
* Changes attribute values of existing features.
222219
* @param attr_map a map containing changed attributes
223220
* @return true in case of success and false in case of failure
224221
*/
225-
virtual bool changeAttributeValues( const QgsChangedAttributesMap & attr_map );
222+
virtual bool changeAttributeValues( const QgsChangedAttributesMap &attr_map );
226223

227224
/**
228225
* Returns the default value for field specified by @c fieldId
@@ -279,15 +276,37 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
279276
*/
280277
virtual QgsAttributeList attributeIndexes();
281278

282-
/**Returns the names of the numerical types*/
283-
const QgsNativeTypeMap &supportedNativeTypes() const;
284-
285279
/**
286280
* Set whether provider should also return features that don't have
287281
* associated geometry. FALSE by default
288282
*/
289283
void enableGeometrylessFeatures( bool fetch );
290284

285+
/**
286+
* check if provider supports type of field
287+
* @note added in 1.2
288+
*/
289+
bool supportedType( const QgsField &field ) const;
290+
291+
struct NativeType
292+
{
293+
NativeType( QString typeDesc, QString typeName, QVariant::Type type, int minLen = 0, int maxLen = 0, int minPrec = 0, int maxPrec = 0 ) :
294+
mTypeDesc( typeDesc ), mTypeName( typeName ), mType( type ), mMinLen( minLen ), mMaxLen( maxLen ), mMinPrec( minPrec ), mMaxPrec( maxPrec ) {};
295+
296+
QString mTypeDesc;
297+
QString mTypeName;
298+
QVariant::Type mType;
299+
int mMinLen, mMaxLen;
300+
int mMinPrec, mMaxPrec;
301+
};
302+
303+
304+
/**
305+
* Returns the names of the numerical types
306+
* @note added in 1.2
307+
*/
308+
const QList< NativeType > &nativeTypes() const;
309+
291310
protected:
292311
QVariant convertValue( QVariant::Type type, QString value );
293312

@@ -309,7 +328,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
309328
QgsAttributeList mAttributesToFetch;
310329

311330
/**The names of the providers native types*/
312-
QgsNativeTypeMap mSupportedNativeTypes;
331+
QList< NativeType > mNativeTypes;
313332
};
314333

315334
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.