Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor visibility presets, so that all non-app specific
methods are now in core

A new QgsVisibilityPresetCollection object has been created
which stores a set of visibility presets. QgsProject now
contains a preset collection object.

This allows plugins and other core objects to retrieve and
modify visibility presets.
  • Loading branch information
nyalldawson committed Aug 19, 2015
1 parent 612df6a commit 73522dc
Show file tree
Hide file tree
Showing 13 changed files with 687 additions and 352 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -127,6 +127,7 @@
%Include qgsprojectfiletransform.sip
%Include qgsvectorlayereditutils.sip
%Include qgsvectorlayerfeatureiterator.sip
%Include qgsvisibilitypresetcollection.sip
%Include qgslayerdefinition.sip

%Include composer/qgsaddremoveitemcommand.sip
Expand Down
5 changes: 5 additions & 0 deletions python/core/qgsproject.sip
Expand Up @@ -258,6 +258,11 @@ class QgsProject : QObject
*/
QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const;

/** Returns pointer to the project's visibility preset collection.
* @note added in QGIS 2.12
*/
QgsVisibilityPresetCollection* visibilityPresetCollection();

protected:

/** Set error message from read/write operation */
Expand Down
131 changes: 131 additions & 0 deletions python/core/qgsvisibilitypresetcollection.sip
@@ -0,0 +1,131 @@
/**
\class QgsVisibilityPresetCollection
\ingroup core
\brief Container class that allows storage of visibility presets consisting of visible
map layers and layer styles.
\note added in QGIS 2.12
*/

class QgsVisibilityPresetCollection : QObject
{
%TypeHeaderCode
#include <qgsvisibilitypresetcollection.h>
%End
public:

/** Individual preset record of visible layers and styles.
*/
class PresetRecord
{
public:

bool operator==( const QgsVisibilityPresetCollection::PresetRecord& other ) const;
bool operator!=( const QgsVisibilityPresetCollection::PresetRecord& other ) const;

//! 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
* @note not available in Python bindings
*/
//QMap<QString, QSet<QString> > mPerLayerCheckedLegendSymbols;
//! For layers that use multiple styles - which one is currently selected
QMap<QString, QString> mPerLayerCurrentStyle;
};

QgsVisibilityPresetCollection();

/** Returns whether a preset with a matching name exists.
* @param name name of preset to check
* @returns true if preset exists
*/
bool hasPreset( const QString& name ) const;

/** Inserts a new preset to the collection.
* @param name name of preset
* @param state preset record
* @see update()
*/
void insert( const QString& name, const PresetRecord& state );

/** Updates a preset within the collection.
* @param name name of preset to update
* @param state preset record to replace existing preset
* @see insert()
*/
void update( const QString& name, const PresetRecord& state );

/** Remove existing preset from collection.
* @param name preset name
*/
void removePreset( const QString& name );

//! Remove all presets from the collection.
void clear();

//! Returns a list of existing preset names.
QStringList presets() const;

/** Returns the recorded state of a preset.
* @param name name of preset
*/
PresetRecord presetState( const QString& name ) const;

/** Returns the list of layer IDs that should be visible for the specified preset.
* @note The order of the returned list is not guaranteed to reflect the order of layers
* in the canvas.
* @param name preset name
*/
QStringList presetVisibleLayers( const QString& name ) const;

/** Apply check states of legend nodes of a given layer as defined in the preset.
* @param preset name
* @param layerID layer ID
*/
void applyPresetCheckedLegendNodesToLayer( const QString& name, const QString& layerID );

/** Get layer style overrides (for QgsMapSettings) of the visible layers for given preset.
* @param name preset name
*/
QMap<QString, QString> presetStyleOverrides( const QString& name );

/** Reads the preset collection state from XML
* @param doc DOM document
* @see writeXML
*/
void readXML( const QDomDocument& doc );

/** Writes the preset collection state to XML.
* @param doc DOM document
* @see readXML
*/
void writeXML( QDomDocument& doc );

/** Static method for adding visible layers from a layer tree group to a preset
* record.
* @param parent layer tree group parent
* @param rec preset record to amend
*/
static void addVisibleLayersToPreset( QgsLayerTreeGroup* parent, PresetRecord& rec );

signals:

/** Emitted when presets within the collection are changed.
*/
void presetsChanged();

protected slots:

/** Handles updates of the preset collection when layers are removed from the registry
*/
void registryLayersRemoved( QStringList layerIDs );

//! Update style name if a stored style gets renamed
void layerStyleRenamed( const QString& oldName, const QString& newName );

protected:

/** Reconnects all preset layers to handle style renames
*/
void reconnectToLayersStyleManager();
};

12 changes: 6 additions & 6 deletions src/app/composer/qgscomposermapwidget.cpp
Expand Up @@ -37,7 +37,7 @@
#include "qgsexpressionbuilderdialog.h"
#include "qgsgenericprojectionselector.h"
#include "qgsproject.h"
#include "qgsvisibilitypresets.h"
#include "qgsvisibilitypresetcollection.h"
#include "qgisgui.h"

#include <QMessageBox>
Expand Down Expand Up @@ -285,12 +285,12 @@ void QgsComposerMapWidget::aboutToShowVisibilityPresetsMenu()
return;

menu->clear();
foreach ( QString presetName, QgsVisibilityPresets::instance()->presets() )
foreach ( QString presetName, QgsProject::instance()->visibilityPresetCollection()->presets() )
{
QAction* a = menu->addAction( presetName, this, SLOT( visibilityPresetSelected() ) );
a->setCheckable( true );
QStringList layers = QgsVisibilityPresets::instance()->presetVisibleLayers( presetName );
QMap<QString, QString> styles = QgsVisibilityPresets::instance()->presetStyleOverrides( presetName );
QStringList layers = QgsProject::instance()->visibilityPresetCollection()->presetVisibleLayers( presetName );
QMap<QString, QString> styles = QgsProject::instance()->visibilityPresetCollection()->presetStyleOverrides( presetName );
if ( layers == mComposerMap->layerSet() && styles == mComposerMap->layerStyleOverrides() )
a->setChecked( true );
}
Expand All @@ -306,15 +306,15 @@ void QgsComposerMapWidget::visibilityPresetSelected()
return;

QString presetName = action->text();
QStringList lst = QgsVisibilityPresets::instance()->presetVisibleLayers( presetName );
QStringList lst = QgsProject::instance()->visibilityPresetCollection()->presetVisibleLayers( presetName );
if ( mComposerMap )
{
mKeepLayerListCheckBox->setChecked( true );
mComposerMap->setLayerSet( lst );

mKeepLayerStylesCheckBox->setChecked( true );

mComposerMap->setLayerStyleOverrides( QgsVisibilityPresets::instance()->presetStyleOverrides( presetName ) );
mComposerMap->setLayerStyleOverrides( QgsProject::instance()->visibilityPresetCollection()->presetStyleOverrides( presetName ) );

mComposerMap->cache();
mComposerMap->update();
Expand Down
2 changes: 0 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -3756,8 +3756,6 @@ void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )
fileNewFromDefaultTemplate();
}

QgsVisibilityPresets::instance()->clear();

// set the initial map tool
#ifndef HAVE_TOUCH
mMapCanvas->setMapTool( mMapTools.mPan );
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsdxfexportdialog.cpp
Expand Up @@ -26,7 +26,7 @@
#include "qgsfieldcombobox.h"
#include "qgisapp.h"
#include "qgslayertreemapcanvasbridge.h"
#include "qgsvisibilitypresets.h"
#include "qgsvisibilitypresetcollection.h"

#include <QFileDialog>
#include <QPushButton>
Expand Down Expand Up @@ -329,7 +329,7 @@ QList< QPair<QgsVectorLayer *, int> > QgsVectorLayerAndAttributeModel::layers()

void QgsVectorLayerAndAttributeModel::applyVisibilityPreset( const QString &name )
{
QSet<QString> visibleLayers = QgsVisibilityPresets::instance()->presetVisibleLayers( name ).toSet();
QSet<QString> visibleLayers = QgsProject::instance()->visibilityPresetCollection()->presetVisibleLayers( name ).toSet();
if ( visibleLayers.isEmpty() )
return;

Expand Down Expand Up @@ -429,7 +429,7 @@ QgsDxfExportDialog::QgsDxfExportDialog( QWidget *parent, Qt::WindowFlags f )
mScaleWidget->setScale( s.value( "qgis/lastSymbologyExportScale", "1/50000" ).toDouble() );
mMapExtentCheckBox->setChecked( s.value( "qgis/lastDxfMapRectangle", "false" ).toBool() );

QStringList ids = QgsVisibilityPresets::instance()->presets();
QStringList ids = QgsProject::instance()->visibilityPresetCollection()->presets();
ids.prepend( "" );
mVisibilityPresets->addItems( ids );

Expand Down

1 comment on commit 73522dc

@NathanW2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet!

Please sign in to comment.