Skip to content

Commit

Permalink
Merge pull request #901 from sbrunner/legend-rule
Browse files Browse the repository at this point in the history
[qgis server] Add RULE parameter support in GetLegendGraphic
  • Loading branch information
mhugent committed Oct 21, 2013
2 parents 0f82df1 + 91c8a3c commit c777dc6
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 75 deletions.
6 changes: 3 additions & 3 deletions src/core/composer/qgscomposerlegenditem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void QgsComposerLayerItem::readXML( const QDomElement& itemElem, bool xServerAva
}
}

void QgsComposerLayerItem::setDefaultStyle( double scaleDenominator )
void QgsComposerLayerItem::setDefaultStyle( double scaleDenominator, QString rule )
{
// set default style according to number of symbols
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerID() ) );
Expand All @@ -295,8 +295,8 @@ void QgsComposerLayerItem::setDefaultStyle( double scaleDenominator )
QgsFeatureRendererV2* renderer = vLayer->rendererV2();
if ( renderer )
{
QPair<QString, QgsSymbolV2*> symbolItem = renderer->legendSymbolItems( scaleDenominator ).value( 0 );
if ( renderer->legendSymbolItems().size() > 1 || !symbolItem.first.isEmpty() )
QPair<QString, QgsSymbolV2*> symbolItem = renderer->legendSymbolItems( scaleDenominator, rule ).value( 0 );
if ( renderer->legendSymbolItems( scaleDenominator, rule ).size() > 1 || !symbolItem.first.isEmpty() )
{
setStyle( QgsComposerLegendStyle::Subgroup );
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerlegenditem.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class CORE_EXPORT QgsComposerLayerItem : public QgsComposerLegendItem
void setShowFeatureCount( bool show ) { mShowFeatureCount = show; }
bool showFeatureCount() const { return mShowFeatureCount; }

void setDefaultStyle( double scaleDenominator = -1 );
void setDefaultStyle( double scaleDenominator = -1, QString rule = "" );

private:
QString mLayerID;
Expand Down
30 changes: 17 additions & 13 deletions src/core/composer/qgslegendmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void QgsLegendModel::setLayerSetAndGroups( const QStringList& layerIds, const QL
}
}

void QgsLegendModel::setLayerSet( const QStringList& layerIds, double scaleDenominator )
void QgsLegendModel::setLayerSet( const QStringList& layerIds, double scaleDenominator, QString rule )
{
mLayerIds = layerIds;

Expand All @@ -111,7 +111,7 @@ void QgsLegendModel::setLayerSet( const QStringList& layerIds, double scaleDenom
for ( ; idIter != mLayerIds.constEnd(); ++idIter )
{
currentLayer = QgsMapLayerRegistry::instance()->mapLayer( *idIter );
addLayer( currentLayer, scaleDenominator );
addLayer( currentLayer, scaleDenominator, rule );
}
}

Expand All @@ -135,7 +135,7 @@ QStandardItem* QgsLegendModel::addGroup( QString text, int position )
return groupItem;
}

int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLayer* vlayer, double scaleDenominator )
int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLayer* vlayer, double scaleDenominator, QString rule )
{
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );

Expand All @@ -158,12 +158,12 @@ int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLa
}
}

QgsLegendSymbolList lst = renderer->legendSymbolItems( scaleDenominator );
QgsLegendSymbolList lst = renderer->legendSymbolItems( scaleDenominator, rule );
QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin();
int row = 0;
for ( ; symbolIt != lst.constEnd(); ++symbolIt )
{
if ( scaleDenominator == -1 )
if ( scaleDenominator == -1 && rule.isEmpty() )
{
QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( "" );

Expand All @@ -187,20 +187,24 @@ int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLa

// updateSymbolV2ItemText needs layer set
updateSymbolV2ItemText( currentSymbolItem );

row++;
}
else
{
QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( "" );
currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) );
if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server
{
currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) );
}
currentSymbolItem->setSymbolV2( symbolIt->second );
layerItem->setChild( 0, 0, currentSymbolItem );
layerItem->setChild( row, 0, currentSymbolItem );
currentSymbolItem->setText( symbolIt->first );
}

row++;
}

if ( scaleDenominator == -1 )
// Don't remove row on getLegendGraphic (read only with filter)
if ( scaleDenominator == -1 && rule.isEmpty() )
{
// Delete following old items (if current number of items decreased)
for ( int i = layerItem->rowCount() - 1; i >= row; --i )
Expand Down Expand Up @@ -467,7 +471,7 @@ void QgsLegendModel::removeLayer( const QString& layerId )
}
}

void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator )
void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator, QString rule )
{
if ( !theMapLayer )
{
Expand All @@ -481,7 +485,7 @@ void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator
layerItem->setUserText( theMapLayer->title() );
}
layerItem->setLayerID( theMapLayer->id() );
layerItem->setDefaultStyle( scaleDenominator );
layerItem->setDefaultStyle( scaleDenominator, rule );
layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );

QList<QStandardItem *> itemsList;
Expand All @@ -495,7 +499,7 @@ void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( theMapLayer );
if ( vl )
{
addVectorLayerItemsV2( layerItem, vl, scaleDenominator );
addVectorLayerItemsV2( layerItem, vl, scaleDenominator, rule );
}
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/composer/qgslegendmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel

/**Sets layer set and groups*/
void setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo );
void setLayerSet( const QStringList& layerIds, double scaleDenominator = -1 );
void setLayerSet( const QStringList& layerIds, double scaleDenominator = -1, QString rule = "" );
/**Adds a group
@param text name of group (defaults to translation of "Group")
@param position insertion position (toplevel position (or -1 if it should be placed at the end of the legend).
Expand Down Expand Up @@ -97,14 +97,14 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel

public slots:
void removeLayer( const QString& layerId );
void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1 );
void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule="" );

signals:
void layersChanged();

private:
/**Adds classification items of vector layers using new symbology*/
int addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLayer* vlayer, double scaleDenominator = -1 );
int addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLayer* vlayer, double scaleDenominator = -1, QString rule = "" );

/**Adds item of raster layer
@return 0 in case of success*/
Expand Down
7 changes: 5 additions & 2 deletions src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ QgsLegendSymbologyList QgsCategorizedSymbolRendererV2::legendSymbologyItems( QSi
return lst;
}

QgsLegendSymbolList QgsCategorizedSymbolRendererV2::legendSymbolItems( double scaleDenominator )
QgsLegendSymbolList QgsCategorizedSymbolRendererV2::legendSymbolItems( double scaleDenominator, QString rule )
{
Q_UNUSED( scaleDenominator );
QSettings settings;
Expand All @@ -649,7 +649,10 @@ QgsLegendSymbolList QgsCategorizedSymbolRendererV2::legendSymbolItems( double sc

foreach ( const QgsRendererCategoryV2& cat, mCategories )
{
lst << qMakePair( cat.label(), cat.symbol() );
if ( rule.isEmpty() || cat.label() == rule )
{
lst << qMakePair( cat.label(), cat.symbol() );
}
}
return lst;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgscategorizedsymbolrendererv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
//! return a list of item text / symbol
//! @note: this method was added in version 1.5
//! @note not available in python bindings
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1 );
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );

QgsSymbolV2* sourceSymbol();
void setSourceSymbol( QgsSymbolV2* sym );
Expand Down
21 changes: 12 additions & 9 deletions src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ QgsLegendSymbologyList QgsGraduatedSymbolRendererV2::legendSymbologyItems( QSize
return lst;
}

QgsLegendSymbolList QgsGraduatedSymbolRendererV2::legendSymbolItems( double scaleDenominator )
QgsLegendSymbolList QgsGraduatedSymbolRendererV2::legendSymbolItems( double scaleDenominator, QString rule )
{
Q_UNUSED( scaleDenominator );
QSettings settings;
Expand All @@ -1109,16 +1109,19 @@ QgsLegendSymbolList QgsGraduatedSymbolRendererV2::legendSymbolItems( double scal

foreach ( const QgsRendererRangeV2& range, mRanges )
{
QgsSymbolV2* symbol;
if ( mRotationFieldIdx == -1 && mSizeScaleFieldIdx == -1 )
if ( rule.isEmpty() || range.label() == rule )
{
symbol = range.symbol();
}
else
{
symbol = mTempSymbols[range.symbol()];
QgsSymbolV2* symbol;
if ( mRotationFieldIdx == -1 && mSizeScaleFieldIdx == -1 )
{
symbol = range.symbol();
}
else
{
symbol = mTempSymbols[range.symbol()];
}
lst << qMakePair( range.label(), symbol );
}
lst << qMakePair( range.label(), symbol );
}
return lst;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsgraduatedsymbolrendererv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
//! return a list of item text / symbol
//! @note: this method was added in version 1.5
//! @note not available in python bindings
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1 );
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );

QgsSymbolV2* sourceSymbol();
void setSourceSymbol( QgsSymbolV2* sym );
Expand Down
4 changes: 2 additions & 2 deletions src/core/symbology-ng/qgspointdisplacementrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ QgsLegendSymbologyList QgsPointDisplacementRenderer::legendSymbologyItems( QSize
return QgsLegendSymbologyList();
}

QgsLegendSymbolList QgsPointDisplacementRenderer::legendSymbolItems()
QgsLegendSymbolList QgsPointDisplacementRenderer::legendSymbolItems( double scaleDenominator, QString rule )
{
if ( mRenderer )
{
return mRenderer->legendSymbolItems();
return mRenderer->legendSymbolItems( scaleDenominator, rule );
}
return QgsLegendSymbolList();
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgspointdisplacementrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CORE_EXPORT QgsPointDisplacementRenderer: public QgsFeatureRendererV2
QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );

//! @note not available in python bindings
QgsLegendSymbolList legendSymbolItems();
QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );

void setLabelAttributeName( const QString& name ) { mLabelAttributeName = name; }
QString labelAttributeName() const { return mLabelAttributeName; }
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgsrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,10 @@ QgsLegendSymbologyList QgsFeatureRendererV2::legendSymbologyItems( QSize iconSiz
return QgsLegendSymbologyList();
}

QgsLegendSymbolList QgsFeatureRendererV2::legendSymbolItems( double scaleDenominator )
QgsLegendSymbolList QgsFeatureRendererV2::legendSymbolItems( double scaleDenominator, QString rule )
{
Q_UNUSED( scaleDenominator );
Q_UNUSED( rule );
return QgsLegendSymbolList();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsrendererv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class CORE_EXPORT QgsFeatureRendererV2
//! return a list of item text / symbol
//! @note: this method was added in version 1.5
//! @note: not available in python bindings
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1 );
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );

//! set type and size of editing vertex markers for subsequent rendering
void setVertexMarkerAppearance( int type, int size );
Expand Down
15 changes: 6 additions & 9 deletions src/core/symbology-ng/qgsrulebasedrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,17 @@ void QgsRuleBasedRendererV2::Rule::setSymbol( QgsSymbolV2* sym )
mSymbol = sym;
}

QgsLegendSymbolList QgsRuleBasedRendererV2::Rule::legendSymbolItems( double scaleDenominator )
QgsLegendSymbolList QgsRuleBasedRendererV2::Rule::legendSymbolItems( double scaleDenominator, QString ruleFilter )
{
QgsLegendSymbolList lst;
if ( mSymbol )
if ( mSymbol && ( ruleFilter.isEmpty() || mLabel == ruleFilter ) )
lst << qMakePair( mLabel, mSymbol );

for ( RuleList::iterator it = mChildren.begin(); it != mChildren.end(); ++it )
{
Rule* rule = *it;
if ( scaleDenominator == -1 || (
( rule->mScaleMinDenom == -1 || rule->mScaleMinDenom < scaleDenominator ) &&
( rule->mScaleMaxDenom == -1 || scaleDenominator < rule->mScaleMaxDenom ) ) )
{
lst << rule->legendSymbolItems( scaleDenominator );
if ( scaleDenominator == -1 || rule->isScaleOK(scaleDenominator) ) {
lst << rule->legendSymbolItems( scaleDenominator, ruleFilter );
}
}
return lst;
Expand Down Expand Up @@ -849,9 +846,9 @@ QgsLegendSymbologyList QgsRuleBasedRendererV2::legendSymbologyItems( QSize iconS
return lst;
}

QgsLegendSymbolList QgsRuleBasedRendererV2::legendSymbolItems( double scaleDenominator )
QgsLegendSymbolList QgsRuleBasedRendererV2::legendSymbolItems( double scaleDenominator, QString rule )
{
return mRootRule->legendSymbolItems( scaleDenominator );
return mRootRule->legendSymbolItems( scaleDenominator, rule );
}


Expand Down
4 changes: 2 additions & 2 deletions src/core/symbology-ng/qgsrulebasedrendererv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
QSet<QString> usedAttributes();
QgsSymbolV2List symbols();
//! @note not available in python bindings
QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1 );
QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );
bool isFilterOK( QgsFeature& f ) const;
bool isScaleOK( double scale ) const;

Expand Down Expand Up @@ -226,7 +226,7 @@ class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
//! return a list of item text / symbol
//! @note: this method was added in version 1.5
//! @note not available in python bindings
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1 );
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );

//! for debugging
virtual QString dump() const;
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgssinglesymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,10 @@ QgsLegendSymbologyList QgsSingleSymbolRendererV2::legendSymbologyItems( QSize ic
return lst;
}

QgsLegendSymbolList QgsSingleSymbolRendererV2::legendSymbolItems( double scaleDenominator )
QgsLegendSymbolList QgsSingleSymbolRendererV2::legendSymbolItems( double scaleDenominator, QString rule )
{
Q_UNUSED( scaleDenominator );
Q_UNUSED( rule );
QgsLegendSymbolList lst;
lst << qMakePair( QString(), mSymbol );
return lst;
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgssinglesymbolrendererv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class CORE_EXPORT QgsSingleSymbolRendererV2 : public QgsFeatureRendererV2
//! return a list of item text / symbol
//! @note: this method was added in version 1.5
//! @note not available in python bindings
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1 );
virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );

protected:
QgsSymbolV2* mSymbol;
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgssymbollayerv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ void QgsLineSymbolLayerV2::renderPolygonOutline( const QPolygonF& points, QList<

void QgsFillSymbolLayerV2::drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size )
{
QPolygonF poly = QRectF( QPointF( 0, 0 ), QPointF( size.width() - 1, size.height() - 1 ) );
QPolygonF poly = QRectF( QPointF( 0, 0 ), QPointF( size.width(), size.height() ) );
startRender( context );
renderPolygon( poly, NULL, context );
stopRender( context );
Expand Down
Loading

0 comments on commit c777dc6

Please sign in to comment.