Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve the Styles menu in legend context menu
  • Loading branch information
wonder-sk committed Jan 7, 2015
1 parent 547d018 commit edf3ebb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
27 changes: 15 additions & 12 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -62,8 +62,6 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
{
QgsMapLayer* layer = QgsLayerTree::toLayer( node )->layer();

QgsMapLayerStyleGuiUtils::instance()->addStyleManagerMenu( menu, layer );

menu->addAction( actions->actionZoomToLayer( mCanvas, menu ) );
menu->addAction( actions->actionShowInOverview( menu ) );

Expand All @@ -90,6 +88,21 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
// assign layer crs to project
menu->addAction( QgsApplication::getThemeIcon( "/mActionSetProjectCRS.png" ), tr( "Set &Project CRS from Layer" ), QgisApp::instance(), SLOT( setProjectCRSFromLayer() ) );

// style-related actions
if ( mView->selectedLayerNodes().count() == 1 )
{
QMenu* menuStyleManager = QgsMapLayerStyleGuiUtils::instance()->createStyleManagerMenu( layer );

QgisApp* app = QgisApp::instance();
menuStyleManager->addSeparator();
menuStyleManager->addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );
if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
menuStyleManager->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
}
menu->addMenu( menuStyleManager );
}

menu->addSeparator();

if ( layer && layer->type() == QgsMapLayer::VectorLayer )
Expand Down Expand Up @@ -160,16 +173,6 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()

if ( mView->selectedNodes( true ).count() >= 2 )
menu->addAction( actions->actionGroupSelected( menu ) );

if ( mView->selectedLayerNodes().count() == 1 )
{
QgisApp* app = QgisApp::instance();
menu->addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );
if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
menu->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
}
}
}

}
Expand Down
24 changes: 19 additions & 5 deletions src/app/qgsmaplayerstyleguiutils.cpp
Expand Up @@ -9,16 +9,20 @@
#include "qgsmaplayerstylemanager.h"


void QgsMapLayerStyleGuiUtils::addStyleManagerMenu( QMenu* menu, QgsMapLayer* layer )
QMenu* QgsMapLayerStyleGuiUtils::createStyleManagerMenu( QgsMapLayer* layer )
{
layer->enableStyleManager();
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();

if ( !mgr )
return m;

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

QgsMapLayerStyleManager* mgr = layer->styleManager();
foreach ( QString name, mgr->styles() )
{
bool active = name == mgr->currentStyle();
Expand All @@ -33,7 +37,7 @@ void QgsMapLayerStyleGuiUtils::addStyleManagerMenu( QMenu* menu, QgsMapLayer* la
actionRemove->setData( QVariant::fromValue<QObject*>( layer ) );
}

menu->addMenu( m );
return m;
}

QString QgsMapLayerStyleGuiUtils::defaultStyleName()
Expand All @@ -58,6 +62,8 @@ void QgsMapLayerStyleGuiUtils::addStyle()
if ( !ok || text.isEmpty() )
return;

layer->enableStyleManager(); // make sure it exists

bool res = layer->styleManager()->addStyleFromLayer( text );
qDebug( "ADD: %d", res );

Expand Down Expand Up @@ -92,6 +98,14 @@ void QgsMapLayerStyleGuiUtils::removeStyle()
QgsMapLayer* layer = qobject_cast<QgsMapLayer*>( a->data().value<QObject*>() );
if ( !layer )
return;

if ( layer->styleManager()->styles().count() == 1 )
{
// let's get rid of the style manager altogether
layer->enableStyleManager( false );
return;
}

QString name = a->text();
if ( name == defaultStyleName() )
name.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaplayerstyleguiutils.h
Expand Up @@ -14,7 +14,7 @@ class QgsMapLayerStyleGuiUtils : public QObject, public QgsSingleton<QgsMapLayer
Q_OBJECT
public:

void addStyleManagerMenu( QMenu* menu, QgsMapLayer* layer );
QMenu* createStyleManagerMenu( QgsMapLayer* layer );

private:
QString defaultStyleName();
Expand Down

0 comments on commit edf3ebb

Please sign in to comment.