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