Skip to content
Permalink
Browse files

Support for enumerations as an edit type in the infotool dialog (with…

… combo box selection)

git-svn-id: http://svn.osgeo.org/qgis/trunk@10902 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent
mhugent committed Jun 11, 2009
1 parent e959b2f commit 535490afff55c294b6d993f741db5e1727255510
@@ -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
@@ -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() );
@@ -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 ) );

@@ -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
@@ -69,7 +69,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
Classification,
EditRange,
SliderRange,
FileName
FileName,
Enumeration
};

struct RangeData
@@ -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 )
{
@@ -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();

0 comments on commit 535490a

Please sign in to comment.
You can’t perform that action at this time.