Add QgsProject flag to avoid loading print layouts when reading a project file

Speeds up project read time when these aren't needed, and also potentially
avoids issues caused by non-thread-safety in layouts (since some items rely
on classes which can only be used in the main thread e.g. QWebPage)
This commit is contained in:
Nyall Dawson 2019-10-03 13:22:40 +10:00
parent 532524961c
commit d0ed374662
4 changed files with 11 additions and 1 deletions

View File

@ -237,6 +237,7 @@ Clears the project, removing all settings and resetting it back to an empty, def
enum ReadFlag
{
FlagDontResolveLayers,
FlagDontLoadLayouts,
};
typedef QFlags<QgsProject::ReadFlag> ReadFlags;

View File

@ -1417,7 +1417,8 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
emit labelingEngineSettingsChanged();
mAnnotationManager->readXml( doc->documentElement(), context );
mLayoutManager->readXml( doc->documentElement(), *doc );
if ( !( flags & QgsProject::FlagDontLoadLayouts ) )
mLayoutManager->readXml( doc->documentElement(), *doc );
mBookmarkManager->readXml( doc->documentElement(), *doc );
// reassign change dependencies now that all layers are loaded

View File

@ -272,6 +272,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
enum ReadFlag
{
FlagDontResolveLayers = 1 << 0, //!< Don't resolve layer paths (i.e. don't load any layer content). Dramatically improves project read time if the actual data from the layers is not required.
FlagDontLoadLayouts = 1 << 1, //!< Don't load print layouts. Improves project read time if layouts are not required, and allows projects to be safely read in background threads (since print layouts are not thread safe).
};
Q_DECLARE_FLAGS( ReadFlags, ReadFlag )

View File

@ -25,6 +25,7 @@
#include "qgsunittypes.h"
#include "qgsvectorlayer.h"
#include "qgssymbollayerutils.h"
#include "qgslayoutmanager.h"
class TestQgsProject : public QObject
{
@ -457,6 +458,12 @@ void TestQgsProject::testReadFlags()
QVERIFY( !layers.value( QStringLiteral( "polys20170310142652234" ) )->isValid() );
QCOMPARE( qobject_cast< QgsVectorLayer * >( layers.value( QStringLiteral( "lines20170310142652255" ) ) )->renderer()->type(), QStringLiteral( "categorizedSymbol" ) );
QCOMPARE( qobject_cast< QgsVectorLayer * >( layers.value( QStringLiteral( "polys20170310142652234" ) ) )->renderer()->type(), QStringLiteral( "categorizedSymbol" ) );
QString project3Path = QString( TEST_DATA_DIR ) + QStringLiteral( "/layouts/layout_casting.qgs" );
QgsProject p3;
QVERIFY( p3.read( project3Path, QgsProject::FlagDontLoadLayouts ) );
QCOMPARE( p3.layoutManager()->layouts().count(), 0 );
}
void TestQgsProject::testSetGetCrs()