Skip to content

Commit f29eb7b

Browse files
committedNov 24, 2017
Port and fix unit tests
1 parent f90c500 commit f29eb7b

File tree

29 files changed

+1409
-55
lines changed

29 files changed

+1409
-55
lines changed
 

‎python/core/layout/qgslayoutframe.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class QgsLayoutFrame: QgsLayoutItem
5656
:rtype: QgsLayoutMultiFrame
5757
%End
5858

59+
virtual QgsLayoutSize minimumSize() const;
60+
61+
virtual QgsLayoutSize fixedSize() const;
62+
63+
5964

6065
QRectF extent() const;
6166
%Docstring

‎python/core/layout/qgslayoutitem.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
276276
:rtype: ReferencePoint
277277
%End
278278

279-
QgsLayoutSize fixedSize() const;
279+
virtual QgsLayoutSize fixedSize() const;
280280
%Docstring
281281
Returns the fixed size of the item, if applicable, or an empty size if item can be freely
282282
resized.

‎python/core/layout/qgslayoutitemattributetable.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ class QgsLayoutItemAttributeTable: QgsLayoutTable
2929
RelationChildren
3030
};
3131

32-
QgsLayoutItemAttributeTable( QgsLayout *layout );
32+
QgsLayoutItemAttributeTable( QgsLayout *layout /TransferThis/ );
3333
%Docstring
3434
Constructor for QgsLayoutItemAttributeTable, attached to the specified ``layout``.
35+
36+
Ownership is transferred to the layout.
3537
%End
3638

3739
virtual int type() const;

‎python/core/layout/qgslayoutitemhtml.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ class QgsLayoutItemHtml: QgsLayoutMultiFrame
2727
ManualHtml
2828
};
2929

30-
QgsLayoutItemHtml( QgsLayout *layout );
30+
QgsLayoutItemHtml( QgsLayout *layout /TransferThis/ );
3131
%Docstring
3232
Constructor for QgsLayoutItemHtml, with the specified parent ``layout``.
33+
34+
Ownership is transferred to the layout.
3335
%End
3436

3537
~QgsLayoutItemHtml();

‎python/core/layout/qgslayoutitemtexttable.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ class QgsLayoutItemTextTable : QgsLayoutTable
2121
%End
2222
public:
2323

24-
QgsLayoutItemTextTable( QgsLayout *layout );
24+
QgsLayoutItemTextTable( QgsLayout *layout /TransferThis/ );
2525
%Docstring
2626
Constructor for QgsLayoutItemTextTable, for the specified ``layout``.
27+
28+
Ownership is transferred to the layout.
2729
%End
2830

2931
virtual int type() const;

‎src/core/layout/qgslayoutframe.cpp

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ QgsLayoutFrame::QgsLayoutFrame( QgsLayout *layout, QgsLayoutMultiFrame *multiFra
3535
update();
3636
} );
3737

38-
#if 0 //TODO
3938
//force recalculation of rect, so that multiframe specified sizes can be applied
40-
setSceneRect( QRectF( pos().x(), pos().y(), rect().width(), rect().height() ) );
41-
#endif
39+
refreshItemSize();
4240
}
4341
}
4442

@@ -51,6 +49,29 @@ QgsLayoutMultiFrame *QgsLayoutFrame::multiFrame() const
5149
{
5250
return mMultiFrame;
5351
}
52+
53+
QgsLayoutSize QgsLayoutFrame::minimumSize() const
54+
{
55+
if ( !mMultiFrame )
56+
return QgsLayoutSize();
57+
58+
//calculate index of frame
59+
int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
60+
//check minimum size
61+
return QgsLayoutSize( mMultiFrame->minFrameSize( frameIndex ), QgsUnitTypes::LayoutMillimeters );
62+
}
63+
64+
QgsLayoutSize QgsLayoutFrame::fixedSize() const
65+
{
66+
if ( !mMultiFrame )
67+
return QgsLayoutSize();
68+
69+
//calculate index of frame
70+
int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
71+
//check fixed size
72+
return QgsLayoutSize( mMultiFrame->fixedFrameSize( frameIndex ), QgsUnitTypes::LayoutMillimeters );
73+
}
74+
5475
#if 0// TODO - save/restore multiframe uuid!
5576
bool QgsLayoutFrame::writeXml( QDomElement &elem, QDomDocument &doc ) const
5677
{
@@ -168,37 +189,6 @@ QString QgsLayoutFrame::displayName() const
168189
return tr( "<Frame>" );
169190
}
170191

171-
#if 0 //TODO
172-
void QgsLayoutFrame::setSceneRect( const QRectF &rectangle )
173-
{
174-
QRectF fixedRect = rectangle;
175-
176-
if ( mMultiFrame )
177-
{
178-
//calculate index of frame
179-
int frameIndex = mMultiFrame->frameIndex( this );
180-
181-
QSizeF fixedSize = mMultiFrame->fixedFrameSize( frameIndex );
182-
if ( fixedSize.width() > 0 )
183-
{
184-
fixedRect.setWidth( fixedSize.width() );
185-
}
186-
if ( fixedSize.height() > 0 )
187-
{
188-
fixedRect.setHeight( fixedSize.height() );
189-
}
190-
191-
//check minimum size
192-
QSizeF minSize = mMultiFrame->minFrameSize( frameIndex );
193-
fixedRect.setWidth( std::max( minSize.width(), fixedRect.width() ) );
194-
fixedRect.setHeight( std::max( minSize.height(), fixedRect.height() ) );
195-
}
196-
197-
QgsComposerItem::setSceneRect( fixedRect );
198-
}
199-
#endif
200-
201-
202192
void QgsLayoutFrame::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
203193
{
204194
if ( mMultiFrame )

‎src/core/layout/qgslayoutframe.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ class CORE_EXPORT QgsLayoutFrame: public QgsLayoutItem
6464
*/
6565
QgsLayoutMultiFrame *multiFrame() const;
6666

67-
#if 0 //TODO
68-
//Overridden to handle fixed frame sizes set by multi frame
69-
void setSceneRect( const QRectF &rectangle ) override;
67+
QgsLayoutSize minimumSize() const override;
68+
QgsLayoutSize fixedSize() const override;
7069

71-
void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
70+
#if 0 //TODO
7271
void beginItemCommand( const QString &text ) override;
7372
void endItemCommand() override;
7473
bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;

‎src/core/layout/qgslayoutitem.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,8 +1200,15 @@ QSizeF QgsLayoutItem::applyFixedSize( const QSizeF &targetSize )
12001200
{
12011201
return targetSize;
12021202
}
1203+
1204+
QSizeF size = targetSize;
12031205
QSizeF fixedSizeLayoutUnits = mLayout->convertToLayoutUnits( fixedSize() );
1204-
return targetSize.expandedTo( fixedSizeLayoutUnits );
1206+
if ( fixedSizeLayoutUnits.width() > 0 )
1207+
size.setWidth( fixedSizeLayoutUnits.width() );
1208+
if ( fixedSizeLayoutUnits.height() > 0 )
1209+
size.setHeight( fixedSizeLayoutUnits.height() );
1210+
1211+
return size;
12051212
}
12061213

12071214
void QgsLayoutItem::refreshItemRotation( QPointF *origin )

‎src/core/layout/qgslayoutitem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
299299
* \see setFixedSize()
300300
* \see minimumSize()
301301
*/
302-
QgsLayoutSize fixedSize() const { return mFixedSize; }
302+
virtual QgsLayoutSize fixedSize() const { return mFixedSize; }
303303

304304
/**
305305
* Returns the minimum allowed size of the item, if applicable, or an empty size if item can be freely

‎src/core/layout/qgslayoutitemattributetable.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,11 @@ void QgsLayoutItemAttributeTable::setWrapString( const QString &wrapString )
679679
emit changed();
680680
}
681681

682-
bool QgsLayoutItemAttributeTable::writePropertiesToElement( QDomElement &tableElem, QDomDocument &, const QgsReadWriteContext & ) const
682+
bool QgsLayoutItemAttributeTable::writePropertiesToElement( QDomElement &tableElem, QDomDocument &doc, const QgsReadWriteContext &context ) const
683683
{
684+
if ( !QgsLayoutTable::writePropertiesToElement( tableElem, doc, context ) )
685+
return false;
686+
684687
tableElem.setAttribute( QStringLiteral( "source" ), QString::number( static_cast< int >( mSource ) ) );
685688
tableElem.setAttribute( QStringLiteral( "relationId" ), mRelationId );
686689
tableElem.setAttribute( QStringLiteral( "showUniqueRowsOnly" ), mShowUniqueRowsOnly );
@@ -706,7 +709,7 @@ bool QgsLayoutItemAttributeTable::writePropertiesToElement( QDomElement &tableEl
706709
return true;
707710
}
708711

709-
bool QgsLayoutItemAttributeTable::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext & )
712+
bool QgsLayoutItemAttributeTable::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context )
710713
{
711714
QgsVectorLayer *prevLayer = sourceLayer();
712715
if ( prevLayer )
@@ -715,6 +718,9 @@ bool QgsLayoutItemAttributeTable::readPropertiesFromElement( const QDomElement &
715718
disconnect( prevLayer, &QgsVectorLayer::layerModified, this, &QgsLayoutTable::refreshAttributes );
716719
}
717720

721+
if ( !QgsLayoutTable::readPropertiesFromElement( itemElem, doc, context ) )
722+
return false;
723+
718724
mSource = QgsLayoutItemAttributeTable::ContentSource( itemElem.attribute( QStringLiteral( "source" ), QStringLiteral( "0" ) ).toInt() );
719725
mRelationId = itemElem.attribute( QStringLiteral( "relationId" ), QLatin1String( "" ) );
720726

‎src/core/layout/qgslayoutitemattributetable.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ class CORE_EXPORT QgsLayoutItemAttributeTable: public QgsLayoutTable
5050

5151
/**
5252
* Constructor for QgsLayoutItemAttributeTable, attached to the specified \a layout.
53+
*
54+
* Ownership is transferred to the layout.
5355
*/
54-
QgsLayoutItemAttributeTable( QgsLayout *layout );
56+
QgsLayoutItemAttributeTable( QgsLayout *layout SIP_TRANSFERTHIS );
5557

5658
int type() const override;
5759
QString stringType() const override;

‎src/core/layout/qgslayoutitemhtml.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ class CORE_EXPORT QgsLayoutItemHtml: public QgsLayoutMultiFrame
4848

4949
/**
5050
* Constructor for QgsLayoutItemHtml, with the specified parent \a layout.
51+
*
52+
* Ownership is transferred to the layout.
5153
*/
52-
QgsLayoutItemHtml( QgsLayout *layout );
54+
QgsLayoutItemHtml( QgsLayout *layout SIP_TRANSFERTHIS );
5355

5456
~QgsLayoutItemHtml();
5557

‎src/core/layout/qgslayoutitemtexttable.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ class CORE_EXPORT QgsLayoutItemTextTable : public QgsLayoutTable
3636

3737
/**
3838
* Constructor for QgsLayoutItemTextTable, for the specified \a layout.
39+
*
40+
* Ownership is transferred to the layout.
3941
*/
40-
QgsLayoutItemTextTable( QgsLayout *layout );
42+
QgsLayoutItemTextTable( QgsLayout *layout SIP_TRANSFERTHIS );
4143

4244
int type() const override;
4345
QString stringType() const override;

‎src/core/layout/qgslayoutmultiframe.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ void QgsLayoutMultiFrame::recalculateFrameSizes()
220220

221221
void QgsLayoutMultiFrame::recalculateFrameRects()
222222
{
223-
#if 0 //TODO
224223
if ( mFrameItems.empty() )
225224
{
226225
//no frames, nothing to do
@@ -230,10 +229,8 @@ void QgsLayoutMultiFrame::recalculateFrameRects()
230229
const QList< QgsLayoutFrame * > frames = mFrameItems;
231230
for ( QgsLayoutFrame *frame : frames )
232231
{
233-
frame->setSceneRect( QRectF( frame->scenePos().x(), frame->scenePos().y(),
234-
frame->rect().width(), frame->rect().height() ) );
232+
frame->refreshItemSize();
235233
}
236-
#endif
237234
}
238235

239236
QgsLayoutFrame *QgsLayoutMultiFrame::createNewFrame( QgsLayoutFrame *currentFrame, QPointF pos, QSizeF size )

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ SET(TESTS
147147
testqgslayoutpicture.cpp
148148
testqgslayoutscalebar.cpp
149149
testqgslayoutshapes.cpp
150+
testqgslayouttable.cpp
150151
testqgslayoutunits.cpp
151152
testqgslayoututils.cpp
152153
testqgslegendrenderer.cpp

‎tests/src/core/testqgslayoutitem.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,11 @@ void TestQgsLayoutItem::fixedSize()
10741074
QGSCOMPARENEAR( item->rect().width(), 2.0 * 25.4, 4 * DBL_EPSILON );
10751075
QGSCOMPARENEAR( item->rect().height(), 4.0 * 25.4, 4 * DBL_EPSILON );
10761076

1077+
item->attemptResize( QgsLayoutSize( 7.0, 8.0, QgsUnitTypes::LayoutInches ) );
1078+
//check size matches fixed item size converted to mm
1079+
QGSCOMPARENEAR( item->rect().width(), 2.0 * 25.4, 4 * DBL_EPSILON );
1080+
QGSCOMPARENEAR( item->rect().height(), 4.0 * 25.4, 4 * DBL_EPSILON );
1081+
10771082
//check that setting a fixed size applies this size immediately
10781083
item->updateFixedSize( QgsLayoutSize( 150, 250, QgsUnitTypes::LayoutMillimeters ) );
10791084
QGSCOMPARENEAR( item->rect().width(), 150.0, 4 * DBL_EPSILON );
@@ -1119,8 +1124,8 @@ void TestQgsLayoutItem::minSize()
11191124
//try to resize to less than minimum size
11201125
fixedMinItem->attemptResize( QgsLayoutSize( 1.0, 0.5, QgsUnitTypes::LayoutPoints ) );
11211126
//check size matches fixed item size, not minimum size (converted to mm)
1122-
QGSCOMPARENEAR( fixedMinItem->rect().width(), 50.0, 4 * DBL_EPSILON );
1123-
QGSCOMPARENEAR( fixedMinItem->rect().height(), 90.0, 4 * DBL_EPSILON );
1127+
QGSCOMPARENEAR( fixedMinItem->rect().width(), 20.0, 4 * DBL_EPSILON );
1128+
QGSCOMPARENEAR( fixedMinItem->rect().height(), 40.0, 4 * DBL_EPSILON );
11241129
}
11251130

11261131
void TestQgsLayoutItem::move()

0 commit comments

Comments
 (0)
Please sign in to comment.