Skip to content

Commit 2b8143a

Browse files
committed
Fix a TODO, restore correct variable handling for layouts
1 parent 886e7bf commit 2b8143a

File tree

8 files changed

+169
-6
lines changed

8 files changed

+169
-6
lines changed

python/core/qgsexpressioncontext.sip

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,9 +1026,39 @@ For instance, current page name and number.
10261026
Creates a new scope which contains variables and functions relating to a :py:class:`QgsLayoutItem`.
10271027
For instance, item size and position.
10281028

1029+
.. versionadded:: 3.0
1030+
1031+
.. seealso:: :py:func:`setLayoutItemVariable()`
1032+
1033+
.. seealso:: :py:func:`setLayoutItemVariables()`
1034+
%End
1035+
1036+
static void setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value );
1037+
%Docstring
1038+
Sets a layout ``item`` context variable, with the given ``name`` and ``value``.
1039+
This variable will be contained within scopes retrieved via
1040+
layoutItemScope().
1041+
1042+
.. seealso:: :py:func:`setLayoutItemVariables()`
1043+
1044+
.. seealso:: :py:func:`layoutItemScope()`
1045+
10291046
.. versionadded:: 3.0
10301047
%End
10311048

1049+
static void setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables );
1050+
%Docstring
1051+
Sets all layout item context variables for an ``item``. Existing variables will be removed and replaced
1052+
with the ``variables`` specified.
1053+
1054+
.. seealso:: :py:func:`setLayoutItemVariable()`
1055+
1056+
.. seealso:: :py:func:`layoutItemScope()`
1057+
1058+
.. versionadded:: 3.0
1059+
%End
1060+
1061+
10321062

10331063
static QgsExpressionContext createFeatureBasedContext( const QgsFeature &feature, const QgsFields &fields );
10341064
%Docstring

src/app/layout/qgslayoutpropertieswidget.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ QgsLayoutPropertiesWidget::QgsLayoutPropertiesWidget( QWidget *parent, QgsLayout
8484
mReferenceMapComboBox->setCurrentLayout( mLayout );
8585

8686
connect( mLayout, &QgsLayout::changed, this, &QgsLayoutPropertiesWidget::updateGui );
87+
88+
updateVariables();
89+
connect( mVariableEditor, &QgsVariableEditorWidget::scopeChanged, this, &QgsLayoutPropertiesWidget::variablesChanged );
90+
// listen out for variable edits
91+
connect( QgsApplication::instance(), &QgsApplication::customVariablesChanged, this, &QgsLayoutPropertiesWidget::updateVariables );
92+
connect( QgsProject::instance(), &QgsProject::customVariablesChanged, this, &QgsLayoutPropertiesWidget::updateVariables );
93+
8794
updateGui();
8895
}
8996

@@ -228,6 +235,21 @@ void QgsLayoutPropertiesWidget::forceVectorToggled()
228235
mLayout->setCustomProperty( QStringLiteral( "forceVector" ), mForceVectorCheckBox->isChecked() );
229236
}
230237

238+
void QgsLayoutPropertiesWidget::variablesChanged()
239+
{
240+
QgsExpressionContextUtils::setLayoutVariables( mLayout, mVariableEditor->variablesInActiveScope() );
241+
}
242+
243+
void QgsLayoutPropertiesWidget::updateVariables()
244+
{
245+
QgsExpressionContext context;
246+
context << QgsExpressionContextUtils::globalScope()
247+
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
248+
<< QgsExpressionContextUtils::layoutScope( mLayout );
249+
mVariableEditor->setContext( &context );
250+
mVariableEditor->setEditableScopeIndex( 2 );
251+
}
252+
231253
void QgsLayoutPropertiesWidget::blockSignals( bool block )
232254
{
233255
mGridResolutionSpinBox->blockSignals( block );

src/app/layout/qgslayoutpropertieswidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class QgsLayoutPropertiesWidget: public QgsPanelWidget, private Ui::QgsLayoutWid
4848
void worldFileToggled();
4949
void rasterizeToggled();
5050
void forceVectorToggled();
51+
void variablesChanged();
52+
void updateVariables();
5153

5254
private:
5355

src/core/qgsexpressioncontext.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,41 @@ QgsExpressionContextScope *QgsExpressionContextUtils::layoutItemScope( const Qgs
13391339
return scope;
13401340
}
13411341

1342+
void QgsExpressionContextUtils::setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value )
1343+
{
1344+
if ( !item )
1345+
return;
1346+
1347+
//write variable to composer item
1348+
QStringList variableNames = item->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
1349+
QStringList variableValues = item->customProperty( QStringLiteral( "variableValues" ) ).toStringList();
1350+
1351+
variableNames << name;
1352+
variableValues << value.toString();
1353+
1354+
item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
1355+
item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
1356+
}
1357+
1358+
void QgsExpressionContextUtils::setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables )
1359+
{
1360+
if ( !item )
1361+
return;
1362+
1363+
QStringList variableNames;
1364+
QStringList variableValues;
1365+
1366+
QVariantMap::const_iterator it = variables.constBegin();
1367+
for ( ; it != variables.constEnd(); ++it )
1368+
{
1369+
variableNames << it.key();
1370+
variableValues << it.value().toString();
1371+
}
1372+
1373+
item->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
1374+
item->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
1375+
}
1376+
13421377
void QgsExpressionContextUtils::setComposerItemVariable( QgsComposerItem *composerItem, const QString &name, const QVariant &value )
13431378
{
13441379
if ( !composerItem )

src/core/qgsexpressioncontext.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,31 @@ class CORE_EXPORT QgsExpressionContextUtils
959959
* Creates a new scope which contains variables and functions relating to a QgsLayoutItem.
960960
* For instance, item size and position.
961961
* \since QGIS 3.0
962+
* \see setLayoutItemVariable()
963+
* \see setLayoutItemVariables()
962964
*/
963965
static QgsExpressionContextScope *layoutItemScope( const QgsLayoutItem *item ) SIP_FACTORY;
964966

967+
/**
968+
* Sets a layout \a item context variable, with the given \a name and \a value.
969+
* This variable will be contained within scopes retrieved via
970+
* layoutItemScope().
971+
* \see setLayoutItemVariables()
972+
* \see layoutItemScope()
973+
* \since QGIS 3.0
974+
*/
975+
static void setLayoutItemVariable( QgsLayoutItem *item, const QString &name, const QVariant &value );
976+
977+
/**
978+
* Sets all layout item context variables for an \a item. Existing variables will be removed and replaced
979+
* with the \a variables specified.
980+
* \see setLayoutItemVariable()
981+
* \see layoutItemScope()
982+
* \since QGIS 3.0
983+
*/
984+
static void setLayoutItemVariables( QgsLayoutItem *item, const QVariantMap &variables );
985+
986+
965987
#ifndef SIP_RUN
966988

967989
/**

src/gui/layout/qgslayoutitemwidget.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,7 @@ void QgsLayoutItemPropertiesWidget::changeItemSize()
356356

357357
void QgsLayoutItemPropertiesWidget::variablesChanged()
358358
{
359-
#if 0 //TODO
360-
QgsExpressionContextUtils::setComposerItemVariables( mItem, mVariableEditor->variablesInActiveScope() );
361-
#endif
359+
QgsExpressionContextUtils::setLayoutItemVariables( mItem, mVariableEditor->variablesInActiveScope() );
362360
}
363361

364362
QgsLayoutItem::ReferencePoint QgsLayoutItemPropertiesWidget::positionMode() const

src/ui/layout/qgslayoutwidgetbase.ui

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>311</width>
10-
<height>494</height>
10+
<height>357</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -53,9 +53,9 @@
5353
<property name="geometry">
5454
<rect>
5555
<x>0</x>
56-
<y>-316</y>
56+
<y>-627</y>
5757
<width>297</width>
58-
<height>810</height>
58+
<height>1063</height>
5959
</rect>
6060
</property>
6161
<layout class="QVBoxLayout" name="verticalLayout_2">
@@ -392,6 +392,25 @@
392392
</layout>
393393
</widget>
394394
</item>
395+
<item>
396+
<widget class="QgsCollapsibleGroupBox" name="groupBox_2">
397+
<property name="title">
398+
<string>Variables</string>
399+
</property>
400+
<layout class="QVBoxLayout" name="verticalLayout_4">
401+
<item>
402+
<widget class="QgsVariableEditorWidget" name="mVariableEditor" native="true">
403+
<property name="minimumSize">
404+
<size>
405+
<width>0</width>
406+
<height>200</height>
407+
</size>
408+
</property>
409+
</widget>
410+
</item>
411+
</layout>
412+
</widget>
413+
</item>
395414
<item>
396415
<spacer name="verticalSpacer">
397416
<property name="orientation">
@@ -413,6 +432,12 @@
413432
</widget>
414433
<layoutdefault spacing="6" margin="11"/>
415434
<customwidgets>
435+
<customwidget>
436+
<class>QgsCollapsibleGroupBox</class>
437+
<extends>QGroupBox</extends>
438+
<header>qgscollapsiblegroupbox.h</header>
439+
<container>1</container>
440+
</customwidget>
416441
<customwidget>
417442
<class>QgsScrollArea</class>
418443
<extends>QScrollArea</extends>
@@ -445,6 +470,12 @@
445470
<extends>QComboBox</extends>
446471
<header>qgslayoutitemcombobox.h</header>
447472
</customwidget>
473+
<customwidget>
474+
<class>QgsVariableEditorWidget</class>
475+
<extends>QWidget</extends>
476+
<header location="global">qgsvariableeditorwidget.h</header>
477+
<container>1</container>
478+
</customwidget>
448479
</customwidgets>
449480
<tabstops>
450481
<tabstop>scrollArea</tabstop>

tests/src/core/testqgslayoutitem.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class TestQgsLayoutItem: public QObject
169169
void setSceneRect();
170170
void page();
171171
void itemVariablesFunction();
172+
void variables();
172173

173174
private:
174175

@@ -1422,6 +1423,28 @@ void TestQgsLayoutItem::itemVariablesFunction()
14221423
QCOMPARE( r.toString(), QString( "degrees" ) );
14231424
}
14241425

1426+
void TestQgsLayoutItem::variables()
1427+
{
1428+
QgsLayout l( QgsProject::instance() );
1429+
1430+
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
1431+
std::unique_ptr< QgsExpressionContextScope > scope( QgsExpressionContextUtils::layoutItemScope( map ) );
1432+
int before = scope->variableCount();
1433+
1434+
QgsExpressionContextUtils::setLayoutItemVariable( map, QStringLiteral( "var" ), 5 );
1435+
scope.reset( QgsExpressionContextUtils::layoutItemScope( map ) );
1436+
QCOMPARE( scope->variableCount(), before + 1 );
1437+
QCOMPARE( scope->variable( QStringLiteral( "var" ) ).toInt(), 5 );
1438+
1439+
QVariantMap vars;
1440+
vars.insert( QStringLiteral( "var2" ), 7 );
1441+
QgsExpressionContextUtils::setLayoutItemVariables( map, vars );
1442+
scope.reset( QgsExpressionContextUtils::layoutItemScope( map ) );
1443+
QCOMPARE( scope->variableCount(), before + 1 );
1444+
QVERIFY( !scope->hasVariable( QStringLiteral( "var" ) ) );
1445+
QCOMPARE( scope->variable( QStringLiteral( "var2" ) ).toInt(), 7 );
1446+
}
1447+
14251448
void TestQgsLayoutItem::rotation()
14261449
{
14271450
QgsProject proj;

0 commit comments

Comments
 (0)