Skip to content

Commit

Permalink
Make sure to update visibility presets if any layer's style name changes
Browse files Browse the repository at this point in the history
Also adds signals to the QgsMapLayerStyleManager so others can watch the changes

This code has been funded by Tuscany Region (Italy) - SITA (CIG: 6002233F59)
and commissioned to Gis3W s.a.s.
  • Loading branch information
wonder-sk committed Jan 19, 2015
1 parent 5266fd9 commit 375dc32
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 18 deletions.
15 changes: 14 additions & 1 deletion python/core/qgsmaplayerstylemanager.sip
Expand Up @@ -32,7 +32,7 @@ class QgsMapLayerStyle
};


class QgsMapLayerStyleManager
class QgsMapLayerStyleManager : QObject
{
%TypeHeaderCode
#include <qgsmaplayerstylemanager.h>
Expand All @@ -41,6 +41,9 @@ class QgsMapLayerStyleManager
//! Construct a style manager associated with a map layer (must not be null)
QgsMapLayerStyleManager( QgsMapLayer* layer );

//! Get pointer to the associated map layer
QgsMapLayer* layer() const;

//! Reset the style manager to a basic state - with one default style which is set as current
void reset();

Expand Down Expand Up @@ -79,4 +82,14 @@ class QgsMapLayerStyleManager
bool setOverrideStyle( const QString& styleDef );
//! Restore the original store after a call to setOverrideStyle()
bool restoreOverrideStyle();

signals:
//! Emitted when a new style has been added
void styleAdded( const QString& name );
//! Emitted when a style has been removed
void styleRemoved( const QString& name );
//! Emitted when a style has been renamed
void styleRenamed( const QString& oldName, const QString& newName );
//! Emitted when the current style has been changed
void currentStyleChanged( const QString& currentName );
};
63 changes: 50 additions & 13 deletions src/app/qgsvisibilitypresets.cpp
Expand Up @@ -129,19 +129,6 @@ QgsVisibilityPresets::PresetRecord QgsVisibilityPresets::currentState()
return rec;
}

QgsVisibilityPresets::PresetRecord QgsVisibilityPresets::currentStateFromLayerList( const QStringList& layerIDs, const QMap<QString, QString>& layerStyleOverrides )
{
PresetRecord rec;
foreach ( const QString& layerID, layerIDs )
rec.mVisibleLayerIDs << layerID;
addPerLayerCheckedLegendSymbols( rec );
addPerLayerCurrentStyle( rec );
foreach ( const QString& layerID, layerStyleOverrides.keys() )
rec.mPerLayerCurrentStyle[layerID] = layerStyleOverrides[layerID];
return rec;
}


QgsVisibilityPresets* QgsVisibilityPresets::instance()
{
if ( !sInstance )
Expand All @@ -153,6 +140,8 @@ QgsVisibilityPresets* QgsVisibilityPresets::instance()
void QgsVisibilityPresets::addPreset( const QString& name )
{
mPresets.insert( name, currentState() );

reconnectToLayersStyleManager();
}

void QgsVisibilityPresets::updatePreset( const QString& name )
Expand All @@ -161,16 +150,22 @@ void QgsVisibilityPresets::updatePreset( const QString& name )
return;

mPresets[name] = currentState();

reconnectToLayersStyleManager();
}

void QgsVisibilityPresets::removePreset( const QString& name )
{
mPresets.remove( name );

reconnectToLayersStyleManager();
}

void QgsVisibilityPresets::clear()
{
mPresets.clear();

reconnectToLayersStyleManager();
}

QStringList QgsVisibilityPresets::presets() const
Expand Down Expand Up @@ -392,6 +387,25 @@ void QgsVisibilityPresets::applyState( const QString& presetName )
}
}

void QgsVisibilityPresets::reconnectToLayersStyleManager()
{
disconnect( 0, 0, this, SLOT( layerStyleRenamed( QString, QString ) ) );

QSet<QString> layerIDs;
foreach ( const QString& grpName, mPresets.keys() )
{
const PresetRecord& rec = mPresets[grpName];
foreach ( const QString& layerID, rec.mPerLayerCurrentStyle.keys() )
layerIDs << layerID;
}

foreach ( const QString& layerID, layerIDs )
{
if ( QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( layerID ) )
connect( ml->styleManager(), SIGNAL( styleRenamed( QString, QString ) ), this, SLOT( layerStyleRenamed( QString, QString ) ) );
}
}


void QgsVisibilityPresets::removeCurrentPreset()
{
Expand Down Expand Up @@ -480,6 +494,8 @@ void QgsVisibilityPresets::readProject( const QDomDocument& doc )

visPresetElem = visPresetElem.nextSiblingElement( "visibility-preset" );
}

reconnectToLayersStyleManager();
}

void QgsVisibilityPresets::writeProject( QDomDocument& doc )
Expand Down Expand Up @@ -531,3 +547,24 @@ void QgsVisibilityPresets::registryLayersRemoved( QStringList layerIDs )
}
}
}

void QgsVisibilityPresets::layerStyleRenamed( const QString& oldName, const QString& newName )
{
QgsMapLayerStyleManager* styleMgr = qobject_cast<QgsMapLayerStyleManager*>( sender() );
if ( !styleMgr )
return;

QString layerID = styleMgr->layer()->id();

foreach ( QString presetName, mPresets.keys() )
{
PresetRecord& rec = mPresets[presetName];

if ( rec.mPerLayerCurrentStyle.contains( layerID ) )
{
QString styleName = rec.mPerLayerCurrentStyle[layerID];
if ( styleName == oldName )
rec.mPerLayerCurrentStyle[layerID] = newName;
}
}
}
8 changes: 5 additions & 3 deletions src/app/qgsvisibilitypresets.h
Expand Up @@ -86,9 +86,6 @@ class QgsVisibilityPresets : public QObject
//! Convenience menu that lists available presets and actions for management
QMenu* menu();

//! Create preset record given a list of visible layers (needs to store per-layer checked legend symbols)
PresetRecord currentStateFromLayerList( const QStringList& layerIDs, const QMap<QString, QString>& layerStyleOverrides );

//! Get layer style overrides (for QgsMapSettings) of the visible layers for given preset
QMap<QString, QString> presetStyleOverrides( const QString& presetName );

Expand All @@ -106,6 +103,9 @@ class QgsVisibilityPresets : public QObject

void registryLayersRemoved( QStringList layerIDs );

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

protected:
QgsVisibilityPresets(); // singleton

Expand All @@ -119,6 +119,8 @@ class QgsVisibilityPresets : public QObject
PresetRecord currentState();
void applyState( const QString& presetName );

void reconnectToLayersStyleManager();

static QgsVisibilityPresets* sInstance;

PresetRecordMap mPresets;
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -342,6 +342,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsmaplayer.h
qgsmaplayerlegend.h
qgsmaplayerregistry.h
qgsmaplayerstylemanager.h
qgsmaprenderer.h
qgsmaprenderercache.h
qgsmaprenderercustompainterjob.h
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaplayerstylemanager.cpp
Expand Up @@ -91,6 +91,7 @@ bool QgsMapLayerStyleManager::addStyle( const QString& name, const QgsMapLayerSt
return false;

mStyles.insert( name, style );
emit styleAdded( name );
return true;
}

Expand Down Expand Up @@ -119,6 +120,7 @@ bool QgsMapLayerStyleManager::removeStyle( const QString& name )
}

mStyles.remove( name );
emit styleRemoved( name );
return true;
}

Expand All @@ -132,6 +134,7 @@ bool QgsMapLayerStyleManager::renameStyle( const QString& name, const QString& n

mStyles[newName] = mStyles[name];
mStyles.remove( name );
emit styleRenamed( name, newName );
return true;
}

Expand All @@ -152,6 +155,8 @@ bool QgsMapLayerStyleManager::setCurrentStyle( const QString& name )
mCurrentStyle = name;
mStyles[mCurrentStyle].writeToLayer( mLayer );
mStyles[mCurrentStyle].clear(); // current style does not keep any stored data
emit currentStyleChanged( mCurrentStyle );

mLayer->triggerRepaint();
return true;
}
Expand Down
16 changes: 15 additions & 1 deletion src/core/qgsmaplayerstylemanager.h
Expand Up @@ -87,12 +87,16 @@ class CORE_EXPORT QgsMapLayerStyle
*
* @note added in 2.8
*/
class CORE_EXPORT QgsMapLayerStyleManager
class CORE_EXPORT QgsMapLayerStyleManager : public QObject
{
Q_OBJECT
public:
//! Construct a style manager associated with a map layer (must not be null)
QgsMapLayerStyleManager( QgsMapLayer* layer );

//! Get pointer to the associated map layer
QgsMapLayer* layer() const { return mLayer; }

//! Reset the style manager to a basic state - with one default style which is set as current
void reset();

Expand Down Expand Up @@ -132,6 +136,16 @@ class CORE_EXPORT QgsMapLayerStyleManager
//! Restore the original store after a call to setOverrideStyle()
bool restoreOverrideStyle();

signals:
//! Emitted when a new style has been added
void styleAdded( const QString& name );
//! Emitted when a style has been removed
void styleRemoved( const QString& name );
//! Emitted when a style has been renamed
void styleRenamed( const QString& oldName, const QString& newName );
//! Emitted when the current style has been changed
void currentStyleChanged( const QString& currentName );

private:
QgsMapLayer* mLayer;
QMap<QString, QgsMapLayerStyle> mStyles;
Expand Down

0 comments on commit 375dc32

Please sign in to comment.