Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support for enumerations as an edit type in the infotool dialog (with…
… combo box selection)

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10902 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jun 11, 2009
1 parent f256b49 commit 460c9cd
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -146,6 +146,13 @@ class QgsVectorDataProvider : QgsDataProvider
*/
virtual void uniqueValues(int index, QList<QVariant> &uniqueValues /Out/);

/**Returns the possible enum values of an attribute. Returns an empty stringlist if a provider does not support enum types
or if the given attribute is not an enum type.
* @param index the index of the attribute
* @param enumList reference to the list to fill
@note: added in version 1.2*/
virtual void enumValues( int index, QStringList& enumList /Out/);

/**
* Adds a list of features
* @return true in case of success and false in case of failure
Expand Down
15 changes: 15 additions & 0 deletions src/app/qgsattributedialog.cpp
Expand Up @@ -132,6 +132,21 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
}
break;

case QgsVectorLayer::Enumeration:
{
QStringList enumValues;
mLayer->dataProvider()->enumValues(it.key(), enumValues);

QComboBox *cb = new QComboBox();
QStringList::const_iterator s_it = enumValues.constBegin();
for(; s_it != enumValues.constEnd(); ++s_it)
{
cb->addItem(*s_it);
}
myWidget = cb;
}
break;

case QgsVectorLayer::ValueMap:
{
const QMap<QString, QVariant> &map = vl->valueMap( it.key() );
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -188,6 +188,7 @@ void QgsVectorLayerProperties::setRow( int row, int idx, const QgsField &field )
cb->addItem( tr( "range (editable)" ), QgsVectorLayer::EditRange );
cb->addItem( tr( "range (slider)" ), QgsVectorLayer::SliderRange );
cb->addItem( tr( "file name" ), QgsVectorLayer::FileName );
cb->addItem( tr( "enumeration" ), QgsVectorLayer::Enumeration);
cb->setSizeAdjustPolicy( QComboBox::AdjustToContentsOnFirstShow );
cb->setCurrentIndex( layer->editType( idx ) );

Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -187,6 +187,13 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
*/
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues );

/**Returns the possible enum values of an attribute. Returns an empty stringlist if a provider does not support enum types
or if the given attribute is not an enum type.
* @param index the index of the attribute
* @param enumList reference to the list to fill
@note: added in version 1.2*/
virtual void enumValues( int index, QStringList& enumList ) { enumList.clear(); }

/**
* Adds a list of features
* @return true in case of success and false in case of failure
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -69,7 +69,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
Classification,
EditRange,
SliderRange,
FileName
FileName,
Enumeration
};

struct RangeData
Expand Down
60 changes: 60 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -1680,6 +1680,66 @@ void QgsPostgresProvider::uniqueValues( int index, QList<QVariant> &uniqueValues
}
}

void QgsPostgresProvider::enumValues( int index, QStringList& enumList )
{
enumList.clear();

QString typeName;
//find out type of index
QgsFieldMap::const_iterator f_it = attributeFields.find(index);
if(f_it != attributeFields.constEnd())
{
typeName = f_it.value().typeName();
}
else
{
return;
}

//is type an enum or a domain type?
QString typeSql = QString("SELECT typtype FROM pg_type where typname = %1").arg(quotedValue(typeName));
Result typeRes = connectionRO->PQexec( typeSql );
if ( PQresultStatus( typeRes ) != PGRES_TUPLES_OK || PQntuples(typeRes) < 1)
{
return;
}

QString typtype = PQgetvalue( typeRes, 0, 0 );
if(typtype.compare("e", Qt::CaseInsensitive) == 0)
{
//parse enum_range
QString enumRangeSql = QString("SELECT enum_range(%1) from %2 limit1").arg(quotedIdentifier(f_it.value().name())).arg(mSchemaTableName);
Result enumRangeRes = connectionRO->PQexec(enumRangeSql);
if ( PQresultStatus( enumRangeRes ) != PGRES_TUPLES_OK || PQntuples(enumRangeRes) > 0)
{
QString enumRangeString = PQgetvalue(enumRangeRes, 0, 0);
//strip away the brackets at begin and end
enumRangeString.chop(1);
enumRangeString.remove(0, 1);
QStringList rangeSplit = enumRangeString.split(",");
QStringList::const_iterator range_it = rangeSplit.constBegin();
for(; range_it != rangeSplit.constEnd(); ++range_it)
{
QString currentEnumValue = *range_it;
//remove quotes from begin and end of the value
if(currentEnumValue.startsWith("'") || currentEnumValue.startsWith("\""))
{
currentEnumValue.remove(0, 1);
}
if(currentEnumValue.endsWith("'") || currentEnumValue.endsWith("\""))
{
currentEnumValue.chop(1);
}
enumList << currentEnumValue;
}
}
}
else if (typtype.compare("d", Qt::CaseInsensitive) == 0)
{
//a domain type. Todo: evaluate the check constraint
}
}

// Returns the maximum value of an attribute
QVariant QgsPostgresProvider::maximumValue( int index )
{
Expand Down
7 changes: 7 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -191,6 +191,13 @@ class QgsPostgresProvider : public QgsVectorDataProvider
* @param values reference to the list of unique values */
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues );

/**Returns the possible enum values of an attribute. Returns an empty stringlist if a provider does not support enum types
or if the given attribute is not an enum type.
* @param index the index of the attribute
* @param enumList reference to the list to fill
@note: added in version 1.2*/
virtual void enumValues( int index, QStringList& enumList);

/**Returns true if layer is valid
*/
bool isValid();
Expand Down

0 comments on commit 460c9cd

Please sign in to comment.