Skip to content

Commit 2c0e214

Browse files
author
mhugent
committed
Add uniqueValues() method for vector layer. Fixes #3528
git-svn-id: http://svn.osgeo.org/qgis/trunk@15310 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent a75d755 commit 2c0e214

File tree

5 files changed

+83
-10
lines changed

5 files changed

+83
-10
lines changed

python/core/qgsvectorlayer.sip

+7
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,13 @@ public:
549549
@note public and static from version 1.4 */
550550
static void drawVertexMarker( double x, double y, QPainter& p, QgsVectorLayer::VertexMarkerType type, int vertexSize );
551551

552+
/**Returns unique values for column
553+
@param index column index for attribute
554+
@param uniqueValues out: result list
555+
@limit maximum number of values to return (-1 if unlimited)
556+
@note: this method was added in version 1.7*/
557+
void uniqueValues( int index, QList<QVariant>& uniqueValues, int limit );
558+
552559
public slots:
553560

554561
/** Select feature by its ID, optionally emit signal selectionChanged() */

src/core/qgsvectorlayer.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -4942,6 +4942,67 @@ void QgsVectorLayer::createJoinCaches()
49424942
}
49434943
}
49444944

4945+
void QgsVectorLayer::uniqueValues( int index, QList<QVariant> &uniqueValues, int limit )
4946+
{
4947+
uniqueValues.clear();
4948+
if ( !mDataProvider )
4949+
{
4950+
return;
4951+
}
4952+
4953+
int maxProviderIndex;
4954+
QgsVectorLayerJoinBuffer::maximumIndex( mDataProvider->fields(), maxProviderIndex );
4955+
4956+
if ( index <= maxProviderIndex && !mEditable ) //a provider field
4957+
{
4958+
return mDataProvider->uniqueValues( index, uniqueValues, limit );
4959+
}
4960+
else // a joined field?
4961+
{
4962+
if ( mJoinBuffer )
4963+
{
4964+
int indexOffset; //offset between layer index and joined provider index
4965+
const QgsVectorJoinInfo* join = mJoinBuffer->joinForFieldIndex( index, maxProviderIndex, indexOffset );
4966+
if ( join )
4967+
{
4968+
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( join->joinLayerId ) );
4969+
if ( vl && vl->dataProvider() )
4970+
{
4971+
return vl->dataProvider()->uniqueValues( index - indexOffset, uniqueValues, limit );
4972+
}
4973+
}
4974+
}
4975+
}
4976+
4977+
4978+
//the layer is editable, but in certain cases it can still be avoided going through all features
4979+
if ( mDeletedFeatureIds.size() < 1 && mAddedFeatures.size() < 1 && !mDeletedAttributeIds.contains( index ) && mChangedAttributeValues.size() < 1 )
4980+
{
4981+
return mDataProvider->uniqueValues( index, uniqueValues, limit );
4982+
}
4983+
4984+
//we need to go through each feature
4985+
QgsAttributeList attList;
4986+
attList << index;
4987+
4988+
select( attList, QgsRectangle(), false, false );
4989+
4990+
QgsFeature f;
4991+
QVariant currentValue;
4992+
QHash<QString, QVariant> val;
4993+
while ( nextFeature( f ) )
4994+
{
4995+
currentValue = f.attributeMap()[index];
4996+
val.insert( currentValue.toString(), currentValue );
4997+
if ( limit >= 0 && val.size() >= limit )
4998+
{
4999+
break;
5000+
}
5001+
}
5002+
5003+
uniqueValues = val.values();
5004+
}
5005+
49455006
void QgsVectorLayer::stopRendererV2( QgsRenderContext& rendererContext, QgsSingleSymbolRendererV2* selRenderer )
49465007
{
49475008
mRendererV2->stopRender( rendererContext );

src/core/qgsvectorlayer.h

+7
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,13 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
638638
/**Caches joined attributes if required (and not already done)*/
639639
void createJoinCaches();
640640

641+
/**Returns unique values for column
642+
@param index column index for attribute
643+
@param uniqueValues out: result list
644+
@limit maximum number of values to return (-1 if unlimited)
645+
@note: this method was added in version 1.7*/
646+
void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 );
647+
641648

642649
public slots:
643650
/** Select feature by its ID, optionally emit signal selectionChanged() */

src/core/qgsvectorlayerjoinbuffer.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ class CORE_EXPORT QgsVectorLayerJoinBuffer
6666

6767
const QList< QgsVectorJoinInfo >& vectorJoins() const { return mVectorJoins; }
6868

69+
/**Finds the vector join for a layer field index.
70+
@param index this layers attribute index
71+
@param maxProviderIndex maximum attribute index of the vectorlayer provider
72+
@param indexOffset out: offset between layer index and original provider index
73+
@return pointer to the join if the index belongs to a joined field, otherwise 0 (possibily provider field or added field)*/
74+
const QgsVectorJoinInfo* joinForFieldIndex( int index, int maxProviderIndex, int& indexOffset ) const;
75+
6976
/** Helper function to find out the maximum index of a field map
7077
@return true in case of success, otherwise false (e.g. empty map)*/
7178
static bool maximumIndex( const QgsFieldMap& fMap, int& index );
@@ -91,13 +98,6 @@ class CORE_EXPORT QgsVectorLayerJoinBuffer
9198
@param attributeIndexOffset index offset to get from join layer attribute index to layer index*/
9299
void addJoinedFeatureAttributes( QgsFeature& f, const QgsVectorJoinInfo& joinInfo, const QString& joinFieldName, const QVariant& joinValue,
93100
const QgsAttributeList& attributes, int attributeIndexOffset );
94-
95-
/**Finds the vector join for a layer field index.
96-
@param index this layers attribute index
97-
@param maxProviderIndex maximum attribute index of the vectorlayer provider
98-
@param indexOffset out: offset between layer index and original provider index
99-
@return pointer to the join if the index belongs to a joined field, otherwise 0 (possibily provider field or added field)*/
100-
const QgsVectorJoinInfo* joinForFieldIndex( int index, int maxProviderIndex, int& indexOffset ) const;
101101
};
102102

103103
#endif // QGSVECTORLAYERJOINBUFFER_H

src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include "qgssymbolv2selectordialog.h"
1111

1212
#include "qgsvectorlayer.h"
13-
#include "qgsvectordataprovider.h" // for uniqueValues
14-
1513
#include <QMenu>
1614
#include <QMessageBox>
1715
#include <QStandardItemModel>
@@ -245,7 +243,7 @@ void QgsCategorizedSymbolRendererV2Widget::addCategories()
245243
QString attrName = cboCategorizedColumn->currentText();
246244
int idx = mLayer->fieldNameIndex( attrName );
247245
QList<QVariant> unique_vals;
248-
mLayer->dataProvider()->uniqueValues( idx, unique_vals );
246+
mLayer->uniqueValues( idx, unique_vals );
249247

250248
//DlgAddCategories dlg(mStyle, createDefaultSymbol(), unique_vals, this);
251249
//if (!dlg.exec())

0 commit comments

Comments
 (0)