Skip to content
Permalink
Browse files

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 73522dce7e65d2aeec6a8350eb09fceedee04243
@@ -127,6 +127,7 @@
%Include qgsprojectfiletransform.sip
%Include qgsvectorlayereditutils.sip
%Include qgsvectorlayerfeatureiterator.sip
%Include qgsvisibilitypresetcollection.sip
%Include qgslayerdefinition.sip

%Include composer/qgsaddremoveitemcommand.sip
@@ -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 */
@@ -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();
};

@@ -37,7 +37,7 @@
#include "qgsexpressionbuilderdialog.h"
#include "qgsgenericprojectionselector.h"
#include "qgsproject.h"
#include "qgsvisibilitypresets.h"
#include "qgsvisibilitypresetcollection.h"
#include "qgisgui.h"

#include <QMessageBox>
@@ -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 );
}
@@ -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();
@@ -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 );
@@ -26,7 +26,7 @@
#include "qgsfieldcombobox.h"
#include "qgisapp.h"
#include "qgslayertreemapcanvasbridge.h"
#include "qgsvisibilitypresets.h"
#include "qgsvisibilitypresetcollection.h"

#include <QFileDialog>
#include <QPushButton>
@@ -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;

@@ -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 );

1 comment on commit 73522dc

@NathanW2

This comment has been minimized.

Copy link
Member

@NathanW2 NathanW2 commented on 73522dc Aug 19, 2015

Sweet!

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