[QGIS Commit] r15310 - in trunk/qgis: python/core src/core src/gui/symbology-ng

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Mar 2 09:45:49 EST 2011


Author: mhugent
Date: 2011-03-02 06:45:49 -0800 (Wed, 02 Mar 2011)
New Revision: 15310

Modified:
   trunk/qgis/python/core/qgsvectorlayer.sip
   trunk/qgis/src/core/qgsvectorlayer.cpp
   trunk/qgis/src/core/qgsvectorlayer.h
   trunk/qgis/src/core/qgsvectorlayerjoinbuffer.h
   trunk/qgis/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
Log:
Add uniqueValues() method for vector layer. Fixes #3528

Modified: trunk/qgis/python/core/qgsvectorlayer.sip
===================================================================
--- trunk/qgis/python/core/qgsvectorlayer.sip	2011-03-02 10:16:18 UTC (rev 15309)
+++ trunk/qgis/python/core/qgsvectorlayer.sip	2011-03-02 14:45:49 UTC (rev 15310)
@@ -549,6 +549,13 @@
       @note public and static from version 1.4 */
     static void drawVertexMarker( double x, double y, QPainter& p, QgsVectorLayer::VertexMarkerType type, int vertexSize );
 
+    /**Returns unique values for column
+      @param index column index for attribute
+      @param uniqueValues out: result list
+      @limit maximum number of values to return (-1 if unlimited)
+      @note: this method was added in version 1.7*/
+    void uniqueValues( int index, QList<QVariant>& uniqueValues, int limit );
+
 public slots:
 
   /** Select feature by its ID, optionally emit signal selectionChanged() */

Modified: trunk/qgis/src/core/qgsvectorlayer.cpp
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.cpp	2011-03-02 10:16:18 UTC (rev 15309)
+++ trunk/qgis/src/core/qgsvectorlayer.cpp	2011-03-02 14:45:49 UTC (rev 15310)
@@ -4942,6 +4942,67 @@
   }
 }
 
+void QgsVectorLayer::uniqueValues( int index, QList<QVariant> &uniqueValues, int limit )
+{
+  uniqueValues.clear();
+  if ( !mDataProvider )
+  {
+    return;
+  }
+
+  int maxProviderIndex;
+  QgsVectorLayerJoinBuffer::maximumIndex( mDataProvider->fields(), maxProviderIndex );
+
+  if ( index <= maxProviderIndex && !mEditable ) //a provider field
+  {
+    return mDataProvider->uniqueValues( index, uniqueValues, limit );
+  }
+  else // a joined field?
+  {
+    if ( mJoinBuffer )
+    {
+      int indexOffset; //offset between layer index and joined provider index
+      const QgsVectorJoinInfo* join = mJoinBuffer->joinForFieldIndex( index, maxProviderIndex, indexOffset );
+      if ( join )
+      {
+        QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( join->joinLayerId ) );
+        if ( vl && vl->dataProvider() )
+        {
+          return vl->dataProvider()->uniqueValues( index - indexOffset, uniqueValues, limit );
+        }
+      }
+    }
+  }
+
+
+  //the layer is editable, but in certain cases it can still be avoided going through all features
+  if ( mDeletedFeatureIds.size() < 1 && mAddedFeatures.size() < 1 && !mDeletedAttributeIds.contains( index ) && mChangedAttributeValues.size() < 1 )
+  {
+    return mDataProvider->uniqueValues( index, uniqueValues, limit );
+  }
+
+  //we need to go through each feature
+  QgsAttributeList attList;
+  attList << index;
+
+  select( attList, QgsRectangle(), false, false );
+
+  QgsFeature f;
+  QVariant currentValue;
+  QHash<QString, QVariant> val;
+  while ( nextFeature( f ) )
+  {
+    currentValue = f.attributeMap()[index];
+    val.insert( currentValue.toString(), currentValue );
+    if ( limit >= 0 && val.size() >= limit )
+    {
+      break;
+    }
+  }
+
+  uniqueValues = val.values();
+}
+
 void QgsVectorLayer::stopRendererV2( QgsRenderContext& rendererContext, QgsSingleSymbolRendererV2* selRenderer )
 {
   mRendererV2->stopRender( rendererContext );

Modified: trunk/qgis/src/core/qgsvectorlayer.h
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.h	2011-03-02 10:16:18 UTC (rev 15309)
+++ trunk/qgis/src/core/qgsvectorlayer.h	2011-03-02 14:45:49 UTC (rev 15310)
@@ -638,7 +638,14 @@
     /**Caches joined attributes if required (and not already done)*/
     void createJoinCaches();
 
+    /**Returns unique values for column
+      @param index column index for attribute
+      @param uniqueValues out: result list
+      @limit maximum number of values to return (-1 if unlimited)
+      @note: this method was added in version 1.7*/
+    void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 );
 
+
   public slots:
     /** Select feature by its ID, optionally emit signal selectionChanged() */
     void select( int featureId, bool emitSignal = true );

Modified: trunk/qgis/src/core/qgsvectorlayerjoinbuffer.h
===================================================================
--- trunk/qgis/src/core/qgsvectorlayerjoinbuffer.h	2011-03-02 10:16:18 UTC (rev 15309)
+++ trunk/qgis/src/core/qgsvectorlayerjoinbuffer.h	2011-03-02 14:45:49 UTC (rev 15310)
@@ -66,6 +66,13 @@
 
     const QList< QgsVectorJoinInfo >& vectorJoins() const { return mVectorJoins; }
 
+    /**Finds the vector join for a layer field index.
+      @param index this layers attribute index
+      @param maxProviderIndex maximum attribute index of the vectorlayer provider
+      @param indexOffset out: offset between layer index and original provider index
+       @return pointer to the join if the index belongs to a joined field, otherwise 0 (possibily provider field or added field)*/
+    const QgsVectorJoinInfo* joinForFieldIndex( int index, int maxProviderIndex, int& indexOffset ) const;
+
     /** Helper function to find out the maximum index of a field map
         @return true in case of success, otherwise false (e.g. empty map)*/
     static bool maximumIndex( const QgsFieldMap& fMap, int& index );
@@ -91,13 +98,6 @@
       @param attributeIndexOffset index offset to get from join layer attribute index to layer index*/
     void addJoinedFeatureAttributes( QgsFeature& f, const QgsVectorJoinInfo& joinInfo, const QString& joinFieldName, const QVariant& joinValue,
                                      const QgsAttributeList& attributes, int attributeIndexOffset );
-
-    /**Finds the vector join for a layer field index.
-      @param index this layers attribute index
-      @param maxProviderIndex maximum attribute index of the vectorlayer provider
-      @param indexOffset out: offset between layer index and original provider index
-       @return pointer to the join if the index belongs to a joined field, otherwise 0 (possibily provider field or added field)*/
-    const QgsVectorJoinInfo* joinForFieldIndex( int index, int maxProviderIndex, int& indexOffset ) const;
 };
 
 #endif // QGSVECTORLAYERJOINBUFFER_H

Modified: trunk/qgis/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
===================================================================
--- trunk/qgis/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp	2011-03-02 10:16:18 UTC (rev 15309)
+++ trunk/qgis/src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp	2011-03-02 14:45:49 UTC (rev 15310)
@@ -10,8 +10,6 @@
 #include "qgssymbolv2selectordialog.h"
 
 #include "qgsvectorlayer.h"
-#include "qgsvectordataprovider.h" // for uniqueValues
-
 #include <QMenu>
 #include <QMessageBox>
 #include <QStandardItemModel>
@@ -245,7 +243,7 @@
   QString attrName = cboCategorizedColumn->currentText();
   int idx = mLayer->fieldNameIndex( attrName );
   QList<QVariant> unique_vals;
-  mLayer->dataProvider()->uniqueValues( idx, unique_vals );
+  mLayer->uniqueValues( idx, unique_vals );
 
   //DlgAddCategories dlg(mStyle, createDefaultSymbol(), unique_vals, this);
   //if (!dlg.exec())



More information about the QGIS-commit mailing list