Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add total feature count in legend, fixes #3402
  • Loading branch information
blazek committed Nov 5, 2012
1 parent 983535f commit 5de2fee
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 11 deletions.
20 changes: 17 additions & 3 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -88,6 +88,9 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
connect( this, SIGNAL( itemChanged( QTreeWidgetItem*, int ) ),
this, SLOT( handleItemChange( QTreeWidgetItem*, int ) ) );

connect( itemDelegate(), SIGNAL( closeEditor( QWidget *, QAbstractItemDelegate::EndEditHint ) ),
this, SLOT( handleCloseEditor( QWidget *, QAbstractItemDelegate::EndEditHint ) ) );

connect( this, SIGNAL( currentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ),
this, SLOT( handleCurrentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ) );

Expand Down Expand Up @@ -2153,6 +2156,14 @@ void QgsLegend::removePixmapHeightValue( int height )
//todo: adapt the icon size if height is the largest value and the size of the next element is higher than the minimum
}

void QgsLegend::handleCloseEditor( QWidget * editor, QAbstractItemDelegate::EndEditHint hint )
{
QgsLegendItem *item = dynamic_cast<QgsLegendItem *>( currentItem() );
if ( item )
{
item->afterEdit();
}
}

void QgsLegend::handleItemChange( QTreeWidgetItem* item, int column )
{
Expand Down Expand Up @@ -2185,8 +2196,10 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int column )
if ( ll )
{
//if the text of a QgsLegendLayer has changed, change the display names of all its maplayers
// TODO: is this still necessary?
ll->layer()->setLayerName( ll->text( 0 ) );
if ( ll->layerName() != ll->layer()->name() )
{
ll->layer()->setLayerName( ll->layerName() );
}
}

bool changing = mChanging;
Expand Down Expand Up @@ -2257,11 +2270,12 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int column )

void QgsLegend::openEditor()
{
QTreeWidgetItem* theItem = currentItem();
QgsLegendItem* theItem = dynamic_cast<QgsLegendItem*>( currentItem() );
if ( theItem )
{
if ( !groupEmbedded( theItem ) && !parentGroupEmbedded( theItem ) )
{
theItem->beforeEdit();
editItem( theItem, 0 );
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/legend/qgslegend.h
Expand Up @@ -443,6 +443,7 @@ class QgsLegend : public QTreeWidget
/** toogle update drawing order */
void toggleDrawingOrderUpdate();
void handleItemChange( QTreeWidgetItem* item, int row );
void handleCloseEditor( QWidget * editor, QAbstractItemDelegate::EndEditHint hint );
/** delegates current layer to map canvas */
void handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetItem* previous );
/**Calls openPersistentEditor for the current item*/
Expand Down
4 changes: 4 additions & 0 deletions src/app/legend/qgslegenditem.h
Expand Up @@ -106,6 +106,10 @@ class QgsLegendItem : public QTreeWidgetItem, public QObject
virtual void receive( QgsLegendItem *newChild ) { Q_UNUSED( newChild ); }
/**Do cleanups after a child item leaves (default empty)*/
virtual void release( QgsLegendItem *formerChild ) { Q_UNUSED( formerChild ); }
/**Called before edit*/
virtual void beforeEdit() {}
/**Called after edit*/
virtual void afterEdit() {}

protected:
LEGEND_ITEM_TYPE mType;
Expand Down
51 changes: 44 additions & 7 deletions src/app/legend/qgslegendlayer.cpp
Expand Up @@ -51,6 +51,7 @@ QgsLegendLayer::QgsLegendLayer( QgsMapLayer* layer )
, mLyr( layer )
, mDrawingOrder( -1 )
, mShowFeatureCount( false )
, mFeatureCount( -1 )
{
mType = LEGEND_LAYER;

Expand All @@ -66,7 +67,7 @@ QgsLegendLayer::QgsLegendLayer( QgsMapLayer* layer )

setCheckState( 0, Qt::Checked );

setText( 0, layer->name() );
layerNameChanged();
setupFont();

// Set the initial visibility flag for layers
Expand Down Expand Up @@ -296,6 +297,7 @@ void QgsLegendLayer::vectorLayerSymbologyV2( QgsVectorLayer* layer )
}

changeSymbologySettings( layer, itemList );
layerNameChanged(); // update total count
}
}

Expand Down Expand Up @@ -556,10 +558,41 @@ QgsMapCanvasLayer& QgsLegendLayer::canvasLayer()
return mLyr;
}

void QgsLegendLayer::layerNameChanged()
QString QgsLegendLayer::label() const
{
QString name = mLyr.layer()->name();
setText( 0, name );
if ( mShowFeatureCount && mFeatureCount >= 0 )
{
name += QString( " [%1]" ).arg( mFeatureCount );
}
return name;
}

void QgsLegendLayer::layerNameChanged()
{
setText( 0, label() );
}

void QgsLegendLayer::beforeEdit()
{
// Reset to layer name without possible feature count
setText( 0, mLyr.layer()->name() );
}

void QgsLegendLayer::afterEdit()
{
// Reset label with possible feature count, important if text was not changed
layerNameChanged();
}

QString QgsLegendLayer::layerName() const
{
// The text could be edited (Rename), in that case we have to return the new name
if ( text( 0 ) != label() && text( 0 ) != mLyr.layer()->name() )
{
return text( 0 );
}
return mLyr.layer()->name();
}

void QgsLegendLayer::updateAfterLayerModification()
Expand All @@ -580,10 +613,12 @@ void QgsLegendLayer::updateAfterLayerModification( bool onlyGeomChanged )
widthScale = canvas->map()->paintDevice().logicalDpiX() / 25.4;
}
refreshSymbology( mLyr.layer()->id(), widthScale );
layerNameChanged();
}

void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLayer* layer )
{
mFeatureCount = -1;
if ( !layer )
{
return;
Expand All @@ -598,12 +633,12 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
renderer->startRender( dummyContext, layer );

//create map holding the symbol count
QMap< QgsSymbolV2*, int > mSymbolCountMap;
QMap< QgsSymbolV2*, int > symbolCountMap;
QgsLegendSymbolList symbolList = renderer->legendSymbolItems();
QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin();
for ( ; symbolIt != symbolList.constEnd(); ++symbolIt )
{
mSymbolCountMap.insert( symbolIt->second, 0 );
symbolCountMap.insert( symbolIt->second, 0 );
}

//go through all features and count the number of occurrences
Expand All @@ -625,7 +660,7 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
QgsSymbolV2List symbolList = renderer->symbolsForFeature( f );
for ( QgsSymbolV2List::iterator symbolIt = symbolList.begin(); symbolIt != symbolList.end(); ++symbolIt )
{
mSymbolCountMap[*symbolIt] += 1;
symbolCountMap[*symbolIt] += 1;
}
++featuresCounted;
if ( featuresCounted % 50 == 0 )
Expand All @@ -637,9 +672,11 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
p.setValue( featuresCounted );
if ( p.wasCanceled() )
{
mFeatureCount = -1;
return;
}
}
mFeatureCount++;
}
renderer->stopRender( renderContext );
p.setValue( nFeatures );
Expand All @@ -655,7 +692,7 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
symbolIt = symbolList.constBegin();
for ( ; symbolIt != symbolList.constEnd(); ++symbolIt )
{
itemList.push_back( qMakePair( symbolIt->first + " [" + QString::number( mSymbolCountMap[symbolIt->second] ) + "]", itemMap[symbolIt->first] ) );
itemList.push_back( qMakePair( symbolIt->first + " [" + QString::number( symbolCountMap[symbolIt->second] ) + "]", itemMap[symbolIt->first] ) );
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/app/legend/qgslegendlayer.h
Expand Up @@ -75,6 +75,15 @@ class QgsLegendLayer : public QgsLegendItem
void setDrawingOrder( int order );
int drawingOrder() const { return mDrawingOrder; }

/** Get layer name currently set in legend */
QString layerName() const;

/**Called before edit*/
void beforeEdit();

/**Called after edit*/
void afterEdit();

public slots:

/**Toggle show in overview*/
Expand Down Expand Up @@ -120,6 +129,9 @@ class QgsLegendLayer : public QgsLegendItem
*/
void setupFont();

/** Label, may be layer name or layer name + [feature count] */
QString label() const;

protected:

/** layer identified by its layer id */
Expand All @@ -130,6 +142,9 @@ class QgsLegendLayer : public QgsLegendItem

/**True if number of features per legend class should is shown in the legend items*/
bool mShowFeatureCount;

/** Last vector features count, -1 if not counted */
int mFeatureCount;
};

#endif
4 changes: 3 additions & 1 deletion src/core/qgsmaplayer.cpp
Expand Up @@ -103,7 +103,9 @@ QString QgsMapLayer::id() const
void QgsMapLayer::setLayerName( const QString & name )
{
QgsDebugMsg( "new name is '" + name + "'" );
mLayerName = capitaliseLayerName( name );
QString newName = capitaliseLayerName( name );
if ( newName == mLayerName ) return;
mLayerName = newName;
emit layerNameChanged();
}

Expand Down

0 comments on commit 5de2fee

Please sign in to comment.