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" );
410 QgsDebugMsg(
"Symbol of new name already exists" );
422 QgsDebugMsg(
"Sorry! Cannot open database to tag." );
429 QgsDebugMsg(
"No such symbol for tagging in database: " + oldName );
442 QgsDebugMsg(
"Color ramp of new name already exists." );
453 sqlite3_stmt *ppStmt;
454 char *query = sqlite3_mprintf(
"SELECT id FROM colorramp WHERE name='%q'", oldName.toUtf8().constData() );
455 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
456 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
458 rampid = sqlite3_column_int( ppStmt, 0 );
460 sqlite3_finalize( ppStmt );
469 sqlite3_stmt *ppStmt;
470 const char *query =
"SELECT * FROM symgroup";
471 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
472 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
474 groupNames << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
SymgroupName ) );
476 sqlite3_finalize( ppStmt );
485 QgsDebugMsg(
"Cannot open database for listing groups" );
491 sqlite3_stmt *ppStmt;
494 if ( parent ==
"" || parent == QString() )
496 query = sqlite3_mprintf(
"SELECT * FROM symgroup WHERE parent=0" );
500 char *subquery = sqlite3_mprintf(
"SELECT * FROM symgroup WHERE name='%q'", parent.toUtf8().constData() );
501 nError = sqlite3_prepare_v2(
mCurrentDB, subquery, -1, &ppStmt, NULL );
502 if ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
504 query = sqlite3_mprintf(
"SELECT * FROM symgroup WHERE parent=%d", sqlite3_column_int( ppStmt,
SymgroupId ) );
506 sqlite3_finalize( ppStmt );
515 nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
516 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
518 QString
group = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
SymgroupName ) );
519 groupNames.insert( sqlite3_column_int( ppStmt,
SymgroupId ), group );
522 sqlite3_finalize( ppStmt );
531 QgsDebugMsg( QString(
"Cannot Open database for getting group symbols of groupid: %1" ).arg( groupid ) );
532 return QStringList();
538 query = sqlite3_mprintf(
"SELECT name FROM symbol WHERE groupid=%d", groupid );
542 query = sqlite3_mprintf(
"SELECT name FROM colorramp WHERE groupid=%d", groupid );
547 return QStringList();
550 sqlite3_stmt *ppStmt;
551 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
554 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
556 symbols << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
559 sqlite3_finalize( ppStmt );
568 QgsDebugMsg( QString(
"Cannot open database to get symbols of tagid %1" ).arg( tagid ) );
569 return QStringList();
575 subquery = sqlite3_mprintf(
"SELECT symbol_id FROM tagmap WHERE tag_id=%d", tagid );
579 subquery = sqlite3_mprintf(
"SELECT symbol_id FROM ctagmap WHERE tag_id=%d", tagid );
584 return QStringList();
587 sqlite3_stmt *ppStmt;
588 int nErr = sqlite3_prepare_v2(
mCurrentDB, subquery, -1, &ppStmt, NULL );
592 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
594 int symbolId = sqlite3_column_int( ppStmt, 0 );
597 ? sqlite3_mprintf(
"SELECT name FROM symbol WHERE id=%d", symbolId )
598 : sqlite3_mprintf(
"SELECT name FROM colorramp WHERE id=%d", symbolId );
600 sqlite3_stmt *ppStmt2;
601 int sErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt2, NULL );
602 while ( sErr == SQLITE_OK && sqlite3_step( ppStmt2 ) == SQLITE_ROW )
604 symbols << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt2, 0 ) );
606 sqlite3_finalize( ppStmt2 );
608 sqlite3_finalize( ppStmt );
618 char *query = sqlite3_mprintf(
"INSERT INTO symgroup VALUES (NULL, '%q', %d)", groupName.toUtf8().constData(), parentid );
620 sqlite3_stmt *ppStmt;
621 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
622 if ( nErr == SQLITE_OK )
623 ( void )sqlite3_step( ppStmt );
625 sqlite3_finalize( ppStmt );
627 return (
int )sqlite3_last_insert_rowid(
mCurrentDB );
634 sqlite3_stmt *ppStmt;
636 char *query = sqlite3_mprintf(
"INSERT INTO tag VALUES (NULL, '%q')", tagname.toUtf8().constData() );
637 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
638 if ( nErr == SQLITE_OK )
639 ( void )sqlite3_step( ppStmt );
640 sqlite3_finalize( ppStmt );
642 return (
int )sqlite3_last_insert_rowid(
mCurrentDB );
651 query = sqlite3_mprintf(
"UPDATE symbol SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
654 query = sqlite3_mprintf(
"UPDATE symgroup SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
657 query = sqlite3_mprintf(
"UPDATE tag SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
660 query = sqlite3_mprintf(
"UPDATE colorramp SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
663 query = sqlite3_mprintf(
"UPDATE smartgroup SET name='%q' WHERE id=%d", newName.toUtf8().constData(), id );
675 char *query = sqlite3_mprintf(
"SELECT parent FROM symgroup WHERE id=%d",
id );
677 sqlite3_stmt *ppStmt;
678 int err = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
681 if ( err == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
682 parentid = sqlite3_column_int( ppStmt, 0 );
684 sqlite3_finalize( ppStmt );
686 return sqlite3_mprintf(
"UPDATE symbol SET groupid=%d WHERE groupid=%d;" 687 "UPDATE symgroup SET parent=%d WHERE parent=%d;" 688 "DELETE FROM symgroup WHERE id=%d", parentid,
id, parentid,
id,
id );
697 query = sqlite3_mprintf(
"DELETE FROM symbol WHERE id=%d; DELETE FROM tagmap WHERE symbol_id=%d",
id,
id );
703 query = sqlite3_mprintf(
"DELETE FROM tag WHERE id=%d; DELETE FROM tagmap WHERE tag_id=%d",
id,
id );
706 query = sqlite3_mprintf(
"DELETE FROM colorramp WHERE id=%d",
id );
709 query = sqlite3_mprintf(
"DELETE FROM smartgroup WHERE id=%d",
id );
728 int nErr = sqlite3_exec(
mCurrentDB, query, NULL, NULL, &zErr );
732 sqlite3_free( query );
735 if ( nErr != SQLITE_OK )
740 return zErr == SQLITE_OK;
750 query = sqlite3_mprintf(
"UPDATE symbol SET groupid=%d WHERE name='%q'", groupid, name.toUtf8().constData() );
753 query = sqlite3_mprintf(
"UPDATE colorramp SET groupid=%d WHERE name='%q'", groupid, name.toUtf8().constData() );
757 QgsDebugMsg(
"Wrong entity value. cannot apply group" );
768 QgsDebugMsg(
"Sorry! Cannot open database to search" );
769 return QStringList();
772 QString item = ( type ==
SymbolEntity ) ?
"symbol" :
"colorramp";
773 char *query = sqlite3_mprintf(
"SELECT name FROM %q WHERE xml LIKE '%%%q%%'",
774 item.toUtf8().constData(), qword.toUtf8().constData() );
776 sqlite3_stmt *ppStmt;
777 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
780 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
782 symbols << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
785 sqlite3_finalize( ppStmt );
787 query = sqlite3_mprintf(
"SELECT id FROM tag WHERE name LIKE '%%%q%%'", qword.toUtf8().constData() );
788 nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
791 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
793 tagids << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
796 sqlite3_finalize( ppStmt );
799 QString dummy = tagids.join(
", " );
803 query = sqlite3_mprintf(
"SELECT symbol_id FROM tagmap WHERE tag_id IN (%q)",
804 dummy.toUtf8().constData() );
808 query = sqlite3_mprintf(
"SELECT colorramp_id FROM ctagmap WHERE tag_id IN (%q)",
809 dummy.toUtf8().constData() );
811 nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
813 QStringList symbolids;
814 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
816 symbolids << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
819 sqlite3_finalize( ppStmt );
822 dummy = symbolids.join(
", " );
823 query = sqlite3_mprintf(
"SELECT name FROM %q WHERE id IN (%q)",
824 item.toUtf8().constData(), dummy.toUtf8().constData() );
825 nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
826 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
828 QString symbolName = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
829 if ( !symbols.contains( symbolName ) )
830 symbols << symbolName;
833 sqlite3_finalize( ppStmt );
842 QgsDebugMsg(
"Sorry! Cannot open database to tag." );
849 QgsDebugMsg(
"No such symbol for tagging in database: " + symbol );
854 foreach (
const QString &tag, tags )
857 char *query = sqlite3_mprintf(
"SELECT id FROM tag WHERE name='%q'", tag.toUtf8().constData() );
859 sqlite3_stmt *ppStmt;
860 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
863 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
865 tagid = sqlite3_column_int( ppStmt, 0 );
872 sqlite3_finalize( ppStmt );
876 ? sqlite3_mprintf(
"INSERT INTO tagmap VALUES (%d,%d)", tagid, symbolid )
877 : sqlite3_mprintf(
"INSERT INTO ctagmap VALUES (%d,%d)", tagid, symbolid );
880 nErr = sqlite3_exec(
mCurrentDB, query, NULL, NULL, &zErr );
894 QgsDebugMsg(
"Sorry! Cannot open database for detgging." );
899 ? sqlite3_mprintf(
"SELECT id FROM symbol WHERE name='%q'", symbol.toUtf8().constData() )
900 : sqlite3_mprintf(
"SELECT id FROM colorramp WHERE name='%q'", symbol.toUtf8().constData() );
901 sqlite3_stmt *ppStmt;
902 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
905 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
907 symbolid = sqlite3_column_int( ppStmt, 0 );
910 sqlite3_finalize( ppStmt );
912 foreach (
const QString &tag, tags )
914 query = sqlite3_mprintf(
"SELECT id FROM tag WHERE name='%q'", tag.toUtf8().constData() );
916 sqlite3_stmt *ppStmt2;
917 nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt2, NULL );
920 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt2 ) == SQLITE_ROW )
922 tagid = sqlite3_column_int( ppStmt2, 0 );
925 sqlite3_finalize( ppStmt2 );
931 ? sqlite3_mprintf(
"DELETE FROM tagmap WHERE tag_id=%d AND symbol_id=%d", tagid, symbolid )
932 : sqlite3_mprintf(
"DELETE FROM ctagmap WHERE tag_id=%d AND colorramp_id=%d", tagid, symbolid );
947 QgsDebugMsg(
"Sorry! Cannot open database for getting the tags." );
948 return QStringList();
953 return QStringList();
957 ? sqlite3_mprintf(
"SELECT tag_id FROM tagmap WHERE symbol_id=%d", symbolid )
958 : sqlite3_mprintf(
"SELECT tag_id FROM ctagmap WHERE colorramp_id=%d", symbolid );
960 sqlite3_stmt *ppStmt;
961 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
964 while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
966 char *subquery = sqlite3_mprintf(
"SELECT name FROM tag WHERE id=%d", sqlite3_column_int( ppStmt, 0 ) );
968 sqlite3_stmt *ppStmt2;
969 int pErr = sqlite3_prepare_v2(
mCurrentDB, subquery, -1, &ppStmt2, NULL );
970 if ( pErr == SQLITE_OK && sqlite3_step( ppStmt2 ) == SQLITE_ROW )
972 tagList << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt2, 0 ) );
974 sqlite3_finalize( ppStmt2 );
977 sqlite3_finalize( ppStmt );
984 char *query = sqlite3_mprintf(
"SELECT id FROM %q WHERE name='%q'", table.toUtf8().constData(), name.toUtf8().constData() );
986 sqlite3_stmt *ppStmt;
987 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
990 if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
992 id = sqlite3_column_int( ppStmt, 0 );
995 sqlite3_finalize( ppStmt );
1002 return getId(
"symbol", name );
1007 return getId(
"colorramp", name );
1012 return getId(
"symgroup", name );
1017 return getId(
"tag", name );
1022 return getId(
"smartgroup", name );
1027 QDomDocument doc(
"dummy" );
1028 QDomElement smartEl = doc.createElement(
"smartgroup" );
1029 smartEl.setAttribute(
"name", name );
1030 smartEl.setAttribute(
"operator", op );
1032 QStringList constraints;
1033 constraints <<
"tag" <<
"group" <<
"name" <<
"!tag" <<
"!group" <<
"!name";
1035 foreach (
const QString &constraint, constraints )
1037 QStringList parameters = conditions.values( constraint );
1038 foreach (
const QString ¶m, parameters )
1040 QDomElement condEl = doc.createElement(
"condition" );
1041 condEl.setAttribute(
"constraint", constraint );
1042 condEl.setAttribute(
"param", param );
1043 smartEl.appendChild( condEl );
1047 QByteArray xmlArray;
1048 QTextStream stream( &xmlArray );
1049 stream.setCodec(
"UTF-8" );
1050 smartEl.save( stream, 4 );
1051 char *query = sqlite3_mprintf(
"INSERT INTO smartgroup VALUES (NULL, '%q', '%q')",
1052 name.toUtf8().constData(), xmlArray.constData() );
1056 return (
int )sqlite3_last_insert_rowid(
mCurrentDB );
1060 QgsDebugMsg(
"Couldn't insert symbol into the database!" );
1069 QgsDebugMsg(
"Cannot open database for listing groups" );
1073 char *query = sqlite3_mprintf(
"SELECT * FROM smartgroup" );
1076 sqlite3_stmt *ppStmt;
1077 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1080 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
1082 QString
group = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt,
SmartgroupName ) );
1083 groupNames.insert( sqlite3_column_int( ppStmt,
SmartgroupId ), group );
1086 sqlite3_finalize( ppStmt );
1095 QgsDebugMsg(
"Cannot open database for listing groups" );
1096 return QStringList();
1099 char *query = sqlite3_mprintf(
"SELECT name FROM smartgroup" );
1102 sqlite3_stmt *ppStmt;
1103 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1106 while ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
1108 groups << QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
1111 sqlite3_finalize( ppStmt );
1118 QStringList symbols;
1120 char *query = sqlite3_mprintf(
"SELECT xml FROM smartgroup WHERE id=%d",
id );
1122 sqlite3_stmt *ppStmt;
1123 int nErr = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1124 if ( !( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW ) )
1126 sqlite3_finalize( ppStmt );
1127 return QStringList();
1132 QString xmlstr = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
1133 if ( !doc.setContent( xmlstr ) )
1135 QgsDebugMsg( QString(
"Cannot open smartgroup id: %1" ).arg(
id ) );
1137 QDomElement smartEl = doc.documentElement();
1138 QString op = smartEl.attribute(
"operator" );
1139 QDomNodeList conditionNodes = smartEl.childNodes();
1141 bool firstSet =
true;
1142 for (
int i = 0; i < conditionNodes.count(); i++ )
1144 QDomElement condEl = conditionNodes.at( i ).toElement();
1145 QString constraint = condEl.attribute(
"constraint" );
1146 QString param = condEl.attribute(
"param" );
1148 QStringList resultNames;
1150 if ( constraint ==
"tag" )
1154 else if ( constraint ==
"group" )
1160 else if ( constraint ==
"name" )
1164 resultNames =
symbolNames().filter( param, Qt::CaseInsensitive );
1168 resultNames =
colorRampNames().filter( param, Qt::CaseInsensitive );
1171 else if ( constraint ==
"!tag" )
1175 foreach ( QString name, unwanted )
1177 resultNames.removeAll( name );
1180 else if ( constraint ==
"!group" )
1184 foreach ( QString name, unwanted )
1186 resultNames.removeAll( name );
1189 else if ( constraint ==
"!name" )
1192 foreach (
const QString &str, all )
1194 if ( !str.contains( param, Qt::CaseInsensitive ) )
1202 symbols = resultNames;
1209 symbols << resultNames;
1211 else if ( op ==
"AND" )
1213 QStringList dummy = symbols;
1215 foreach (
const QString &result, resultNames )
1217 if ( dummy.contains( result ) )
1225 sqlite3_finalize( ppStmt );
1234 QgsDebugMsg(
"Cannot open database for listing groups" );
1240 char *query = sqlite3_mprintf(
"SELECT xml FROM smartgroup WHERE id=%d",
id );
1242 sqlite3_stmt *ppStmt;
1243 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1244 if ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
1247 QString xmlstr = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
1248 if ( !doc.setContent( xmlstr ) )
1250 QgsDebugMsg( QString(
"Cannot open smartgroup id: %1" ).arg(
id ) );
1253 QDomElement smartEl = doc.documentElement();
1254 QString op = smartEl.attribute(
"operator" );
1255 QDomNodeList conditionNodes = smartEl.childNodes();
1257 for (
int i = 0; i < conditionNodes.count(); i++ )
1259 QDomElement condEl = conditionNodes.at( i ).toElement();
1260 QString constraint = condEl.attribute(
"constraint" );
1261 QString param = condEl.attribute(
"param" );
1263 condition.insert( constraint, param );
1267 sqlite3_finalize( ppStmt );
1276 QgsDebugMsg(
"Cannot open database for listing groups" );
1282 char *query = sqlite3_mprintf(
"SELECT xml FROM smartgroup WHERE id=%d",
id );
1284 sqlite3_stmt *ppStmt;
1285 int nError = sqlite3_prepare_v2(
mCurrentDB, query, -1, &ppStmt, NULL );
1286 if ( nError == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
1289 QString xmlstr = QString::fromUtf8((
const char * ) sqlite3_column_text( ppStmt, 0 ) );
1290 if ( !doc.setContent( xmlstr ) )
1292 QgsDebugMsg( QString(
"Cannot open smartgroup id: %1" ).arg(
id ) );
1294 QDomElement smartEl = doc.documentElement();
1295 op = smartEl.attribute(
"operator" );
1298 sqlite3_finalize( ppStmt );
1305 if ( filename.isEmpty() )
1307 QgsDebugMsg(
"Invalid filename for style export." );
1311 QDomDocument doc(
"qgis_style" );
1312 QDomElement root = doc.createElement(
"qgis_style" );
1314 doc.appendChild( root );
1318 QDomElement rampsElem = doc.createElement(
"colorramps" );
1321 for ( QMap<QString, QgsVectorColorRampV2*>::iterator itr =
mColorRamps.begin(); itr !=
mColorRamps.end(); ++itr )
1324 rampsElem.appendChild( rampEl );
1327 root.appendChild( symbolsElem );
1328 root.appendChild( rampsElem );
1331 QFile f( filename );
1332 if ( !f.open( QFile::WriteOnly ) )
1334 mErrorString =
"Couldn't open file for writing: " + filename;
1338 QTextStream ts( &f );
1339 ts.setCodec(
"UTF-8" );
1350 QDomDocument doc(
"style" );
1351 QFile f( filename );
1352 if ( !f.open( QFile::ReadOnly ) )
1355 QgsDebugMsg(
"Error opening the style XML file." );
1359 if ( !doc.setContent( &f ) )
1361 mErrorString = QString(
"Unable to understand the style file: %1" ).arg( filename );
1368 QDomElement docEl = doc.documentElement();
1369 if ( docEl.tagName() !=
"qgis_style" )
1371 mErrorString =
"Incorrect root tag in style: " + docEl.tagName();
1375 QString version = docEl.attribute(
"version" );
1378 mErrorString =
"Unknown style file version: " + version;
1384 QDomElement symbolsElement = docEl.firstChildElement(
"symbols" );
1385 QDomElement e = symbolsElement.firstChildElement();
1390 while ( !e.isNull() )
1392 if ( e.tagName() ==
"symbol" )
1397 symbols.insert( e.attribute(
"name" ),
symbol );
1404 e = e.nextSiblingElement();
1414 for ( QMap<QString, QgsSymbolV2*>::iterator it = symbols.begin(); it != symbols.end(); ++it )
1420 QDomElement rampsElement = docEl.firstChildElement(
"colorramps" );
1421 e = rampsElement.firstChildElement();
1422 while ( !e.isNull() )
1424 if ( e.tagName() ==
"colorramp" )
1436 e = e.nextSiblingElement();
1445 QDomDocument doc(
"dummy" );
1447 QByteArray xmlArray;
1448 QTextStream stream( &xmlArray );
1449 stream.setCodec(
"UTF-8" );
1458 QgsDebugMsg(
"Update request received for unavailable symbol" );
1463 if ( symEl.isNull() )
1465 QgsDebugMsg(
"Couldn't convert symbol to valid XML!" );
1468 symEl.save( stream, 4 );
1469 query = sqlite3_mprintf(
"UPDATE symbol SET xml='%q' WHERE name='%q';",
1470 xmlArray.constData(), name.toUtf8().constData() );
1476 QgsDebugMsg(
"Update requested for unavailable color ramp." );
1481 if ( symEl.isNull() )
1483 QgsDebugMsg(
"Couldn't convert color ramp to valid XML!" );
1486 symEl.save( stream, 4 );
1487 query = sqlite3_mprintf(
"UPDATE colorramp SET xml='%q' WHERE name='%q';",
1488 xmlArray.constData(), name.toUtf8().constData() );
1492 QgsDebugMsg(
"Updating the unsupported StyleEntity" );
1499 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 ...
static QgsSymbolV2 * loadSymbol(const QDomElement &element)
Attempts to load a symbol from a DOM element.
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).
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
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.
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