Skip to content

Commit b1073b8

Browse files
author
Arunmozhi
committedJul 15, 2012
fixed a number of colo ramp bugs
1 parent fcdee9a commit b1073b8

File tree

2 files changed

+65
-42
lines changed

2 files changed

+65
-42
lines changed
 

‎src/core/symbology-ng/qgsstylev2.cpp

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,13 @@ bool QgsStyleV2::removeSymbol( QString name )
130130

131131
// TODO
132132
// Simplify this work here, its STUPID to run two DB queries for the sake of remove()
133-
QByteArray array = name.toUtf8();
134-
char *query;
135133
if ( mCurrentDB == NULL )
136134
{
137135
QgsDebugMsg( "Sorry! Cannot open database to tag." );
138136
return false;
139137
}
140138

141-
int symbolid = 0;
142-
query = sqlite3_mprintf( "SELECT id FROM symbol WHERE name='%q';", array.constData() );
143-
sqlite3_stmt *ppStmt;
144-
int err = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, NULL );
145-
if ( err == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
146-
symbolid = sqlite3_column_int( ppStmt, 0 );
147-
sqlite3_finalize( ppStmt );
139+
int symbolid = symbolId( name );
148140
if ( !symbolid )
149141
{
150142
QgsDebugMsg( "No such symbol for deleting in database: " + name + ". Cheers." );
@@ -188,6 +180,28 @@ bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
188180
if ( mColorRamps.contains( name ) )
189181
delete mColorRamps.value( name );
190182

183+
// insert it into the database
184+
QDomDocument doc( "dummy" );
185+
QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp( name, colorRamp, doc );
186+
if ( rampEl.isNull() )
187+
{
188+
QgsDebugMsg( "Couldnot convert color ramp to valid XML!" );
189+
return false;
190+
}
191+
192+
QByteArray *xmlArray = new QByteArray();
193+
QTextStream stream( xmlArray );
194+
rampEl.save( stream, 4 );
195+
QByteArray nameArray = name.toUtf8();
196+
char *query = sqlite3_mprintf( "INSERT INTO colorramp VALUES (NULL, '%q', '%q');",
197+
nameArray.constData(), xmlArray->constData() );
198+
199+
if ( !runEmptyQuery( query ) )
200+
{
201+
QgsDebugMsg( "Couldnot insert colorramp into the Database!" );
202+
return false;
203+
}
204+
191205
mColorRamps.insert( name, colorRamp );
192206
return true;
193207
}
@@ -197,6 +211,13 @@ bool QgsStyleV2::removeColorRamp( QString name )
197211
if ( !mColorRamps.contains( name ) )
198212
return false;
199213

214+
QByteArray array = name.toUtf8();
215+
char *query = sqlite3_mprintf( "DELETE FROM colorramp WHERE name='%q';", array.constData() );
216+
if ( !runEmptyQuery( query ) )
217+
{
218+
QgsDebugMsg( "Couldn't remove color ramp from the database." );
219+
return false;
220+
}
200221
// remove from map and delete
201222
delete mColorRamps.take( name );
202223
return true;
@@ -285,7 +306,7 @@ bool QgsStyleV2::load( QString filename )
285306
QDomElement rampElement = doc.documentElement();
286307
QgsVectorColorRampV2* ramp = QgsSymbolLayerV2Utils::loadColorRamp( rampElement );
287308
if ( ramp != NULL )
288-
addColorRamp( ramp_name, ramp );
309+
mColorRamps.insert( ramp_name, ramp );
289310
}
290311

291312
mFileName = filename;
@@ -628,32 +649,27 @@ QStringList QgsStyleV2::findSymbols( QString qword )
628649

629650
bool QgsStyleV2::tagSymbol( QString symbol, QStringList tags )
630651
{
631-
QByteArray array = symbol.toUtf8();
632-
char *query;
633652
if ( mCurrentDB == NULL )
634653
{
635654
QgsDebugMsg( "Sorry! Cannot open database to tag." );
636655
return false;
637656
}
638657

639-
int symbolid = 0;
640-
query = sqlite3_mprintf( "SELECT id FROM symbol WHERE name='%q';", array.constData() );
641-
sqlite3_stmt *ppStmt;
642-
int err = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, NULL );
643-
if ( err == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
644-
symbolid = sqlite3_column_int( ppStmt, 0 );
645-
sqlite3_finalize( ppStmt );
658+
int symbolid = symbolId( symbol );
646659
if ( !symbolid )
647660
{
648661
QgsDebugMsg( "No such symbol for tagging in database: " + symbol );
649662
return false;
650663
}
651664

665+
char *query;
666+
int nErr;
667+
sqlite3_stmt *ppStmt;
668+
652669
foreach ( const QString &tag, tags )
653670
{
654671
int tagid;
655672
char *zErr = 0;
656-
int nErr;
657673
QByteArray tagArray = tag.toUtf8();
658674
// sql: gets the id of the tag if present or insert the tag and get the id of the tag
659675
query = sqlite3_mprintf( "SELECT id FROM tag WHERE name='%q';", tagArray.constData() );
@@ -664,14 +680,7 @@ bool QgsStyleV2::tagSymbol( QString symbol, QStringList tags )
664680
}
665681
else
666682
{
667-
query = sqlite3_mprintf( "INSERT INTO tag VALUES (NULL,'%q');", tagArray.constData() );
668-
nErr = sqlite3_exec( mCurrentDB, query, NULL, NULL, &zErr );
669-
if ( nErr )
670-
{
671-
QgsDebugMsg( zErr );
672-
return false;
673-
}
674-
tagid = (int)sqlite3_last_insert_rowid( mCurrentDB );
683+
tagid = addTag( tag );
675684
}
676685
sqlite3_finalize( ppStmt );
677686
// Now map the tag to the symbol
@@ -731,27 +740,21 @@ bool QgsStyleV2::detagSymbol( QString symbol, QStringList tags )
731740

732741
QStringList QgsStyleV2::tagsOfSymbol( QString symbol )
733742
{
734-
QStringList tagList;
735-
QByteArray array = symbol.toUtf8();
736-
char *query;
737-
sqlite3_stmt *ppStmt;
738-
int symbolid;
739743
if ( mCurrentDB == NULL )
740744
{
741745
QgsDebugMsg( "Sorry! Cannot open database for getting the tags." );
742746
return QStringList();
743747
}
744-
// get the symbol id
745-
query = sqlite3_mprintf( "SELECT id FROM symbol WHERE name='%q';", array.constData() );
746-
int nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, NULL );
747-
if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
748-
{
749-
symbolid = sqlite3_column_int( ppStmt, 0 );
750-
}
751-
sqlite3_finalize( ppStmt );
748+
QStringList tagList;
749+
char *query;
750+
sqlite3_stmt *ppStmt;
751+
int symbolid = symbolId( symbol );
752+
if ( !symbolid )
753+
return QStringList();
754+
752755
// get the ids of tags for the symbol
753756
query = sqlite3_mprintf( "SELECT tag_id FROM tagmap WHERE symbol_id=%d;", symbolid );
754-
nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, NULL );
757+
int nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, NULL );
755758
while ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
756759
{
757760
int tagid = sqlite3_column_int( ppStmt, 0 );
@@ -770,3 +773,19 @@ QStringList QgsStyleV2::tagsOfSymbol( QString symbol )
770773

771774
return tagList;
772775
}
776+
777+
int QgsStyleV2::symbolId( QString name )
778+
{
779+
int symbolid = 0;
780+
char *query;
781+
sqlite3_stmt *ppStmt;
782+
QByteArray array = name.toUtf8();
783+
query = sqlite3_mprintf( "SELECT id FROM symbol WHERE name='%q';", array.constData() );
784+
int nErr = sqlite3_prepare_v2( mCurrentDB, query, -1, &ppStmt, NULL );
785+
if ( nErr == SQLITE_OK && sqlite3_step( ppStmt ) == SQLITE_ROW )
786+
{
787+
symbolid = sqlite3_column_int( ppStmt, 0 );
788+
}
789+
sqlite3_finalize( ppStmt );
790+
return symbolid;
791+
}

‎src/core/symbology-ng/qgsstylev2.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class CORE_EXPORT QgsStyleV2
7878
//! return a list of names of symbols
7979
QStringList symbolNames();
8080

81+
//! return the id in the style database for the given symbol name
82+
//! returns 0 if not found
83+
int symbolId( QString name );
84+
8185
//! return a map of all groupid and names for the given parent
8286
//! Returns the First order groups when empty QString is passed
8387
QgsSymbolGroupMap groupNames( QString parent = "" );

0 commit comments

Comments
 (0)
Please sign in to comment.