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@9196 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
jef 2008-08-27 22:13:27 +00:00
parent efc21daa54
commit e3cfb78796
11 changed files with 61 additions and 86 deletions

View File

@ -132,7 +132,7 @@ class QgsVectorDataProvider : QgsDataProvider
* and maximal values. If provider has facilities to retrieve maximal
* value directly, override this function.
*/
virtual QVariant maxValue(int index);
virtual QVariant maximumValue(int index);
/**
* Return unique values of an attribute
@ -141,7 +141,7 @@ class QgsVectorDataProvider : QgsDataProvider
*
* Default implementation simply iterates the features
*/
virtual void getUniqueValues(int index, QStringList &uniqueValues);
virtual void uniqueValues(int index, QList<QVariant> &uniqueValues);
/**
* Adds a list of features

View File

@ -112,12 +112,14 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
{
case QgsVectorLayer::UniqueValues:
{
QStringList values;
mLayer->dataProvider()->getUniqueValues( it.key(), values );
QList<QVariant> values;
mLayer->dataProvider()->uniqueValues( it.key(), values );
QComboBox *cb = new QComboBox();
cb->setEditable( true );
cb->addItems( values );
for ( QList<QVariant>::iterator it = values.begin(); it != values.end(); it++ )
cb->addItem( it->toString() );
int idx = cb->findText( myFieldValue.toString() );
if ( idx >= 0 )
@ -219,10 +221,14 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
if ( editType == QgsVectorLayer::UniqueValuesEditable )
{
QStringList values;
mLayer->dataProvider()->getUniqueValues( it.key(), values );
QList<QVariant> values;
mLayer->dataProvider()->uniqueValues( it.key(), values );
QCompleter *c = new QCompleter( values );
QStringList svalues;
for ( QList<QVariant>::const_iterator it = values.begin(); it != values.end(); it++ )
svalues << it->toString();
QCompleter *c = new QCompleter( svalues );
c->setCompletionMode( QCompleter::PopupCompletion );
le->setCompleter( c );
}

View File

@ -23,6 +23,7 @@
#include "qgssymbol.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include <QColorDialog>
@ -168,11 +169,11 @@ void QgsContinuousColorDialog::apply()
if ( provider )
{
minimum = provider->minimumValue( classfield ).toDouble();
maximum = provider->maxValue( classfield ).toDouble();
maximum = provider->maximumValue( classfield ).toDouble();
}
else
{
qWarning( "Warning, provider is null in QgsGraSyExtensionWidget::QgsGraSyExtensionWidget(...)" );
QgsDebugMsg( "Warning, provider is null" );
return;
}

View File

@ -254,7 +254,7 @@ void QgsGraduatedSymbolDialog::adjustClassification()
if ( modeComboBox->currentText() == tr( "Equal Interval" ) )
{
minimum = provider->minimumValue( field ).toDouble();
maximum = provider->maxValue( field ).toDouble();
maximum = provider->maximumValue( field ).toDouble();
}
else //don't waste performance if mMode is QgsGraduatedSymbolDialog::EMPTY
{
@ -435,7 +435,7 @@ void QgsGraduatedSymbolDialog::deleteCurrentClass()
int currentIndex = mClassListWidget->currentRow();
mEntries.erase( classValue );
delete( mClassListWidget->takeItem( currentIndex ) );
QgsDebugMsg( QString("numRows: %1").arg( mClassListWidget->count() ) );
QgsDebugMsg( QString( "numRows: %1" ).arg( mClassListWidget->count() ) );
//
if ( mClassListWidget->count() < ( currentIndex + 1 ) )
{

View File

@ -298,13 +298,13 @@ void QgsUniqueValueDialog::changeClassificationAttribute()
return;
}
QStringList values;
provider->getUniqueValues( nr, values );
QList<QVariant> values;
provider->uniqueValues( nr, values );
for ( int i = 0; i < values.size(); i++ )
{
if ( !mValues.contains( values[i] ) )
addClass( values[i] );
if ( !mValues.contains( values[i].toString() ) )
addClass( values[i].toString() );
}
}
}

View File

@ -273,7 +273,7 @@ QVariant QgsVectorDataProvider::minimumValue( int index )
return mCacheMinValues[index];
}
QVariant QgsVectorDataProvider::maxValue( int index )
QVariant QgsVectorDataProvider::maximumValue( int index )
{
if ( !fields().contains( index ) )
{
@ -292,19 +292,24 @@ QVariant QgsVectorDataProvider::maxValue( int index )
return mCacheMaxValues[index];
}
void QgsVectorDataProvider::getUniqueValues( int index, QStringList &values )
void QgsVectorDataProvider::uniqueValues( int index, QList<QVariant> &values )
{
QgsFeature f;
QgsAttributeList keys;
keys.append( index );
select( keys, QgsRect(), false );
QMap<QString, int> map;
QSet<QString> set;
values.clear();
while ( getNextFeature( f ) )
map.insert( f.attributeMap()[index].toString(), 1 );
values = map.keys();
{
if ( set.contains( f.attributeMap()[index].toString() ) )
{
values.append( f.attributeMap()[index] );
set.insert( f.attributeMap()[index].toString() );
}
}
}
void QgsVectorDataProvider::fillMinMaxCache()
@ -356,3 +361,13 @@ void QgsVectorDataProvider::fillMinMaxCache()
mCacheMinMaxDirty = FALSE;
}
QVariant QgsVectorDataProvider::convertValue( QVariant::Type type, QString value )
{
QVariant v( value );
if ( !v.convert( type ) )
v = QVariant( QString::null );
return v;
}

View File

@ -168,7 +168,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
* and maximal values. If provider has facilities to retrieve maximal
* value directly, override this function.
*/
virtual QVariant maxValue( int index );
virtual QVariant maximumValue( int index );
/**
* Return unique values of an attribute
@ -177,7 +177,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
*
* Default implementation simply iterates the features
*/
virtual void getUniqueValues( int index, QStringList &uniqueValues );
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues );
/**
* Adds a list of features
@ -274,6 +274,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
void setFetchFeaturesWithoutGeom( bool fetch );
protected:
QVariant convertValue( QVariant::Type type, QString value );
void fillMinMaxCache();

View File

@ -1256,7 +1256,7 @@ QgsCoordinateReferenceSystem QgsOgrProvider::getCRS()
return srs;
}
void QgsOgrProvider::getUniqueValues( int index, QStringList &uniqueValues )
void QgsOgrProvider::uniqueValues( int index, QList<QVariant> &uniqueValues )
{
QgsField fld = mAttributeFields[index];
QFileInfo fi( dataSourceUri() );
@ -1274,7 +1274,7 @@ void QgsOgrProvider::getUniqueValues( int index, QStringList &uniqueValues )
OGRFeatureH f;
while ( 0 != ( f = OGR_L_GetNextFeature( lyr ) ) )
{
uniqueValues.append( mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) ) );
uniqueValues << convertValue( fld.type(), mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) ) );
OGR_F_Destroy( f );
}
@ -1304,26 +1304,15 @@ QVariant QgsOgrProvider::minimumValue( int index )
return QVariant();
}
QString str = mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) );
QVariant value = convertValue( fld.type(), mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) ) );
OGR_F_Destroy( f );
QVariant value;
switch ( fld.type() )
{
case QVariant::String: value = QVariant( str ); break;
case QVariant::Int: value = QVariant( str.toInt() ); break;
case QVariant::Double: value = QVariant( str.toDouble() ); break;
//case QVariant::DateTime: value = QVariant(QDateTime::fromString(str)); break;
default: assert( NULL && "unsupported field type" );
}
OGR_DS_ReleaseResultSet( ogrDataSource, l );
return value;
}
QVariant QgsOgrProvider::maxValue( int index )
QVariant QgsOgrProvider::maximumValue( int index )
{
QgsField fld = mAttributeFields[index];
QFileInfo fi( dataSourceUri() );
@ -1343,20 +1332,9 @@ QVariant QgsOgrProvider::maxValue( int index )
return QVariant();
}
QString str = mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) );
QVariant value = convertValue( fld.type(), mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) ) );
OGR_F_Destroy( f );
QVariant value;
switch ( fld.type() )
{
case QVariant::String: value = QVariant( str ); break;
case QVariant::Int: value = QVariant( str.toInt() ); break;
case QVariant::Double: value = QVariant( str.toDouble() ); break;
//case QVariant::DateTime: value = QVariant(QDateTime::fromString(str)); break;
default: assert( NULL && "unsupported field type" );
}
OGR_DS_ReleaseResultSet( ogrDataSource, l );
return value;

View File

@ -175,12 +175,12 @@ class QgsOgrProvider : public QgsVectorDataProvider
/** Returns the maximum value of an attribute
* @param index the index of the attribute */
QVariant maxValue( int index );
QVariant maximumValue( int index );
/** Return the unique values of an attribute
* @param index the index of the attribute
* @param values reference to the list of unique values */
virtual void getUniqueValues( int index, QStringList &uniqueValues );
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues );
protected:
/** loads fields from input file to member attributeFields */

View File

@ -494,30 +494,7 @@ bool QgsPostgresProvider::getFeature( PGresult *queryResult, int row, bool fetch
if ( !PQgetisnull( queryResult, row, col ) )
{
QString val = QString::fromUtf8( PQgetvalue( queryResult, row, col ) );
switch ( fld.type() )
{
case QVariant::LongLong:
feature.addAttribute( *it, val.toLongLong() );
break;
case QVariant::Int:
feature.addAttribute( *it, val.toInt() );
break;
case QVariant::Double:
feature.addAttribute( *it, val.toDouble() );
break;
case QVariant::String:
feature.addAttribute( *it, val );
break;
default:
QgsDebugMsg( QString( "feature %1, field %2, value '%3': unexpected variant type %4 considered as NULL" )
.arg( oid )
.arg( fld.name() )
.arg( val )
.arg( fld.type() ) );
feature.addAttribute( *it, QVariant( QString::null ) );
}
feature.addAttribute( *it, convertValue( fld.type(), QString::fromUtf8( PQgetvalue( queryResult, row, col ) ) ) );
}
else
{
@ -1540,8 +1517,7 @@ QVariant QgsPostgresProvider::minimumValue( int index )
.arg( sqlWhereClause );
}
Result rmin = connectionRO->PQexec( sql );
QString minimumValue = QString::fromUtf8( PQgetvalue( rmin, 0, 0 ) );
return minimumValue.toDouble();
return convertValue( fld.type(), QString::fromUtf8( PQgetvalue( rmin, 0, 0 ) ) );
}
catch ( PGFieldNotFound )
{
@ -1550,7 +1526,7 @@ QVariant QgsPostgresProvider::minimumValue( int index )
}
// Returns the list of unique values of an attribute
void QgsPostgresProvider::getUniqueValues( int index, QStringList &uniqueValues )
void QgsPostgresProvider::uniqueValues( int index, QList<QVariant> &uniqueValues )
{
uniqueValues.clear();
@ -1586,8 +1562,7 @@ void QgsPostgresProvider::getUniqueValues( int index, QStringList &uniqueValues
}
// Returns the maximum value of an attribute
QVariant QgsPostgresProvider::maxValue( int index )
QVariant QgsPostgresProvider::maximumValue( int index )
{
try
{
@ -1608,8 +1583,7 @@ QVariant QgsPostgresProvider::maxValue( int index )
.arg( sqlWhereClause );
}
Result rmax = connectionRO->PQexec( sql );
QString maxValue = QString::fromUtf8( PQgetvalue( rmax, 0, 0 ) );
return maxValue.toDouble();
return convertValue( fld.type(), QString::fromUtf8( PQgetvalue( rmax, 0, 0 ) ) );
}
catch ( PGFieldNotFound )
{

View File

@ -184,12 +184,12 @@ class QgsPostgresProvider : public QgsVectorDataProvider
/** Returns the maximum value of an attribute
* @param index the index of the attribute */
QVariant maxValue( int index );
QVariant maximumValue( int index );
/** Return the unique values of an attribute
* @param index the index of the attribute
* @param values reference to the list of unique values */
virtual void getUniqueValues( int index, QStringList &uniqueValues );
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues );
/**Returns true if layer is valid
*/