Skip to content

Commit 2e39c73

Browse files
author
jef
committed
vector provider update:
- rename maxValue to maximumValue - rename getUniqueValues to uniqueValues and let it return a list of variants instead of strings. - add protected convertValue to convert string values to attribute values - update attribute dialog, continuous color dialog, graduated symbol dialog, unique value dialog, postgres provider and ogr provider accordingly - fixes #1250 git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9196 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent d810ee0 commit 2e39c73

11 files changed

+61
-86
lines changed

python/core/qgsvectordataprovider.sip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class QgsVectorDataProvider : QgsDataProvider
132132
* and maximal values. If provider has facilities to retrieve maximal
133133
* value directly, override this function.
134134
*/
135-
virtual QVariant maxValue(int index);
135+
virtual QVariant maximumValue(int index);
136136

137137
/**
138138
* Return unique values of an attribute
@@ -141,7 +141,7 @@ class QgsVectorDataProvider : QgsDataProvider
141141
*
142142
* Default implementation simply iterates the features
143143
*/
144-
virtual void getUniqueValues(int index, QStringList &uniqueValues);
144+
virtual void uniqueValues(int index, QList<QVariant> &uniqueValues);
145145

146146
/**
147147
* Adds a list of features

src/app/qgsattributedialog.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,14 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
112112
{
113113
case QgsVectorLayer::UniqueValues:
114114
{
115-
QStringList values;
116-
mLayer->dataProvider()->getUniqueValues( it.key(), values );
115+
QList<QVariant> values;
116+
mLayer->dataProvider()->uniqueValues( it.key(), values );
117117

118118
QComboBox *cb = new QComboBox();
119119
cb->setEditable( true );
120-
cb->addItems( values );
120+
121+
for ( QList<QVariant>::iterator it = values.begin(); it != values.end(); it++ )
122+
cb->addItem( it->toString() );
121123

122124
int idx = cb->findText( myFieldValue.toString() );
123125
if ( idx >= 0 )
@@ -219,10 +221,14 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
219221

220222
if ( editType == QgsVectorLayer::UniqueValuesEditable )
221223
{
222-
QStringList values;
223-
mLayer->dataProvider()->getUniqueValues( it.key(), values );
224+
QList<QVariant> values;
225+
mLayer->dataProvider()->uniqueValues( it.key(), values );
226+
227+
QStringList svalues;
228+
for ( QList<QVariant>::const_iterator it = values.begin(); it != values.end(); it++ )
229+
svalues << it->toString();
224230

225-
QCompleter *c = new QCompleter( values );
231+
QCompleter *c = new QCompleter( svalues );
226232
c->setCompletionMode( QCompleter::PopupCompletion );
227233
le->setCompleter( c );
228234
}

src/app/qgscontinuouscolordialog.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgssymbol.h"
2424
#include "qgsvectordataprovider.h"
2525
#include "qgsvectorlayer.h"
26+
#include "qgslogger.h"
2627

2728
#include <QColorDialog>
2829

@@ -168,11 +169,11 @@ void QgsContinuousColorDialog::apply()
168169
if ( provider )
169170
{
170171
minimum = provider->minimumValue( classfield ).toDouble();
171-
maximum = provider->maxValue( classfield ).toDouble();
172+
maximum = provider->maximumValue( classfield ).toDouble();
172173
}
173174
else
174175
{
175-
qWarning( "Warning, provider is null in QgsGraSyExtensionWidget::QgsGraSyExtensionWidget(...)" );
176+
QgsDebugMsg( "Warning, provider is null" );
176177
return;
177178
}
178179

src/app/qgsgraduatedsymboldialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
254254
if ( modeComboBox->currentText() == tr( "Equal Interval" ) )
255255
{
256256
minimum = provider->minimumValue( field ).toDouble();
257-
maximum = provider->maxValue( field ).toDouble();
257+
maximum = provider->maximumValue( field ).toDouble();
258258
}
259259
else //don't waste performance if mMode is QgsGraduatedSymbolDialog::EMPTY
260260
{
@@ -435,7 +435,7 @@ void QgsGraduatedSymbolDialog::deleteCurrentClass()
435435
int currentIndex = mClassListWidget->currentRow();
436436
mEntries.erase( classValue );
437437
delete( mClassListWidget->takeItem( currentIndex ) );
438-
QgsDebugMsg( QString("numRows: %1").arg( mClassListWidget->count() ) );
438+
QgsDebugMsg( QString( "numRows: %1" ).arg( mClassListWidget->count() ) );
439439
//
440440
if ( mClassListWidget->count() < ( currentIndex + 1 ) )
441441
{

src/app/qgsuniquevaluedialog.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,13 @@ void QgsUniqueValueDialog::changeClassificationAttribute()
298298
return;
299299
}
300300

301-
QStringList values;
302-
provider->getUniqueValues( nr, values );
301+
QList<QVariant> values;
302+
provider->uniqueValues( nr, values );
303303

304304
for ( int i = 0; i < values.size(); i++ )
305305
{
306-
if ( !mValues.contains( values[i] ) )
307-
addClass( values[i] );
306+
if ( !mValues.contains( values[i].toString() ) )
307+
addClass( values[i].toString() );
308308
}
309309
}
310310
}

src/core/qgsvectordataprovider.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ QVariant QgsVectorDataProvider::minimumValue( int index )
273273
return mCacheMinValues[index];
274274
}
275275

276-
QVariant QgsVectorDataProvider::maxValue( int index )
276+
QVariant QgsVectorDataProvider::maximumValue( int index )
277277
{
278278
if ( !fields().contains( index ) )
279279
{
@@ -292,19 +292,24 @@ QVariant QgsVectorDataProvider::maxValue( int index )
292292
return mCacheMaxValues[index];
293293
}
294294

295-
void QgsVectorDataProvider::getUniqueValues( int index, QStringList &values )
295+
void QgsVectorDataProvider::uniqueValues( int index, QList<QVariant> &values )
296296
{
297297
QgsFeature f;
298298
QgsAttributeList keys;
299299
keys.append( index );
300300
select( keys, QgsRect(), false );
301301

302-
QMap<QString, int> map;
302+
QSet<QString> set;
303+
values.clear();
303304

304305
while ( getNextFeature( f ) )
305-
map.insert( f.attributeMap()[index].toString(), 1 );
306-
307-
values = map.keys();
306+
{
307+
if ( set.contains( f.attributeMap()[index].toString() ) )
308+
{
309+
values.append( f.attributeMap()[index] );
310+
set.insert( f.attributeMap()[index].toString() );
311+
}
312+
}
308313
}
309314

310315
void QgsVectorDataProvider::fillMinMaxCache()
@@ -356,3 +361,13 @@ void QgsVectorDataProvider::fillMinMaxCache()
356361

357362
mCacheMinMaxDirty = FALSE;
358363
}
364+
365+
QVariant QgsVectorDataProvider::convertValue( QVariant::Type type, QString value )
366+
{
367+
QVariant v( value );
368+
369+
if ( !v.convert( type ) )
370+
v = QVariant( QString::null );
371+
372+
return v;
373+
}

src/core/qgsvectordataprovider.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
168168
* and maximal values. If provider has facilities to retrieve maximal
169169
* value directly, override this function.
170170
*/
171-
virtual QVariant maxValue( int index );
171+
virtual QVariant maximumValue( int index );
172172

173173
/**
174174
* Return unique values of an attribute
@@ -177,7 +177,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
177177
*
178178
* Default implementation simply iterates the features
179179
*/
180-
virtual void getUniqueValues( int index, QStringList &uniqueValues );
180+
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues );
181181

182182
/**
183183
* Adds a list of features
@@ -274,6 +274,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
274274
void setFetchFeaturesWithoutGeom( bool fetch );
275275

276276
protected:
277+
QVariant convertValue( QVariant::Type type, QString value );
277278

278279
void fillMinMaxCache();
279280

src/providers/ogr/qgsogrprovider.cpp

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ QgsCoordinateReferenceSystem QgsOgrProvider::getCRS()
12561256
return srs;
12571257
}
12581258

1259-
void QgsOgrProvider::getUniqueValues( int index, QStringList &uniqueValues )
1259+
void QgsOgrProvider::uniqueValues( int index, QList<QVariant> &uniqueValues )
12601260
{
12611261
QgsField fld = mAttributeFields[index];
12621262
QFileInfo fi( dataSourceUri() );
@@ -1274,7 +1274,7 @@ void QgsOgrProvider::getUniqueValues( int index, QStringList &uniqueValues )
12741274
OGRFeatureH f;
12751275
while ( 0 != ( f = OGR_L_GetNextFeature( lyr ) ) )
12761276
{
1277-
uniqueValues.append( mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) ) );
1277+
uniqueValues << convertValue( fld.type(), mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) ) );
12781278
OGR_F_Destroy( f );
12791279
}
12801280

@@ -1304,26 +1304,15 @@ QVariant QgsOgrProvider::minimumValue( int index )
13041304
return QVariant();
13051305
}
13061306

1307-
QString str = mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) );
1307+
QVariant value = convertValue( fld.type(), mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) ) );
13081308
OGR_F_Destroy( f );
13091309

1310-
QVariant value;
1311-
1312-
switch ( fld.type() )
1313-
{
1314-
case QVariant::String: value = QVariant( str ); break;
1315-
case QVariant::Int: value = QVariant( str.toInt() ); break;
1316-
case QVariant::Double: value = QVariant( str.toDouble() ); break;
1317-
//case QVariant::DateTime: value = QVariant(QDateTime::fromString(str)); break;
1318-
default: assert( NULL && "unsupported field type" );
1319-
}
1320-
13211310
OGR_DS_ReleaseResultSet( ogrDataSource, l );
13221311

13231312
return value;
13241313
}
13251314

1326-
QVariant QgsOgrProvider::maxValue( int index )
1315+
QVariant QgsOgrProvider::maximumValue( int index )
13271316
{
13281317
QgsField fld = mAttributeFields[index];
13291318
QFileInfo fi( dataSourceUri() );
@@ -1343,20 +1332,9 @@ QVariant QgsOgrProvider::maxValue( int index )
13431332
return QVariant();
13441333
}
13451334

1346-
QString str = mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) );
1335+
QVariant value = convertValue( fld.type(), mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) ) );
13471336
OGR_F_Destroy( f );
13481337

1349-
QVariant value;
1350-
1351-
switch ( fld.type() )
1352-
{
1353-
case QVariant::String: value = QVariant( str ); break;
1354-
case QVariant::Int: value = QVariant( str.toInt() ); break;
1355-
case QVariant::Double: value = QVariant( str.toDouble() ); break;
1356-
//case QVariant::DateTime: value = QVariant(QDateTime::fromString(str)); break;
1357-
default: assert( NULL && "unsupported field type" );
1358-
}
1359-
13601338
OGR_DS_ReleaseResultSet( ogrDataSource, l );
13611339

13621340
return value;

src/providers/ogr/qgsogrprovider.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ class QgsOgrProvider : public QgsVectorDataProvider
175175

176176
/** Returns the maximum value of an attribute
177177
* @param index the index of the attribute */
178-
QVariant maxValue( int index );
178+
QVariant maximumValue( int index );
179179

180180
/** Return the unique values of an attribute
181181
* @param index the index of the attribute
182182
* @param values reference to the list of unique values */
183-
virtual void getUniqueValues( int index, QStringList &uniqueValues );
183+
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues );
184184

185185
protected:
186186
/** loads fields from input file to member attributeFields */

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -494,30 +494,7 @@ bool QgsPostgresProvider::getFeature( PGresult *queryResult, int row, bool fetch
494494

495495
if ( !PQgetisnull( queryResult, row, col ) )
496496
{
497-
QString val = QString::fromUtf8( PQgetvalue( queryResult, row, col ) );
498-
499-
switch ( fld.type() )
500-
{
501-
case QVariant::LongLong:
502-
feature.addAttribute( *it, val.toLongLong() );
503-
break;
504-
case QVariant::Int:
505-
feature.addAttribute( *it, val.toInt() );
506-
break;
507-
case QVariant::Double:
508-
feature.addAttribute( *it, val.toDouble() );
509-
break;
510-
case QVariant::String:
511-
feature.addAttribute( *it, val );
512-
break;
513-
default:
514-
QgsDebugMsg( QString( "feature %1, field %2, value '%3': unexpected variant type %4 considered as NULL" )
515-
.arg( oid )
516-
.arg( fld.name() )
517-
.arg( val )
518-
.arg( fld.type() ) );
519-
feature.addAttribute( *it, QVariant( QString::null ) );
520-
}
497+
feature.addAttribute( *it, convertValue( fld.type(), QString::fromUtf8( PQgetvalue( queryResult, row, col ) ) ) );
521498
}
522499
else
523500
{
@@ -1540,8 +1517,7 @@ QVariant QgsPostgresProvider::minimumValue( int index )
15401517
.arg( sqlWhereClause );
15411518
}
15421519
Result rmin = connectionRO->PQexec( sql );
1543-
QString minimumValue = QString::fromUtf8( PQgetvalue( rmin, 0, 0 ) );
1544-
return minimumValue.toDouble();
1520+
return convertValue( fld.type(), QString::fromUtf8( PQgetvalue( rmin, 0, 0 ) ) );
15451521
}
15461522
catch ( PGFieldNotFound )
15471523
{
@@ -1550,7 +1526,7 @@ QVariant QgsPostgresProvider::minimumValue( int index )
15501526
}
15511527

15521528
// Returns the list of unique values of an attribute
1553-
void QgsPostgresProvider::getUniqueValues( int index, QStringList &uniqueValues )
1529+
void QgsPostgresProvider::uniqueValues( int index, QList<QVariant> &uniqueValues )
15541530
{
15551531
uniqueValues.clear();
15561532

@@ -1586,8 +1562,7 @@ void QgsPostgresProvider::getUniqueValues( int index, QStringList &uniqueValues
15861562
}
15871563

15881564
// Returns the maximum value of an attribute
1589-
1590-
QVariant QgsPostgresProvider::maxValue( int index )
1565+
QVariant QgsPostgresProvider::maximumValue( int index )
15911566
{
15921567
try
15931568
{
@@ -1608,8 +1583,7 @@ QVariant QgsPostgresProvider::maxValue( int index )
16081583
.arg( sqlWhereClause );
16091584
}
16101585
Result rmax = connectionRO->PQexec( sql );
1611-
QString maxValue = QString::fromUtf8( PQgetvalue( rmax, 0, 0 ) );
1612-
return maxValue.toDouble();
1586+
return convertValue( fld.type(), QString::fromUtf8( PQgetvalue( rmax, 0, 0 ) ) );
16131587
}
16141588
catch ( PGFieldNotFound )
16151589
{

src/providers/postgres/qgspostgresprovider.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ class QgsPostgresProvider : public QgsVectorDataProvider
184184

185185
/** Returns the maximum value of an attribute
186186
* @param index the index of the attribute */
187-
QVariant maxValue( int index );
187+
QVariant maximumValue( int index );
188188

189189
/** Return the unique values of an attribute
190190
* @param index the index of the attribute
191191
* @param values reference to the list of unique values */
192-
virtual void getUniqueValues( int index, QStringList &uniqueValues );
192+
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues );
193193

194194
/**Returns true if layer is valid
195195
*/

0 commit comments

Comments
 (0)