Skip to content

Commit 3c66bcf

Browse files
committed
Show active visibility group in menu also in map composer map widget
1 parent dc63b44 commit 3c66bcf

File tree

3 files changed

+68
-36
lines changed

3 files changed

+68
-36
lines changed

src/app/composer/qgscomposermapwidget.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,15 @@ void QgsComposerMapWidget::aboutToShowVisibilityGroupsMenu()
265265
if ( !menu )
266266
return;
267267

268+
QgsVisibilityGroups::GroupRecord rec = QgsVisibilityGroups::instance()->currentStateFromLayerList( mComposerMap->layerSet() );
269+
268270
menu->clear();
269271
foreach ( QString groupName, QgsVisibilityGroups::instance()->groups() )
270272
{
271-
menu->addAction( groupName, this, SLOT( visibilityGroupSelected() ) );
273+
QAction* a = menu->addAction( groupName, this, SLOT( visibilityGroupSelected() ) );
274+
a->setCheckable( true );
275+
if ( rec == QgsVisibilityGroups::instance()->groupState( groupName ) )
276+
a->setChecked( true );
272277
}
273278

274279
if ( menu->actions().isEmpty() )

src/app/qgsvisibilitygroups.cpp

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,39 @@ void QgsVisibilityGroups::addVisibleLayersToGroup( QgsLayerTreeGroup* parent, Qg
6666
{
6767
QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node );
6868
if ( nodeLayer->isVisible() )
69-
{
7069
rec.mVisibleLayerIDs << nodeLayer->layerId();
70+
}
71+
}
72+
}
7173

72-
QgsLayerTreeModel* model = QgisApp::instance()->layerTreeView()->layerTreeModel();
73-
bool hasCheckableItems = false;
74-
bool someItemsUnchecked = false;
75-
QSet<QString> checkedItems;
76-
foreach ( QgsLayerTreeModelLegendNode* legendNode, model->layerLegendNodes( nodeLayer ) )
77-
{
78-
if ( legendNode->flags() & Qt::ItemIsUserCheckable )
79-
{
80-
hasCheckableItems = true;
74+
void QgsVisibilityGroups::addPerLayerCheckedLegendSymbols( QgsVisibilityGroups::GroupRecord& rec )
75+
{
76+
QgsLayerTreeModel* model = QgisApp::instance()->layerTreeView()->layerTreeModel();
8177

82-
if ( legendNode->data( Qt::CheckStateRole ).toInt() == Qt::Checked )
83-
checkedItems << legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
84-
else
85-
someItemsUnchecked = true;
86-
}
87-
}
78+
foreach ( QString layerID, rec.mVisibleLayerIDs )
79+
{
80+
QgsLayerTreeLayer* nodeLayer = model->rootGroup()->findLayer( layerID );
81+
if ( !nodeLayer )
82+
continue;
83+
84+
bool hasCheckableItems = false;
85+
bool someItemsUnchecked = false;
86+
QSet<QString> checkedItems;
87+
foreach ( QgsLayerTreeModelLegendNode* legendNode, model->layerLegendNodes( nodeLayer ) )
88+
{
89+
if ( legendNode->flags() & Qt::ItemIsUserCheckable )
90+
{
91+
hasCheckableItems = true;
8892

89-
if ( hasCheckableItems && someItemsUnchecked )
90-
rec.mPerLayerCheckedLegendSymbols.insert( nodeLayer->layerId(), checkedItems );
93+
if ( legendNode->data( Qt::CheckStateRole ).toInt() == Qt::Checked )
94+
checkedItems << legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
95+
else
96+
someItemsUnchecked = true;
9197
}
9298
}
99+
100+
if ( hasCheckableItems && someItemsUnchecked )
101+
rec.mPerLayerCheckedLegendSymbols.insert( nodeLayer->layerId(), checkedItems );
93102
}
94103
}
95104

@@ -98,6 +107,16 @@ QgsVisibilityGroups::GroupRecord QgsVisibilityGroups::currentState()
98107
GroupRecord rec;
99108
QgsLayerTreeGroup* root = QgsProject::instance()->layerTreeRoot();
100109
addVisibleLayersToGroup( root, rec );
110+
addPerLayerCheckedLegendSymbols( rec );
111+
return rec;
112+
}
113+
114+
QgsVisibilityGroups::GroupRecord QgsVisibilityGroups::currentStateFromLayerList( const QStringList& layerIDs )
115+
{
116+
GroupRecord rec;
117+
foreach ( const QString& layerID, layerIDs )
118+
rec.mVisibleLayerIDs << layerID;
119+
addPerLayerCheckedLegendSymbols( rec );
101120
return rec;
102121
}
103122

src/app/qgsvisibilitygroups.h

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ class QgsVisibilityGroups : public QObject
3737
Q_OBJECT
3838
public:
3939

40+
typedef struct GroupRecord
41+
{
42+
bool operator==( const GroupRecord& other ) const
43+
{
44+
return mVisibleLayerIDs == other.mVisibleLayerIDs && mPerLayerCheckedLegendSymbols == other.mPerLayerCheckedLegendSymbols;
45+
}
46+
bool operator!=( const GroupRecord& other ) const
47+
{
48+
return !( *this == other );
49+
}
50+
51+
//! List of layers that are visible
52+
QSet<QString> mVisibleLayerIDs;
53+
//! For layers that have checkable legend symbols and not all symbols are checked - list which ones are
54+
QMap<QString, QSet<QString> > mPerLayerCheckedLegendSymbols;
55+
} GroupRecord;
56+
57+
4058
static QgsVisibilityGroups* instance();
4159

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

73+
//! Return recorded state of a group
74+
GroupRecord groupState( const QString& groupName ) const { return mGroups[groupName]; }
75+
5576
//! Return list of layer IDs that should be visible for particular group
5677
QStringList groupVisibleLayers( const QString& name ) const;
5778

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

85+
//! Create group record given a list of visible layers (needs to store per-layer checked legend symbols)
86+
GroupRecord currentStateFromLayerList( const QStringList& layerIDs );
87+
6488
signals:
6589
void groupsChanged();
6690

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

81-
typedef struct GroupRecord
82-
{
83-
bool operator==( const GroupRecord& other ) const
84-
{
85-
return mVisibleLayerIDs == other.mVisibleLayerIDs && mPerLayerCheckedLegendSymbols == other.mPerLayerCheckedLegendSymbols;
86-
}
87-
bool operator!=( const GroupRecord& other ) const
88-
{
89-
return !( *this == other );
90-
}
91-
92-
//! List of layers that are visible
93-
QSet<QString> mVisibleLayerIDs;
94-
//! For layers that have checkable legend symbols and not all symbols are checked - list which ones are
95-
QMap<QString, QSet<QString> > mPerLayerCheckedLegendSymbols;
96-
} GroupRecord;
97-
98105
typedef QMap<QString, GroupRecord> GroupRecordMap;
99106

100107
void addVisibleLayersToGroup( QgsLayerTreeGroup* parent, GroupRecord& rec );
101108
void applyStateToLayerTreeGroup( QgsLayerTreeGroup* parent, const GroupRecord& rec );
109+
void addPerLayerCheckedLegendSymbols( GroupRecord& rec );
102110

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

0 commit comments

Comments
 (0)