Skip to content

Commit 0665080

Browse files
committed
Fix resize of grouped items with non-top left reference points
1 parent e836fc4 commit 0665080

File tree

4 files changed

+152
-14
lines changed

4 files changed

+152
-14
lines changed

src/app/layout/qgslayoutguidewidget.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ QgsLayoutGuideWidget::QgsLayoutGuideWidget( QWidget *parent, QgsLayout *layout,
3939
mVertGuidesTableView->setEditTriggers( QAbstractItemView::AllEditTriggers );
4040

4141

42-
mHozGuidesTableView->setItemDelegateForColumn( 0, new QgsLayoutGuidePositionDelegate( mLayout, mHozProxyModel ) );
43-
mHozGuidesTableView->setItemDelegateForColumn( 1, new QgsLayoutGuideUnitDelegate( mLayout, mHozProxyModel ) );
42+
mHozGuidesTableView->setItemDelegateForColumn( 0, new QgsLayoutGuidePositionDelegate( mHozGuidesTableView, mLayout, mHozProxyModel ) );
43+
mHozGuidesTableView->setItemDelegateForColumn( 1, new QgsLayoutGuideUnitDelegate( mHozGuidesTableView, mLayout, mHozProxyModel ) );
4444

45-
mVertGuidesTableView->setItemDelegateForColumn( 0, new QgsLayoutGuidePositionDelegate( mLayout, mVertProxyModel ) );
46-
mVertGuidesTableView->setItemDelegateForColumn( 1, new QgsLayoutGuideUnitDelegate( mLayout, mVertProxyModel ) );
45+
mVertGuidesTableView->setItemDelegateForColumn( 0, new QgsLayoutGuidePositionDelegate( mVertGuidesTableView, mLayout, mVertProxyModel ) );
46+
mVertGuidesTableView->setItemDelegateForColumn( 1, new QgsLayoutGuideUnitDelegate( mVertGuidesTableView, mLayout, mVertProxyModel ) );
4747

4848
connect( mAddHozGuideButton, &QPushButton::clicked, this, &QgsLayoutGuideWidget::addHorizontalGuide );
4949
connect( mAddVertGuideButton, &QPushButton::clicked, this, &QgsLayoutGuideWidget::addVerticalGuide );
@@ -139,8 +139,9 @@ void QgsLayoutGuideWidget::applyToAll()
139139
}
140140

141141

142-
QgsLayoutGuidePositionDelegate::QgsLayoutGuidePositionDelegate( QgsLayout *layout, QAbstractItemModel *model )
143-
: mLayout( layout )
142+
QgsLayoutGuidePositionDelegate::QgsLayoutGuidePositionDelegate( QObject *parent, QgsLayout *layout, QAbstractItemModel *model )
143+
: QStyledItemDelegate( parent )
144+
, mLayout( layout )
144145
, mModel( model )
145146
{
146147

@@ -172,8 +173,9 @@ void QgsLayoutGuidePositionDelegate::setModelData( const QModelIndex &index, con
172173
mModel->setData( index, value, role );
173174
}
174175

175-
QgsLayoutGuideUnitDelegate::QgsLayoutGuideUnitDelegate( QgsLayout *layout, QAbstractItemModel *model )
176-
: mLayout( layout )
176+
QgsLayoutGuideUnitDelegate::QgsLayoutGuideUnitDelegate( QObject *parent, QgsLayout *layout, QAbstractItemModel *model )
177+
: QStyledItemDelegate( parent )
178+
, mLayout( layout )
177179
, mModel( model )
178180
{
179181

src/app/layout/qgslayoutguidewidget.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class QgsLayoutGuidePositionDelegate : public QStyledItemDelegate
6262

6363
public:
6464

65-
QgsLayoutGuidePositionDelegate( QgsLayout *layout, QAbstractItemModel *model );
65+
QgsLayoutGuidePositionDelegate( QObject *parent, QgsLayout *layout, QAbstractItemModel *model );
6666

6767
protected:
6868
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex &index ) const override;
@@ -82,7 +82,7 @@ class QgsLayoutGuideUnitDelegate : public QStyledItemDelegate
8282

8383
public:
8484

85-
QgsLayoutGuideUnitDelegate( QgsLayout *layout, QAbstractItemModel *model );
85+
QgsLayoutGuideUnitDelegate( QObject *parent, QgsLayout *layout, QAbstractItemModel *model );
8686

8787
protected:
8888
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex &index ) const override;

src/core/layout/qgslayoutitemgroup.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,13 @@ void QgsLayoutItemGroup::attemptResize( const QgsLayoutSize &size )
204204
itemRect = itemRect.normalized();
205205
QPointF newPos = mapToScene( itemRect.topLeft() );
206206

207-
// translate new position to current item units
208-
QgsLayoutPoint itemPos = mLayout->convertFromLayoutUnits( newPos, item->positionWithUnits().units() );
209-
item->attemptMove( itemPos );
210-
211207
QgsLayoutSize itemSize = mLayout->convertFromLayoutUnits( itemRect.size(), item->sizeWithUnits().units() );
212208
item->attemptResize( itemSize );
213209

210+
// translate new position to current item units
211+
QgsLayoutPoint itemPos = mLayout->convertFromLayoutUnits( newPos, item->positionWithUnits().units() );
212+
item->attemptMove( itemPos, false );
213+
214214
if ( command )
215215
{
216216
command->saveAfterState();

tests/src/core/testqgslayoutitemgroup.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class TestQgsLayoutItemGroup : public QObject
4848
void deleteGroup(); //test deleting group works
4949
void groupVisibility();
5050
void moveGroup();
51+
void moveGroupReferencePos();
5152
void resizeGroup();
53+
void resizeGroupReferencePos();
5254
void undoRedo(); //test that group/ungroup undo/redo commands don't crash
5355

5456
private:
@@ -354,6 +356,69 @@ void TestQgsLayoutItemGroup::moveGroup()
354356
QCOMPARE( item2->positionWithUnits().units(), QgsUnitTypes::LayoutInches );
355357
}
356358

359+
void TestQgsLayoutItemGroup::moveGroupReferencePos()
360+
{
361+
QgsProject proj;
362+
QgsLayout l( &proj );
363+
364+
QgsLayoutItemRectangularShape *item = new QgsLayoutItemRectangularShape( &l );
365+
l.addLayoutItem( item );
366+
item->attemptMove( QgsLayoutPoint( 5, 9 ) );
367+
item->attemptResize( QgsLayoutSize( 4, 7 ) );
368+
item->setReferencePoint( QgsLayoutItem::UpperRight );
369+
370+
QCOMPARE( item->positionWithUnits().x(), 9.0 );
371+
QCOMPARE( item->positionWithUnits().y(), 9.0 );
372+
QCOMPARE( item->scenePos().x(), 5.0 );
373+
QCOMPARE( item->scenePos().y(), 9.0 );
374+
375+
QgsLayoutItemRectangularShape *item2 = new QgsLayoutItemRectangularShape( &l );
376+
l.addLayoutItem( item2 );
377+
item2->attemptMove( QgsLayoutPoint( 15, 19 ) );
378+
item2->attemptResize( QgsLayoutSize( 6, 3 ) );
379+
item2->setReferencePoint( QgsLayoutItem::LowerLeft );
380+
381+
QCOMPARE( item2->positionWithUnits().x(), 15.0 );
382+
QCOMPARE( item2->positionWithUnits().y(), 22.0 );
383+
QCOMPARE( item2->scenePos().x(), 15.0 );
384+
QCOMPARE( item2->scenePos().y(), 19.0 );
385+
386+
//group items
387+
QList<QgsLayoutItem *> groupItems;
388+
groupItems << item << item2;
389+
QgsLayoutItemGroup *group = l.groupItems( groupItems );
390+
391+
QCOMPARE( group->positionWithUnits().x(), 5.0 );
392+
QCOMPARE( group->positionWithUnits().y(), 9.0 );
393+
QCOMPARE( group->positionWithUnits().units(), QgsUnitTypes::LayoutMillimeters );
394+
QCOMPARE( group->sizeWithUnits().width(), 16.0 );
395+
QCOMPARE( group->sizeWithUnits().height(), 13.0 );
396+
QCOMPARE( group->scenePos().x(), 5.0 );
397+
QCOMPARE( group->scenePos().y(), 9.0 );
398+
QCOMPARE( group->rect().width(), 16.0 );
399+
QCOMPARE( group->rect().height(), 13.0 );
400+
401+
group->attemptMove( QgsLayoutPoint( 2, 4 ) );
402+
QCOMPARE( group->positionWithUnits().x(), 2.0 );
403+
QCOMPARE( group->positionWithUnits().y(), 4.0 );
404+
QCOMPARE( group->scenePos().x(), 2.0 );
405+
QCOMPARE( group->scenePos().y(), 4.0 );
406+
QCOMPARE( group->sizeWithUnits().width(), 16.0 );
407+
QCOMPARE( group->sizeWithUnits().height(), 13.0 );
408+
QCOMPARE( group->rect().width(), 16.0 );
409+
QCOMPARE( group->rect().height(), 13.0 );
410+
411+
QCOMPARE( item->pos().x(), 2.0 );
412+
QCOMPARE( item->pos().y(), 4.0 );
413+
QCOMPARE( item->positionWithUnits().x(), 6.0 );
414+
QCOMPARE( item->positionWithUnits().y(), 4.0 );
415+
416+
QCOMPARE( item2->pos().x(), 12.0 );
417+
QCOMPARE( item2->pos().y(), 14.0 );
418+
QCOMPARE( item2->positionWithUnits().x(), 12.0 );
419+
QCOMPARE( item2->positionWithUnits().y(), 17.0 );
420+
}
421+
357422
void TestQgsLayoutItemGroup::resizeGroup()
358423
{
359424
QgsProject proj;
@@ -402,6 +467,77 @@ void TestQgsLayoutItemGroup::resizeGroup()
402467
QCOMPARE( item2->sizeWithUnits().units(), QgsUnitTypes::LayoutInches );
403468
}
404469

470+
void TestQgsLayoutItemGroup::resizeGroupReferencePos()
471+
{
472+
QgsProject proj;
473+
QgsLayout l( &proj );
474+
475+
QgsLayoutItemRectangularShape *item = new QgsLayoutItemRectangularShape( &l );
476+
l.addLayoutItem( item );
477+
item->attemptMove( QgsLayoutPoint( 5, 9 ) );
478+
item->attemptResize( QgsLayoutSize( 4, 7 ) );
479+
item->setReferencePoint( QgsLayoutItem::UpperRight );
480+
481+
QCOMPARE( item->positionWithUnits().x(), 9.0 );
482+
QCOMPARE( item->positionWithUnits().y(), 9.0 );
483+
QCOMPARE( item->scenePos().x(), 5.0 );
484+
QCOMPARE( item->scenePos().y(), 9.0 );
485+
486+
QgsLayoutItemRectangularShape *item2 = new QgsLayoutItemRectangularShape( &l );
487+
l.addLayoutItem( item2 );
488+
item2->attemptMove( QgsLayoutPoint( 15, 19 ) );
489+
item2->attemptResize( QgsLayoutSize( 6, 3 ) );
490+
item2->setReferencePoint( QgsLayoutItem::LowerLeft );
491+
492+
QCOMPARE( item2->positionWithUnits().x(), 15.0 );
493+
QCOMPARE( item2->positionWithUnits().y(), 22.0 );
494+
QCOMPARE( item2->scenePos().x(), 15.0 );
495+
QCOMPARE( item2->scenePos().y(), 19.0 );
496+
497+
//group items
498+
QList<QgsLayoutItem *> groupItems;
499+
groupItems << item << item2;
500+
QgsLayoutItemGroup *group = l.groupItems( groupItems );
501+
502+
QCOMPARE( group->positionWithUnits().x(), 5.0 );
503+
QCOMPARE( group->positionWithUnits().y(), 9.0 );
504+
QCOMPARE( group->positionWithUnits().units(), QgsUnitTypes::LayoutMillimeters );
505+
QCOMPARE( group->sizeWithUnits().width(), 16.0 );
506+
QCOMPARE( group->sizeWithUnits().height(), 13.0 );
507+
QCOMPARE( group->scenePos().x(), 5.0 );
508+
QCOMPARE( group->scenePos().y(), 9.0 );
509+
QCOMPARE( group->rect().width(), 16.0 );
510+
QCOMPARE( group->rect().height(), 13.0 );
511+
512+
group->attemptResize( QgsLayoutSize( 32.0, 26.0 ) );
513+
QCOMPARE( group->positionWithUnits().x(), 5.0 );
514+
QCOMPARE( group->positionWithUnits().y(), 9.0 );
515+
QCOMPARE( group->scenePos().x(), 5.0 );
516+
QCOMPARE( group->scenePos().y(), 9.0 );
517+
QCOMPARE( group->sizeWithUnits().width(), 32.0 );
518+
QCOMPARE( group->sizeWithUnits().height(), 26.0 );
519+
QCOMPARE( group->rect().width(), 32.0 );
520+
QCOMPARE( group->rect().height(), 26.0 );
521+
522+
QCOMPARE( item->pos().x(), 5.0 );
523+
QCOMPARE( item->pos().y(), 9.0 );
524+
QCOMPARE( item->positionWithUnits().x(), 13.0 );
525+
QCOMPARE( item->positionWithUnits().y(), 9.0 );
526+
QCOMPARE( item->sizeWithUnits().width(), 8.0 );
527+
QCOMPARE( item->sizeWithUnits().height(), 14.0 );
528+
QCOMPARE( item->rect().width(), 8.0 );
529+
QCOMPARE( item->rect().height(), 14.0 );
530+
531+
QCOMPARE( item2->pos().x(), 25.0 );
532+
QCOMPARE( item2->pos().y(), 29.0 );
533+
QCOMPARE( item2->positionWithUnits().x(), 25.0 );
534+
QCOMPARE( item2->positionWithUnits().y(), 35.0 );
535+
QCOMPARE( item2->sizeWithUnits().width(), 12.0 );
536+
QCOMPARE( item2->sizeWithUnits().height(), 6.0 );
537+
QCOMPARE( item2->rect().width(), 12.0 );
538+
QCOMPARE( item2->rect().height(), 6.0 );
539+
}
540+
405541
Q_DECLARE_METATYPE( QgsLayoutItemGroup * )
406542
Q_DECLARE_METATYPE( QgsLayoutItemRectangularShape * )
407543
Q_DECLARE_METATYPE( QgsLayoutItem * )

0 commit comments

Comments
 (0)