Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgsstylev2.cpp 00003 --------------------- 00004 begin : November 2009 00005 copyright : (C) 2009 by Martin Dobias 00006 email : wonder.sk at gmail.com 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 00016 #include "qgsstylev2.h" 00017 00018 #include "qgssymbolv2.h" 00019 #include "qgsvectorcolorrampv2.h" 00020 00021 #include "qgssymbollayerv2registry.h" 00022 00023 #include "qgsapplication.h" 00024 #include "qgslogger.h" 00025 00026 #include <QDomDocument> 00027 #include <QDomElement> 00028 #include <QFile> 00029 #include <QTextStream> 00030 00031 #define STYLE_CURRENT_VERSION "0" 00032 00033 QgsStyleV2* QgsStyleV2::mDefaultStyle = NULL; 00034 00035 00036 QgsStyleV2::QgsStyleV2() 00037 { 00038 } 00039 00040 QgsStyleV2::~QgsStyleV2() 00041 { 00042 clear(); 00043 } 00044 00045 QgsStyleV2* QgsStyleV2::defaultStyle() // static 00046 { 00047 if ( mDefaultStyle == NULL ) 00048 { 00049 QString styleFilename = QgsApplication::userStyleV2Path(); 00050 00051 // copy default style if user style doesn't exist 00052 if ( !QFile::exists( styleFilename ) ) 00053 { 00054 QFile::copy( QgsApplication::defaultStyleV2Path(), styleFilename ); 00055 } 00056 00057 mDefaultStyle = new QgsStyleV2; 00058 mDefaultStyle->load( styleFilename ); 00059 } 00060 return mDefaultStyle; 00061 } 00062 00063 00064 void QgsStyleV2::clear() 00065 { 00066 for ( QMap<QString, QgsSymbolV2*>::iterator its = mSymbols.begin(); its != mSymbols.end(); ++its ) 00067 delete its.value(); 00068 for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr = mColorRamps.begin(); itr != mColorRamps.end(); ++itr ) 00069 delete itr.value(); 00070 00071 mSymbols.clear(); 00072 mColorRamps.clear(); 00073 } 00074 00075 bool QgsStyleV2::addSymbol( QString name, QgsSymbolV2* symbol ) 00076 { 00077 if ( !symbol || name.count() == 0 ) 00078 return false; 00079 00080 // delete previous symbol (if any) 00081 if ( mSymbols.contains( name ) ) 00082 delete mSymbols.value( name ); 00083 00084 mSymbols.insert( name, symbol ); 00085 return true; 00086 } 00087 00088 bool QgsStyleV2::removeSymbol( QString name ) 00089 { 00090 if ( !mSymbols.contains( name ) ) 00091 return false; 00092 00093 // remove from map and delete 00094 delete mSymbols.take( name ); 00095 return true; 00096 } 00097 00098 QgsSymbolV2* QgsStyleV2::symbol( QString name ) 00099 { 00100 if ( !mSymbols.contains( name ) ) 00101 return NULL; 00102 return mSymbols[name]->clone(); 00103 } 00104 00105 const QgsSymbolV2* QgsStyleV2::symbolRef( QString name ) const 00106 { 00107 if ( !mSymbols.contains( name ) ) 00108 return NULL; 00109 return mSymbols[name]; 00110 } 00111 00112 int QgsStyleV2::symbolCount() 00113 { 00114 return mSymbols.count(); 00115 } 00116 00117 QStringList QgsStyleV2::symbolNames() 00118 { 00119 return mSymbols.keys(); 00120 } 00121 00122 00123 bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp ) 00124 { 00125 if ( !colorRamp || name.count() == 0 ) 00126 return false; 00127 00128 // delete previous symbol (if any) 00129 if ( mColorRamps.contains( name ) ) 00130 delete mColorRamps.value( name ); 00131 00132 mColorRamps.insert( name, colorRamp ); 00133 return true; 00134 } 00135 00136 bool QgsStyleV2::removeColorRamp( QString name ) 00137 { 00138 if ( !mColorRamps.contains( name ) ) 00139 return false; 00140 00141 // remove from map and delete 00142 delete mColorRamps.take( name ); 00143 return true; 00144 } 00145 00146 QgsVectorColorRampV2* QgsStyleV2::colorRamp( QString name ) 00147 { 00148 if ( !mColorRamps.contains( name ) ) 00149 return NULL; 00150 return mColorRamps[name]->clone(); 00151 } 00152 00153 const QgsVectorColorRampV2* QgsStyleV2::colorRampRef( QString name ) const 00154 { 00155 if ( !mColorRamps.contains( name ) ) 00156 return NULL; 00157 return mColorRamps[name]; 00158 } 00159 00160 int QgsStyleV2::colorRampCount() 00161 { 00162 return mColorRamps.count(); 00163 } 00164 00165 QStringList QgsStyleV2::colorRampNames() 00166 { 00167 return mColorRamps.keys(); 00168 } 00169 00170 00171 bool QgsStyleV2::load( QString filename ) 00172 { 00173 mErrorString = QString(); 00174 00175 // import xml file 00176 QDomDocument doc( "style" ); 00177 QFile f( filename ); 00178 if ( !f.open( QFile::ReadOnly ) ) 00179 { 00180 mErrorString = "Couldn't open the style file: " + filename; 00181 return false; 00182 } 00183 00184 // parse the document 00185 if ( !doc.setContent( &f ) ) 00186 { 00187 mErrorString = "Couldn't parse the style file: " + filename; 00188 f.close(); 00189 return false; 00190 } 00191 f.close(); 00192 00193 QDomElement docElem = doc.documentElement(); 00194 if ( docElem.tagName() != "qgis_style" ) 00195 { 00196 mErrorString = "Incorrect root tag in style: " + docElem.tagName(); 00197 return false; 00198 } 00199 00200 // check for style version 00201 QString version = docElem.attribute( "version" ); 00202 if ( version != STYLE_CURRENT_VERSION ) 00203 { 00204 mErrorString = "Unknown style file version: " + version; 00205 return false; 00206 } 00207 00208 // load symbols 00209 QDomElement symbolsElement = docElem.firstChildElement( "symbols" ); 00210 if ( !symbolsElement.isNull() ) 00211 { 00212 mSymbols = QgsSymbolLayerV2Utils::loadSymbols( symbolsElement ); 00213 } 00214 00215 // load color ramps 00216 QDomElement rampsElement = docElem.firstChildElement( "colorramps" ); 00217 QDomElement e = rampsElement.firstChildElement(); 00218 while ( !e.isNull() ) 00219 { 00220 if ( e.tagName() == "colorramp" ) 00221 { 00222 QgsVectorColorRampV2* ramp = QgsSymbolLayerV2Utils::loadColorRamp( e ); 00223 if ( ramp != NULL ) 00224 addColorRamp( e.attribute( "name" ), ramp ); 00225 } 00226 else 00227 { 00228 QgsDebugMsg( "unknown tag: " + e.tagName() ); 00229 } 00230 e = e.nextSiblingElement(); 00231 } 00232 00233 mFileName = filename; 00234 return true; 00235 } 00236 00237 00238 00239 bool QgsStyleV2::save( QString filename ) 00240 { 00241 mErrorString = QString(); 00242 if ( filename.isEmpty() ) 00243 filename = mFileName; 00244 00245 QDomDocument doc( "qgis_style" ); 00246 QDomElement root = doc.createElement( "qgis_style" ); 00247 root.setAttribute( "version", STYLE_CURRENT_VERSION ); 00248 doc.appendChild( root ); 00249 00250 QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( mSymbols, "symbols", doc ); 00251 00252 QDomElement rampsElem = doc.createElement( "colorramps" ); 00253 00254 // save color ramps 00255 for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr = mColorRamps.begin(); itr != mColorRamps.end(); ++itr ) 00256 { 00257 QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp( itr.key(), itr.value(), doc ); 00258 rampsElem.appendChild( rampEl ); 00259 } 00260 00261 root.appendChild( symbolsElem ); 00262 root.appendChild( rampsElem ); 00263 00264 // save 00265 QFile f( filename ); 00266 if ( !f.open( QFile::WriteOnly ) ) 00267 { 00268 mErrorString = "Couldn't open file for writing: " + filename; 00269 return false; 00270 } 00271 QTextStream ts( &f ); 00272 doc.save( ts, 2 ); 00273 f.close(); 00274 00275 mFileName = filename; 00276 return true; 00277 } 00278 00279 bool QgsStyleV2::renameSymbol( QString oldName, QString newName ) 00280 { 00281 if ( !mSymbols.contains( oldName ) ) 00282 return NULL; 00283 00284 mSymbols.insert( newName, mSymbols.take( oldName ) ); 00285 return true; 00286 } 00287 00288 bool QgsStyleV2::renameColorRamp( QString oldName, QString newName ) 00289 { 00290 if ( !mColorRamps.contains( oldName ) ) 00291 return NULL; 00292 00293 mColorRamps.insert( newName, mColorRamps.take( oldName ) ); 00294 return true; 00295 }