Skip to content

Commit 6bf56e8

Browse files
committedAug 23, 2012
Merge branch 'style-docs' of https://github.com/tecoholic/Quantum-GIS
2 parents b6760ce + 447c0d1 commit 6bf56e8

File tree

3 files changed

+249
-51
lines changed

3 files changed

+249
-51
lines changed
 

‎src/core/symbology-ng/qgsstylev2.cpp

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void QgsStyleV2::clear()
8080
sqlite3_close( mCurrentDB );
8181
}
8282

83-
bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
83+
bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol, bool update )
8484
{
8585
if ( !symbol || name.isEmpty() )
8686
return false;
@@ -89,6 +89,9 @@ bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
8989

9090
mSymbols.insert( name, symbol );
9191

92+
if ( update )
93+
updateSymbol( SymbolEntity, name );
94+
9295
return true;
9396
}
9497

@@ -108,7 +111,7 @@ bool QgsStyleV2::saveSymbol( QString name, QgsSymbolV2* symbol, int groupid, QSt
108111
QByteArray xmlArray;
109112
QTextStream stream( &xmlArray );
110113
symEl.save( stream, 4 );
111-
char *query = sqlite3_mprintf( "INSERT INTO symbol VALUES (NULL, '%q', '%q', %d)",
114+
char *query = sqlite3_mprintf( "INSERT INTO symbol VALUES (NULL, '%q', '%q', %d);",
112115
name.toUtf8().constData(), xmlArray.constData(), groupid );
113116

114117
if ( !runEmptyQuery( query ) )
@@ -170,17 +173,19 @@ QStringList QgsStyleV2::symbolNames()
170173
}
171174

172175

173-
bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
176+
bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp, bool update )
174177
{
175178
if ( !colorRamp || name.isEmpty() )
176179
return false;
177180

178181
// delete previous symbol (if any)
179182
delete mColorRamps.value( name );
180183

181-
QgsDebugMsg( "Inserted " + name );
182184
mColorRamps.insert( name, colorRamp );
183185

186+
if ( update )
187+
updateSymbol( ColorrampEntity, name );
188+
184189
return true;
185190
}
186191

@@ -201,7 +206,7 @@ bool QgsStyleV2::saveColorRamp( QString name, QgsVectorColorRampV2* ramp, int gr
201206
QByteArray xmlArray;
202207
QTextStream stream( &xmlArray );
203208
rampEl.save( stream, 4 );
204-
char *query = sqlite3_mprintf( "INSERT INTO colorramp VALUES (NULL, '%q', '%q', %d)",
209+
char *query = sqlite3_mprintf( "INSERT INTO colorramp VALUES (NULL, '%q', '%q', %d);",
205210
name.toUtf8().constData(), xmlArray.constData(), groupid );
206211

207212
if ( !runEmptyQuery( query ) )
@@ -1378,3 +1383,64 @@ bool QgsStyleV2::importXML( QString filename )
13781383
mFileName = filename;
13791384
return true;
13801385
}
1386+
1387+
bool QgsStyleV2::updateSymbol( StyleEntity type, QString name )
1388+
{
1389+
QDomDocument doc( "dummy" );
1390+
QDomElement symEl;
1391+
QByteArray xmlArray;
1392+
QTextStream stream( &xmlArray );
1393+
1394+
char *query;
1395+
1396+
if ( type == SymbolEntity )
1397+
{
1398+
// check if it is an existing symbol
1399+
if ( !symbolNames().contains( name ) )
1400+
{
1401+
QgsDebugMsg( "Update request recieved for unavailable symbol" );
1402+
return false;
1403+
}
1404+
1405+
symEl = QgsSymbolLayerV2Utils::saveSymbol( name, symbol( name ), doc );
1406+
if ( symEl.isNull() )
1407+
{
1408+
QgsDebugMsg( "Couldn't convert symbol to valid XML!" );
1409+
return false;
1410+
}
1411+
symEl.save( stream, 4 );
1412+
query = sqlite3_mprintf( "UPDATE symbol SET xml='%q' WHERE name='%q';",
1413+
xmlArray.constData(), name.toUtf8().constData() );
1414+
}
1415+
else if ( type == ColorrampEntity )
1416+
{
1417+
if ( !colorRampNames().contains( name ) )
1418+
{
1419+
QgsDebugMsg( "Update requested for unavailable color ramp." );
1420+
return false;
1421+
}
1422+
1423+
symEl = QgsSymbolLayerV2Utils::saveColorRamp( name, colorRamp( name ), doc );
1424+
if ( symEl.isNull() )
1425+
{
1426+
QgsDebugMsg( "Couldn't convert color ramp to valid XML!" );
1427+
return false;
1428+
}
1429+
symEl.save( stream, 4 );
1430+
query = sqlite3_mprintf( "UPDATE colorramp SET xml='%q' WHERE name='%q';",
1431+
xmlArray.constData(), name.toUtf8().constData() );
1432+
}
1433+
else
1434+
{
1435+
QgsDebugMsg( "Updating the unsupported StyleEntity" );
1436+
return false;
1437+
}
1438+
1439+
1440+
if ( !runEmptyQuery( query ) )
1441+
{
1442+
QgsDebugMsg( "Couldn't insert symbol into the database!" );
1443+
return false;
1444+
}
1445+
return true;
1446+
}

‎src/core/symbology-ng/qgsstylev2.h

Lines changed: 176 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ class QDomElement;
3333

3434
typedef QMap<QString, QgsVectorColorRampV2* > QgsVectorColorRampV2Map;
3535
typedef QMap<int, QString> QgsSymbolGroupMap;
36+
37+
/*!
38+
* A multimap to hold the smart group conditions as constraint and parameter pairs.
39+
* Both the key and the value of the map are QString. The key is the constraint of the condition and the value is the parameter which is applied for the constraint.
40+
*
41+
* The supported constraints are:
42+
* tag -> symbol has the tag matching the parameter
43+
* !tag -> symbol doesnot have the tag matching the parameter
44+
* group -> symbol belongs to group specified by the parameter
45+
* !group -> symbol doesn't belong to the group specified by the parameter
46+
* name -> symbol has a part of its name matching the parameter
47+
* !name -> symbol doesn't have any part of the name matching the parameter
48+
*
49+
* Example Usage:
50+
* QgsSmartConditionMap conditions;
51+
* conditions.insert( "tag", "red" ); // adds the condition: Symbol has the tag red
52+
* conditions.insert( "!name", "way" ); // add the condition: Symbol doesn't have any part of its name matching `way`
53+
*
54+
* \note This is a Multimap, which means it will contain multiple values for the same key.
55+
*/
3656
typedef QMultiMap<QString, QString> QgsSmartConditionMap;
3757

3858
// enumerators representing sqlite DB columns
@@ -43,7 +63,12 @@ enum TagmapTable { TagmapTagId, TagmapSymbolId };
4363
enum ColorrampTable { ColorrampId, ColorrampName, ColorrampXML, ColorrampGroupId };
4464
enum SmartgroupTable { SmartgroupId, SmartgroupName, SmartgroupXML };
4565

46-
// Enums for types
66+
//! Enum for Entities involved in a style
67+
/*!
68+
The enumarator is used for identifying the entity being operated on when generic
69+
database functions are being run.
70+
\sa group(), rename(), remove(), symbolsOfGroup(), symbolsWithTag(), symbolsOfSmartgroup()
71+
*/
4772
enum StyleEntity { SymbolEntity, GroupEntity, TagEntity, ColorrampEntity, SmartgroupEntity };
4873

4974
class CORE_EXPORT QgsStyleV2
@@ -53,14 +78,93 @@ class CORE_EXPORT QgsStyleV2
5378
QgsStyleV2();
5479
~QgsStyleV2();
5580

56-
//! return default application-wide style
57-
static QgsStyleV2* defaultStyle();
81+
//! add color ramp to style. takes ramp's ownership
82+
/*!
83+
* \note Adding a color ramp with the name of existing one replaces it.
84+
* \param name is the name of the color ramp being added or updated
85+
* \param colorRamp is the Vector color ramp
86+
* \param update set to true when the style DB has to be updated, by default it is false
87+
* \return sucess status of the operation
88+
*/
89+
bool addColorRamp( QString name, QgsVectorColorRampV2* colorRamp, bool update = false );
90+
91+
//! adds a new group and returns the group's id
92+
/*!
93+
* \param groupname the name of the new group as QString
94+
* \param parent is the id of the parent group when a subgrouo is to be created. By default it is 0 indicating it is not a sub-group
95+
* \return returns an int, which is the DB id of the new group created, 0 if the group couldn't be created
96+
*/
97+
int addGroup( QString groupName, int parent = 0 );
98+
99+
//! adds new smartgroup to the database and returns the id
100+
/*!
101+
* \param name is the name of the new Smart Group to be added
102+
* \param op is the operator between the conditions; AND/OR as QString
103+
* \param conditions are the smart group conditions
104+
*/
105+
int addSmartgroup( QString name, QString op, QgsSmartConditionMap conditions );
106+
107+
//! add symbol to style. takes symbol's ownership
108+
/*!
109+
* \note Adding a symbol with the name of existing one replaces it.
110+
* \param name is the name of the symbol being added or updated
111+
* \param symbol is the Vector symbol
112+
* \param update set to true when the style DB has to be updated, by default it is false
113+
* \return sucess status of the operation
114+
*/
115+
bool addSymbol( QString name, QgsSymbolV2* symbol, bool update = false );
116+
117+
//! adds a new tag and returns the tag's id
118+
/*!
119+
* \param tagName the name of the new tag to be created
120+
* \return returns an int, which is the DB id of the new tag created, 0 if the tag couldn't be created
121+
*/
122+
int addTag( QString tagName );
123+
124+
//! return a map of groupid and names for the given parent group
125+
QgsSymbolGroupMap childGroupNames( QString parent = "" );
58126

59127
//! remove all contents of the style
60128
void clear();
61129

62-
//! add symbol to style. takes symbol's ownership
63-
bool addSymbol( QString name, QgsSymbolV2* symbol );
130+
//! return a NEW copy of color ramp
131+
QgsVectorColorRampV2* colorRamp( QString name );
132+
133+
//! return count of color ramps
134+
int colorRampCount();
135+
136+
//! return a list of names of color ramps
137+
QStringList colorRampNames();
138+
139+
//! return a const pointer to a symbol (doesn't create new instance)
140+
const QgsVectorColorRampV2* colorRampRef( QString name ) const;
141+
142+
//! return the id in the style database for the given colorramp name
143+
//! returns 0 if not found
144+
int colorrampId( QString name );
145+
146+
//! return default application-wide style
147+
static QgsStyleV2* defaultStyle();
148+
149+
//! tags the symbol with the tags in the list
150+
/*!
151+
* Applies the given tags to the given symbol or colorramp
152+
* \param type is either SymbolEntity or ColorrampEntity
153+
* \param symbol is the name of the symbol or colorramp as QString
154+
* \param tags is the list of the tags that are to be applied as QStringList
155+
* \return returns the success state of the operation
156+
*/
157+
bool tagSymbol( StyleEntity type, QString symbol, QStringList tags );
158+
159+
//! detags the symbol with the given list
160+
/*!
161+
* Removes the given tags for the specified symbol or colorramp
162+
* \param type is either SymbolEntity or ColorrampEntity
163+
* \param symbol is the name of the symbol or colorramp
164+
* \param tags is the list of tags that are to be removed as QStringList
165+
* \return returns the sucess state of the operation
166+
*/
167+
bool detagSymbol( StyleEntity type, QString symbol, QStringList tags );
64168

65169
//! remove symbol from style (and delete it)
66170
bool removeSymbol( QString name );
@@ -84,63 +188,83 @@ class CORE_EXPORT QgsStyleV2
84188
//! return the id in the style database for the given symbol name
85189
//! returns 0 if not found
86190
int symbolId( QString name );
87-
int colorrampId( QString name );
88-
89-
//! return the id in the style database for the given group name
191+
//! return the DB id for the given group name
90192
int groupId( QString group );
193+
//! return the DB id for the given tag name
91194
int tagId( QString tag );
195+
//! return the DB id for the given smartgroup name
92196
int smartgroupId( QString smartgroup );
93197

94198
//! return the all the groups in the style
95199
QStringList groupNames();
96200

97-
//! return a map of groupid and names for the given parent
98-
QgsSymbolGroupMap childGroupNames( QString parent = "" );
99-
100201
//! returns the symbolnames of a given groupid
202+
/*!
203+
* \param type is either SymbolEntity or ColorampEntity
204+
* \param groupid is id of the group to which the symbols belong to, as int
205+
* \return A QStringList of the symbol or colorramp names for the given group id
206+
*/
101207
QStringList symbolsOfGroup( StyleEntity type, int groupid );
208+
102209
//! returns the symbol names with which have the given tag
210+
/*!
211+
* \param type is either SymbolEntity or ColorampEntity
212+
* \param tagid is id of the tag which has been applied over the symbol as int
213+
* \return A QStringList of the symbol or colorramp names for the given tag id
214+
*/
103215
QStringList symbolsWithTag( StyleEntity type, int tagid );
104-
//! adds a new group and returns the group's id
105-
int addGroup( QString groupName, int parent = 0 );
106-
//! adds a new tag and returns the tag's id
107-
int addTag( QString tagName );
108216

109217
//! applies the specifed group to the symbol or colorramp specified by StyleEntity
218+
/*!
219+
* \param type is either SymbolEntity of ColorrampEntity
220+
* \param name is the name of the symbol or coloramp whose group is to be set
221+
* \param groupid is the id of the group to which the entity is assigned
222+
* \return returns the success state as bool
223+
*/
110224
bool group( StyleEntity type, QString name, int groupid );
111225

112226
//! rename the given entity with the specified id
227+
/*!
228+
* \param type is any of the style entites. Refer enum StyleEntity.
229+
* \param id is the DB id of the entity which is to be renamed
230+
* \param newName is the new name of the entity
231+
*/
113232
void rename( StyleEntity type, int id, QString newName );
233+
114234
//! remove the specified entity from the db
235+
/*!
236+
* \param type is any of the style entites. Refer enum StyleEntity.
237+
* \param id is the DB id of the entity to be removed
238+
*/
115239
void remove( StyleEntity type, int id );
116240

117241
//! add the symbol to the DB with the tags
242+
/*!
243+
* \param name is the name of the symbol as QString
244+
* \param symbol is the pointer to the new QgsSymbolV2 being saved
245+
* \param groupid is the id of the group to which the symbol belongs. Pass 0 if it doesn't belong to any group or not known.
246+
* \param tags is a list of tags that are associated with the symbol as a QStringList.
247+
* \return returns the sucess state of the save operation
248+
*/
118249
bool saveSymbol( QString name, QgsSymbolV2* symbol, int groupid, QStringList tags );
250+
119251
//! add the colorramp to the DB
252+
/*!
253+
* \param name is the name of the colorramp as QString
254+
* \param ramp is the pointer to the new QgsVectorColorRampV2 being saved
255+
* \param groupid is the id of the group to which the Color Ramp belongs. Pass 0 if it doesn't belong to any group or not known.
256+
* \param tags is a list of tags that are associated with the color ramp as a QStringList.
257+
* \return returns the sucess state of the save operation
258+
*/
120259
bool saveColorRamp( QString name, QgsVectorColorRampV2* ramp, int groupid, QStringList tags );
121260

122-
//! add color ramp to style. takes ramp's ownership
123-
bool addColorRamp( QString name, QgsVectorColorRampV2* colorRamp );
124-
125261
//! remove color ramp from style (and delete it)
126262
bool removeColorRamp( QString name );
127263

128264
//! change ramp's name
129265
//! @note added in v1.7
130266
bool renameColorRamp( QString oldName, QString newName );
131267

132-
//! return a NEW copy of color ramp
133-
QgsVectorColorRampV2* colorRamp( QString name );
134-
135-
//! return a const pointer to a symbol (doesn't create new instance)
136-
const QgsVectorColorRampV2* colorRampRef( QString name ) const;
137-
138-
//! return count of color ramps
139-
int colorRampCount();
140-
141-
//! return a list of names of color ramps
142-
QStringList colorRampNames();
143-
144268

145269
//! load a file into the style
146270
bool load( QString filename );
@@ -157,19 +281,15 @@ class CORE_EXPORT QgsStyleV2
157281
//! return the names of the symbols which have a matching 'substring' in its defintion
158282
QStringList findSymbols( QString qword );
159283

160-
//! tags the symbol with the tags in the list, the remove flag DE-TAGS
161-
bool tagSymbol( StyleEntity type, QString symbol, QStringList tags );
162-
163-
//! detags the symbol with the given list
164-
bool detagSymbol( StyleEntity type, QString symbol, QStringList tags );
165-
166284
//! return the tags associated with the symbol
285+
/*!
286+
* \param type is either SymbolEntity or ColorrampEntity
287+
* \param symbol is the name of the symbol or color ramp
288+
* \return A QStringList of the tags that have been applied to that symbol/colorramp
289+
*/
167290
QStringList tagsOfSymbol( StyleEntity type, QString symbol );
168291

169-
//! adds the smartgroup to the database and returns the id
170-
int addSmartgroup( QString name, QString op, QgsSmartConditionMap conditions );
171-
172-
//! returns the smart groups map
292+
//! returns the smart groups map with id as key and name as value
173293
QgsSymbolGroupMap smartgroupsListMap();
174294

175295
//! returns the smart groups list
@@ -179,7 +299,7 @@ class CORE_EXPORT QgsStyleV2
179299
QgsSmartConditionMap smartgroup( int id );
180300

181301
//! returns the operator for the smartgroup
182-
//! @note: clumsy implementation TODO create a class for smartgroups
302+
//clumsy implementation TODO create a class for smartgroups
183303
QString smartgroupOperator( int id );
184304

185305
//! returns the symbols for the smartgroup
@@ -205,15 +325,27 @@ class CORE_EXPORT QgsStyleV2
205325

206326
//! convenience function to open the DB and return a sqlite3 object
207327
bool openDB( QString filename );
328+
208329
//! convenience function that would run queries which don't generate return values
209-
//! @param query query to run
210-
//! @param freeQuery release query memory
211-
//! @return success true on success
330+
//! \param query query to run
331+
//! \param freeQuery release query memory
332+
//! \return success true on success
212333
bool runEmptyQuery( char* query, bool freeQuery = true );
334+
213335
//! prepares the complex query for removing a group, so that the children are not abandoned
214336
char* getGroupRemoveQuery( int id );
337+
215338
//! gets the id from the table for the given name from the database, 0 if not found
216339
int getId( QString table, QString name );
340+
341+
//! updates the properties of an existing symbol/colorramp
342+
/*!
343+
* \note This should not be called seperately, only called through addSymbol or addColorRamp
344+
* \param type is either SymbolEntity or ColorrampEntity
345+
* \param name is the name of an existing symbol or a color ramp
346+
* \return Success state of the update operation
347+
*/
348+
bool updateSymbol( StyleEntity type, QString name );
217349
};
218350

219351

‎src/gui/symbology-ng/qgsstylev2managerdialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ bool QgsStyleV2ManagerDialog::editSymbol()
490490
}
491491

492492
// by adding symbol to style with the same name the old effectively gets overwritten
493-
mStyle->addSymbol( symbolName, symbol );
493+
mStyle->addSymbol( symbolName, symbol, true );
494494
mModified = true;
495495
return true;
496496
}
@@ -548,7 +548,7 @@ bool QgsStyleV2ManagerDialog::editColorRamp()
548548
Q_ASSERT( 0 && "invalid ramp type" );
549549
}
550550

551-
mStyle->addColorRamp( name, ramp );
551+
mStyle->addColorRamp( name, ramp, true );
552552
mModified = true;
553553
return true;
554554
}

0 commit comments

Comments
 (0)
Please sign in to comment.