Skip to content

Commit

Permalink
[FEATURE] Access to visibility groups in map composer
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Sep 4, 2014
1 parent e3c2711 commit 1f8659c
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 18 deletions.
42 changes: 42 additions & 0 deletions src/app/composer/qgscomposermapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsmapcanvas.h"
#include "qgscomposermapgrid.h"
#include "qgscomposermapoverview.h"
Expand All @@ -36,6 +37,7 @@
#include "qgsexpressionbuilderdialog.h"
#include "qgsgenericprojectionselector.h"
#include "qgsproject.h"
#include "qgsvisibilitygroups.h"
#include <QColorDialog>
#include <QFontDialog>
#include <QMessageBox>
Expand Down Expand Up @@ -110,6 +112,14 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QgsCo
//set initial state of frame style controls
toggleFrameControls( false, false, false );

QMenu* m = new QMenu( this );
m->addAction( "No groups" )->setEnabled( false );
mLayerListFromGroupButton->setMenu( m );
mLayerListFromGroupButton->setIcon( QgsApplication::getThemeIcon( "/mActionShowAllLayers.png" ) );
mLayerListFromGroupButton->setToolTip( tr( "Set layer list from a visibility group" ) );

connect( m, SIGNAL( aboutToShow() ), this, SLOT( aboutToShowVisibilityGroupsMenu() ) );

if ( composerMap )
{
connect( composerMap, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
Expand Down Expand Up @@ -249,6 +259,38 @@ void QgsComposerMapWidget::compositionAtlasToggled( bool atlasEnabled )
}
}

void QgsComposerMapWidget::aboutToShowVisibilityGroupsMenu()
{
QMenu* menu = qobject_cast<QMenu*>( sender() );
if ( !menu )
return;

menu->clear();
foreach ( QString groupName, QgsVisibilityGroups::instance()->groups() )
{
menu->addAction( groupName, this, SLOT( visibilityGroupSelected() ) );
}

if ( menu->actions().isEmpty() )
menu->addAction( tr( "No groups defined" ) )->setEnabled( false );
}

void QgsComposerMapWidget::visibilityGroupSelected()
{
QAction* action = qobject_cast<QAction*>( sender() );
if ( !action )
return;

QStringList lst = QgsVisibilityGroups::instance()->groupVisibleLayers( action->text() );
if ( mComposerMap )
{
mKeepLayerListCheckBox->setChecked( true );
mComposerMap->setLayerSet( lst );
mComposerMap->cache();
mComposerMap->update();
}
}

void QgsComposerMapWidget::on_mAtlasCheckBox_toggled( bool checked )
{
if ( !mComposerMap )
Expand Down
4 changes: 4 additions & 0 deletions src/app/composer/qgscomposermapwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ class QgsComposerMapWidget: public QgsComposerItemBaseWidget, private Ui::QgsCom
/**Enables or disables the atlas controls when composer atlas is toggled on/off*/
void compositionAtlasToggled( bool atlasEnabled );

void aboutToShowVisibilityGroupsMenu();

void visibilityGroupSelected();

private:
QgsComposerMap* mComposerMap;

Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! returns pointer to map legend
QgsLayerTreeView* layerTreeView();

QgsLayerTreeMapCanvasBridge* layerTreeCanvasBridge() { return mLayerTreeCanvasBridge; }

//! returns pointer to plugin manager
QgsPluginManager *pluginManager();

Expand Down
20 changes: 19 additions & 1 deletion src/app/qgsvisibilitygroups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "qgsvisibilitygroups.h"

#include "qgslayertree.h"
#include "qgslayertreemapcanvasbridge.h"
#include "qgsproject.h"
#include "qgisapp.h"

Expand Down Expand Up @@ -123,6 +124,23 @@ QStringList QgsVisibilityGroups::groups() const
return mGroups.keys();
}

QStringList QgsVisibilityGroups::groupVisibleLayers( const QString& name ) const
{
QSet<QString> visibleIds = mGroups.value( name ).mVisibleLayerIDs;

// also make sure to order the layers according to map canvas order
QgsLayerTreeMapCanvasBridge* bridge = QgisApp::instance()->layerTreeCanvasBridge();
QStringList order = bridge->hasCustomLayerOrder() ? bridge->customLayerOrder() : bridge->defaultLayerOrder();
QStringList order2;
foreach ( QString layerID, order )
{
if ( visibleIds.contains( layerID ) )
order2 << layerID;
}

return order2;
}

QMenu* QgsVisibilityGroups::menu()
{
return mMenu;
Expand Down Expand Up @@ -171,7 +189,7 @@ void QgsVisibilityGroups::applyState( const QString& groupName )
return;

const GroupRecord& rec = mGroups[groupName];
applyStateToLayerTreeGroup( QgsProject::instance()->layerTreeRoot(), QSet<QString>::fromList( rec.mVisibleLayerIDs ) );
applyStateToLayerTreeGroup( QgsProject::instance()->layerTreeRoot(), rec.mVisibleLayerIDs );

mMenuDirty = true;
}
Expand Down
17 changes: 15 additions & 2 deletions src/app/qgsvisibilitygroups.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <QMap>
#include <QObject>
#include <QStringList>
#include <QSet>

class QAction;
class QDomDocument;
Expand All @@ -27,21 +27,34 @@ class QMenu;
class QgsLayerTreeNode;
class QgsLayerTreeGroup;

/**
* Controller class that allows creation of visibility groups consisting of currently visible
* map layers in map canvas.
*/
class QgsVisibilityGroups : public QObject
{
Q_OBJECT
public:

static QgsVisibilityGroups* instance();

//! Add a new group using the current state of project's layer tree
void addGroup( const QString& name );
//! Update existing group using the current state of project's layer tree
void updateGroup( const QString& name );
//! Remove existing group
void removeGroup( const QString& name );

//! Remove all groups
void clear();

//! Return list of existing group names
QStringList groups() const;

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

//! Convenience menu that lists available groups and actions for management
QMenu* menu();

signals:
Expand Down Expand Up @@ -70,7 +83,7 @@ class QgsVisibilityGroups : public QObject
}

//! List of layers that are visible
QStringList mVisibleLayerIDs;
QSet<QString> mVisibleLayerIDs;
//! For layers that have checkable legend symbols and not all symbols are checked - list which ones are
//QMap<QString, QStringList> mPerLayerCheckedLegendSymbols;
} GroupRecord;
Expand Down
136 changes: 121 additions & 15 deletions src/ui/qgscomposermapwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-628</y>
<width>444</width>
<height>2231</height>
<y>0</y>
<width>447</width>
<height>2274</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -77,7 +77,7 @@
<property name="collapsed" stdset="0">
<bool>false</bool>
</property>
<layout class="QFormLayout" name="formLayout">
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
Expand Down Expand Up @@ -163,17 +163,44 @@
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="mKeepLayerListCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Lock layers for map item</string>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_10">
<item>
<widget class="QCheckBox" name="mKeepLayerListCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Lock layers for map item</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="mLayerListFromGroupButton">
<property name="text">
<string>...</string>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down Expand Up @@ -1268,6 +1295,85 @@
<header>qgsblendmodecombobox.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>scrollArea</tabstop>
<tabstop>mPreviewModeComboBox</tabstop>
<tabstop>mUpdatePreviewButton</tabstop>
<tabstop>mScaleLineEdit</tabstop>
<tabstop>mScaleDDBtn</tabstop>
<tabstop>mMapRotationSpinBox</tabstop>
<tabstop>mMapRotationDDBtn</tabstop>
<tabstop>mDrawCanvasItemsCheckBox</tabstop>
<tabstop>mKeepLayerListCheckBox</tabstop>
<tabstop>mLayerListFromGroupButton</tabstop>
<tabstop>mXMinLineEdit</tabstop>
<tabstop>mXMinDDBtn</tabstop>
<tabstop>mYMinLineEdit</tabstop>
<tabstop>mYMinDDBtn</tabstop>
<tabstop>mXMaxLineEdit</tabstop>
<tabstop>mXMaxDDBtn</tabstop>
<tabstop>mYMaxLineEdit</tabstop>
<tabstop>mYMaxDDBtn</tabstop>
<tabstop>mSetToMapCanvasExtentButton</tabstop>
<tabstop>mViewExtentInCanvasButton</tabstop>
<tabstop>mAtlasCheckBox</tabstop>
<tabstop>mAtlasMarginSpinBox</tabstop>
<tabstop>mAtlasPredefinedScaleRadio</tabstop>
<tabstop>mAtlasFixedScaleRadio</tabstop>
<tabstop>mAtlasMarginRadio</tabstop>
<tabstop>mAddGridPushButton</tabstop>
<tabstop>mRemoveGridPushButton</tabstop>
<tabstop>mGridUpButton</tabstop>
<tabstop>mGridDownButton</tabstop>
<tabstop>mGridListWidget</tabstop>
<tabstop>mGridCheckBox</tabstop>
<tabstop>mGridTypeComboBox</tabstop>
<tabstop>mMapGridCRSButton</tabstop>
<tabstop>mMapGridUnitComboBox</tabstop>
<tabstop>mIntervalXSpinBox</tabstop>
<tabstop>mIntervalYSpinBox</tabstop>
<tabstop>mOffsetXSpinBox</tabstop>
<tabstop>mOffsetYSpinBox</tabstop>
<tabstop>mCrossWidthSpinBox</tabstop>
<tabstop>mGridLineStyleButton</tabstop>
<tabstop>mGridBlendComboBox</tabstop>
<tabstop>mGridFramePenSizeSpinBox</tabstop>
<tabstop>mGridFramePenColorButton</tabstop>
<tabstop>mGridFrameFill1ColorButton</tabstop>
<tabstop>mGridFrameFill2ColorButton</tabstop>
<tabstop>mCheckGridLeftSide</tabstop>
<tabstop>mCheckGridRightSide</tabstop>
<tabstop>mCheckGridTopSide</tabstop>
<tabstop>mCheckGridBottomSide</tabstop>
<tabstop>mFrameStyleComboBox</tabstop>
<tabstop>mFrameWidthSpinBox</tabstop>
<tabstop>mDrawAnnotationGroupBox</tabstop>
<tabstop>mAnnotationFormatComboBox</tabstop>
<tabstop>mAnnotationPositionLeftComboBox</tabstop>
<tabstop>mAnnotationDirectionComboBoxLeft</tabstop>
<tabstop>mAnnotationPositionRightComboBox</tabstop>
<tabstop>mAnnotationDirectionComboBoxRight</tabstop>
<tabstop>mAnnotationPositionTopComboBox</tabstop>
<tabstop>mAnnotationDirectionComboBoxTop</tabstop>
<tabstop>mAnnotationPositionBottomComboBox</tabstop>
<tabstop>mAnnotationDirectionComboBoxBottom</tabstop>
<tabstop>mAnnotationFontButton</tabstop>
<tabstop>mAnnotationFontColorButton</tabstop>
<tabstop>mDistanceToMapFrameSpinBox</tabstop>
<tabstop>mCoordinatePrecisionSpinBox</tabstop>
<tabstop>mGridMarkerStyleButton</tabstop>
<tabstop>mAddOverviewPushButton</tabstop>
<tabstop>mRemoveOverviewPushButton</tabstop>
<tabstop>mOverviewUpButton</tabstop>
<tabstop>mOverviewDownButton</tabstop>
<tabstop>mOverviewListWidget</tabstop>
<tabstop>mOverviewCheckBox</tabstop>
<tabstop>mOverviewFrameMapComboBox</tabstop>
<tabstop>mOverviewFrameStyleButton</tabstop>
<tabstop>mOverviewBlendModeComboBox</tabstop>
<tabstop>mOverviewInvertCheckbox</tabstop>
<tabstop>mOverviewCenterCheckbox</tabstop>
</tabstops>
<resources>
<include location="../../images/images.qrc"/>
</resources>
Expand Down

0 comments on commit 1f8659c

Please sign in to comment.