Skip to content
Permalink
Browse files

Show active visibility group in menu also in map composer map widget

  • Loading branch information
wonder-sk committed Sep 15, 2014
1 parent dc63b44 commit 3c66bcfc00e72c7a20422e3ee8f8d6aff89033a1
Showing with 68 additions and 36 deletions.
  1. +6 −1 src/app/composer/qgscomposermapwidget.cpp
  2. +37 −18 src/app/qgsvisibilitygroups.cpp
  3. +25 −17 src/app/qgsvisibilitygroups.h
@@ -265,10 +265,15 @@ void QgsComposerMapWidget::aboutToShowVisibilityGroupsMenu()
if ( !menu )
return;

QgsVisibilityGroups::GroupRecord rec = QgsVisibilityGroups::instance()->currentStateFromLayerList( mComposerMap->layerSet() );

menu->clear();
foreach ( QString groupName, QgsVisibilityGroups::instance()->groups() )
{
menu->addAction( groupName, this, SLOT( visibilityGroupSelected() ) );
QAction* a = menu->addAction( groupName, this, SLOT( visibilityGroupSelected() ) );
a->setCheckable( true );
if ( rec == QgsVisibilityGroups::instance()->groupState( groupName ) )
a->setChecked( true );
}

if ( menu->actions().isEmpty() )
@@ -66,30 +66,39 @@ void QgsVisibilityGroups::addVisibleLayersToGroup( QgsLayerTreeGroup* parent, Qg
{
QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node );
if ( nodeLayer->isVisible() )
{
rec.mVisibleLayerIDs << nodeLayer->layerId();
}
}
}

QgsLayerTreeModel* model = QgisApp::instance()->layerTreeView()->layerTreeModel();
bool hasCheckableItems = false;
bool someItemsUnchecked = false;
QSet<QString> checkedItems;
foreach ( QgsLayerTreeModelLegendNode* legendNode, model->layerLegendNodes( nodeLayer ) )
{
if ( legendNode->flags() & Qt::ItemIsUserCheckable )
{
hasCheckableItems = true;
void QgsVisibilityGroups::addPerLayerCheckedLegendSymbols( QgsVisibilityGroups::GroupRecord& rec )
{
QgsLayerTreeModel* model = QgisApp::instance()->layerTreeView()->layerTreeModel();

if ( legendNode->data( Qt::CheckStateRole ).toInt() == Qt::Checked )
checkedItems << legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
else
someItemsUnchecked = true;
}
}
foreach ( QString layerID, rec.mVisibleLayerIDs )
{
QgsLayerTreeLayer* nodeLayer = model->rootGroup()->findLayer( layerID );
if ( !nodeLayer )
continue;

bool hasCheckableItems = false;
bool someItemsUnchecked = false;
QSet<QString> checkedItems;
foreach ( QgsLayerTreeModelLegendNode* legendNode, model->layerLegendNodes( nodeLayer ) )
{
if ( legendNode->flags() & Qt::ItemIsUserCheckable )
{
hasCheckableItems = true;

if ( hasCheckableItems && someItemsUnchecked )
rec.mPerLayerCheckedLegendSymbols.insert( nodeLayer->layerId(), checkedItems );
if ( legendNode->data( Qt::CheckStateRole ).toInt() == Qt::Checked )
checkedItems << legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
else
someItemsUnchecked = true;
}
}

if ( hasCheckableItems && someItemsUnchecked )
rec.mPerLayerCheckedLegendSymbols.insert( nodeLayer->layerId(), checkedItems );
}
}

@@ -98,6 +107,16 @@ QgsVisibilityGroups::GroupRecord QgsVisibilityGroups::currentState()
GroupRecord rec;
QgsLayerTreeGroup* root = QgsProject::instance()->layerTreeRoot();
addVisibleLayersToGroup( root, rec );
addPerLayerCheckedLegendSymbols( rec );
return rec;
}

QgsVisibilityGroups::GroupRecord QgsVisibilityGroups::currentStateFromLayerList( const QStringList& layerIDs )
{
GroupRecord rec;
foreach ( const QString& layerID, layerIDs )
rec.mVisibleLayerIDs << layerID;
addPerLayerCheckedLegendSymbols( rec );
return rec;
}

@@ -37,6 +37,24 @@ class QgsVisibilityGroups : public QObject
Q_OBJECT
public:

typedef struct GroupRecord
{
bool operator==( const GroupRecord& other ) const
{
return mVisibleLayerIDs == other.mVisibleLayerIDs && mPerLayerCheckedLegendSymbols == other.mPerLayerCheckedLegendSymbols;
}
bool operator!=( const GroupRecord& other ) const
{
return !( *this == other );
}

//! List of layers that are visible
QSet<QString> mVisibleLayerIDs;
//! For layers that have checkable legend symbols and not all symbols are checked - list which ones are
QMap<QString, QSet<QString> > mPerLayerCheckedLegendSymbols;
} GroupRecord;


static QgsVisibilityGroups* instance();

//! Add a new group using the current state of project's layer tree
@@ -52,6 +70,9 @@ class QgsVisibilityGroups : public QObject
//! Return list of existing group names
QStringList groups() const;

//! Return recorded state of a group
GroupRecord groupState( const QString& groupName ) const { return mGroups[groupName]; }

//! Return list of layer IDs that should be visible for particular group
QStringList groupVisibleLayers( const QString& name ) const;

@@ -61,6 +82,9 @@ class QgsVisibilityGroups : public QObject
//! Convenience menu that lists available groups and actions for management
QMenu* menu();

//! Create group record given a list of visible layers (needs to store per-layer checked legend symbols)
GroupRecord currentStateFromLayerList( const QStringList& layerIDs );

signals:
void groupsChanged();

@@ -78,27 +102,11 @@ class QgsVisibilityGroups : public QObject
protected:
QgsVisibilityGroups(); // singleton

typedef struct GroupRecord
{
bool operator==( const GroupRecord& other ) const
{
return mVisibleLayerIDs == other.mVisibleLayerIDs && mPerLayerCheckedLegendSymbols == other.mPerLayerCheckedLegendSymbols;
}
bool operator!=( const GroupRecord& other ) const
{
return !( *this == other );
}

//! List of layers that are visible
QSet<QString> mVisibleLayerIDs;
//! For layers that have checkable legend symbols and not all symbols are checked - list which ones are
QMap<QString, QSet<QString> > mPerLayerCheckedLegendSymbols;
} GroupRecord;

typedef QMap<QString, GroupRecord> GroupRecordMap;

void addVisibleLayersToGroup( QgsLayerTreeGroup* parent, GroupRecord& rec );
void applyStateToLayerTreeGroup( QgsLayerTreeGroup* parent, const GroupRecord& rec );
void addPerLayerCheckedLegendSymbols( GroupRecord& rec );

GroupRecord currentState();
void applyState( const QString& groupName );

0 comments on commit 3c66bcf

Please sign in to comment.
You can’t perform that action at this time.