mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Allow buiding of QgsLegendModel from layer tree
This commit is contained in:
parent
36b4f1689d
commit
6bef78f49d
@ -19,15 +19,21 @@ class QgsLegendModel : QStandardItemModel
|
||||
QgsLegendModel();
|
||||
~QgsLegendModel();
|
||||
|
||||
/**Sets layer set and groups*/
|
||||
void setLayerSetAndGroups( const QStringList& layerIds, const QList< QPair< QString, QList<QString> > >& 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<QString> > >& 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();
|
||||
|
@ -26,12 +26,12 @@
|
||||
#include <QFontDialog>
|
||||
#include <QColorDialog>
|
||||
|
||||
#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 <QMessageBox>
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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<QStandardItem *> 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<QStandardItem *> itemsList;
|
||||
itemsList << layerItem << new QgsComposerStyleItem( layerItem );
|
||||
invisibleRootItem()->appendRow( itemsList );
|
||||
parentItem->appendRow( itemsList );
|
||||
|
||||
switch ( theMapLayer->type() )
|
||||
{
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user