Skip to content

Commit 0143d0b

Browse files
committedNov 7, 2017
Add a signal to QgsLayoutContext when flags change
1 parent 91c3b5d commit 0143d0b

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed
 

‎python/core/layout/qgslayoutcontext.sip

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010

11-
class QgsLayoutContext
11+
class QgsLayoutContext : QObject
1212
{
1313
%Docstring
1414
Stores information relating to the current context and rendering settings for a layout.
@@ -168,11 +168,20 @@ class QgsLayoutContext
168168
:rtype: bool
169169
%End
170170

171+
signals:
172+
173+
void flagsChanged( QgsLayoutContext::Flags flags );
174+
%Docstring
175+
Emitted whenever the context's ``flags`` change.
176+
.. seealso:: setFlags()
177+
%End
178+
171179
};
172180

173181

174182

175183

184+
176185
/************************************************************************
177186
* This file has been generated automatically from *
178187
* *

‎src/core/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,6 @@ SET(QGIS_CORE_HDRS
972972
composer/qgspaperitem.h
973973

974974
layout/qgslayoutaligner.h
975-
layout/qgslayoutcontext.h
976975
layout/qgslayoutgridsettings.h
977976
layout/qgslayoutitemundocommand.h
978977
layout/qgslayoutmeasurement.h

‎src/core/layout/qgslayoutcontext.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,26 @@ QgsLayoutContext::QgsLayoutContext()
2424

2525
void QgsLayoutContext::setFlags( const QgsLayoutContext::Flags flags )
2626
{
27+
if ( flags == mFlags )
28+
return;
29+
2730
mFlags = flags;
31+
emit flagsChanged( mFlags );
2832
}
2933

3034
void QgsLayoutContext::setFlag( const QgsLayoutContext::Flag flag, const bool on )
3135
{
36+
Flags newFlags = mFlags;
3237
if ( on )
33-
mFlags |= flag;
38+
newFlags |= flag;
3439
else
35-
mFlags &= ~flag;
40+
newFlags &= ~flag;
41+
42+
if ( newFlags == mFlags )
43+
return;
44+
45+
mFlags = newFlags;
46+
emit flagsChanged( mFlags );
3647
}
3748

3849
QgsLayoutContext::Flags QgsLayoutContext::flags() const

‎src/core/layout/qgslayoutcontext.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ class QgsVectorLayer;
3131
* \brief Stores information relating to the current context and rendering settings for a layout.
3232
* \since QGIS 3.0
3333
*/
34-
class CORE_EXPORT QgsLayoutContext
34+
class CORE_EXPORT QgsLayoutContext : public QObject
3535
{
3636

37+
Q_OBJECT
38+
3739
public:
3840

3941
//! Flags for controlling how a layout is rendered
@@ -179,6 +181,14 @@ class CORE_EXPORT QgsLayoutContext
179181
*/
180182
bool pagesVisible() const { return mPagesVisible; }
181183

184+
signals:
185+
186+
/**
187+
* Emitted whenever the context's \a flags change.
188+
* \see setFlags()
189+
*/
190+
void flagsChanged( QgsLayoutContext::Flags flags );
191+
182192
private:
183193

184194
Flags mFlags = 0;
@@ -195,6 +205,8 @@ class CORE_EXPORT QgsLayoutContext
195205

196206
};
197207

208+
Q_DECLARE_METATYPE( QgsLayoutContext::Flags )
209+
198210
#endif //QGSLAYOUTCONTEXT_H
199211

200212

‎src/core/qgsapplication.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "qgsuserprofilemanager.h"
4444
#include "qgsreferencedgeometry.h"
4545
#include "qgs3drendererregistry.h"
46+
#include "qgslayoutcontext.h"
4647

4748
#include "gps/qgsgpsconnectionregistry.h"
4849
#include "processing/qgsprocessingregistry.h"
@@ -152,6 +153,7 @@ void QgsApplication::init( QString profileFolder )
152153
qRegisterMetaType<QgsMessageLog::MessageLevel>( "QgsMessageLog::MessageLevel" );
153154
qRegisterMetaType<QgsReferencedRectangle>( "QgsReferencedRectangle" );
154155
qRegisterMetaType<QgsReferencedPointXY>( "QgsReferencedPointXY" );
156+
qRegisterMetaType<QgsLayoutContext::Flags>( "QgsLayoutContext::Flags" );
155157

156158
QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : applicationDirPath() );
157159
// QgsDebugMsg( QString( "prefixPath(): %1" ).arg( prefixPath ) );

‎tests/src/core/testqgslayoutcontext.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgsvectorlayer.h"
2222
#include <QObject>
2323
#include "qgstest.h"
24+
#include <QtTest/QSignalSpy>
2425

2526
class TestQgsLayoutContext: public QObject
2627
{
@@ -81,16 +82,27 @@ void TestQgsLayoutContext::creation()
8182
void TestQgsLayoutContext::flags()
8283
{
8384
QgsLayoutContext context;
85+
QSignalSpy spyFlagsChanged( &context, &QgsLayoutContext::flagsChanged );
86+
8487
//test getting and setting flags
8588
context.setFlags( QgsLayoutContext::Flags( QgsLayoutContext::FlagAntialiasing | QgsLayoutContext::FlagUseAdvancedEffects ) );
89+
// default flags, so should be no signal
90+
QCOMPARE( spyFlagsChanged.count(), 0 );
91+
8692
QVERIFY( context.flags() == ( QgsLayoutContext::FlagAntialiasing | QgsLayoutContext::FlagUseAdvancedEffects ) );
8793
QVERIFY( context.testFlag( QgsLayoutContext::FlagAntialiasing ) );
8894
QVERIFY( context.testFlag( QgsLayoutContext::FlagUseAdvancedEffects ) );
8995
QVERIFY( ! context.testFlag( QgsLayoutContext::FlagDebug ) );
9096
context.setFlag( QgsLayoutContext::FlagDebug );
97+
QCOMPARE( spyFlagsChanged.count(), 1 );
9198
QVERIFY( context.testFlag( QgsLayoutContext::FlagDebug ) );
9299
context.setFlag( QgsLayoutContext::FlagDebug, false );
100+
QCOMPARE( spyFlagsChanged.count(), 2 );
93101
QVERIFY( ! context.testFlag( QgsLayoutContext::FlagDebug ) );
102+
context.setFlag( QgsLayoutContext::FlagDebug, false ); //no change
103+
QCOMPARE( spyFlagsChanged.count(), 2 );
104+
context.setFlags( QgsLayoutContext::FlagDebug );
105+
QCOMPARE( spyFlagsChanged.count(), 3 );
94106
}
95107

96108
void TestQgsLayoutContext::feature()

0 commit comments

Comments
 (0)
Please sign in to comment.