Skip to content

Commit e2a03df

Browse files
committedJul 19, 2016
Make sure variable editor widgets always show current variables
On behalf of Faunalia, sponsored by ENEL (cherry-picked from c7ffdfa)
1 parent b6a967a commit e2a03df

19 files changed

+164
-19
lines changed
 

‎python/core/composer/qgscomposition.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,4 +856,9 @@ class QgsComposition : QGraphicsScene
856856

857857
/** Is emitted when the composition has an updated status bar message for the composer window*/
858858
void statusMsgChanged( QString message );
859+
860+
/** Emitted whenever the expression variables stored in the composition have been changed.
861+
* @note added in QGIS 3.0
862+
*/
863+
void variablesChanged();
859864
};

‎python/core/qgsapplication.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,23 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
379379
bool x11EventFilter ( XEvent * event );
380380
%End
381381

382+
public slots:
383+
384+
/** Causes the application instance to emit the settingsChanged() signal. This should
385+
* be called whenever global, application-wide settings are altered to advise listeners
386+
* that they may need to update their state.
387+
* @see settingsChanged()
388+
* @note added in QGIS 3.0
389+
*/
390+
void emitSettingsChanged();
391+
382392
signals:
383393
//! @note not available in python bindings
384394
// void preNotify( QObject * receiver, QEvent * event, bool * done );
395+
396+
/** Emitted whenever any global, application-wide settings are changed.
397+
* @note added in QGIS 3.0
398+
* @see emitSettingsChanged()
399+
*/
400+
void settingsChanged();
385401
};

‎python/core/qgsproject.sip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,20 @@ class QgsProject : QObject
345345

346346
void snapSettingsChanged();
347347

348+
/** Emitted whenever the expression variables stored in the project have been changed.
349+
* @note added in QGIS 3.0
350+
*/
351+
void variablesChanged();
352+
353+
public slots:
354+
355+
/** Causes the project to emit the variablesChanged() signal. This should
356+
* be called whenever expression variables related to the project are changed.
357+
* @see variablesChanged()
358+
* @note added in QGIS 3.0
359+
*/
360+
void emitVariablesChanged();
361+
348362
private:
349363

350364
QgsProject(); // private 'cause it's a singleton

‎src/app/composer/qgscomposeritemwidget.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgspoint.h"
2424
#include "qgsdatadefinedbutton.h"
2525
#include "qgsexpressioncontext.h"
26+
#include "qgsproject.h"
2627
#include <QColorDialog>
2728
#include <QPen>
2829

@@ -109,6 +110,14 @@ QgsVectorLayer* QgsComposerItemBaseWidget::atlasCoverageLayer() const
109110

110111
//QgsComposerItemWidget
111112

113+
void QgsComposerItemWidget::updateVariables()
114+
{
115+
QgsExpressionContext* context = mItem->createExpressionContext();
116+
mVariableEditor->setContext( context );
117+
mVariableEditor->setEditableScopeIndex( context->scopeCount() - 1 );
118+
delete context;
119+
}
120+
112121
QgsComposerItemWidget::QgsComposerItemWidget( QWidget* parent, QgsComposerItem* item )
113122
: QgsComposerItemBaseWidget( parent, item )
114123
, mItem( item )
@@ -141,12 +150,17 @@ QgsComposerItemWidget::QgsComposerItemWidget( QWidget* parent, QgsComposerItem*
141150

142151
connect( mTransparencySlider, SIGNAL( valueChanged( int ) ), mTransparencySpnBx, SLOT( setValue( int ) ) );
143152

144-
QgsExpressionContext* context = mItem->createExpressionContext();
145-
mVariableEditor->setContext( context );
146-
mVariableEditor->setEditableScopeIndex( context->scopeCount() - 1 );
147-
delete context;
148-
153+
updateVariables();
149154
connect( mVariableEditor, SIGNAL( scopeChanged() ), this, SLOT( variablesChanged() ) );
155+
// listen out for variable edits
156+
QgsApplication* app = qobject_cast<QgsApplication*>( QgsApplication::instance() );
157+
if ( app )
158+
{
159+
connect( app, SIGNAL( settingsChanged() ), this, SLOT( updateVariables() ) );
160+
}
161+
connect( QgsProject::instance(), SIGNAL( variablesChanged() ), this, SLOT( updateVariables() ) );
162+
if ( mItem->composition() )
163+
connect( mItem->composition(), SIGNAL( variablesChanged() ), this, SLOT( updateVariables() ) );
150164

151165
//connect atlas signals to data defined buttons
152166
QgsAtlasComposition* atlas = atlasComposition();

‎src/app/composer/qgscomposeritemwidget.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ class QgsComposerItemWidget: public QgsComposerItemBaseWidget, private Ui::QgsCo
142142
private slots:
143143

144144
void variablesChanged();
145-
146-
145+
void updateVariables();
147146
};
148147

149148
#endif //QGSCOMPOSERITEMWIDGET_H

‎src/app/composer/qgscompositionwidget.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgssymbolv2selectordialog.h"
2424
#include "qgssymbollayerv2utils.h"
2525
#include "qgsexpressioncontext.h"
26+
#include "qgsproject.h"
2627
#include <QColorDialog>
2728
#include <QWidget>
2829
#include <QPrinter> //for screen resolution
@@ -45,13 +46,15 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
4546
//read with/height from composition and find suitable entries to display
4647
displayCompositionWidthHeight();
4748

48-
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::globalScope() );
49-
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::projectScope() );
50-
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::compositionScope( mComposition ) );
51-
mVariableEditor->reloadContext();
52-
mVariableEditor->setEditableScopeIndex( 2 );
53-
49+
updateVariables();
5450
connect( mVariableEditor, SIGNAL( scopeChanged() ), this, SLOT( variablesChanged() ) );
51+
// listen out for variable edits
52+
QgsApplication* app = qobject_cast<QgsApplication*>( QgsApplication::instance() );
53+
if ( app )
54+
{
55+
connect( app, SIGNAL( settingsChanged() ), this, SLOT( updateVariables() ) );
56+
}
57+
connect( QgsProject::instance(), SIGNAL( variablesChanged() ), this, SLOT( updateVariables() ) );
5558

5659
if ( mComposition )
5760
{
@@ -221,6 +224,16 @@ void QgsCompositionWidget::resizeMarginsChanged()
221224
mLeftMarginSpinBox->value() );
222225
}
223226

227+
void QgsCompositionWidget::updateVariables()
228+
{
229+
QgsExpressionContext context;
230+
context << QgsExpressionContextUtils::globalScope()
231+
<< QgsExpressionContextUtils::projectScope()
232+
<< QgsExpressionContextUtils::compositionScope( mComposition );
233+
mVariableEditor->setContext( &context );
234+
mVariableEditor->setEditableScopeIndex( 2 );
235+
}
236+
224237
void QgsCompositionWidget::setDataDefinedProperty( const QgsDataDefinedButton* ddBtn, QgsComposerObject::DataDefinedProperty property )
225238
{
226239
if ( !mComposition )

‎src/app/composer/qgscompositionwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
8888

8989
void resizeMarginsChanged();
9090

91+
void updateVariables();
92+
9193
private:
9294
QgsComposition* mComposition;
9395
QMap<QString, QgsCompositionPaper> mPaperMap;

‎src/app/qgsoptions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,12 @@ void QgsOptions::saveOptions()
14181418
}
14191419

14201420
saveDefaultDatumTransformations();
1421+
1422+
QgsApplication* app = qobject_cast<QgsApplication*>( QgsApplication::instance() );
1423+
if ( app )
1424+
{
1425+
app->emitSettingsChanged();
1426+
}
14211427
}
14221428

14231429
void QgsOptions::rejectOptions()

‎src/app/qgsprojectproperties.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ void QgsProjectProperties::apply()
11511151

11521152
//save variables
11531153
QgsExpressionContextUtils::setProjectVariables( mVariableEditor->variablesInActiveScope() );
1154+
QgsProject::instance()->emitVariablesChanged();
11541155

11551156
emit refresh();
11561157
}

‎src/core/composer/qgscomposition.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,8 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
10571057

10581058
updateBounds();
10591059

1060+
emit variablesChanged();
1061+
10601062
return true;
10611063
}
10621064

@@ -3287,6 +3289,9 @@ void QgsComposition::setDataDefinedProperty( const QgsComposerObject::DataDefine
32873289
void QgsComposition::setCustomProperty( const QString& key, const QVariant& value )
32883290
{
32893291
mCustomProperties.setValue( key, value );
3292+
3293+
if ( key.startsWith( "variable" ) )
3294+
emit variablesChanged();
32903295
}
32913296

32923297
QVariant QgsComposition::customProperty( const QString& key, const QVariant& defaultValue ) const

‎src/core/composer/qgscomposition.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,11 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
10881088
/** Is emitted when the composition has an updated status bar message for the composer window*/
10891089
void statusMsgChanged( const QString& message );
10901090

1091+
/** Emitted whenever the expression variables stored in the composition have been changed.
1092+
* @note added in QGIS 3.0
1093+
*/
1094+
void variablesChanged();
1095+
10911096
friend class QgsComposerObject; //for accessing dataDefinedEvaluate, readDataDefinedPropertyMap and writeDataDefinedPropertyMap
10921097
friend class QgsComposerModel; //for accessing updateZValues (should not be public)
10931098
};

‎src/core/qgsapplication.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,3 +1361,8 @@ void QgsApplication::setMaxThreads( int maxThreads )
13611361
QgsDebugMsg( QString( "set QThreadPool max thread count to %1" ).arg( QThreadPool::globalInstance()->maxThreadCount() ) );
13621362
}
13631363

1364+
void QgsApplication::emitSettingsChanged()
1365+
{
1366+
emit settingsChanged();
1367+
}
1368+

‎src/core/qgsapplication.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,26 @@ class CORE_EXPORT QgsApplication : public QApplication
363363
}
364364
#endif
365365

366+
public slots:
367+
368+
/** Causes the application instance to emit the settingsChanged() signal. This should
369+
* be called whenever global, application-wide settings are altered to advise listeners
370+
* that they may need to update their state.
371+
* @see settingsChanged()
372+
* @note added in QGIS 3.0
373+
*/
374+
void emitSettingsChanged();
375+
366376
signals:
367377
//! @note not available in python bindings
368378
void preNotify( QObject * receiver, QEvent * event, bool * done );
369379

380+
/** Emitted whenever any global, application-wide settings are changed.
381+
* @note added in QGIS 3.0
382+
* @see emitSettingsChanged()
383+
*/
384+
void settingsChanged();
385+
370386
private:
371387
static void copyPath( const QString& src, const QString& dst );
372388
static QObject* ABISYM( mFileOpenEventReceiver );

‎src/core/qgsproject.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ void QgsProject::setDirty( bool b )
417417
dirty( b );
418418
}
419419

420+
void QgsProject::emitVariablesChanged()
421+
{
422+
emit variablesChanged();
423+
}
420424

421425

422426
void QgsProject::setFileName( QString const &name )

‎src/core/qgsproject.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,20 @@ class CORE_EXPORT QgsProject : public QObject
391391

392392
void snapSettingsChanged();
393393

394+
/** Emitted whenever the expression variables stored in the project have been changed.
395+
* @note added in QGIS 3.0
396+
*/
397+
void variablesChanged();
398+
399+
public slots:
400+
401+
/** Causes the project to emit the variablesChanged() signal. This should
402+
* be called whenever expression variables related to the project are changed.
403+
* @see variablesChanged()
404+
* @note added in QGIS 3.0
405+
*/
406+
void emitVariablesChanged();
407+
394408
private:
395409

396410
QgsProject(); // private 'cause it's a singleton

‎src/gui/qgsvariableeditorwidget.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ class GUI_EXPORT QgsVariableEditorWidget : public QWidget
6565
*/
6666
QgsExpressionContext* context() const { return mContext.data(); }
6767

68-
/** Reloads all scopes from the editor's current context. This method should be called
69-
* after adding or removing scopes from the attached context.
70-
* @see context()
71-
*/
72-
void reloadContext();
73-
7468
/** Sets the editable scope for the widget. Only variables from the editable scope can
7569
* be modified by users.
7670
* @param scopeIndex index of current editable scope. Set to -1 to disable
@@ -107,6 +101,14 @@ class GUI_EXPORT QgsVariableEditorWidget : public QWidget
107101
*/
108102
QgsStringMap variablesInActiveScope() const;
109103

104+
public slots:
105+
106+
/** Reloads all scopes from the editor's current context. This method should be called
107+
* after adding or removing scopes from the attached context.
108+
* @see context()
109+
*/
110+
void reloadContext();
111+
110112
signals:
111113

112114
/** Emitted when the user has modified a scope using the widget.

‎tests/src/core/testqgsapplication.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void TestQgsApplication::platformName()
8383
QCOMPARE( QgsApplication::platform(), QString( "desktop" ) );
8484
}
8585

86+
8687
void TestQgsApplication::checkPaths()
8788
{
8889
QString myPath = QgsApplication::authorsFilePath();

‎tests/src/core/testqgscomposition.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class TestQgsComposition : public QObject
5151
void resizeToContents();
5252
void resizeToContentsMargin();
5353
void resizeToContentsMultiPage();
54+
void variablesEdited();
5455

5556
private:
5657
QgsComposition *mComposition;
@@ -511,5 +512,19 @@ void TestQgsComposition::resizeToContentsMultiPage()
511512
delete composition;
512513
}
513514

515+
void TestQgsComposition::variablesEdited()
516+
{
517+
QgsMapSettings ms;
518+
QgsComposition c( ms );
519+
QSignalSpy spyVariablesChanged( &c, SIGNAL( variablesChanged() ) );
520+
521+
c.setCustomProperty( "not a variable", "1" );
522+
QVERIFY( spyVariablesChanged.count() == 0 );
523+
c.setCustomProperty( "variableNames", "1" );
524+
QVERIFY( spyVariablesChanged.count() == 1 );
525+
c.setCustomProperty( "variableValues", "1" );
526+
QVERIFY( spyVariablesChanged.count() == 2 );
527+
}
528+
514529
QTEST_MAIN( TestQgsComposition )
515530
#include "testqgscomposition.moc"

‎tests/src/core/testqgsproject.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class TestQgsProject : public QObject
3131

3232
void testReadPath();
3333
void testProjectUnits();
34+
void variablesChanged();
3435
};
3536

3637
void TestQgsProject::init()
@@ -124,6 +125,13 @@ void TestQgsProject::testProjectUnits()
124125
QCOMPARE( prj->areaUnits(), QgsUnitTypes::Acres );
125126
}
126127

128+
void TestQgsProject::variablesChanged()
129+
{
130+
QSignalSpy spyVariablesChanged( QgsProject::instance(), SIGNAL( variablesChanged() ) );
131+
QgsProject::instance()->emitVariablesChanged();
132+
QVERIFY( spyVariablesChanged.count() == 1 );
133+
}
134+
127135

128136
QTEST_MAIN( TestQgsProject )
129137
#include "testqgsproject.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.