Make sure to update visibility presets if any layer's style name changes

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.
This commit is contained in:
Martin Dobias 2015-01-19 18:39:03 +07:00
parent 5266fd97ee
commit 375dc32045
6 changed files with 90 additions and 18 deletions

View File

@ -32,7 +32,7 @@ class QgsMapLayerStyle
};
class QgsMapLayerStyleManager
class QgsMapLayerStyleManager : QObject
{
%TypeHeaderCode
#include <qgsmaplayerstylemanager.h>
@ -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();
@ -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 );
};

View File

@ -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 )
@ -153,6 +140,8 @@ QgsVisibilityPresets* QgsVisibilityPresets::instance()
void QgsVisibilityPresets::addPreset( const QString& name )
{
mPresets.insert( name, currentState() );
reconnectToLayersStyleManager();
}
void QgsVisibilityPresets::updatePreset( const QString& name )
@ -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
@ -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()
{
@ -480,6 +494,8 @@ void QgsVisibilityPresets::readProject( const QDomDocument& doc )
visPresetElem = visPresetElem.nextSiblingElement( "visibility-preset" );
}
reconnectToLayersStyleManager();
}
void QgsVisibilityPresets::writeProject( QDomDocument& doc )
@ -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;
}
}
}

View File

@ -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 );
@ -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
@ -119,6 +119,8 @@ class QgsVisibilityPresets : public QObject
PresetRecord currentState();
void applyState( const QString& presetName );
void reconnectToLayersStyleManager();
static QgsVisibilityPresets* sInstance;
PresetRecordMap mPresets;

View File

@ -342,6 +342,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsmaplayer.h
qgsmaplayerlegend.h
qgsmaplayerregistry.h
qgsmaplayerstylemanager.h
qgsmaprenderer.h
qgsmaprenderercache.h
qgsmaprenderercustompainterjob.h

View File

@ -91,6 +91,7 @@ bool QgsMapLayerStyleManager::addStyle( const QString& name, const QgsMapLayerSt
return false;
mStyles.insert( name, style );
emit styleAdded( name );
return true;
}
@ -119,6 +120,7 @@ bool QgsMapLayerStyleManager::removeStyle( const QString& name )
}
mStyles.remove( name );
emit styleRemoved( name );
return true;
}
@ -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;
}
@ -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;
}

View File

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