style-fix.patch

Arunmozhi P, 2012-08-22 10:34 AM

Download (20.7 KB)

View differences:

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

  
34 34
typedef QMap<QString, QgsVectorColorRampV2* > QgsVectorColorRampV2Map;
35 35
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
 */
36 56
typedef QMultiMap<QString, QString> QgsSmartConditionMap;
37 57

  
38 58
// enumerators representing sqlite DB columns
......
43 63
enum ColorrampTable { ColorrampId, ColorrampName, ColorrampXML, ColorrampGroupId };
44 64
enum SmartgroupTable { SmartgroupId, SmartgroupName, SmartgroupXML };
45 65

  
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
 */
47 72
enum StyleEntity { SymbolEntity, GroupEntity, TagEntity, ColorrampEntity, SmartgroupEntity };
48 73

  
49 74
class CORE_EXPORT QgsStyleV2
......
53 78
    QgsStyleV2();
54 79
    ~QgsStyleV2();
55 80

  
56
    //! return default application-wide style
57
    static QgsStyleV2* defaultStyle();
81
    //! add color ramp to style. takes ramp's ownership
82
    bool addColorRamp( QString name, QgsVectorColorRampV2* colorRamp );
58 83

  
59
    //! remove all contents of the style
60
    void clear();
84
    //! adds a new group and returns the group's id
85
    /*!
86
     *  \param groupname the name of the new group as QString
87
     *  \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
88
     *  \return returns an int, which is the DB id of the new group created, 0 if the group couldn't be created
89
     */
90
    int addGroup( QString groupName, int parent = 0 );
91

  
92
    //! adds new smartgroup to the database and returns the id
93
    /*!
94
     *  \param name is the name of the new Smart Group to be added
95
     *  \param op is the operator between the conditions; AND/OR as QString
96
     *  \param conditions are the smart group conditions
97
     */
98
    int addSmartgroup( QString name, QString op, QgsSmartConditionMap conditions );
61 99

  
62 100
    //! add symbol to style. takes symbol's ownership
63 101
    bool addSymbol( QString name, QgsSymbolV2* symbol );
64 102

  
103
    //! adds a new tag and returns the tag's id
104
    /*!
105
     *  \param tagName the name of the new tag to be created
106
     *  \return returns an int, which is the DB id of the new tag created, 0 if the tag couldn't be created
107
     */
108
    int addTag( QString tagName );
109

  
110
    //! return a map of groupid and names for the given parent
111
    QgsSymbolGroupMap childGroupNames( QString parent = "" );
112

  
113
    //! remove all contents of the style
114
    void clear();
115

  
116
    //! return a NEW copy of color ramp
117
    QgsVectorColorRampV2* colorRamp( QString name );
118

  
119
    //! return count of color ramps
120
    int colorRampCount();
121

  
122
    //! return a list of names of color ramps
123
    QStringList colorRampNames();
124

  
125
    //! return a const pointer to a symbol (doesn't create new instance)
126
    const QgsVectorColorRampV2* colorRampRef( QString name ) const;
127

  
128
    //! return the id in the style database for the given colorramp name
129
    //! returns 0 if not found
130
    int colorrampId( QString name );
131

  
132
    //! return default application-wide style
133
    static QgsStyleV2* defaultStyle();
134

  
135
    //! tags the symbol with the tags in the list
136
    /*!
137
     *  Applies the given tags to the given symbol or colorramp
138
     *  \param type is either SymbolEntity or ColorrampEntity
139
     *  \param symbol is the name of the symbol or colorramp as QString
140
     *  \param tags is the list of the tags that are to be applied as QStringList
141
     *  \return returns the success state of the operation
142
     */
143
    bool tagSymbol( StyleEntity type, QString symbol, QStringList tags );
144

  
145
    //! detags the symbol with the given list
146
    /*!
147
     *  Removes the given tags for the specified symbol or colorramp
148
     *  \param type is either SymbolEntity or ColorrampEntity
149
     *  \param symbol is the name of the symbol or colorramp
150
     *  \param tags is the list of tags that are to be removed as QStringList
151
     *  \return returns the sucess state of the operation
152
     */
153
    bool detagSymbol( StyleEntity type, QString symbol, QStringList tags );
154

  
65 155
    //! remove symbol from style (and delete it)
66 156
    bool removeSymbol( QString name );
67 157

  
......
84 174
    //! return the id in the style database for the given symbol name
85 175
    //! returns 0 if not found
86 176
    int symbolId( QString name );
87
    int colorrampId( QString name );
88

  
89
    //! return the id in the style database for the given group name
177
    //! return the DB id for the given group name
90 178
    int groupId( QString group );
179
    //! return the DB id for the given tag name
91 180
    int tagId( QString tag );
181
    //! return the DB id for the given smartgroup name
92 182
    int smartgroupId( QString smartgroup );
93 183

  
94 184
    //! return the all the groups in the style
95 185
    QStringList groupNames();
96 186

  
97
    //! return a map of groupid and names for the given parent
98
    QgsSymbolGroupMap childGroupNames( QString parent = "" );
99

  
100 187
    //! returns the symbolnames of a given groupid
188
    /*!
189
     *  \param type is either SymbolEntity or ColorampEntity
190
     *  \param groupid is id of the group to which the symbols belong to, as int
191
     *  \return A QStringList of the symbol or colorramp names for the given group id
192
     */
101 193
    QStringList symbolsOfGroup( StyleEntity type, int groupid );
194

  
102 195
    //! returns the symbol names with which have the given tag
196
    /*!
197
     *  \param type is either SymbolEntity or ColorampEntity
198
     *  \param tagid is id of the tag which has been applied over the symbol as int
199
     *  \return A QStringList of the symbol or colorramp names for the given tag id
200
     */
103 201
    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 );
108 202

  
109 203
    //! applies the specifed group to the symbol or colorramp specified by StyleEntity
204
    /*!
205
     *  \param type is either SymbolEntity of ColorrampEntity
206
     *  \param name is the name of the symbol or coloramp whose group is to be set
207
     *  \param groupid is the id of the group to which the entity is assigned
208
     *  \return returns the success state as bool
209
     */
110 210
    bool group( StyleEntity type, QString name, int groupid );
111 211

  
112 212
    //! rename the given entity with the specified id
213
    /*!
214
     *  \param type is any of the style entites. Refer enum StyleEntity.
215
     *  \param id is the DB id of the entity which is to be renamed
216
     *  \param newName is the new name of the entity
217
     */
113 218
    void rename( StyleEntity type, int id, QString newName );
219

  
114 220
    //! remove the specified entity from the db
221
    /*!
222
     *  \param type is any of the style entites. Refer enum StyleEntity.
223
     *  \param id is the DB id of the entity to be removed
224
     */
115 225
    void remove( StyleEntity type, int id );
116 226

  
117 227
    //! add the symbol to the DB with the tags
228
    /*!
229
     *  \param name is the name of the symbol as QString
230
     *  \param symbol is the pointer to the new QgsSymbolV2 being saved
231
     *  \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.
232
     *  \param tags is a list of tags that are associated with the symbol as a QStringList.
233
     *  \return returns the sucess state of the save operation
234
     */
118 235
    bool saveSymbol( QString name, QgsSymbolV2* symbol, int groupid, QStringList tags );
236

  
119 237
    //! add the colorramp to the DB
238
    /*!
239
     *  \param name is the name of the colorramp as QString
240
     *  \param ramp is the pointer to the new QgsVectorColorRampV2 being saved
241
     *  \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.
242
     *  \param tags is a list of tags that are associated with the color ramp as a QStringList.
243
     *  \return returns the sucess state of the save operation
244
     */
120 245
    bool saveColorRamp( QString name, QgsVectorColorRampV2* ramp, int groupid, QStringList tags );
121 246

  
122
    //! add color ramp to style. takes ramp's ownership
123
    bool addColorRamp( QString name, QgsVectorColorRampV2* colorRamp );
124

  
125 247
    //! remove color ramp from style (and delete it)
126 248
    bool removeColorRamp( QString name );
127 249

  
......
129 251
    //! @note added in v1.7
130 252
    bool renameColorRamp( QString oldName, QString newName );
131 253

  
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

  
144 254

  
145 255
    //! load a file into the style
146 256
    bool load( QString filename );
......
157 267
    //! return the names of the symbols which have a matching 'substring' in its defintion
158 268
    QStringList findSymbols( QString qword );
159 269

  
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

  
166 270
    //! return the tags associated with the symbol
271
    /*!
272
     *  \param type is either SymbolEntity or ColorrampEntity
273
     *  \param symbol is the name of the symbol or color ramp
274
     *  \return A QStringList of the tags that have been applied to that symbol/colorramp
275
     */
167 276
    QStringList tagsOfSymbol( StyleEntity type, QString symbol );
168 277

  
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
278
    //! returns the smart groups map with id as key and name as value
173 279
    QgsSymbolGroupMap smartgroupsListMap();
174 280

  
175 281
    //! returns the smart groups list
......
179 285
    QgsSmartConditionMap smartgroup( int id );
180 286

  
181 287
    //! returns the operator for the smartgroup
182
    //! @note: clumsy implementation TODO create a class for smartgroups
288
    //clumsy implementation TODO create a class for smartgroups
183 289
    QString smartgroupOperator( int id );
184 290

  
185 291
    //! returns the symbols for the smartgroup
......
205 311

  
206 312
    //! convenience function to open the DB and return a sqlite3 object
207 313
    bool openDB( QString filename );
314

  
208 315
    //! 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
316
    //! \param query query to run
317
    //! \param freeQuery release query memory
318
    //! \return success true on success
212 319
    bool runEmptyQuery( char* query, bool freeQuery = true );
320

  
213 321
    //! prepares the complex query for removing a group, so that the children are not abandoned
214 322
    char* getGroupRemoveQuery( int id );
323

  
215 324
    //! gets the id from the table for the given name from the database, 0 if not found
216 325
    int getId( QString table, QString name );
217 326
};
218
- 
src/core/symbology-ng/qgsstylev2.cpp
80 80
    sqlite3_close( mCurrentDB );
81 81
}
82 82

  
83
bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
83
bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol, bool update )
84 84
{
85 85
  if ( !symbol || name.isEmpty() )
86 86
    return false;
......
89 89

  
90 90
  mSymbols.insert( name, symbol );
91 91

  
92
  if ( update )
93
    updateSymbol( SymbolEntity, name );
94

  
92 95
  return true;
93 96
}
94 97

  
......
108 111
  QByteArray xmlArray;
109 112
  QTextStream stream( &xmlArray );
110 113
  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);",
112 115
                                 name.toUtf8().constData(), xmlArray.constData(), groupid );
113 116

  
114 117
  if ( !runEmptyQuery( query ) )
......
170 173
}
171 174

  
172 175

  
173
bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
176
bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp, bool update )
174 177
{
175 178
  if ( !colorRamp || name.isEmpty() )
176 179
    return false;
......
178 181
  // delete previous symbol (if any)
179 182
  delete mColorRamps.value( name );
180 183

  
181
  QgsDebugMsg( "Inserted " + name );
182 184
  mColorRamps.insert( name, colorRamp );
183 185

  
186
  if ( update )
187
    updateSymbol( ColorrampEntity, name );
188

  
184 189
  return true;
185 190
}
186 191

  
......
201 206
  QByteArray xmlArray;
202 207
  QTextStream stream( &xmlArray );
203 208
  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);",
205 210
                                 name.toUtf8().constData(), xmlArray.constData(), groupid );
206 211

  
207 212
  if ( !runEmptyQuery( query ) )
......
1378 1383
  mFileName = filename;
1379 1384
  return true;
1380 1385
}
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
79 79
    ~QgsStyleV2();
80 80

  
81 81
    //! add color ramp to style. takes ramp's ownership
82
    bool addColorRamp( QString name, QgsVectorColorRampV2* colorRamp );
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 );
83 90

  
84 91
    //! adds a new group and returns the group's id
85 92
    /*!
......
98 105
    int addSmartgroup( QString name, QString op, QgsSmartConditionMap conditions );
99 106

  
100 107
    //! add symbol to style. takes symbol's ownership
101
    bool addSymbol( QString name, QgsSymbolV2* symbol );
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 );
102 116

  
103 117
    //! adds a new tag and returns the tag's id
104 118
    /*!
......
107 121
     */
108 122
    int addTag( QString tagName );
109 123

  
110
    //! return a map of groupid and names for the given parent
124
    //! return a map of groupid and names for the given parent group
111 125
    QgsSymbolGroupMap childGroupNames( QString parent = "" );
112 126

  
113 127
    //! remove all contents of the style
......
323 337

  
324 338
    //! gets the id from the table for the given name from the database, 0 if not found
325 339
    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 );
326 349
};
327 350

  
328 351

  
src/gui/symbology-ng/qgsstylev2managerdialog.cpp
490 490
  }
491 491

  
492 492
  // 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 );
494 494
  mModified = true;
495 495
  return true;
496 496
}
......
548 548
    Q_ASSERT( 0 && "invalid ramp type" );
549 549
  }
550 550

  
551
  mStyle->addColorRamp( name, ramp );
551
  mStyle->addColorRamp( name, ramp, true );
552 552
  mModified = true;
553 553
  return true;
554 554
}
555
-