26 #include <QDomDocument>
27 #include <QDomElement>
29 #include <QDomNodeList>
31 #include <QTextStream>
36 #define STYLE_CURRENT_VERSION "1"
58 if ( !QFile::exists( styleFilename ) )
72 for ( QMap<QString, QgsSymbolV2*>::iterator its =
mSymbols.begin(); its !=
mSymbols.end(); ++its )
74 for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr =
mColorRamps.begin(); itr !=
mColorRamps.end(); ++itr )
85 if ( !symbol || name.isEmpty() )
112 QDomDocument doc(
"dummy" );
114 if ( symEl.isNull() )
116 QgsDebugMsg(
"Couldn't convert symbol to valid XML!" );
121 QTextStream stream( &xmlArray );
122 stream.setCodec(
"UTF-8" );
123 symEl.save( stream, 4 );
124 char *query = sqlite3_mprintf(
"INSERT INTO symbol VALUES (NULL, '%q', '%q', %d);",
125 name.toUtf8().constData(), xmlArray.constData(), groupid );
129 QgsDebugMsg(
"Couldn't insert symbol into the database!" );
151 QgsDebugMsg(
"Sorry! Cannot open database to tag." );
158 QgsDebugMsg(
"No such symbol for deleting in database: " + name +
". Cheers." );
169 return symbol ? symbol->
clone() : 0;
190 if ( !colorRamp || name.isEmpty() )
218 QDomDocument doc(
"dummy" );
220 if ( rampEl.isNull() )
222 QgsDebugMsg(
"Couldn't convert color ramp to valid XML!" );
227 QTextStream stream( &xmlArray );
228 stream.setCodec(
"UTF-8" );
229 rampEl.save( stream, 4 );
230 char *query = sqlite3_mprintf(
"INSERT INTO colorramp VALUES (NULL, '%q', '%q', %d);",
231 name.toUtf8().constData(), xmlArray.constData(), groupid );
235 QgsDebugMsg(
"Couldn't insert colorramp into the database!" );
248 char *query = sqlite3_mprintf(
"DELETE FROM colorramp WHERE name='%q'", name.toUtf8().constData() );
251 QgsDebugMsg(
"Couldn't remove color ramp from the database." );
263 return ramp ? ramp->
clone() : 0;
283 int rc = sqlite3_open( filename.toUtf8(), &
mCurrentDB );
299 if ( !
openDB( filename ) )
301 mErrorString =
"Unable to open database file specified";
307 char *query = sqlite3_mprintf(
"UPDATE symbol SET groupid=0 WHERE groupid IS NULL;"
308 "UPDATE colorramp SET groupid=0 WHERE groupid IS NULL;"
309 "UPDATE symgroup SET parent=0 WHERE parent IS NULL;" );
313 query = sqlite3_mprintf(
"SELECT * FROM symbol" );
315 sqlite3_stmt *ppStmt;
316 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
317 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
320 QString symbol_name = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
SymbolName ) );
321 QString xmlstring = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
SymbolXML ) );
322 if ( !doc.setContent( xmlstring ) )
324 QgsDebugMsg(
"Cannot open symbol " + symbol_name );
328 QDomElement symElement = doc.documentElement();
330 if ( symbol != NULL )
331 mSymbols.insert( symbol_name, symbol );
334 sqlite3_finalize( ppStmt );
336 query = sqlite3_mprintf(
"SELECT * FROM colorramp" );
337 nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
338 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
341 QString ramp_name = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
ColorrampName ) );
342 QString xmlstring = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
ColorrampXML ) );
343 if ( !doc.setContent( xmlstring ) )
348 QDomElement rampElement = doc.documentElement();
364 if ( filename.isEmpty() )
370 QDomDocument doc(
"qgis_style" );
371 QDomElement root = doc.createElement(
"qgis_style" );
373 doc.appendChild( root );
377 QDomElement rampsElem = doc.createElement(
"colorramps" );
380 for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr =
mColorRamps.begin(); itr !=
mColorRamps.end(); ++itr )
383 rampsElem.appendChild( rampEl );
386 root.appendChild( symbolsElem );
387 root.appendChild( rampsElem );
391 if ( !f.open( QFile::WriteOnly ) )
393 mErrorString =
"Couldn't open file for writing: " + filename;
396 QTextStream ts( &f );
397 ts.setCodec(
"UTF-8" );
416 QgsDebugMsg(
"Sorry! Cannot open database to tag." );
423 QgsDebugMsg(
"No such symbol for tagging in database: " + oldName );
441 sqlite3_stmt *ppStmt;
442 char *query = sqlite3_mprintf(
"SELECT id FROM colorramp WHERE name='%q'", oldName.toUtf8().constData() );
443 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
444 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
446 rampid = sqlite3_column_int( ppStmt, 0 );
448 sqlite3_finalize( ppStmt );
457 sqlite3_stmt *ppStmt;
458 const char *query =
"SELECT * FROM symgroup";
459 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
460 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
462 groupNames << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
SymgroupName ) );
464 sqlite3_finalize( ppStmt );
473 QgsDebugMsg(
"Cannot open database for listing groups" );
479 sqlite3_stmt *ppStmt;
482 if ( parent ==
"" || parent == QString() )
484 query = sqlite3_mprintf(
"SELECT * FROM symgroup WHERE parent=0" );
488 char *subquery = sqlite3_mprintf(
"SELECT * FROM symgroup WHERE name='%q'", parent.toUtf8().constData() );
489 nError = sqlite3_prepare_v2(
mCurrentDB, subquery, -1, &ppStmt, NULL );
490 if ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
492 query = sqlite3_mprintf(
"SELECT * FROM symgroup WHERE parent=%d", sqlite3_column_int( ppStmt,
SymgroupId ) );
494 sqlite3_finalize( ppStmt );
503 nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
504 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
506 QString
group = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
SymgroupName ) );
507 groupNames.insert( sqlite3_column_int( ppStmt,
SymgroupId ), group );
510 sqlite3_finalize( ppStmt );
519 QgsDebugMsg( QString(
"Cannot Open database for getting group symbols of groupid: %1" ).arg( groupid ) );
520 return QStringList();
526 query = sqlite3_mprintf(
"SELECT name FROM symbol WHERE groupid=%d", groupid );
530 query = sqlite3_mprintf(
"SELECT name FROM colorramp WHERE groupid=%d", groupid );
535 return QStringList();
538 sqlite3_stmt *ppStmt;
539 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
542 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
544 symbols << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
547 sqlite3_finalize( ppStmt );
556 QgsDebugMsg( QString(
"Cannot open database to get symbols of tagid %1" ).arg( tagid ) );
557 return QStringList();
563 subquery = sqlite3_mprintf(
"SELECT symbol_id FROM tagmap WHERE tag_id=%d", tagid );
567 subquery = sqlite3_mprintf(
"SELECT symbol_id FROM ctagmap WHERE tag_id=%d", tagid );
572 return QStringList();
575 sqlite3_stmt *ppStmt;
576 int nErr = sqlite3_prepare_v2(
mCurrentDB, subquery, -1, &ppStmt, NULL );
580 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
582 int symbolId = sqlite3_column_int( ppStmt, 0 );
585 ? sqlite3_mprintf(
"SELECT name FROM symbol WHERE id=%d", symbolId )
586 : sqlite3_mprintf(
"SELECT name FROM colorramp WHERE id=%d", symbolId );
588 sqlite3_stmt *ppStmt2;
589 int sErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt2, NULL );
590 while ( sErr == SQLITE_OK && sqlite3_step( ppStmt2 ) == SQLITE_ROW )
592 symbols << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt2, 0 ) );
594 sqlite3_finalize( ppStmt2 );
596 sqlite3_finalize( ppStmt );
606 char *query = sqlite3_mprintf(
"INSERT INTO symgroup VALUES (NULL, '%q', %d)", groupName.toUtf8().constData(), parentid );
608 sqlite3_stmt *ppStmt;
609 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
610 if ( nErr == SQLITE_OK )
611 sqlite3_step( ppStmt );
613 sqlite3_finalize( ppStmt );
615 return (
int )sqlite3_last_insert_rowid(
mCurrentDB );
622 sqlite3_stmt *ppStmt;
624 char *query = sqlite3_mprintf(
"INSERT INTO tag VALUES (NULL, '%q')", tagname.toUtf8().constData() );
625 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
626 if ( nErr == SQLITE_OK )
627 sqlite3_step( ppStmt );
628 sqlite3_finalize( ppStmt );
630 return (
int )sqlite3_last_insert_rowid(
mCurrentDB );
639 query = sqlite3_mprintf(
"UPDATE symbol SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
642 query = sqlite3_mprintf(
"UPDATE symgroup SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
645 query = sqlite3_mprintf(
"UPDATE tag SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
648 query = sqlite3_mprintf(
"UPDATE colorramp SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
651 query = sqlite3_mprintf(
"UPDATE smartgroup SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
663 char *query = sqlite3_mprintf(
"SELECT parent FROM symgroup WHERE id=%d",
id );
665 sqlite3_stmt *ppStmt;
666 int err = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
669 if ( err == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
670 parentid = sqlite3_column_int( ppStmt, 0 );
672 sqlite3_finalize( ppStmt );
674 return sqlite3_mprintf(
"UPDATE symbol SET groupid=%d WHERE groupid=%d;"
675 "UPDATE symgroup SET parent=%d WHERE parent=%d;"
676 "DELETE FROM symgroup WHERE id=%d", parentid,
id, parentid,
id,
id );
685 query = sqlite3_mprintf(
"DELETE FROM symbol WHERE id=%d; DELETE FROM tagmap WHERE symbol_id=%d",
id,
id );
691 query = sqlite3_mprintf(
"DELETE FROM tag WHERE id=%d; DELETE FROM tagmap WHERE tag_id=%d",
id,
id );
694 query = sqlite3_mprintf(
"DELETE FROM colorramp WHERE id=%d",
id );
697 query = sqlite3_mprintf(
"DELETE FROM smartgroup WHERE id=%d",
id );
716 int nErr = sqlite3_exec(
mCurrentDB, query, NULL, NULL, &zErr );
720 sqlite3_free( query );
723 if ( nErr != SQLITE_OK )
728 return zErr == SQLITE_OK;
738 query = sqlite3_mprintf(
"UPDATE symbol SET groupid=%d WHERE name='%q'", groupid, name.toUtf8().constData() );
741 query = sqlite3_mprintf(
"UPDATE colorramp SET groupid=%d WHERE name='%q'", groupid, name.toUtf8().constData() );
745 QgsDebugMsg(
"Wrong entity value. cannot apply group" );
756 QgsDebugMsg(
"Sorry! Cannot open database to search" );
757 return QStringList();
760 QString item = ( type ==
SymbolEntity ) ?
"symbol" :
"colorramp";
761 char *query = sqlite3_mprintf(
"SELECT name FROM %q WHERE xml LIKE '%%%q%%'",
762 item.toUtf8().constData(), qword.toUtf8().constData() );
764 sqlite3_stmt *ppStmt;
765 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
768 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
770 symbols << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
773 sqlite3_finalize( ppStmt );
775 query = sqlite3_mprintf(
"SELECT id FROM tag WHERE name LIKE '%%%q%%'", qword.toUtf8().constData() );
776 nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
779 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
781 tagids << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
784 sqlite3_finalize( ppStmt );
787 QString dummy = tagids.join(
", " );
791 query = sqlite3_mprintf(
"SELECT symbol_id FROM tagmap WHERE tag_id IN (%q)",
792 dummy.toUtf8().constData() );
796 query = sqlite3_mprintf(
"SELECT colorramp_id FROM ctagmap WHERE tag_id IN (%q)",
797 dummy.toUtf8().constData() );
799 nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
801 QStringList symbolids;
802 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
804 symbolids << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
807 sqlite3_finalize( ppStmt );
810 dummy = symbolids.join(
", " );
811 query = sqlite3_mprintf(
"SELECT name FROM %q WHERE id IN (%q)",
812 item.toUtf8().constData(), dummy.toUtf8().constData() );
813 nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
814 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
816 QString symbolName = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
817 if ( !symbols.contains( symbolName ) )
818 symbols << symbolName;
821 sqlite3_finalize( ppStmt );
830 QgsDebugMsg(
"Sorry! Cannot open database to tag." );
837 QgsDebugMsg(
"No such symbol for tagging in database: " + symbol );
842 foreach (
const QString &tag, tags )
845 char *query = sqlite3_mprintf(
"SELECT id FROM tag WHERE name='%q'", tag.toUtf8().constData() );
847 sqlite3_stmt *ppStmt;
848 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
851 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
853 tagid = sqlite3_column_int( ppStmt, 0 );
860 sqlite3_finalize( ppStmt );
864 ? sqlite3_mprintf(
"INSERT INTO tagmap VALUES (%d,%d)", tagid, symbolid )
865 : sqlite3_mprintf(
"INSERT INTO ctagmap VALUES (%d,%d)", tagid, symbolid );
868 nErr = sqlite3_exec(
mCurrentDB, query, NULL, NULL, &zErr );
882 QgsDebugMsg(
"Sorry! Cannot open database for detgging." );
887 ? sqlite3_mprintf(
"SELECT id FROM symbol WHERE name='%q'", symbol.toUtf8().constData() )
888 : sqlite3_mprintf(
"SELECT id FROM colorramp WHERE name='%q'", symbol.toUtf8().constData() );
889 sqlite3_stmt *ppStmt;
890 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
893 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
895 symbolid = sqlite3_column_int( ppStmt, 0 );
898 sqlite3_finalize( ppStmt );
900 foreach (
const QString &tag, tags )
902 query = sqlite3_mprintf(
"SELECT id FROM tag WHERE name='%q'", tag.toUtf8().constData() );
904 sqlite3_stmt *ppStmt2;
905 nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt2, NULL );
908 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt2 ) == SQLITE_ROW )
910 tagid = sqlite3_column_int( ppStmt2, 0 );
913 sqlite3_finalize( ppStmt2 );
919 ? sqlite3_mprintf(
"DELETE FROM tagmap WHERE tag_id=%d AND symbol_id=%d", tagid, symbolid )
920 : sqlite3_mprintf(
"DELETE FROM ctagmap WHERE tag_id=%d AND colorramp_id=%d", tagid, symbolid );
935 QgsDebugMsg(
"Sorry! Cannot open database for getting the tags." );
936 return QStringList();
941 return QStringList();
945 ? sqlite3_mprintf(
"SELECT tag_id FROM tagmap WHERE symbol_id=%d", symbolid )
946 : sqlite3_mprintf(
"SELECT tag_id FROM ctagmap WHERE colorramp_id=%d", symbolid );
948 sqlite3_stmt *ppStmt;
949 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
952 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
954 char *subquery = sqlite3_mprintf(
"SELECT name FROM tag WHERE id=%d", sqlite3_column_int( ppStmt, 0 ) );
956 sqlite3_stmt *ppStmt2;
957 int pErr = sqlite3_prepare_v2(
mCurrentDB, subquery, -1, &ppStmt2, NULL );
958 if ( pErr == SQLITE_OK && sqlite3_step( ppStmt2 ) == SQLITE_ROW )
960 tagList << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt2, 0 ) );
962 sqlite3_finalize( ppStmt2 );
965 sqlite3_finalize( ppStmt );
972 char *query = sqlite3_mprintf(
"SELECT id FROM %q WHERE name='%q'", table.toUtf8().constData(), name.toUtf8().constData() );
974 sqlite3_stmt *ppStmt;
975 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
978 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
980 id = sqlite3_column_int( ppStmt, 0 );
983 sqlite3_finalize( ppStmt );
990 return getId(
"symbol", name );
995 return getId(
"colorramp", name );
1000 return getId(
"symgroup", name );
1005 return getId(
"tag", name );
1010 return getId(
"smartgroup", name );
1015 QDomDocument doc(
"dummy" );
1016 QDomElement smartEl = doc.createElement(
"smartgroup" );
1017 smartEl.setAttribute(
"name", name );
1018 smartEl.setAttribute(
"operator", op );
1020 QStringList constraints;
1021 constraints <<
"tag" <<
"group" <<
"name" <<
"!tag" <<
"!group" <<
"!name";
1023 foreach (
const QString &constraint, constraints )
1025 QStringList parameters = conditions.values( constraint );
1026 foreach (
const QString ¶m, parameters )
1028 QDomElement condEl = doc.createElement(
"condition" );
1029 condEl.setAttribute(
"constraint", constraint );
1030 condEl.setAttribute(
"param", param );
1031 smartEl.appendChild( condEl );
1035 QByteArray xmlArray;
1036 QTextStream stream( &xmlArray );
1037 stream.setCodec(
"UTF-8" );
1038 smartEl.save( stream, 4 );
1039 char *query = sqlite3_mprintf(
"INSERT INTO smartgroup VALUES (NULL, '%q', '%q')",
1040 name.toUtf8().constData(), xmlArray.constData() );
1044 return (
int )sqlite3_last_insert_rowid(
mCurrentDB );
1048 QgsDebugMsg(
"Couldn't insert symbol into the database!" );
1057 QgsDebugMsg(
"Cannot open database for listing groups" );
1061 char *query = sqlite3_mprintf(
"SELECT * FROM smartgroup" );
1064 sqlite3_stmt *ppStmt;
1065 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1068 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
1070 QString
group = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
SmartgroupName ) );
1071 groupNames.insert( sqlite3_column_int( ppStmt,
SmartgroupId ), group );
1074 sqlite3_finalize( ppStmt );
1083 QgsDebugMsg(
"Cannot open database for listing groups" );
1084 return QStringList();
1087 char *query = sqlite3_mprintf(
"SELECT name FROM smartgroup" );
1090 sqlite3_stmt *ppStmt;
1091 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1094 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
1096 groups << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
1099 sqlite3_finalize( ppStmt );
1106 QStringList symbols;
1108 char *query = sqlite3_mprintf(
"SELECT xml FROM smartgroup WHERE id=%d",
id );
1110 sqlite3_stmt *ppStmt;
1111 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1112 if ( !( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW ) )
1114 sqlite3_finalize( ppStmt );
1115 return QStringList();
1120 QString xmlstr = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
1121 if ( !doc.setContent( xmlstr ) )
1123 QgsDebugMsg( QString(
"Cannot open smartgroup id: %1" ).arg(
id ) );
1125 QDomElement smartEl = doc.documentElement();
1126 QString op = smartEl.attribute(
"operator" );
1127 QDomNodeList conditionNodes = smartEl.childNodes();
1129 bool firstSet =
true;
1130 for (
int i = 0; i < conditionNodes.count(); i++ )
1132 QDomElement condEl = conditionNodes.at( i ).toElement();
1133 QString constraint = condEl.attribute(
"constraint" );
1134 QString param = condEl.attribute(
"param" );
1136 QStringList resultNames;
1138 if ( constraint ==
"tag" )
1142 else if ( constraint ==
"group" )
1148 else if ( constraint ==
"name" )
1152 resultNames =
symbolNames().filter( param, Qt::CaseInsensitive );
1156 resultNames =
colorRampNames().filter( param, Qt::CaseInsensitive );
1159 else if ( constraint ==
"!tag" )
1163 foreach ( QString name, unwanted )
1165 resultNames.removeAll( name );
1168 else if ( constraint ==
"!group" )
1172 foreach ( QString name, unwanted )
1174 resultNames.removeAll( name );
1177 else if ( constraint ==
"!name" )
1180 foreach (
const QString &str, all )
1182 if ( !str.contains( param, Qt::CaseInsensitive ) )
1190 symbols = resultNames;
1197 symbols << resultNames;
1199 else if ( op ==
"AND" )
1201 QStringList dummy = symbols;
1203 foreach (
const QString &result, resultNames )
1205 if ( dummy.contains( result ) )
1213 sqlite3_finalize( ppStmt );
1222 QgsDebugMsg(
"Cannot open database for listing groups" );
1228 char *query = sqlite3_mprintf(
"SELECT xml FROM smartgroup WHERE id=%d",
id );
1230 sqlite3_stmt *ppStmt;
1231 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1232 if ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
1235 QString xmlstr = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
1236 if ( !doc.setContent( xmlstr ) )
1238 QgsDebugMsg( QString(
"Cannot open smartgroup id: %1" ).arg(
id ) );
1241 QDomElement smartEl = doc.documentElement();
1242 QString op = smartEl.attribute(
"operator" );
1243 QDomNodeList conditionNodes = smartEl.childNodes();
1245 for (
int i = 0; i < conditionNodes.count(); i++ )
1247 QDomElement condEl = conditionNodes.at( i ).toElement();
1248 QString constraint = condEl.attribute(
"constraint" );
1249 QString param = condEl.attribute(
"param" );
1251 condition.insert( constraint, param );
1255 sqlite3_finalize( ppStmt );
1264 QgsDebugMsg(
"Cannot open database for listing groups" );
1270 char *query = sqlite3_mprintf(
"SELECT xml FROM smartgroup WHERE id=%d",
id );
1272 sqlite3_stmt *ppStmt;
1273 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1274 if ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
1277 QString xmlstr = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
1278 if ( !doc.setContent( xmlstr ) )
1280 QgsDebugMsg( QString(
"Cannot open smartgroup id: %1" ).arg(
id ) );
1282 QDomElement smartEl = doc.documentElement();
1283 op = smartEl.attribute(
"operator" );
1286 sqlite3_finalize( ppStmt );
1293 if ( filename.isEmpty() )
1295 QgsDebugMsg(
"Invalid filename for style export." );
1299 QDomDocument doc(
"qgis_style" );
1300 QDomElement root = doc.createElement(
"qgis_style" );
1302 doc.appendChild( root );
1306 QDomElement rampsElem = doc.createElement(
"colorramps" );
1309 for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr =
mColorRamps.begin(); itr !=
mColorRamps.end(); ++itr )
1312 rampsElem.appendChild( rampEl );
1315 root.appendChild( symbolsElem );
1316 root.appendChild( rampsElem );
1319 QFile f( filename );
1320 if ( !f.open( QFile::WriteOnly ) )
1322 mErrorString =
"Couldn't open file for writing: " + filename;
1326 QTextStream ts( &f );
1327 ts.setCodec(
"UTF-8" );
1338 QDomDocument doc(
"style" );
1339 QFile f( filename );
1340 if ( !f.open( QFile::ReadOnly ) )
1343 QgsDebugMsg(
"Error opening the style XML file." );
1347 if ( !doc.setContent( &f ) )
1349 mErrorString = QString(
"Unable to understand the style file: %1" ).arg( filename );
1356 QDomElement docEl = doc.documentElement();
1357 if ( docEl.tagName() !=
"qgis_style" )
1359 mErrorString =
"Incorrect root tag in style: " + docEl.tagName();
1363 QString version = docEl.attribute(
"version" );
1366 mErrorString =
"Unknown style file version: " + version;
1372 QDomElement symbolsElement = docEl.firstChildElement(
"symbols" );
1373 QDomElement e = symbolsElement.firstChildElement();
1378 while ( !e.isNull() )
1380 if ( e.tagName() ==
"symbol" )
1385 symbols.insert( e.attribute(
"name" ),
symbol );
1392 e = e.nextSiblingElement();
1402 for ( QMap<QString, QgsSymbolV2*>::iterator it = symbols.begin(); it != symbols.end(); ++it )
1408 QDomElement rampsElement = docEl.firstChildElement(
"colorramps" );
1409 e = rampsElement.firstChildElement();
1410 while ( !e.isNull() )
1412 if ( e.tagName() ==
"colorramp" )
1424 e = e.nextSiblingElement();
1433 QDomDocument doc(
"dummy" );
1435 QByteArray xmlArray;
1436 QTextStream stream( &xmlArray );
1437 stream.setCodec(
"UTF-8" );
1446 QgsDebugMsg(
"Update request received for unavailable symbol" );
1451 if ( symEl.isNull() )
1453 QgsDebugMsg(
"Couldn't convert symbol to valid XML!" );
1456 symEl.save( stream, 4 );
1457 query = sqlite3_mprintf(
"UPDATE symbol SET xml='%q' WHERE name='%q';",
1458 xmlArray.constData(), name.toUtf8().constData() );
1464 QgsDebugMsg(
"Update requested for unavailable color ramp." );
1469 if ( symEl.isNull() )
1471 QgsDebugMsg(
"Couldn't convert color ramp to valid XML!" );
1474 symEl.save( stream, 4 );
1475 query = sqlite3_mprintf(
"UPDATE colorramp SET xml='%q' WHERE name='%q';",
1476 xmlArray.constData(), name.toUtf8().constData() );
1480 QgsDebugMsg(
"Updating the unsupported StyleEntity" );
1487 QgsDebugMsg(
"Couldn't insert symbol into the database!" );
QString smartgroupOperator(int id)
returns the operator for the smartgroup
QMap< QString, QgsSymbolV2 * > QgsSymbolV2Map
static QgsSymbolV2Map loadSymbols(QDomElement &element)
void remove(StyleEntity type, int id)
remove the specified entity from the db
QStringList tagsOfSymbol(StyleEntity type, QString symbol)
return the tags associated with the symbol
bool saveSymbol(QString name, QgsSymbolV2 *symbol, int groupid, QStringList tags)
add the symbol to the DB with the tags
int addSmartgroup(QString name, QString op, QgsSmartConditionMap conditions)
adds new smartgroup to the database and returns the id
static QgsVectorColorRampV2 * loadColorRamp(QDomElement &element)
bool updateSymbol(StyleEntity type, QString name)
updates the properties of an existing symbol/colorramp
int colorrampId(QString name)
return the id in the style database for the given colorramp name returns 0 if not found ...
bool group(StyleEntity type, QString name, int groupid)
applies the specified group to the symbol or colorramp specified by StyleEntity
virtual QgsSymbolV2 * clone() const =0
QMultiMap< QString, QString > QgsSmartConditionMap
QStringList symbolsOfGroup(StyleEntity type, int groupid)
returns the symbolnames of a given groupid
bool importXML(QString filename)
Imports the symbols and colorramps into the default style database from the given XML file...
int getId(QString table, QString name)
gets the id from the table for the given name from the database, 0 if not found
QgsSymbolV2 * symbol(QString name)
return a NEW copy of symbol
void rename(StyleEntity type, int id, QString newName)
rename the given entity with the specified id
QStringList colorRampNames()
return a list of names of color ramps
int addTag(QString tagName)
adds a new tag and returns the tag's id
bool renameSymbol(QString oldName, QString newName)
change symbol's name
char * getGroupRemoveQuery(int id)
prepares the complex query for removing a group, so that the children are not abandoned ...
QgsSymbolGroupMap childGroupNames(QString parent="")
return a map of groupid and names for the given parent group
bool addSymbol(QString name, QgsSymbolV2 *symbol, bool update=false)
add symbol to style. takes symbol's ownership
bool save(QString filename=QString())
save style into a file (will use current filename if empty string is passed)
void symbolSaved(QString name, QgsSymbolV2 *symbol)
static QDomElement saveSymbol(QString symbolName, QgsSymbolV2 *symbol, QDomDocument &doc)
static QDomElement saveColorRamp(QString name, QgsVectorColorRampV2 *ramp, QDomDocument &doc)
const QgsSymbolV2 * symbolRef(QString name) const
return a const pointer to a symbol (doesn't create new instance)
const QgsVectorColorRampV2 * colorRampRef(QString name) const
return a const pointer to a symbol (doesn't create new instance)
int colorRampCount()
return count of color ramps
static QgsStyleV2 * mDefaultStyle
virtual QgsVectorColorRampV2 * clone() const =0
bool openDB(QString filename)
convenience function to open the DB and return a sqlite3 object
static QDomElement saveSymbols(QgsSymbolV2Map &symbols, QString tagName, QDomDocument &doc)
static QgsStyleV2 * defaultStyle()
return default application-wide style
bool renameColorRamp(QString oldName, QString newName)
change ramp's name
bool load(QString filename)
load a file into the style
QStringList symbolsOfSmartgroup(StyleEntity type, int id)
returns the symbols for the smartgroup
bool runEmptyQuery(char *query, bool freeQuery=true)
convenience function that would run queries which don't generate return values
bool saveColorRamp(QString name, QgsVectorColorRampV2 *ramp, int groupid, QStringList tags)
add the colorramp to the DB
QStringList symbolNames()
return a list of names of symbols
QMap< int, QString > QgsSymbolGroupMap
static const QString defaultStyleV2Path()
Returns the path to default style (works as a starting point). Added in QGIS 1.4. ...
QgsSymbolGroupMap smartgroupsListMap()
returns the smart groups map with id as key and name as value
bool exportXML(QString filename)
Exports the style as a XML file.
QgsVectorColorRampV2Map mColorRamps
QStringList smartgroupNames()
returns the smart groups list
QStringList symbolsWithTag(StyleEntity type, int tagid)
returns the symbol names with which have the given tag
void clear()
remove all contents of the style
bool detagSymbol(StyleEntity type, QString symbol, QStringList tags)
detags the symbol with the given list
bool removeSymbol(QString name)
remove symbol from style (and delete it)
bool addColorRamp(QString name, QgsVectorColorRampV2 *colorRamp, bool update=false)
add color ramp to style. takes ramp's ownership
int symbolCount()
return count of symbols in style
bool tagSymbol(StyleEntity type, QString symbol, QStringList tags)
tags the symbol with the tags in the list
bool removeColorRamp(QString name)
remove color ramp from style (and delete it)
int addGroup(QString groupName, int parent=0)
adds a new group and returns the group's id
static QgsSymbolV2 * loadSymbol(QDomElement &element)
int tagId(QString tag)
return the DB id for the given tag name
QgsVectorColorRampV2 * colorRamp(QString name)
return a NEW copy of color ramp
StyleEntity
Enum for Entities involved in a style.
QStringList findSymbols(StyleEntity type, QString qword)
return the names of the symbols which have a matching 'substring' in its defintion ...
static const QString userStyleV2Path()
Returns the path to user's style. Added in QGIS 1.4.
int smartgroupId(QString smartgroup)
return the DB id for the given smartgroup name
int symbolId(QString name)
return the id in the style database for the given symbol name returns 0 if not found ...
QStringList groupNames()
return the all the groups in the style
#define STYLE_CURRENT_VERSION
QgsSmartConditionMap smartgroup(int id)
returns the QgsSmartConditionMap for the given id
int groupId(QString group)
return the DB id for the given group name