Skip to content

Commit

Permalink
Added a GUI utility class to support multiple styles per map layer
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 7, 2015
1 parent 829f99b commit 547d018
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 96 deletions.
2 changes: 2 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -45,6 +45,7 @@ SET(QGIS_APP_SRCS
qgslabelinggui.cpp
qgslabelpreview.cpp
qgsloadstylefromdbdialog.cpp
qgsmaplayerstyleguiutils.cpp
qgsmapmouseevent.cpp
qgssavestyletodbdialog.cpp
qgsguivectorlayertools.cpp
Expand Down Expand Up @@ -200,6 +201,7 @@ SET (QGIS_APP_MOC_HDRS
qgslabelinggui.h
qgslabelpropertydialog.h
qgsloadstylefromdbdialog.h
qgsmaplayerstyleguiutils.h
qgssavestyletodbdialog.h
qgsshortcutsmanager.h
qgsapplayertreeviewmenuprovider.h
Expand Down
91 changes: 2 additions & 89 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -7,6 +7,7 @@
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgsmaplayerstyleguiutils.h"
#include "qgsproject.h"
#include "qgsrasterlayer.h"
#include "qgsvectordataprovider.h"
Expand Down Expand Up @@ -61,7 +62,7 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
{
QgsMapLayer* layer = QgsLayerTree::toLayer( node )->layer();

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

menu->addAction( actions->actionZoomToLayer( mCanvas, menu ) );
menu->addAction( actions->actionShowInOverview( menu ) );
Expand Down Expand Up @@ -321,91 +322,3 @@ void QgsAppLayerTreeViewMenuProvider::addCustomLayerActions( QMenu* menu, QgsMap
menu->addSeparator();
}
}

#include <QInputDialog>
#include "qgsmapcanvas.h"
#include "qgsmaplayerstylemanager.h"
void QgsAppLayerTreeViewMenuProvider::addStyleManagerStuff( QMenu* menu, QgsMapLayer* layer )
{
layer->enableStyleManager();
QMenu* m = new QMenu( tr( "Styles" ) );
m->addAction( "Add", this, SLOT( addStyle() ) );
QMenu* mRemove = m->addMenu( "Remove" );
m->addSeparator();

QgsMapLayerStyleManager* mgr = layer->styleManager();
foreach ( QString name, mgr->styles() )
{
bool active = name == mgr->currentStyle();
if ( name.isEmpty() )
name = "(default)";
QAction* a = m->addAction( name, this, SLOT( useStyle() ) );
a->setCheckable( true );
a->setChecked( active );

mRemove->addAction( name, this, SLOT( removeStyle() ) );
}

menu->addMenu( m );
}

void QgsAppLayerTreeViewMenuProvider::addStyle()
{
QgsMapLayer* layer = mView->currentLayer();
if ( !layer )
return;

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

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

if ( res ) // make it active!
layer->styleManager()->setCurrentStyle( text );
}

void QgsAppLayerTreeViewMenuProvider::useStyle()
{
QgsMapLayer* layer = mView->currentLayer();
if ( !layer )
return;

QAction* a = qobject_cast<QAction*>( sender() );
if ( !a )
return;
QString name = a->text();
if ( name == "(default)" )
name.clear();

bool res = layer->styleManager()->setCurrentStyle( name );
qDebug( "USE: %d", res );

layer->triggerRepaint();
}

void QgsAppLayerTreeViewMenuProvider::removeStyle()
{
QgsMapLayer* layer = mView->currentLayer();
if ( !layer )
return;

QAction* a = qobject_cast<QAction*>( sender() );
if ( !a )
return;
QString name = a->text();
if ( name == "(default)" )
name.clear();

bool needsRefresh = ( layer->styleManager()->currentStyle() == name );

bool res = layer->styleManager()->removeStyle( name );
qDebug( "DEL: %d", res );

if ( needsRefresh )
layer->triggerRepaint();
}
7 changes: 0 additions & 7 deletions src/app/qgsapplayertreeviewmenuprovider.h
Expand Up @@ -36,17 +36,10 @@ class QgsAppLayerTreeViewMenuProvider : public QObject, public QgsLayerTreeViewM
void removeLegendLayerActionsForLayer( QgsMapLayer* layer );
QList< LegendLayerAction > legendLayerActions( QgsMapLayer::LayerType type ) const;

protected slots:
void addStyle();
void useStyle();
void removeStyle();

protected:

void addCustomLayerActions( QMenu* menu, QgsMapLayer* layer );

void addStyleManagerStuff( QMenu* menu, QgsMapLayer* layer );

QgsLayerTreeView* mView;
QgsMapCanvas* mCanvas;

Expand Down
106 changes: 106 additions & 0 deletions src/app/qgsmaplayerstyleguiutils.cpp
@@ -0,0 +1,106 @@
#include "qgsmaplayerstyleguiutils.h"

#include <QAction>
#include <QInputDialog>
#include <QMenu>

#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerstylemanager.h"


void QgsMapLayerStyleGuiUtils::addStyleManagerMenu( QMenu* menu, QgsMapLayer* layer )
{
layer->enableStyleManager();
QMenu* m = new QMenu( tr( "Styles" ) );
QAction* actionAdd = m->addAction( tr( "Add" ), this, SLOT( addStyle() ) );
actionAdd->setData( QVariant::fromValue<QObject*>( layer ) );
QMenu* mRemove = m->addMenu( tr( "Remove" ) );
m->addSeparator();

QgsMapLayerStyleManager* mgr = layer->styleManager();
foreach ( QString name, mgr->styles() )
{
bool active = name == mgr->currentStyle();
if ( name.isEmpty() )
name = defaultStyleName();
QAction* actionUse = m->addAction( name, this, SLOT( useStyle() ) );
actionUse->setCheckable( true );
actionUse->setChecked( active );
actionUse->setData( QVariant::fromValue<QObject*>( layer ) );

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

menu->addMenu( m );
}

QString QgsMapLayerStyleGuiUtils::defaultStyleName()
{
return tr( "(default)" );
}


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

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

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

if ( res ) // make it active!
layer->styleManager()->setCurrentStyle( text );
}

void QgsMapLayerStyleGuiUtils::useStyle()
{
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();

bool res = layer->styleManager()->setCurrentStyle( name );
qDebug( "USE: %d", res );

layer->triggerRepaint();
}


void QgsMapLayerStyleGuiUtils::removeStyle()
{
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();

bool needsRefresh = ( layer->styleManager()->currentStyle() == name );

bool res = layer->styleManager()->removeStyle( name );
qDebug( "DEL: %d", res );

if ( needsRefresh )
layer->triggerRepaint();
}
29 changes: 29 additions & 0 deletions src/app/qgsmaplayerstyleguiutils.h
@@ -0,0 +1,29 @@
#ifndef QGSMAPLAYERSTYLEGUIUTILS_H
#define QGSMAPLAYERSTYLEGUIUTILS_H

#include <QObject>

#include "qgssingleton.h"

class QgsMapLayer;
class QMenu;

/** Various GUI utility functions for dealing with map layer's style manager */
class QgsMapLayerStyleGuiUtils : public QObject, public QgsSingleton<QgsMapLayerStyleGuiUtils>
{
Q_OBJECT
public:

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

private:
QString defaultStyleName();

private slots:
void addStyle();
void useStyle();
void removeStyle();

};

#endif // QGSMAPLAYERSTYLEGUIUTILS_H

0 comments on commit 547d018

Please sign in to comment.