Allow buiding of QgsLegendModel from layer tree

This commit is contained in:
Martin Dobias 2014-06-30 12:23:39 +07:00
parent 36b4f1689d
commit 6bef78f49d
4 changed files with 62 additions and 18 deletions

View File

@ -19,15 +19,21 @@ class QgsLegendModel : QStandardItemModel
QgsLegendModel(); QgsLegendModel();
~QgsLegendModel(); ~QgsLegendModel();
/**Sets layer set and groups*/ /** Set layers and groups from a layer tree
void setLayerSetAndGroups( const QStringList& layerIds, const QList< QPair< QString, QList<QString> > >& groupInfo ); * @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<QString> > >& groupInfo ) /Deprecated/;
void setLayerSet( const QStringList& layerIds, double scaleDenominator = -1, QString rule = "" ); void setLayerSet( const QStringList& layerIds, double scaleDenominator = -1, QString rule = "" );
/**Adds a group /**Adds a group
@param text name of group (defaults to translation of "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). @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 @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)*/ /**Tries to automatically update a model entry (e.g. a whole layer or only a single item)*/
void updateItem( QStandardItem* item ); void updateItem( QStandardItem* item );
@ -63,7 +69,7 @@ class QgsLegendModel : QStandardItemModel
public slots: public slots:
void removeLayer( const QString& layerId ); 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: signals:
void layersChanged(); void layersChanged();

View File

@ -26,12 +26,12 @@
#include <QFontDialog> #include <QFontDialog>
#include <QColorDialog> #include <QColorDialog>
#include "qgsapplegendinterface.h"
#include "qgisapp.h" #include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsmapcanvas.h" #include "qgsmapcanvas.h"
#include "qgsmaplayerregistry.h" #include "qgsmaplayerregistry.h"
#include "qgsmaprenderer.h" #include "qgsmaprenderer.h"
#include "qgsapplication.h" #include "qgsproject.h"
#include "qgsvectorlayer.h" #include "qgsvectorlayer.h"
#include <QMessageBox> #include <QMessageBox>
@ -916,9 +916,7 @@ void QgsComposerLegendWidget::updateLegend()
//and also group info //and also group info
QgsAppLegendInterface legendIface( app->layerTreeView() ); mLegend->model()->setLayerSetAndGroups( QgsProject::instance()->layerTreeRoot() );
QList< GroupLayerInfo > groupInfo = legendIface.groupLayerRelationship();
mLegend->model()->setLayerSetAndGroups( layerIdList, groupInfo );
mLegend->endCommand(); mLegend->endCommand();
} }
} }

View File

@ -18,6 +18,7 @@
#include "qgslegendmodel.h" #include "qgslegendmodel.h"
#include "qgscomposerlegenditem.h" #include "qgscomposerlegenditem.h"
#include "qgsfield.h" #include "qgsfield.h"
#include "qgslayertree.h"
#include "qgsmaplayer.h" #include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h" #include "qgsmaplayerregistry.h"
#include "qgsrasterlayer.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 ) void QgsLegendModel::setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo )
{ {
setLayerSet( layerIds ); 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() ) if ( text.isNull() )
text = tr( "Group" ); text = tr( "Group" );
if ( !parentItem )
parentItem = invisibleRootItem();
QgsComposerGroupItem* groupItem = new QgsComposerGroupItem( text ); QgsComposerGroupItem* groupItem = new QgsComposerGroupItem( text );
groupItem->setUserText( text ); groupItem->setUserText( text );
if ( position == -1 ) if ( position == -1 )
{ {
position = invisibleRootItem()->rowCount(); position = parentItem->rowCount();
} }
QList<QStandardItem *> itemsList; QList<QStandardItem *> itemsList;
itemsList << groupItem << new QgsComposerStyleItem( groupItem ); itemsList << groupItem << new QgsComposerStyleItem( groupItem );
invisibleRootItem()->insertRow( position, itemsList ); parentItem->insertRow( position, itemsList );
emit layersChanged(); emit layersChanged();
return groupItem; 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 ) if ( !theMapLayer )
{ {
return; return;
} }
if ( !parentItem )
parentItem = invisibleRootItem();
QgsComposerLayerItem* layerItem = new QgsComposerLayerItem( theMapLayer->name() ); QgsComposerLayerItem* layerItem = new QgsComposerLayerItem( theMapLayer->name() );
if ( theMapLayer->title() != "" ) if ( theMapLayer->title() != "" )
{ {
@ -543,7 +575,7 @@ void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer, double scaleDenominator
QList<QStandardItem *> itemsList; QList<QStandardItem *> itemsList;
itemsList << layerItem << new QgsComposerStyleItem( layerItem ); itemsList << layerItem << new QgsComposerStyleItem( layerItem );
invisibleRootItem()->appendRow( itemsList ); parentItem->appendRow( itemsList );
switch ( theMapLayer->type() ) switch ( theMapLayer->type() )
{ {

View File

@ -24,6 +24,7 @@
class QDomDocument; class QDomDocument;
class QDomElement; class QDomElement;
class QgsLayerTreeGroup;
class QgsMapLayer; class QgsMapLayer;
class QgsSymbolV2; class QgsSymbolV2;
class QgsVectorLayer; class QgsVectorLayer;
@ -52,15 +53,21 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel
QgsLegendModel(); QgsLegendModel();
~QgsLegendModel(); ~QgsLegendModel();
/**Sets layer set and groups*/ /** Set layers and groups from a layer tree
void setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo ); * @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 = "" ); void setLayerSet( const QStringList& layerIds, double scaleDenominator = -1, QString rule = "" );
/**Adds a group /**Adds a group
@param text name of group (defaults to translation of "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). @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 @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)*/ /**Tries to automatically update a model entry (e.g. a whole layer or only a single item)*/
void updateItem( QStandardItem* item ); void updateItem( QStandardItem* item );
@ -97,7 +104,7 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel
public slots: public slots:
void removeLayer( const QString& layerId ); 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: private slots:
void updateLayer(); void updateLayer();
@ -117,6 +124,7 @@ class CORE_EXPORT QgsLegendModel : public QStandardItemModel
void updateSymbolV2ItemText( QStandardItem* symbolItem ); void updateSymbolV2ItemText( QStandardItem* symbolItem );
void updateRasterSymbolItemText( QStandardItem* symbolItem ); void updateRasterSymbolItemText( QStandardItem* symbolItem );
void addGroupFromLayerTree( QgsLayerTreeGroup* parentGroup, QStandardItem* parentItem );
protected: protected:
QStringList mLayerIds; QStringList mLayerIds;