Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix crash when creating QgsStyle in a non-gui application
When the default style tries to populate with default symbols we
can ONLY create the text based symbols if a QGuiApplication
is available -- otherwise we get a crash when the text symbol
tries to access QFontDatabase (which is not available in non-GUI
applications)
  • Loading branch information
nyalldawson committed Apr 27, 2021
1 parent 298de15 commit 4be828f
Showing 1 changed file with 70 additions and 65 deletions.
135 changes: 70 additions & 65 deletions src/core/symbology/qgsstyle.cpp
Expand Up @@ -2837,92 +2837,97 @@ bool QgsStyle::importXml( const QString &filename, int sinceVersion )
}

// load text formats
if ( version == STYLE_CURRENT_VERSION )

// this is ONLY safe to do if we have a QGuiApplication-- it requires QFontDatabase, which is not available otherwise!
if ( dynamic_cast< QGuiApplication * >( QCoreApplication::instance() ) )
{
const QDomElement textFormatElement = docEl.firstChildElement( QStringLiteral( "textformats" ) );
e = textFormatElement.firstChildElement();
while ( !e.isNull() )
if ( version == STYLE_CURRENT_VERSION )
{
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" ) )
const QDomElement textFormatElement = docEl.firstChildElement( QStringLiteral( "textformats" ) );
e = textFormatElement.firstChildElement();
while ( !e.isNull() )
{
QString name = e.attribute( QStringLiteral( "name" ) );
QStringList tags;
if ( e.hasAttribute( QStringLiteral( "tags" ) ) )
const int entityAddedVersion = e.attribute( QStringLiteral( "addedVersion" ) ).toInt();
if ( entityAddedVersion != 0 && sinceVersion != -1 && entityAddedVersion <= sinceVersion )
{
tags = e.attribute( QStringLiteral( "tags" ) ).split( ',' );
// skip the format, should already be present
continue;
}
bool favorite = false;
if ( e.hasAttribute( QStringLiteral( "favorite" ) ) && e.attribute( QStringLiteral( "favorite" ) ) == QLatin1String( "1" ) )

if ( e.tagName() == QLatin1String( "textformat" ) )
{
favorite = true;
}
QString name = e.attribute( QStringLiteral( "name" ) );
QStringList tags;
if ( e.hasAttribute( QStringLiteral( "tags" ) ) )
{
tags = e.attribute( QStringLiteral( "tags" ) ).split( ',' );
}
bool favorite = false;
if ( e.hasAttribute( QStringLiteral( "favorite" ) ) && e.attribute( QStringLiteral( "favorite" ) ) == QLatin1String( "1" ) )
{
favorite = true;
}

QgsTextFormat format;
const QDomElement styleElem = e.firstChildElement();
format.readXml( styleElem, QgsReadWriteContext() );
addTextFormat( name, format );
if ( mCurrentDB )
QgsTextFormat format;
const QDomElement styleElem = e.firstChildElement();
format.readXml( styleElem, QgsReadWriteContext() );
addTextFormat( name, format );
if ( mCurrentDB )
{
saveTextFormat( name, format, favorite, tags );
}
}
else
{
saveTextFormat( name, format, favorite, tags );
QgsDebugMsg( "unknown tag: " + e.tagName() );
}
e = e.nextSiblingElement();
}
else
{
QgsDebugMsg( "unknown tag: " + e.tagName() );
}
e = e.nextSiblingElement();
}
}

// load label settings
if ( version == STYLE_CURRENT_VERSION )
{
const QDomElement labelSettingsElement = docEl.firstChildElement( QStringLiteral( "labelsettings" ) );
e = labelSettingsElement.firstChildElement();
while ( !e.isNull() )
// load label settings
if ( version == STYLE_CURRENT_VERSION )
{
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" ) )
const QDomElement labelSettingsElement = docEl.firstChildElement( QStringLiteral( "labelsettings" ) );
e = labelSettingsElement.firstChildElement();
while ( !e.isNull() )
{
QString name = e.attribute( QStringLiteral( "name" ) );
QStringList tags;
if ( e.hasAttribute( QStringLiteral( "tags" ) ) )
const int entityAddedVersion = e.attribute( QStringLiteral( "addedVersion" ) ).toInt();
if ( entityAddedVersion != 0 && sinceVersion != -1 && entityAddedVersion <= sinceVersion )
{
tags = e.attribute( QStringLiteral( "tags" ) ).split( ',' );
// skip the settings, should already be present
continue;
}
bool favorite = false;
if ( e.hasAttribute( QStringLiteral( "favorite" ) ) && e.attribute( QStringLiteral( "favorite" ) ) == QLatin1String( "1" ) )

if ( e.tagName() == QLatin1String( "labelsetting" ) )
{
favorite = true;
}
QString name = e.attribute( QStringLiteral( "name" ) );
QStringList tags;
if ( e.hasAttribute( QStringLiteral( "tags" ) ) )
{
tags = e.attribute( QStringLiteral( "tags" ) ).split( ',' );
}
bool favorite = false;
if ( e.hasAttribute( QStringLiteral( "favorite" ) ) && e.attribute( QStringLiteral( "favorite" ) ) == QLatin1String( "1" ) )
{
favorite = true;
}

QgsPalLayerSettings settings;
const QDomElement styleElem = e.firstChildElement();
settings.readXml( styleElem, QgsReadWriteContext() );
addLabelSettings( name, settings );
if ( mCurrentDB )
QgsPalLayerSettings settings;
const QDomElement styleElem = e.firstChildElement();
settings.readXml( styleElem, QgsReadWriteContext() );
addLabelSettings( name, settings );
if ( mCurrentDB )
{
saveLabelSettings( name, settings, favorite, tags );
}
}
else
{
saveLabelSettings( name, settings, favorite, tags );
QgsDebugMsg( "unknown tag: " + e.tagName() );
}
e = e.nextSiblingElement();
}
else
{
QgsDebugMsg( "unknown tag: " + e.tagName() );
}
e = e.nextSiblingElement();
}
}

Expand Down

0 comments on commit 4be828f

Please sign in to comment.