Skip to content

Commit

Permalink
vector provider update:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jef committed Aug 27, 2008
1 parent efc21da commit e3cfb78
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 86 deletions.
4 changes: 2 additions & 2 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 12 additions & 6 deletions src/app/qgsattributedialog.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );

QStringList svalues;
for ( QList<QVariant>::const_iterator it = values.begin(); it != values.end(); it++ )
svalues << it->toString();

QCompleter *c = new QCompleter( values );
QCompleter *c = new QCompleter( svalues );
c->setCompletionMode( QCompleter::PopupCompletion );
le->setCompleter( c );
}
Expand Down
5 changes: 3 additions & 2 deletions src/app/qgscontinuouscolordialog.cpp
Expand Up @@ -23,6 +23,7 @@
#include "qgssymbol.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"

#include <QColorDialog>

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsgraduatedsymboldialog.cpp
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 ) )
{
Expand Down
8 changes: 4 additions & 4 deletions src/app/qgsuniquevaluedialog.cpp
Expand Up @@ -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() );
}
}
}
Expand Down
27 changes: 21 additions & 6 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -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 ) )
{
Expand All @@ -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()
Expand Down Expand Up @@ -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;
}
5 changes: 3 additions & 2 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -274,6 +274,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
void setFetchFeaturesWithoutGeom( bool fetch );

protected:
QVariant convertValue( QVariant::Type type, QString value );

void fillMinMaxCache();

Expand Down
32 changes: 5 additions & 27 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -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() );
Expand All @@ -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 );
}

Expand Down Expand Up @@ -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() );
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/providers/ogr/qgsogrprovider.h
Expand Up @@ -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 */
Expand Down
36 changes: 5 additions & 31 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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();

Expand Down Expand Up @@ -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
{
Expand All @@ -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 )
{
Expand Down
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -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
*/
Expand Down

0 comments on commit e3cfb78

Please sign in to comment.