Skip to content

Commit

Permalink
Show active visibility group in menu also in map composer map widget
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Sep 15, 2014
1 parent dc63b44 commit 3c66bcf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
7 changes: 6 additions & 1 deletion src/app/composer/qgscomposermapwidget.cpp
Expand Up @@ -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() )
Expand Down
55 changes: 37 additions & 18 deletions src/app/qgsvisibilitygroups.cpp
Expand Up @@ -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 );
}
}

Expand All @@ -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;
}

Expand Down
42 changes: 25 additions & 17 deletions src/app/qgsvisibilitygroups.h
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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();

Expand All @@ -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 );
Expand Down

0 comments on commit 3c66bcf

Please sign in to comment.