Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgsstylev2.cpp
Go to the documentation of this file.
1 
2 #include "qgsstylev2.h"
3 
4 #include "qgssymbolv2.h"
5 #include "qgsvectorcolorrampv2.h"
6 
8 
9 #include "qgsapplication.h"
10 #include "qgslogger.h"
11 
12 #include <QDomDocument>
13 #include <QDomElement>
14 #include <QFile>
15 #include <QTextStream>
16 
17 #define STYLE_CURRENT_VERSION "0"
18 
20 
21 
23 {
24 }
25 
27 {
28  clear();
29 }
30 
32 {
33  if ( mDefaultStyle == NULL )
34  {
35  QString styleFilename = QgsApplication::userStyleV2Path();
36 
37  // copy default style if user style doesn't exist
38  if ( !QFile::exists( styleFilename ) )
39  {
40  QFile::copy( QgsApplication::defaultStyleV2Path(), styleFilename );
41  }
42 
44  mDefaultStyle->load( styleFilename );
45  }
46  return mDefaultStyle;
47 }
48 
49 
51 {
52  for ( QMap<QString, QgsSymbolV2*>::iterator its = mSymbols.begin(); its != mSymbols.end(); ++its )
53  delete its.value();
54  for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr = mColorRamps.begin(); itr != mColorRamps.end(); ++itr )
55  delete itr.value();
56 
57  mSymbols.clear();
58  mColorRamps.clear();
59 }
60 
61 bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol )
62 {
63  if ( !symbol || name.count() == 0 )
64  return false;
65 
66  // delete previous symbol (if any)
67  if ( mSymbols.contains( name ) )
68  delete mSymbols.value( name );
69 
70  mSymbols.insert( name, symbol );
71  return true;
72 }
73 
74 bool QgsStyleV2::removeSymbol( QString name )
75 {
76  if ( !mSymbols.contains( name ) )
77  return false;
78 
79  // remove from map and delete
80  delete mSymbols.take( name );
81  return true;
82 }
83 
85 {
86  if ( !mSymbols.contains( name ) )
87  return NULL;
88  return mSymbols[name]->clone();
89 }
90 
91 const QgsSymbolV2* QgsStyleV2::symbolRef( QString name ) const
92 {
93  if ( !mSymbols.contains( name ) )
94  return NULL;
95  return mSymbols[name];
96 }
97 
99 {
100  return mSymbols.count();
101 }
102 
104 {
105  return mSymbols.keys();
106 }
107 
108 
109 bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
110 {
111  if ( !colorRamp || name.count() == 0 )
112  return false;
113 
114  // delete previous symbol (if any)
115  if ( mColorRamps.contains( name ) )
116  delete mColorRamps.value( name );
117 
118  mColorRamps.insert( name, colorRamp );
119  return true;
120 }
121 
122 bool QgsStyleV2::removeColorRamp( QString name )
123 {
124  if ( !mColorRamps.contains( name ) )
125  return false;
126 
127  // remove from map and delete
128  delete mColorRamps.take( name );
129  return true;
130 }
131 
133 {
134  if ( !mColorRamps.contains( name ) )
135  return NULL;
136  return mColorRamps[name]->clone();
137 }
138 
139 const QgsVectorColorRampV2* QgsStyleV2::colorRampRef( QString name ) const
140 {
141  if ( !mColorRamps.contains( name ) )
142  return NULL;
143  return mColorRamps[name];
144 }
145 
147 {
148  return mColorRamps.count();
149 }
150 
152 {
153  return mColorRamps.keys();
154 }
155 
156 
157 bool QgsStyleV2::load( QString filename )
158 {
159  mErrorString = QString();
160 
161  // import xml file
162  QDomDocument doc( "style" );
163  QFile f( filename );
164  if ( !f.open( QFile::ReadOnly ) )
165  {
166  mErrorString = "Couldn't open the style file: " + filename;
167  return false;
168  }
169 
170  // parse the document
171  if ( !doc.setContent( &f ) )
172  {
173  mErrorString = "Couldn't parse the style file: " + filename;
174  f.close();
175  return false;
176  }
177  f.close();
178 
179  QDomElement docElem = doc.documentElement();
180  if ( docElem.tagName() != "qgis_style" )
181  {
182  mErrorString = "Incorrect root tag in style: " + docElem.tagName();
183  return false;
184  }
185 
186  // check for style version
187  QString version = docElem.attribute( "version" );
188  if ( version != STYLE_CURRENT_VERSION )
189  {
190  mErrorString = "Unknown style file version: " + version;
191  return false;
192  }
193 
194  // load symbols
195  QDomElement symbolsElement = docElem.firstChildElement( "symbols" );
196  if ( !symbolsElement.isNull() )
197  {
198  mSymbols = QgsSymbolLayerV2Utils::loadSymbols( symbolsElement );
199  }
200 
201  // load color ramps
202  QDomElement rampsElement = docElem.firstChildElement( "colorramps" );
203  QDomElement e = rampsElement.firstChildElement();
204  while ( !e.isNull() )
205  {
206  if ( e.tagName() == "colorramp" )
207  {
209  if ( ramp != NULL )
210  addColorRamp( e.attribute( "name" ), ramp );
211  }
212  else
213  {
214  QgsDebugMsg( "unknown tag: " + e.tagName() );
215  }
216  e = e.nextSiblingElement();
217  }
218 
219  mFileName = filename;
220  return true;
221 }
222 
223 
224 
225 bool QgsStyleV2::save( QString filename )
226 {
227  mErrorString = QString();
228  if ( filename.isEmpty() )
229  filename = mFileName;
230 
231  QDomDocument doc( "qgis_style" );
232  QDomElement root = doc.createElement( "qgis_style" );
233  root.setAttribute( "version", STYLE_CURRENT_VERSION );
234  doc.appendChild( root );
235 
236  QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( mSymbols, "symbols", doc );
237 
238  QDomElement rampsElem = doc.createElement( "colorramps" );
239 
240  // save color ramps
241  for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr = mColorRamps.begin(); itr != mColorRamps.end(); ++itr )
242  {
243  QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp( itr.key(), itr.value(), doc );
244  rampsElem.appendChild( rampEl );
245  }
246 
247  root.appendChild( symbolsElem );
248  root.appendChild( rampsElem );
249 
250  // save
251  QFile f( filename );
252  if ( !f.open( QFile::WriteOnly ) )
253  {
254  mErrorString = "Couldn't open file for writing: " + filename;
255  return false;
256  }
257  QTextStream ts( &f );
258  doc.save( ts, 2 );
259  f.close();
260 
261  mFileName = filename;
262  return true;
263 }
264 
265 bool QgsStyleV2::renameSymbol( QString oldName, QString newName )
266 {
267  if ( !mSymbols.contains( oldName ) )
268  return NULL;
269 
270  mSymbols.insert( newName, mSymbols.take( oldName ) );
271  return true;
272 }
273 
274 bool QgsStyleV2::renameColorRamp( QString oldName, QString newName )
275 {
276  if ( !mColorRamps.contains( oldName ) )
277  return NULL;
278 
279  mColorRamps.insert( newName, mColorRamps.take( oldName ) );
280  return true;
281 }