[QGIS Commit] r10902 - in trunk/qgis: python/core src/app src/core src/providers/postgres

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Jun 11 03:23:32 EDT 2009


Author: mhugent
Date: 2009-06-11 03:23:31 -0400 (Thu, 11 Jun 2009)
New Revision: 10902

Modified:
   trunk/qgis/python/core/qgsvectordataprovider.sip
   trunk/qgis/src/app/qgsattributedialog.cpp
   trunk/qgis/src/app/qgsvectorlayerproperties.cpp
   trunk/qgis/src/core/qgsvectordataprovider.h
   trunk/qgis/src/core/qgsvectorlayer.h
   trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
   trunk/qgis/src/providers/postgres/qgspostgresprovider.h
Log:
Support for enumerations as an edit type in the infotool dialog (with combo box selection)

Modified: trunk/qgis/python/core/qgsvectordataprovider.sip
===================================================================
--- trunk/qgis/python/core/qgsvectordataprovider.sip	2009-06-11 00:14:35 UTC (rev 10901)
+++ trunk/qgis/python/core/qgsvectordataprovider.sip	2009-06-11 07:23:31 UTC (rev 10902)
@@ -146,6 +146,13 @@
        */
       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

Modified: trunk/qgis/src/app/qgsattributedialog.cpp
===================================================================
--- trunk/qgis/src/app/qgsattributedialog.cpp	2009-06-11 00:14:35 UTC (rev 10901)
+++ trunk/qgis/src/app/qgsattributedialog.cpp	2009-06-11 07:23:31 UTC (rev 10902)
@@ -132,6 +132,21 @@
       }
       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() );

Modified: trunk/qgis/src/app/qgsvectorlayerproperties.cpp
===================================================================
--- trunk/qgis/src/app/qgsvectorlayerproperties.cpp	2009-06-11 00:14:35 UTC (rev 10901)
+++ trunk/qgis/src/app/qgsvectorlayerproperties.cpp	2009-06-11 07:23:31 UTC (rev 10902)
@@ -188,6 +188,7 @@
   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 ) );
 

Modified: trunk/qgis/src/core/qgsvectordataprovider.h
===================================================================
--- trunk/qgis/src/core/qgsvectordataprovider.h	2009-06-11 00:14:35 UTC (rev 10901)
+++ trunk/qgis/src/core/qgsvectordataprovider.h	2009-06-11 07:23:31 UTC (rev 10902)
@@ -187,6 +187,13 @@
      */
     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

Modified: trunk/qgis/src/core/qgsvectorlayer.h
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.h	2009-06-11 00:14:35 UTC (rev 10901)
+++ trunk/qgis/src/core/qgsvectorlayer.h	2009-06-11 07:23:31 UTC (rev 10902)
@@ -69,7 +69,8 @@
       Classification,
       EditRange,
       SliderRange,
-      FileName
+      FileName,
+      Enumeration
     };
 
     struct RangeData

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2009-06-11 00:14:35 UTC (rev 10901)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2009-06-11 07:23:31 UTC (rev 10902)
@@ -1680,6 +1680,66 @@
   }
 }
 
+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 )
 {

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.h
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.h	2009-06-11 00:14:35 UTC (rev 10901)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.h	2009-06-11 07:23:31 UTC (rev 10902)
@@ -191,6 +191,13 @@
      *  @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();



More information about the QGIS-commit mailing list