Skip to content

Commit

Permalink
Add mechanism to allow new symbols to be added to the default style
Browse files Browse the repository at this point in the history
library to be updated when QGIS is upgraded
  • Loading branch information
nyalldawson committed Apr 30, 2020
1 parent b6f890e commit 3069a8e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
16 changes: 15 additions & 1 deletion resources/symbology-style.xml
@@ -1,5 +1,5 @@
<!DOCTYPE qgis_style>
<qgis_style version="1">
<qgis_style version="2">
<symbols>
<symbol force_rhr="0" name="cat trail" alpha="1" tags="Showcase" type="line" clip_to_extent="1">
<layer enabled="1" class="SimpleLine" locked="0" pass="0">
Expand Down Expand Up @@ -5818,4 +5818,18 @@
<prop k="stops" v="0.25;254,204,92,255:0.5;253,141,60,255:0.75;240,59,32,255"/>
</colorramp>
</colorramps>
<legendpatchshapes>
<legendpatchshape tags="" name="Tetris Block" addedVersion="31400">
<definition type="2" wkt="Polygon ((0 0, 0 5, 5 5, 5 10, 10 10, 10 5, 15 5, 15 0, 0 0))" preserveAspect="1"/>
</legendpatchshape>
<legendpatchshape tags="" name="Chunk Missing" addedVersion="31400">
<definition type="2" wkt="Polygon ((0 0, 0 2, 2 2, 2 4, 4 4, 6 4, 6 2, 6 0, 0 0))" preserveAspect="1"/>
</legendpatchshape>
<legendpatchshape tags="" name="Bite of my sandwich" addedVersion="31400">
<definition type="2" wkt="Polygon ((0 0, 0.00000000000000012 2, 2 2, 2 1, 3 1, 3 2, 4 2, 4 1, 4 0, 0 0))" preserveAspect="1"/>
</legendpatchshape>
<legendpatchshape tags="" name="Stray Hair" addedVersion="31400">
<definition type="1" wkt="CompoundCurve (CircularString (6.73424842557187731 6.54184132769836424, -0.18039629802337132 17.31663880861336224, 6.73424842557187819 28.09143628952836025, 6.95635639559388252 27.83239680064827581, 6.62833626160482225 27.73839574297150889, 18.25749928115512688 27.18730083899192707, 12.91182029516728313 16.84491937690900087)) " preserveAspect="1"/>
</legendpatchshape>
</legendpatchshapes>
</qgis_style>
88 changes: 87 additions & 1 deletion src/core/symbology/qgsstyle.cpp
Expand Up @@ -126,7 +126,10 @@ QgsStyle *QgsStyle::defaultStyle() // static
else
{
sDefaultStyle = new QgsStyle;
sDefaultStyle->load( styleFilename );
if ( sDefaultStyle->load( styleFilename ) )
{
sDefaultStyle->upgradeIfRequired();
}
}
}
return sDefaultStyle;
Expand Down Expand Up @@ -2396,6 +2399,11 @@ bool QgsStyle::exportXml( const QString &filename )
}

bool QgsStyle::importXml( const QString &filename )
{
return importXml( filename, -1 );
}

bool QgsStyle::importXml( const QString &filename, int sinceVersion )
{
mErrorString = QString();
QDomDocument doc( QStringLiteral( "style" ) );
Expand Down Expand Up @@ -2444,6 +2452,13 @@ bool QgsStyle::importXml( const QString &filename )
// For the new style, load symbols individually
while ( !e.isNull() )
{
const int entityAddedVersion = e.attribute( QStringLiteral( "addedVersion" ) ).toInt();
if ( entityAddedVersion != 0 && sinceVersion != -1 && entityAddedVersion <= sinceVersion )
{
// skip the symbol, should already be present
continue;
}

if ( e.tagName() == QLatin1String( "symbol" ) )
{
QString name = e.attribute( QStringLiteral( "name" ) );
Expand Down Expand Up @@ -2492,6 +2507,13 @@ bool QgsStyle::importXml( const QString &filename )
e = rampsElement.firstChildElement();
while ( !e.isNull() )
{
const int entityAddedVersion = e.attribute( QStringLiteral( "addedVersion" ) ).toInt();
if ( entityAddedVersion != 0 && sinceVersion != -1 && entityAddedVersion <= sinceVersion )
{
// skip the ramp, should already be present
continue;
}

if ( e.tagName() == QLatin1String( "colorramp" ) )
{
QString name = e.attribute( QStringLiteral( "name" ) );
Expand Down Expand Up @@ -2530,6 +2552,13 @@ bool QgsStyle::importXml( const QString &filename )
e = textFormatElement.firstChildElement();
while ( !e.isNull() )
{
const int entityAddedVersion = e.attribute( QStringLiteral( "addedVersion" ) ).toInt();
if ( entityAddedVersion != 0 && sinceVersion != -1 && entityAddedVersion <= sinceVersion )
{
// skip the format, should already be present
continue;
}

if ( e.tagName() == QLatin1String( "textformat" ) )
{
QString name = e.attribute( QStringLiteral( "name" ) );
Expand Down Expand Up @@ -2568,6 +2597,13 @@ bool QgsStyle::importXml( const QString &filename )
e = labelSettingsElement.firstChildElement();
while ( !e.isNull() )
{
const int entityAddedVersion = e.attribute( QStringLiteral( "addedVersion" ) ).toInt();
if ( entityAddedVersion != 0 && sinceVersion != -1 && entityAddedVersion <= sinceVersion )
{
// skip the settings, should already be present
continue;
}

if ( e.tagName() == QLatin1String( "labelsetting" ) )
{
QString name = e.attribute( QStringLiteral( "name" ) );
Expand Down Expand Up @@ -2606,6 +2642,13 @@ bool QgsStyle::importXml( const QString &filename )
e = legendPatchShapesElement.firstChildElement();
while ( !e.isNull() )
{
const int entityAddedVersion = e.attribute( QStringLiteral( "addedVersion" ) ).toInt();
if ( entityAddedVersion != 0 && sinceVersion != -1 && entityAddedVersion <= sinceVersion )
{
// skip the shape, should already be present
continue;
}

if ( e.tagName() == QLatin1String( "legendpatchshape" ) )
{
QString name = e.attribute( QStringLiteral( "name" ) );
Expand Down Expand Up @@ -2826,6 +2869,49 @@ void QgsStyle::clearCachedTags( QgsStyle::StyleEntity type, const QString &name
mCachedTags[ type ].remove( name );
}

void QgsStyle::upgradeIfRequired()
{
// make sure metadata table exists
auto query = QgsSqlite3Mprintf( "SELECT name FROM sqlite_master WHERE name='stylemetadata'" );
sqlite3_statement_unique_ptr statement;
int rc;
int dbVersion = 0;
statement = mCurrentDB.prepare( query, rc );

if ( rc != SQLITE_OK || sqlite3_step( statement.get() ) != SQLITE_ROW )
{
// no metadata table
query = QgsSqlite3Mprintf( "CREATE TABLE stylemetadata("\
"id INTEGER PRIMARY KEY,"\
"key TEXT UNIQUE,"\
"value TEXT);" );
runEmptyQuery( query );
query = QgsSqlite3Mprintf( "INSERT INTO stylemetadata VALUES (NULL, '%q', '%q')", "version", "31200" );
runEmptyQuery( query );

dbVersion = 31200;
}
else
{
query = QgsSqlite3Mprintf( "SELECT value FROM stylemetadata WHERE key='version'" );
statement = mCurrentDB.prepare( query, rc );
if ( rc == SQLITE_OK && sqlite3_step( statement.get() ) == SQLITE_ROW )
{
dbVersion = statement.columnAsText( 0 ).toInt();
}
}

if ( dbVersion < Qgis::versionInt() )
{
// do upgrade
if ( importXml( QgsApplication::defaultStylePath(), dbVersion ) )
{
query = QgsSqlite3Mprintf( "UPDATE stylemetadata SET value='%q' WHERE key='version'", QString::number( Qgis::versionInt() ).toUtf8().constData() );
runEmptyQuery( query );
}
}
}

QString QgsStyle::entityTableName( QgsStyle::StyleEntity type )
{
switch ( type )
Expand Down
6 changes: 6 additions & 0 deletions src/core/symbology/qgsstyle.h
Expand Up @@ -1028,6 +1028,9 @@ class CORE_EXPORT QgsStyle : public QObject
//! Convenience function to open the DB and return a sqlite3 object
bool openDatabase( const QString &filename );

//! Imports the symbols and colorramps into the default style database from the given XML file
bool importXml( const QString &filename, int sinceVersion );

/**
* Convenience function that would run queries which don't generate return values
*
Expand All @@ -1054,6 +1057,9 @@ class CORE_EXPORT QgsStyle : public QObject

void clearCachedTags( StyleEntity type, const QString &name );


void upgradeIfRequired();

/**
* Returns the table name for the specified entity \a type.
*/
Expand Down

0 comments on commit 3069a8e

Please sign in to comment.