Skip to content

Commit cd380f6

Browse files
committed
Add measurement converter and dpi to layout context
1 parent b2b35dd commit cd380f6

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

python/core/layout/qgslayoutcontext.sip

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,28 @@ class QgsLayoutContext
9797
.. seealso:: layer()
9898
%End
9999

100+
void setDpi( double dpi );
101+
%Docstring
102+
Sets the ``dpi`` for outputting the layout. This also sets the
103+
corresponding DPI for the context's measurementConverter().
104+
.. seealso:: dpi()
105+
%End
106+
107+
double dpi() const;
108+
%Docstring
109+
Returns the ``dpi`` for outputting the layout.
110+
.. seealso:: setDpi()
111+
:rtype: float
112+
%End
113+
114+
115+
QgsLayoutMeasurementConverter &measurementConverter();
116+
%Docstring
117+
Returns the layout measurement converter to be used in the layout. This converter is used
118+
for translating between other measurement units and the layout's native unit.
119+
:rtype: QgsLayoutMeasurementConverter
120+
%End
121+
100122
};
101123

102124

src/core/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,6 @@ SET(QGIS_CORE_MOC_HDRS
672672
gps/qgsgpsdconnection.h
673673

674674
layout/qgslayout.h
675-
layout/qgslayoutcontext.h
676675
layout/qgslayoutitem.h
677676
layout/qgslayoutitemregistry.h
678677
layout/qgslayoutobject.h
@@ -918,7 +917,7 @@ SET(QGIS_CORE_HDRS
918917
composer/qgscomposertexttable.h
919918
composer/qgspaperitem.h
920919

921-
layout/qgslayout.h
920+
layout/qgslayoutcontext.h
922921
layout/qgslayoutmeasurement.h
923922
layout/qgslayoutmeasurementconverter.h
924923
layout/qgspagesizeregistry.h

src/core/layout/qgslayoutcontext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ void QgsLayoutContext::setLayer( QgsVectorLayer *layer )
5454
{
5555
mLayer = layer;
5656
}
57+
58+
void QgsLayoutContext::setDpi( double dpi )
59+
{
60+
mMeasurementConverter.setDpi( dpi );
61+
}
62+
63+
double QgsLayoutContext::dpi() const
64+
{
65+
return mMeasurementConverter.dpi();
66+
}

src/core/layout/qgslayoutcontext.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgis_core.h"
2020
#include "qgsfeature.h"
2121
#include "qgsvectorlayer.h"
22+
#include "qgslayoutmeasurementconverter.h"
2223
#include <QtGlobal>
2324

2425
class QgsFeature;
@@ -108,13 +109,40 @@ class CORE_EXPORT QgsLayoutContext
108109
*/
109110
void setLayer( QgsVectorLayer *layer );
110111

112+
/**
113+
* Sets the \a dpi for outputting the layout. This also sets the
114+
* corresponding DPI for the context's measurementConverter().
115+
* \see dpi()
116+
*/
117+
void setDpi( double dpi );
118+
119+
/**
120+
* Returns the \a dpi for outputting the layout.
121+
* \see setDpi()
122+
*/
123+
double dpi() const;
124+
125+
/**
126+
* Returns the layout measurement converter to be used in the layout. This converter is used
127+
* for translating between other measurement units and the layout's native unit.
128+
*/
129+
SIP_SKIP const QgsLayoutMeasurementConverter &measurementConverter() const { return mMeasurementConverter; }
130+
131+
/**
132+
* Returns the layout measurement converter to be used in the layout. This converter is used
133+
* for translating between other measurement units and the layout's native unit.
134+
*/
135+
QgsLayoutMeasurementConverter &measurementConverter() { return mMeasurementConverter; }
136+
111137
private:
112138

113139
Flags mFlags = 0;
114140

115141
QgsFeature mFeature;
116142
QPointer< QgsVectorLayer > mLayer;
117143

144+
QgsLayoutMeasurementConverter mMeasurementConverter;
145+
118146
};
119147

120148
#endif //QGSLAYOUTCONTEXT_H

tests/src/core/testqgslayoutcontext.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class TestQgsLayoutContext: public QObject
3535
void flags(); //test QgsLayout flags
3636
void feature();
3737
void layer();
38+
void dpi();
3839

3940
private:
4041
QString mReport;
@@ -126,5 +127,13 @@ void TestQgsLayoutContext::layer()
126127
delete layer;
127128
}
128129

130+
void TestQgsLayoutContext::dpi()
131+
{
132+
QgsLayoutContext context;
133+
context.setDpi( 600 );
134+
QCOMPARE( context.dpi(), 600.0 );
135+
QCOMPARE( context.measurementConverter().dpi(), 600.0 );
136+
}
137+
129138
QGSTEST_MAIN( TestQgsLayoutContext )
130139
#include "testqgslayoutcontext.moc"

0 commit comments

Comments
 (0)