Navigation Menu

Skip to content

Commit

Permalink
Added support for renaming of map layer styles
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 9, 2015
1 parent e8aedcb commit 50e77a9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
3 changes: 3 additions & 0 deletions python/core/qgsmaplayerstylemanager.sip
Expand Up @@ -60,6 +60,9 @@ class QgsMapLayerStyleManager
//! Remove a stored style
//! @return true on success (style exists and it is not the last one)
bool removeStyle( const QString& name );
//! Rename a stored style to a different name
//! @return true on success (style exists and new name is unique)
bool renameStyle( const QString& name, const QString& newName );

//! Return name of the current style
QString currentStyle() const;
Expand Down
45 changes: 34 additions & 11 deletions src/app/qgsmaplayerstyleguiutils.cpp
Expand Up @@ -28,12 +28,19 @@
QMenu* QgsMapLayerStyleGuiUtils::createStyleManagerMenu( QgsMapLayer* layer )
{
QMenu* m = new QMenu( tr( "Styles" ) );
QAction* actionAdd = m->addAction( tr( "Add..." ), this, SLOT( addStyle() ) );
QAction* actionAdd = m->addAction( tr( "Add" ), this, SLOT( addStyle() ) );
actionAdd->setData( QVariant::fromValue<QObject*>( layer ) );

QgsMapLayerStyleManager* mgr = layer->styleManager();
bool onlyOneStyle = mgr->styles().count() == 1;

QAction* actionRemove = m->addAction( tr( "Remove Current" ), this, SLOT( removeStyle() ) );
actionRemove->setData( QVariant::fromValue<QObject*>( layer ) );
actionRemove->setEnabled( !onlyOneStyle );

QAction* actionRename = m->addAction( tr( "Rename Current" ), this, SLOT( renameStyle() ) );
actionRename->setData( QVariant::fromValue<QObject*>( layer ) );

QMenu* mRemove = m->addMenu( tr( "Remove" ) );
m->addSeparator();

foreach ( QString name, mgr->styles() )
Expand All @@ -44,10 +51,9 @@ QMenu* QgsMapLayerStyleGuiUtils::createStyleManagerMenu( QgsMapLayer* layer )
QAction* actionUse = m->addAction( name, this, SLOT( useStyle() ) );
actionUse->setCheckable( true );
actionUse->setChecked( active );
actionUse->setData( QVariant::fromValue<QObject*>( layer ) );
actionUse->setEnabled( !onlyOneStyle );

QAction* actionRemove = mRemove->addAction( name, this, SLOT( removeStyle() ) );
actionRemove->setData( QVariant::fromValue<QObject*>( layer ) );
actionUse->setData( QVariant::fromValue<QObject*>( layer ) );
}

return m;
Expand Down Expand Up @@ -110,14 +116,31 @@ void QgsMapLayerStyleGuiUtils::removeStyle()
if ( !layer )
return;

if ( layer->styleManager()->styles().count() == 1 )
bool res = layer->styleManager()->removeStyle( layer->styleManager()->currentStyle() );
if ( !res )
QgsDebugMsg( "Failed to remove current style" );
}


void QgsMapLayerStyleGuiUtils::renameStyle()
{
QAction* a = qobject_cast<QAction*>( sender() );
if ( !a )
return;
QgsMapLayer* layer = qobject_cast<QgsMapLayer*>( a->data().value<QObject*>() );
if ( !layer )
return;

QString name = a->text();
if ( name == defaultStyleName() )
name.clear();
QString name = layer->styleManager()->currentStyle();

bool ok;
QString text = QInputDialog::getText( 0, tr( "Rename style" ),
tr( "Style name:" ), QLineEdit::Normal,
name, &ok );
if ( !ok )
return;

bool res = layer->styleManager()->removeStyle( name );
bool res = layer->styleManager()->renameStyle( name, text );
if ( !res )
QgsDebugMsg( "Failed to remove style: " + name );
QgsDebugMsg( "Failed to rename style: " + name );
}
2 changes: 1 addition & 1 deletion src/app/qgsmaplayerstyleguiutils.h
Expand Up @@ -39,7 +39,7 @@ class QgsMapLayerStyleGuiUtils : public QObject, public QgsSingleton<QgsMapLayer
void addStyle();
void useStyle();
void removeStyle();

void renameStyle();
};

#endif // QGSMAPLAYERSTYLEGUIUTILS_H
13 changes: 13 additions & 0 deletions src/core/qgsmaplayerstylemanager.cpp
Expand Up @@ -122,6 +122,19 @@ bool QgsMapLayerStyleManager::removeStyle( const QString& name )
return true;
}

bool QgsMapLayerStyleManager::renameStyle( const QString& name, const QString& newName )
{
if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
return false;

if ( name == mCurrentStyle )
mCurrentStyle = newName;

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

QString QgsMapLayerStyleManager::currentStyle() const
{
return mCurrentStyle;
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsmaplayerstylemanager.h
Expand Up @@ -109,6 +109,9 @@ class CORE_EXPORT QgsMapLayerStyleManager
//! Remove a stored style
//! @return true on success (style exists and it is not the last one)
bool removeStyle( const QString& name );
//! Rename a stored style to a different name
//! @return true on success (style exists and new name is unique)
bool renameStyle( const QString& name, const QString& newName );

//! Return name of the current style
QString currentStyle() const;
Expand Down

0 comments on commit 50e77a9

Please sign in to comment.