Skip to content

Commit

Permalink
Defer load of 3d symbols in default style until after 3d symbol regis…
Browse files Browse the repository at this point in the history
…try is populated

Otherwise it's impossible to load these symbols
  • Loading branch information
nyalldawson committed Jul 29, 2020
1 parent d8e4453 commit 396540a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/3d/CMakeLists.txt
Expand Up @@ -160,6 +160,7 @@ INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}/src/core/expression
${CMAKE_SOURCE_DIR}/src/core/layout
${CMAKE_SOURCE_DIR}/src/core/3d
${CMAKE_SOURCE_DIR}/src/core/textrenderer
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/nlohmann

Expand Down
6 changes: 6 additions & 0 deletions src/3d/qgs3d.cpp
Expand Up @@ -35,6 +35,8 @@
#include "qgspoint3dsymbol_p.h"
#include "qgsline3dsymbol_p.h"

#include "qgsstyle.h"

Qgs3D *Qgs3D::instance()
{
static Qgs3D *sInstance( new Qgs3D() );
Expand Down Expand Up @@ -65,6 +67,10 @@ void Qgs3D::initialize()

instance()->materialRegistry()->addMaterialSettingsType( new QgsMaterialSettingsMetadata( QStringLiteral( "phong" ), QObject::tr( "Realistic (Phong)" ),
QgsPhongMaterialSettings::create, nullptr ) );

// because we are usually populating the 3d registry AFTER QgsApplication initialisation, we need to defer creation
// of 3d symbols in the default style until now
QgsStyle::defaultStyle()->handleDeferred3DSymbolCreation();
}

QgsMaterialRegistry *Qgs3D::materialRegistry()
Expand Down
45 changes: 38 additions & 7 deletions src/core/symbology/qgsstyle.cpp
Expand Up @@ -456,6 +456,27 @@ QStringList QgsStyle::colorRampNames() const
return mColorRamps.keys();
}

void QgsStyle::handleDeferred3DSymbolCreation()
{
for ( auto it = mDeferred3DsymbolElements.constBegin(); it != mDeferred3DsymbolElements.constEnd(); ++it )
{
const QString symbolType = it.value().attribute( QStringLiteral( "type" ) );
std::unique_ptr< QgsAbstract3DSymbol > symbol( QgsApplication::symbol3DRegistry()->createSymbol( symbolType ) );
if ( symbol )
{
symbol->readXml( it.value(), QgsReadWriteContext() );
addSymbol3D( it.key(), symbol.release(), false );
emit entityAdded( Symbol3DEntity, it.key() );
}
else
{
QgsDebugMsg( "Cannot open 3d symbol " + it.key() );
continue;
}
}
mDeferred3DsymbolElements.clear();
}

bool QgsStyle::openDatabase( const QString &filename )
{
int rc = mCurrentDB.open( filename );
Expand Down Expand Up @@ -760,6 +781,9 @@ bool QgsStyle::load( const QString &filename )
QgsScopedRuntimeProfile profile( tr( "Load 3d symbols shapes" ) );
query = QgsSqlite3Mprintf( "SELECT * FROM symbol3d" );
statement = mCurrentDB.prepare( query, rc );

const bool registry3dPopulated = !QgsApplication::symbol3DRegistry()->symbolTypes().empty();

while ( rc == SQLITE_OK && sqlite3_step( statement.get() ) == SQLITE_ROW )
{
QDomDocument doc;
Expand All @@ -773,17 +797,24 @@ bool QgsStyle::load( const QString &filename )
}
QDomElement settingsElement = doc.documentElement();

const QString symbolType = settingsElement.attribute( QStringLiteral( "type" ) );
std::unique_ptr< QgsAbstract3DSymbol > symbol( QgsApplication::symbol3DRegistry()->createSymbol( symbolType ) );
if ( symbol )
if ( !registry3dPopulated )
{
symbol->readXml( settingsElement, QgsReadWriteContext() );
m3dSymbols.insert( settingsName, symbol.release() );
mDeferred3DsymbolElements.insert( settingsName, settingsElement );
}
else
{
QgsDebugMsg( "Cannot open 3d symbol " + settingsName );
continue;
const QString symbolType = settingsElement.attribute( QStringLiteral( "type" ) );
std::unique_ptr< QgsAbstract3DSymbol > symbol( QgsApplication::symbol3DRegistry()->createSymbol( symbolType ) );
if ( symbol )
{
symbol->readXml( settingsElement, QgsReadWriteContext() );
m3dSymbols.insert( settingsName, symbol.release() );
}
else
{
QgsDebugMsg( "Cannot open 3d symbol " + settingsName );
continue;
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/symbology/qgsstyle.h
Expand Up @@ -1084,6 +1084,9 @@ class CORE_EXPORT QgsStyle : public QObject
mutable QHash< QgsSymbol::SymbolType, QHash< QSizeF, QgsLegendPatchShape > > mDefaultPatchCache;
mutable QHash< QgsSymbol::SymbolType, QHash< QSizeF, QList< QList< QPolygonF > > > > mDefaultPatchQPolygonFCache;

QMap< QString, QDomElement > mDeferred3DsymbolElements;
void handleDeferred3DSymbolCreation();

static QgsStyle *sDefaultStyle;

//! Convenience function to open the DB and return a sqlite3 object
Expand Down Expand Up @@ -1136,6 +1139,8 @@ class CORE_EXPORT QgsStyle : public QObject
*/
static QString tagmapEntityIdFieldName( StyleEntity type );

friend class Qgs3D;

Q_DISABLE_COPY( QgsStyle )
};

Expand Down

0 comments on commit 396540a

Please sign in to comment.