Skip to content

Commit a958c8a

Browse files
committedNov 19, 2016
[style] add createMemoryDB() to QgsStyle to create temporary db
1 parent 9679b6a commit a958c8a

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed
 

‎python/core/symbology-ng/qgsstyle.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ class QgsStyle : QObject
208208
//! Changes ramp's name
209209
bool renameColorRamp( const QString& oldName, const QString& newName );
210210

211+
//! Creates a temporary memory database
212+
bool createMemoryDB();
213+
211214
//! Loads a file into the style
212215
bool load( const QString& filename );
213216

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,42 @@ bool QgsStyle::openDB( const QString& filename )
289289
return true;
290290
}
291291

292+
bool QgsStyle::createMemoryDB()
293+
{
294+
mErrorString.clear();
295+
if ( !openDB( QStringLiteral( ":memory:" ) ) )
296+
{
297+
mErrorString = QStringLiteral( "Unable to create temporary memory database" );
298+
QgsDebugMsg( mErrorString );
299+
return false;
300+
}
301+
char *query = sqlite3_mprintf( "CREATE TABLE symbol("\
302+
"id INTEGER PRIMARY KEY,"\
303+
"name TEXT UNIQUE,"\
304+
"xml TEXT,"\
305+
"favorite INTEGER);"\
306+
"CREATE TABLE colorramp("\
307+
"id INTEGER PRIMARY KEY,"\
308+
"name TEXT UNIQUE,"\
309+
"xml TEXT,"\
310+
"favorite INTEGER);"\
311+
"CREATE TABLE tag("\
312+
"id INTEGER PRIMARY KEY,"\
313+
"name TEXT);"\
314+
"CREATE TABLE tagmap("\
315+
"tag_id INTEGER NOT NULL,"\
316+
"symbol_id INTEGER);"\
317+
"CREATE TABLE ctagmap("\
318+
"tag_id INTEGER NOT NULL,"\
319+
"colorramp_id INTEGER);"\
320+
"CREATE TABLE smartgroup("\
321+
"id INTEGER PRIMARY KEY,"\
322+
"name TEXT,"\
323+
"xml TEXT);" );
324+
runEmptyQuery( query );
325+
return true;
326+
}
327+
292328
bool QgsStyle::load( const QString& filename )
293329
{
294330
mErrorString.clear();
@@ -1391,14 +1427,42 @@ bool QgsStyle::exportXml( const QString& filename )
13911427
root.setAttribute( QStringLiteral( "version" ), STYLE_CURRENT_VERSION );
13921428
doc.appendChild( root );
13931429

1394-
// TODO work on the groups and tags
1430+
QStringList favoriteSymbols = symbolsOfFavorite( SymbolEntity );
1431+
QStringList favoriteColorramps = symbolsOfFavorite( ColorrampEntity );
1432+
1433+
// save symbols and attach tags
13951434
QDomElement symbolsElem = QgsSymbolLayerUtils::saveSymbols( mSymbols, QStringLiteral( "symbols" ), doc );
1396-
QDomElement rampsElem = doc.createElement( QStringLiteral( "colorramps" ) );
1435+
QDomNodeList symbolsList = symbolsElem.elementsByTagName( QStringLiteral( "symbol" ) );
1436+
int nbSymbols = symbolsList.count();
1437+
for ( int i = 0; i < nbSymbols; ++i )
1438+
{
1439+
QDomElement symbol = symbolsList.at( i ).toElement();
1440+
QString name = symbol.attribute( QStringLiteral( "name" ) );
1441+
QStringList tags = tagsOfSymbol( SymbolEntity, name );
1442+
if ( tags.count() > 0 )
1443+
{
1444+
symbol.setAttribute( QStringLiteral( "tags" ), tags.join( "," ) );
1445+
}
1446+
if ( favoriteSymbols.contains( name ) )
1447+
{
1448+
symbol.setAttribute( QStringLiteral( "favorite" ), "1" );
1449+
}
1450+
}
13971451

13981452
// save color ramps
1453+
QDomElement rampsElem = doc.createElement( QStringLiteral( "colorramps" ) );
13991454
for ( QMap<QString, QgsColorRamp*>::const_iterator itr = mColorRamps.constBegin(); itr != mColorRamps.constEnd(); ++itr )
14001455
{
14011456
QDomElement rampEl = QgsSymbolLayerUtils::saveColorRamp( itr.key(), itr.value(), doc );
1457+
QStringList tags = tagsOfSymbol( ColorrampEntity, itr.key() );
1458+
if ( tags.count() > 0 )
1459+
{
1460+
rampEl.setAttribute( QStringLiteral( "tags" ), tags.join( "," ) );
1461+
}
1462+
if ( favoriteColorramps.contains( itr.key() ) )
1463+
{
1464+
rampEl.setAttribute( QStringLiteral( "favorite" ), "1" );
1465+
}
14021466
rampsElem.appendChild( rampEl );
14031467
}
14041468

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,19 @@ class CORE_EXPORT QgsStyle : public QObject
273273
//! Changes ramp's name
274274
bool renameColorRamp( const QString& oldName, const QString& newName );
275275

276-
//! Loads a file into the style
276+
/** Creates a temporary memory database
277+
*
278+
* This function is used if you do not need to associate styles with a permanent on-disk database.
279+
* \return returns the success state of the temporary memory database creation
280+
*/
281+
bool createMemoryDB();
282+
283+
/** Loads a file into the style
284+
*
285+
* This function will populate styles from an on-disk database.
286+
* \param filename location of the database to load styles from
287+
* \return returns the success state of the database being loaded
288+
*/
277289
bool load( const QString& filename );
278290

279291
//! Saves style into a file (will use current filename if empty string is passed)

0 commit comments

Comments
 (0)
Please sign in to comment.