Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1755 from wonder-sk/multiple-styles
[FEATURE] Support for multiple styles per map layer Available in legend context menu in Styles sub-menu where it is possible to add/remove styles and quickly switch between them.
- Loading branch information
Showing
18 changed files
with
846 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
class QgsMapLayerStyle | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsmaplayerstylemanager.h> | ||
%End | ||
public: | ||
//! construct invalid style | ||
QgsMapLayerStyle(); | ||
|
||
//! Tell whether the style is valid (i.e. there is something stored in it) | ||
bool isValid() const; | ||
|
||
//! Return information about the style - for debugging purposes only | ||
QString dump() const; | ||
|
||
//! Store layer's active style information in the instance | ||
void readFromLayer( QgsMapLayer* layer ); | ||
//! Apply stored layer's style information to the layer | ||
void writeToLayer( QgsMapLayer* layer ) const; | ||
|
||
//! Read style configuration (for project file reading) | ||
void readXml( const QDomElement& styleElement ); | ||
//! Write style configuration (for project file writing) | ||
void writeXml( QDomElement& styleElement ) const; | ||
}; | ||
|
||
|
||
class QgsMapLayerStyleManager | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsmaplayerstylemanager.h> | ||
%End | ||
public: | ||
//! Construct a style manager associated with a map layer (must not be null) | ||
QgsMapLayerStyleManager( QgsMapLayer* layer ); | ||
|
||
//! Read configuration (for project loading) | ||
void readXml( const QDomElement& mgrElement ); | ||
//! Write configuration (for project saving) | ||
void writeXml( QDomElement& mgrElement ) const; | ||
|
||
//! Return list of all defined style names | ||
QStringList styles() const; | ||
//! Return data of a stored style - accessed by its unique name | ||
QgsMapLayerStyle style( const QString& name ) const; | ||
|
||
//! Add a style with given name and data | ||
//! @return true on success (name is unique and style is valid) | ||
bool addStyle( const QString& name, const QgsMapLayerStyle& style ); | ||
//! Add style by cloning the current one | ||
//! @return true on success | ||
bool addStyleFromLayer( const QString& name ); | ||
//! Remove a stored style | ||
//! @return true on success (style exists and it is not the last one) | ||
bool removeStyle( const QString& name ); | ||
|
||
//! Return name of the current style | ||
QString currentStyle() const; | ||
//! Set a different style as the current style - will apply it to the layer | ||
//! @return true on success | ||
bool setCurrentStyle( const QString& name ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/*************************************************************************** | ||
qgsmaplayerstyleguiutils.cpp | ||
-------------------------------------- | ||
Date : January 2015 | ||
Copyright : (C) 2015 by Martin Dobias | ||
Email : wonder dot sk at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsmaplayerstyleguiutils.h" | ||
|
||
#include <QAction> | ||
#include <QInputDialog> | ||
#include <QMenu> | ||
|
||
#include "qgslogger.h" | ||
#include "qgsmapcanvas.h" | ||
#include "qgsmaplayer.h" | ||
#include "qgsmaplayerstylemanager.h" | ||
|
||
|
||
QMenu* QgsMapLayerStyleGuiUtils::createStyleManagerMenu( QgsMapLayer* layer ) | ||
{ | ||
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(); | ||
|
||
if ( !mgr ) | ||
return m; | ||
|
||
QMenu* mRemove = m->addMenu( tr( "Remove" ) ); | ||
m->addSeparator(); | ||
|
||
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 ) ); | ||
} | ||
|
||
return 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, | ||
"new style", &ok ); | ||
if ( !ok || text.isEmpty() ) | ||
return; | ||
|
||
layer->enableStyleManager(); // make sure it exists | ||
|
||
bool res = layer->styleManager()->addStyleFromLayer( text ); | ||
|
||
if ( res ) // make it active! | ||
layer->styleManager()->setCurrentStyle( text ); | ||
else | ||
QgsDebugMsg( "Failed to add style: " + 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 ); | ||
if ( !res ) | ||
QgsDebugMsg( "Failed to set current style: " + name ); | ||
|
||
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; | ||
|
||
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(); | ||
|
||
bool needsRefresh = ( layer->styleManager()->currentStyle() == name ); | ||
|
||
bool res = layer->styleManager()->removeStyle( name ); | ||
if ( !res ) | ||
QgsDebugMsg( "Failed to remove style: " + name ); | ||
|
||
if ( needsRefresh ) | ||
layer->triggerRepaint(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/*************************************************************************** | ||
qgsmaplayerstyleguiutils.h | ||
-------------------------------------- | ||
Date : January 2015 | ||
Copyright : (C) 2015 by Martin Dobias | ||
Email : wonder dot sk at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#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: | ||
|
||
//! Return menu instance with actions for the give map layer | ||
QMenu* createStyleManagerMenu( QgsMapLayer* layer ); | ||
|
||
private: | ||
QString defaultStyleName(); | ||
|
||
private slots: | ||
void addStyle(); | ||
void useStyle(); | ||
void removeStyle(); | ||
|
||
}; | ||
|
||
#endif // QGSMAPLAYERSTYLEGUIUTILS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.