Skip to content

Commit f00f4fd

Browse files
committedJan 7, 2015
Style manager code cleanups
1 parent 43d6042 commit f00f4fd

File tree

5 files changed

+134
-27
lines changed

5 files changed

+134
-27
lines changed
 

‎src/app/qgsmaplayerstyleguiutils.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
/***************************************************************************
2+
qgsmaplayerstyleguiutils.cpp
3+
--------------------------------------
4+
Date : January 2015
5+
Copyright : (C) 2015 by Martin Dobias
6+
Email : wonder dot sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
116
#include "qgsmaplayerstyleguiutils.h"
217

318
#include <QAction>
419
#include <QInputDialog>
520
#include <QMenu>
621

22+
#include "qgslogger.h"
723
#include "qgsmapcanvas.h"
824
#include "qgsmaplayer.h"
925
#include "qgsmaplayerstylemanager.h"
@@ -58,17 +74,18 @@ void QgsMapLayerStyleGuiUtils::addStyle()
5874
bool ok;
5975
QString text = QInputDialog::getText( 0, tr( "New style" ),
6076
tr( "Style name:" ), QLineEdit::Normal,
61-
"newstyle", &ok );
77+
"new style", &ok );
6278
if ( !ok || text.isEmpty() )
6379
return;
6480

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

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

7085
if ( res ) // make it active!
7186
layer->styleManager()->setCurrentStyle( text );
87+
else
88+
QgsDebugMsg( "Failed to add style: " + text );
7289
}
7390

7491
void QgsMapLayerStyleGuiUtils::useStyle()
@@ -84,7 +101,8 @@ void QgsMapLayerStyleGuiUtils::useStyle()
84101
name.clear();
85102

86103
bool res = layer->styleManager()->setCurrentStyle( name );
87-
qDebug( "USE: %d", res );
104+
if ( !res )
105+
QgsDebugMsg( "Failed to set current style: " + name );
88106

89107
layer->triggerRepaint();
90108
}
@@ -113,7 +131,8 @@ void QgsMapLayerStyleGuiUtils::removeStyle()
113131
bool needsRefresh = ( layer->styleManager()->currentStyle() == name );
114132

115133
bool res = layer->styleManager()->removeStyle( name );
116-
qDebug( "DEL: %d", res );
134+
if ( !res )
135+
QgsDebugMsg( "Failed to remove style: " + name );
117136

118137
if ( needsRefresh )
119138
layer->triggerRepaint();

‎src/app/qgsmaplayerstyleguiutils.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/***************************************************************************
2+
qgsmaplayerstyleguiutils.h
3+
--------------------------------------
4+
Date : January 2015
5+
Copyright : (C) 2015 by Martin Dobias
6+
Email : wonder dot sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
116
#ifndef QGSMAPLAYERSTYLEGUIUTILS_H
217
#define QGSMAPLAYERSTYLEGUIUTILS_H
318

@@ -14,6 +29,7 @@ class QgsMapLayerStyleGuiUtils : public QObject, public QgsSingleton<QgsMapLayer
1429
Q_OBJECT
1530
public:
1631

32+
//! Return menu instance with actions for the give map layer
1733
QMenu* createStyleManagerMenu( QgsMapLayer* layer );
1834

1935
private:

‎src/core/qgsmaplayerstylemanager.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1+
/***************************************************************************
2+
qgsmaplayerstylemanager.cpp
3+
--------------------------------------
4+
Date : January 2015
5+
Copyright : (C) 2015 by Martin Dobias
6+
Email : wonder dot sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
116
#include "qgsmaplayerstylemanager.h"
217

18+
#include "qgslogger.h"
319
#include "qgsmaplayer.h"
420

521
#include <QDomElement>
@@ -10,7 +26,7 @@ QgsMapLayerStyleManager::QgsMapLayerStyleManager( QgsMapLayer* layer )
1026
: mLayer( layer )
1127
{
1228
QgsMapLayerStyle defaultStyle;
13-
defaultStyle.loadFromLayer( mLayer );
29+
defaultStyle.readFromLayer( mLayer );
1430
mStyles.insert( QString(), defaultStyle );
1531
}
1632

@@ -74,7 +90,7 @@ bool QgsMapLayerStyleManager::addStyle( const QString& name, const QgsMapLayerSt
7490
bool QgsMapLayerStyleManager::addStyleFromLayer( const QString& name )
7591
{
7692
QgsMapLayerStyle style;
77-
style.loadFromLayer( mLayer );
93+
style.readFromLayer( mLayer );
7894
return addStyle( name, style );
7995
}
8096

@@ -114,13 +130,13 @@ bool QgsMapLayerStyleManager::setCurrentStyle( const QString& name )
114130

115131
syncCurrentStyle(); // sync before unloading it
116132
mCurrentStyle = name;
117-
mStyles[mCurrentStyle].applyToLayer( mLayer );
133+
mStyles[mCurrentStyle].writeToLayer( mLayer );
118134
return true;
119135
}
120136

121137
void QgsMapLayerStyleManager::syncCurrentStyle()
122138
{
123-
mStyles[mCurrentStyle].loadFromLayer( mLayer );
139+
mStyles[mCurrentStyle].readFromLayer( mLayer );
124140
}
125141

126142
// -----
@@ -139,22 +155,22 @@ QString QgsMapLayerStyle::dump() const
139155
return mXmlData;
140156
}
141157

142-
void QgsMapLayerStyle::loadFromLayer( QgsMapLayer* layer )
158+
void QgsMapLayerStyle::readFromLayer( QgsMapLayer* layer )
143159
{
144160
QString errorMsg;
145161
QDomDocument doc;
146162
layer->exportNamedStyle( doc, errorMsg );
147163
if ( !errorMsg.isEmpty() )
148164
{
149-
qDebug( " ERR: %s", errorMsg.toAscii().data() );
165+
QgsDebugMsg( "Failed to export style from layer: " + errorMsg );
150166
return;
151167
}
152168

153169
QTextStream stream( &mXmlData );
154170
doc.save( stream, 0 );
155171
}
156172

157-
void QgsMapLayerStyle::applyToLayer( QgsMapLayer* layer ) const
173+
void QgsMapLayerStyle::writeToLayer( QgsMapLayer* layer ) const
158174
{
159175
// QgsMapLayer does not have a importNamedStyle() method - working it around like this
160176
QTemporaryFile f;
@@ -164,9 +180,8 @@ void QgsMapLayerStyle::applyToLayer( QgsMapLayer* layer ) const
164180

165181
bool res;
166182
QString status = layer->loadNamedStyle( f.fileName(), res );
167-
qDebug( "APPLY: %s", status.toAscii().data() );
168183
if ( !res )
169-
qDebug( " APPLY ERR!" );
184+
QgsDebugMsg( "Failed to import style to layer: " + status );
170185
}
171186

172187
void QgsMapLayerStyle::readXml( const QDomElement& styleElement )

‎src/core/qgsmaplayerstylemanager.h

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,113 @@
1+
/***************************************************************************
2+
qgsmaplayerstylemanager.h
3+
--------------------------------------
4+
Date : January 2015
5+
Copyright : (C) 2015 by Martin Dobias
6+
Email : wonder dot sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
116
#ifndef QGSMAPLAYERSTYLEMANAGER_H
217
#define QGSMAPLAYERSTYLEMANAGER_H
318

419

520
class QgsMapLayer;
621

7-
822
#include <QByteArray>
923
#include <QMap>
1024
#include <QStringList>
1125

1226
class QDomElement;
1327

14-
/** stores style information (renderer, transparency, labeling, diagrams etc.) applicable to a map layer */
15-
class QgsMapLayerStyle
28+
/**
29+
* Stores style information (renderer, transparency, labeling, diagrams etc.) applicable to a map layer.
30+
*
31+
* Stored data are considered as opaque - it is not possible to access them directly or modify them - it is
32+
* only possible to read or write layer's current style.
33+
*
34+
* @note added in 2.8
35+
*/
36+
class CORE_EXPORT QgsMapLayerStyle
1637
{
1738
public:
18-
QgsMapLayerStyle(); // consutrct invalid style
39+
//! construct invalid style
40+
QgsMapLayerStyle();
1941

42+
//! Tell whether the style is valid (i.e. there is something stored in it)
2043
bool isValid() const;
2144

22-
QString dump() const; // for debugging only
45+
//! Return information about the style - for debugging purposes only
46+
QString dump() const;
2347

24-
void loadFromLayer( QgsMapLayer* layer );
25-
void applyToLayer( QgsMapLayer* layer ) const;
48+
//! Store layer's active style information in the instance
49+
void readFromLayer( QgsMapLayer* layer );
50+
//! Apply stored layer's style information to the layer
51+
void writeToLayer( QgsMapLayer* layer ) const;
2652

53+
//! Read style configuration (for project file reading)
2754
void readXml( const QDomElement& styleElement );
55+
//! Write style configuration (for project file writing)
2856
void writeXml( QDomElement& styleElement ) const;
2957

3058
private:
3159
QByteArray mXmlData;
3260
};
3361

3462

35-
/** Management of styles for use with one map layer */
36-
class QgsMapLayerStyleManager
63+
/**
64+
* Management of styles for use with one map layer. Stored styles are identified by their names. The manager
65+
* always keep track of which style of the stored ones is currently active. When the current style is changed,
66+
* the new style is applied to the associated layer.
67+
*
68+
* The class takes care of updating itself when the layer's current style configuration changes.
69+
* When some of layer style's properties change (e.g. transparency / colors), the style manager will
70+
* record them in the currently active style without any extra effort required.
71+
*
72+
* When an instance is created, it creates "default" style (with empty name) recorded from the associated map layer
73+
* and it is set as the current style.
74+
*
75+
* The instance must always contain at least one style. If no extra styles are wanted, the style manager should get
76+
* disabled in QgsMapLayer instance.
77+
*
78+
* @note added in 2.8
79+
*/
80+
class CORE_EXPORT QgsMapLayerStyleManager
3781
{
3882
public:
83+
//! Construct a style manager associated with a map layer (must not be null)
3984
QgsMapLayerStyleManager( QgsMapLayer* layer );
4085

86+
//! Read configuration (for project loading)
4187
void readXml( const QDomElement& mgrElement );
88+
//! Write configuration (for project saving)
4289
void writeXml( QDomElement& mgrElement ) const;
4390

91+
//! Return list of all defined style names
4492
QStringList styles() const;
93+
//! Return data of a stored style - accessed by its unique name
4594
QgsMapLayerStyle style( const QString& name ) const;
4695

96+
//! Add a style with given name and data
97+
//! @return true on success (name is unique and style is valid)
4798
bool addStyle( const QString& name, const QgsMapLayerStyle& style );
4899
//! Add style by cloning the current one
100+
//! @return true on success
49101
bool addStyleFromLayer( const QString& name );
102+
//! Remove a stored style
103+
//! @return true on success (style exists and it is not the last one)
50104
bool removeStyle( const QString& name );
51105

106+
//! Return name of the current style
52107
QString currentStyle() const;
53-
bool setCurrentStyle( const QString& name ); // applies to the mLayer! (+ sync previous style)
108+
//! Set a different style as the current style - will apply it to the layer
109+
//! @return true on success
110+
bool setCurrentStyle( const QString& name );
54111

55112
private:
56113
void syncCurrentStyle();

‎tests/src/core/testqgsmaplayerstylemanager.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,25 @@ void TestQgsMapLayerStyleManager::testStyle()
8686
mVL->setRendererV2( new QgsSingleSymbolRendererV2( sym1 ) );
8787

8888
QgsMapLayerStyle st1;
89-
st1.loadFromLayer( mVL );
89+
st1.readFromLayer( mVL );
9090
QCOMPARE( st1.isValid(), true );
9191

9292
qDebug( "CNT-1: %s", st1.dump().toAscii().data() );
9393

9494
mVL->setRendererV2( new QgsSingleSymbolRendererV2( sym2 ) );
9595

9696
QgsMapLayerStyle st2;
97-
st2.loadFromLayer( mVL );
97+
st2.readFromLayer( mVL );
9898

9999
qDebug( "CNT-2: %s", st2.dump().toAscii().data() );
100100

101-
st1.applyToLayer( mVL );
101+
st1.writeToLayer( mVL );
102102

103103
QgsSingleSymbolRendererV2* r1 = dynamic_cast<QgsSingleSymbolRendererV2*>( mVL->rendererV2() );
104104
QVERIFY( r1 );
105105
QCOMPARE( r1->symbol()->color(), QColor( Qt::magenta ) );
106106

107-
st2.applyToLayer( mVL );
107+
st2.writeToLayer( mVL );
108108

109109
QgsSingleSymbolRendererV2* r2 = dynamic_cast<QgsSingleSymbolRendererV2*>( mVL->rendererV2() );
110110
QVERIFY( r2 );

0 commit comments

Comments
 (0)
Please sign in to comment.