Skip to content

Commit

Permalink
Make the style GUI utils more granular + move copy/paste to the top o…
Browse files Browse the repository at this point in the history
…f menu

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 0cde4f1 commit 7e3bef6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
7 changes: 5 additions & 2 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,18 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
// style-related actions
if ( mView->selectedLayerNodes().count() == 1 )
{
QMenu* menuStyleManager = QgsMapLayerStyleGuiUtils::instance()->createStyleManagerMenu( layer );
QMenu* menuStyleManager = new QMenu( tr( "Styles" ) );

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

menuStyleManager->addSeparator();
QgsMapLayerStyleGuiUtils::instance()->addStyleManagerActions( menuStyleManager, layer );

menu->addMenu( menuStyleManager );
}

Expand Down
54 changes: 39 additions & 15 deletions src/app/qgsmaplayerstyleguiutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,62 @@
#include "qgsmaplayerstylemanager.h"


QMenu* QgsMapLayerStyleGuiUtils::createStyleManagerMenu( QgsMapLayer* layer )
QAction* QgsMapLayerStyleGuiUtils::actionAddStyle( QgsMapLayer* layer, QObject* parent )
{
QMenu* m = new QMenu( tr( "Styles" ) );
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* a = new QAction( tr( "Add" ), parent );
a->setData( QVariant::fromValue<QObject*>( layer ) );
connect( a, SIGNAL( triggered() ), this, SLOT( addStyle() ) );
return a;
}

QAction* actionRemove = m->addAction( tr( "Remove Current" ), this, SLOT( removeStyle() ) );
actionRemove->setData( QVariant::fromValue<QObject*>( layer ) );
actionRemove->setEnabled( !onlyOneStyle );
QAction* QgsMapLayerStyleGuiUtils::actionRemoveStyle( QgsMapLayer* layer, QObject* parent )
{
QAction* a = new QAction( tr( "Remove Current" ), parent );
a->connect( a, SIGNAL( triggered() ), this, SLOT( removeStyle() ) );
a->setData( QVariant::fromValue<QObject*>( layer ) );
a->setEnabled( layer->styleManager()->styles().count() > 1 );
return a;
}

QAction* actionRename = m->addAction( tr( "Rename Current" ), this, SLOT( renameStyle() ) );
actionRename->setData( QVariant::fromValue<QObject*>( layer ) );
QAction* QgsMapLayerStyleGuiUtils::actionRenameStyle( QgsMapLayer* layer, QObject* parent )
{
QAction* a = new QAction( tr( "Rename Current" ), parent );
a->connect( a, SIGNAL( triggered() ), this, SLOT( renameStyle() ) );
a->setData( QVariant::fromValue<QObject*>( layer ) );
return a;
}

m->addSeparator();
QList<QAction*> QgsMapLayerStyleGuiUtils::actionsUseStyle( QgsMapLayer* layer, QObject* parent )
{
QgsMapLayerStyleManager* mgr = layer->styleManager();
bool onlyOneStyle = mgr->styles().count() == 1;

QList<QAction*> actions;
foreach ( QString name, mgr->styles() )
{
bool active = name == mgr->currentStyle();
if ( name.isEmpty() )
name = defaultStyleName();
QAction* actionUse = m->addAction( name, this, SLOT( useStyle() ) );
QAction* actionUse = new QAction( name, parent );
connect( actionUse, SIGNAL( triggered() ), this, SLOT( useStyle() ) );
actionUse->setCheckable( true );
actionUse->setChecked( active );
actionUse->setEnabled( !onlyOneStyle );

actionUse->setData( QVariant::fromValue<QObject*>( layer ) );
actions << actionUse;
}
return actions;
}

return m;
void QgsMapLayerStyleGuiUtils::addStyleManagerActions( QMenu* m, QgsMapLayer* layer )
{
m->addAction( actionAddStyle( layer ) );
m->addAction( actionRemoveStyle( layer ) );
m->addAction( actionRenameStyle( layer ) );
m->addSeparator();
foreach ( QAction* a, actionsUseStyle( layer ) )
m->addAction( a );
}

QString QgsMapLayerStyleGuiUtils::defaultStyleName()
Expand Down
11 changes: 9 additions & 2 deletions src/app/qgsmaplayerstyleguiutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "qgssingleton.h"

class QgsMapLayer;

class QAction;
class QMenu;

/** Various GUI utility functions for dealing with map layer's style manager */
Expand All @@ -29,8 +31,13 @@ class QgsMapLayerStyleGuiUtils : public QObject, public QgsSingleton<QgsMapLayer
Q_OBJECT
public:

//! Return menu instance with actions for the give map layer
QMenu* createStyleManagerMenu( QgsMapLayer* layer );
QAction* actionAddStyle( QgsMapLayer* layer, QObject* parent = 0 );
QAction* actionRemoveStyle( QgsMapLayer* layer, QObject* parent = 0 );
QAction* actionRenameStyle( QgsMapLayer* layer, QObject* parent = 0 );
QList<QAction*> actionsUseStyle( QgsMapLayer* layer, QObject* parent = 0 );

//! Add actions for the given map layer to the menu
void addStyleManagerActions( QMenu* m, QgsMapLayer* layer );

private:
QString defaultStyleName();
Expand Down

0 comments on commit 7e3bef6

Please sign in to comment.