From 6bef78f49d2408a9fadcbc46ca7436c96c5a35d7 Mon Sep 17 00:00:00 2001 From: Martin Dobias Date: Mon, 30 Jun 2014 12:23:39 +0700 Subject: [PATCH] Allow buiding of QgsLegendModel from layer tree --- python/core/composer/qgslegendmodel.sip | 14 +++++-- src/app/composer/qgscomposerlegendwidget.cpp | 8 ++-- src/core/composer/qgslegendmodel.cpp | 42 +++++++++++++++++--- src/core/composer/qgslegendmodel.h | 16 ++++++-- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/python/core/composer/qgslegendmodel.sip b/python/core/composer/qgslegendmodel.sip index c50a3d921d3..b36012f4d29 100644 --- a/python/core/composer/qgslegendmodel.sip +++ b/python/core/composer/qgslegendmodel.sip @@ -19,15 +19,21 @@ class QgsLegendModel : QStandardItemModel QgsLegendModel(); ~QgsLegendModel(); - /**Sets layer set and groups*/ - void setLayerSetAndGroups( const QStringList& layerIds, const QList< QPair< QString, QList > >& groupInfo ); + /** Set layers and groups from a layer tree + * @note added in 2.6 + */ + void setLayerSetAndGroups( QgsLayerTreeGroup* rootGroup ); + /** Sets layer set and groups + * @deprecated in 2.6 + */ + void setLayerSetAndGroups( const QStringList& layerIds, const QList< QPair< QString, QList > >& groupInfo ) /Deprecated/; 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). @returns a pointer to the added group */ - QStandardItem *addGroup( QString text = QString::null, int position = -1 ); + QStandardItem *addGroup( QString text = QString::null, int position = -1, QStandardItem* parentItem = 0 ); /**Tries to automatically update a model entry (e.g. a whole layer or only a single item)*/ void updateItem( QStandardItem* item ); @@ -63,7 +69,7 @@ class QgsLegendModel : QStandardItemModel public slots: void removeLayer( const QString& layerId ); - void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule = "" ); + void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule = "", QStandardItem* parentItem = 0 ); signals: void layersChanged(); diff --git a/src/app/composer/qgscomposerlegendwidget.cpp b/src/app/composer/qgscomposerlegendwidget.cpp index f8e7a9efc27..f2dfa17aae0 100644 --- a/src/app/composer/qgscomposerlegendwidget.cpp +++ b/src/app/composer/qgscomposerlegendwidget.cpp @@ -26,12 +26,12 @@ #include #include -#include "qgsapplegendinterface.h" #include "qgisapp.h" +#include "qgsapplication.h" #include "qgsmapcanvas.h" #include "qgsmaplayerregistry.h" #include "qgsmaprenderer.h" -#include "qgsapplication.h" +#include "qgsproject.h" #include "qgsvectorlayer.h" #include @@ -916,9 +916,7 @@ void QgsComposerLegendWidget::updateLegend() //and also group info - QgsAppLegendInterface legendIface( app->layerTreeView() ); - QList< GroupLayerInfo > groupInfo = legendIface.groupLayerRelationship(); - mLegend->model()->setLayerSetAndGroups( layerIdList, groupInfo ); + mLegend->model()->setLayerSetAndGroups( QgsProject::instance()->layerTreeRoot() ); mLegend->endCommand(); } } diff --git a/src/core/composer/qgslegendmodel.cpp b/src/core/composer/qgslegendmodel.cpp index 572990b47fb..7fa2891231e 100644 --- a/src/core/composer/qgslegendmodel.cpp +++ b/src/core/composer/qgslegendmodel.cpp @@ -18,6 +18,7 @@ #include "qgslegendmodel.h" #include "qgscomposerlegenditem.h" #include "qgsfield.h" +#include "qgslayertree.h" #include "qgsmaplayer.h" #include "qgsmaplayerregistry.h" #include "qgsrasterlayer.h" @@ -50,6 +51,31 @@ QgsLegendModel::~QgsLegendModel() { } +void QgsLegendModel::setLayerSetAndGroups( QgsLayerTreeGroup* rootGroup ) +{ + clear(); + addGroupFromLayerTree( rootGroup, invisibleRootItem() ); +} + +void QgsLegendModel::addGroupFromLayerTree( QgsLayerTreeGroup* parentGroup, QStandardItem* parentItem ) +{ + foreach ( QgsLayerTreeNode* node, parentGroup->children() ) + { + if ( QgsLayerTree::isGroup( node ) ) + { + QgsLayerTreeGroup* nodeGroup = QgsLayerTree::toGroup( node ); + QStandardItem* groupItem = addGroup( nodeGroup->name(), -1, parentItem ); + addGroupFromLayerTree( nodeGroup, groupItem ); + } + else if ( QgsLayerTree::isLayer( node ) ) + { + QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node ); + if ( nodeLayer->layer() ) + addLayer( nodeLayer->layer(), -1, QString(), parentItem ); + } + } +} + void QgsLegendModel::setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo ) { setLayerSet( layerIds ); @@ -115,21 +141,24 @@ void QgsLegendModel::setLayerSet( const QStringList& layerIds, double scaleDenom } } -QStandardItem* QgsLegendModel::addGroup( QString text, int position ) +QStandardItem* QgsLegendModel::addGroup( QString text, int position, QStandardItem* parentItem ) { if ( text.isNull() ) text = tr( "Group" ); + if ( !parentItem ) + parentItem = invisibleRootItem(); + QgsComposerGroupItem* groupItem = new QgsComposerGroupItem( text ); groupItem->setUserText( text ); if ( position == -1 ) { - position = invisibleRootItem()->rowCount(); + position = parentItem->rowCount(); } QList itemsList; itemsList << groupItem << new QgsComposerStyleItem( groupItem ); - invisibleRootItem()->insertRow( position, itemsList ); + parentItem->insertRow( position, itemsList ); emit layersChanged(); return groupItem; @@ -524,13 +553,16 @@ void QgsLegendModel::removeLayer( const QString& layerId ) } } -void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator, QString rule ) +void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator, QString rule, QStandardItem* parentItem ) { if ( !theMapLayer ) { return; } + if ( !parentItem ) + parentItem = invisibleRootItem(); + QgsComposerLayerItem* layerItem = new QgsComposerLayerItem( theMapLayer->name() ); if ( theMapLayer->title() != "" ) { @@ -543,7 +575,7 @@ void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator QList itemsList; itemsList << layerItem << new QgsComposerStyleItem( layerItem ); - invisibleRootItem()->appendRow( itemsList ); + parentItem->appendRow( itemsList ); switch ( theMapLayer->type() ) { diff --git a/src/core/composer/qgslegendmodel.h b/src/core/composer/qgslegendmodel.h index faa93875c53..654a54af962 100644 --- a/src/core/composer/qgslegendmodel.h +++ b/src/core/composer/qgslegendmodel.h @@ -24,6 +24,7 @@ class QDomDocument; class QDomElement; +class QgsLayerTreeGroup; class QgsMapLayer; class QgsSymbolV2; class QgsVectorLayer; @@ -52,15 +53,21 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel QgsLegendModel(); ~QgsLegendModel(); - /**Sets layer set and groups*/ - void setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo ); + /** Set layers and groups from a layer tree + * @note added in 2.6 + */ + void setLayerSetAndGroups( QgsLayerTreeGroup* rootGroup ); + /** Sets layer set and groups + * @deprecated in 2.6 + */ + Q_DECL_DEPRECATED void setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo ); 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). @returns a pointer to the added group */ - QStandardItem *addGroup( QString text = QString::null, int position = -1 ); + QStandardItem *addGroup( QString text = QString::null, int position = -1, QStandardItem* parentItem = 0 ); /**Tries to automatically update a model entry (e.g. a whole layer or only a single item)*/ void updateItem( QStandardItem* item ); @@ -97,7 +104,7 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel public slots: void removeLayer( const QString& layerId ); - void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule = "" ); + void addLayer( QgsMapLayer* theMapLayer, double scaleDenominator = -1, QString rule = "", QStandardItem* parentItem = 0 ); private slots: void updateLayer(); @@ -117,6 +124,7 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel void updateSymbolV2ItemText( QStandardItem* symbolItem ); void updateRasterSymbolItemText( QStandardItem* symbolItem ); + void addGroupFromLayerTree( QgsLayerTreeGroup* parentGroup, QStandardItem* parentItem ); protected: QStringList mLayerIds;