Skip to content

Commit

Permalink
Initial core work on multiple styles per layer
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 7, 2015
1 parent b905f6b commit 829f99b
Show file tree
Hide file tree
Showing 9 changed files with 546 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -61,6 +61,8 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
{
QgsMapLayer* layer = QgsLayerTree::toLayer( node )->layer();

addStyleManagerStuff( menu, layer );

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

Expand Down Expand Up @@ -319,3 +321,91 @@ 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();
}
6 changes: 6 additions & 0 deletions src/app/qgsapplayertreeviewmenuprovider.h
Expand Up @@ -36,11 +36,17 @@ 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
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -106,6 +106,7 @@ SET(QGIS_CORE_SRCS
qgsmaplayer.cpp
qgsmaplayerlegend.cpp
qgsmaplayerregistry.cpp
qgsmaplayerstylemanager.cpp
qgsmaprenderer.cpp
qgsmaprenderercache.cpp
qgsmaprenderercustompainterjob.cpp
Expand Down Expand Up @@ -495,6 +496,7 @@ SET(QGIS_CORE_HDRS
qgsmaplayer.h
qgsmaplayerlegend.h
qgsmaplayerregistry.h
qgsmaplayerstylemanager.h
qgsmaprenderer.h
qgsmaprenderercache.h
qgsmaprenderercustompainterjob.h
Expand Down
23 changes: 23 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -36,6 +36,7 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsapplication.h"
#include "qgsmaplayerlegend.h"
#include "qgsmaplayerstylemanager.h"
#include "qgsproject.h"
#include "qgspluginlayerregistry.h"
#include "qgsprojectfiletransform.h"
Expand All @@ -55,6 +56,7 @@ QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
mLayerType( type ),
mBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal blending
, mLegend( 0 )
, mStyleManager( 0 )
{
mCRS = new QgsCoordinateReferenceSystem();

Expand Down Expand Up @@ -1424,6 +1426,27 @@ QgsMapLayerLegend*QgsMapLayer::legend() const
return mLegend;
}

void QgsMapLayer::enableStyleManager( bool enable )
{
if ( ( enable && mStyleManager ) || ( !enable && !mStyleManager ) )
return;

if ( enable )
{
mStyleManager = new QgsMapLayerStyleManager( this );
}
else
{
delete mStyleManager;
mStyleManager = 0;
}
}

QgsMapLayerStyleManager* QgsMapLayer::styleManager() const
{
return mStyleManager;
}

void QgsMapLayer::clearCacheImage()
{
emit repaintRequested();
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -36,6 +36,7 @@ class QgsRenderContext;
class QgsCoordinateReferenceSystem;
class QgsMapLayerLegend;
class QgsMapLayerRenderer;
class QgsMapLayerStyleManager;

class QDomDocument;
class QKeyEvent;
Expand Down Expand Up @@ -387,6 +388,21 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
QgsMapLayerLegend* legend() const;

/**
* Enable or disable layer's style manager. When disabled (default), the styleManager() will return null pointer.
* By enabling the style manager will be created with one default style (same as the layer's active style).
* By disabling the style manager all associated styles will be lost (only the layer's active style will stay).
* @note added in 2.8
*/
void enableStyleManager( bool enable = true );

/**
* Get access to the layer's style manager. Style manager allows switching between multiple styles.
* If the style manager is not enabled, null pointer will be returned.
* @note added in 2.8
*/
QgsMapLayerStyleManager* styleManager() const;

/**Returns the minimum scale denominator at which the layer is visible.
* Scale based visibility is only used if hasScaleBasedVisibility is true.
* @returns minimum scale denominator at which the layer will render
Expand Down Expand Up @@ -621,6 +637,9 @@ class CORE_EXPORT QgsMapLayer : public QObject

//! Controller of legend items of this layer
QgsMapLayerLegend* mLegend;

//! Manager of multiple styles available for a layer (may be null)
QgsMapLayerStyleManager* mStyleManager;
};

#endif

0 comments on commit 829f99b

Please sign in to comment.