Skip to content

Commit

Permalink
[FEATURE][layouts] Add 'resize to square' action
Browse files Browse the repository at this point in the history
Resizes all selected items so that they are square
  • Loading branch information
nyalldawson committed Oct 6, 2017
1 parent 73077c4 commit c022bc8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions python/core/layout/qgslayoutaligner.sip
Expand Up @@ -50,6 +50,7 @@ class QgsLayoutAligner
ResizeWidest,
ResizeShortest,
ResizeTallest,
ResizeToSquare,
};

static void alignItems( QgsLayout *layout, const QList< QgsLayoutItem * > &items, Alignment alignment );
Expand Down
5 changes: 5 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -213,6 +213,7 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
resizeToolButton->addAction( mActionResizeWidest );
resizeToolButton->addAction( mActionResizeShortest );
resizeToolButton->addAction( mActionResizeTallest );
resizeToolButton->addAction( mActionResizeToSquare );
resizeToolButton->setDefaultAction( mActionResizeNarrowest );
mActionsToolbar->addWidget( resizeToolButton );

Expand Down Expand Up @@ -318,6 +319,10 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
{
mView->resizeSelectedItems( QgsLayoutAligner::ResizeTallest );
} );
connect( mActionResizeToSquare, &QAction::triggered, this, [ = ]
{
mView->resizeSelectedItems( QgsLayoutAligner::ResizeToSquare );
} );

connect( mActionAddPages, &QAction::triggered, this, &QgsLayoutDesignerDialog::addPages );

Expand Down
15 changes: 14 additions & 1 deletion src/core/layout/qgslayoutaligner.cpp
Expand Up @@ -178,7 +178,7 @@ void QgsLayoutAligner::distributeItems( QgsLayout *layout, const QList<QgsLayout

void QgsLayoutAligner::resizeItems( QgsLayout *layout, const QList<QgsLayoutItem *> &items, QgsLayoutAligner::Resize resize )
{
if ( items.size() < 2 )
if ( !( items.size() >= 2 || ( items.size() == 1 && resize == ResizeToSquare ) ) )
return;

auto collectSize = [resize]( QgsLayoutItem * item )->double
Expand All @@ -188,6 +188,7 @@ void QgsLayoutAligner::resizeItems( QgsLayout *layout, const QList<QgsLayoutItem
{
case ResizeNarrowest:
case ResizeWidest:
case ResizeToSquare:
return itemBBox.width();
case ResizeShortest:
case ResizeTallest:
Expand All @@ -211,6 +212,8 @@ void QgsLayoutAligner::resizeItems( QgsLayout *layout, const QList<QgsLayoutItem
case ResizeWidest:
newSize = std::max( size, newSize );
break;
case ResizeToSquare:
break;
}
}

Expand All @@ -227,6 +230,14 @@ void QgsLayoutAligner::resizeItems( QgsLayout *layout, const QList<QgsLayoutItem
case ResizeShortest:
newSize.setHeight( size );
break;
case ResizeToSquare:
{
if ( newSize.width() > newSize.height() )
newSize.setHeight( newSize.width() );
else
newSize.setWidth( newSize.height() );
break;
}
}

// need to keep item units
Expand Down Expand Up @@ -315,6 +326,8 @@ QString QgsLayoutAligner::undoText( QgsLayoutAligner::Resize resize )
return QObject::tr( "Resized items to shortest" );
case ResizeTallest:
return QObject::tr( "Resized items to tallest" );
case ResizeToSquare:
return QObject::tr( "Resized items to square" );
}
return QString(); //no warnings
}
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutaligner.h
Expand Up @@ -67,6 +67,7 @@ class CORE_EXPORT QgsLayoutAligner
ResizeWidest, //!< Resize width to match widest width
ResizeShortest, //!< Resize height to match shortest height
ResizeTallest, //!< Resize height to match tallest height
ResizeToSquare, //!< Resize items to square
};

/**
Expand Down
14 changes: 14 additions & 0 deletions src/ui/layout/qgslayoutdesignerbase.ui
Expand Up @@ -191,6 +191,8 @@
<addaction name="separator"/>
<addaction name="mActionResizeShortest"/>
<addaction name="mActionResizeTallest"/>
<addaction name="separator"/>
<addaction name="mActionResizeToSquare"/>
</widget>
<addaction name="mActionRaiseItems"/>
<addaction name="mActionLowerItems"/>
Expand Down Expand Up @@ -911,6 +913,18 @@
<string>Del</string>
</property>
</action>
<action name="mActionResizeToSquare">
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionResizeTallest.svg</normaloff>:/images/themes/default/mActionResizeTallest.svg</iconset>
</property>
<property name="text">
<string>To S&amp;quare</string>
</property>
<property name="toolTip">
<string>Resizes items to squares</string>
</property>
</action>
</widget>
<resources>
<include location="../../../images/images.qrc"/>
Expand Down
11 changes: 11 additions & 0 deletions tests/src/python/test_qgslayoutaligner.py
Expand Up @@ -228,6 +228,17 @@ def testResize(self):
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 16, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 16, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters))
l.undoStack().stack().undo()

item2.attemptResize(QgsLayoutSize(10, 19, QgsUnitTypes.LayoutMillimeters))
QgsLayoutAligner.resizeItems(l, [item1, item2, item3], QgsLayoutAligner.ResizeToSquare)
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 18, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(19, 19, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.8, QgsUnitTypes.LayoutCentimeters))

l.undoStack().stack().undo()
QgsLayoutAligner.resizeItems(l, [item1], QgsLayoutAligner.ResizeToSquare)
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 18, QgsUnitTypes.LayoutMillimeters))


if __name__ == '__main__':
Expand Down
7 changes: 7 additions & 0 deletions tests/src/python/test_qgslayoutview.py
Expand Up @@ -626,6 +626,13 @@ def testResize(self):
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 16, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 16, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters))
l.undoStack().stack().undo()

item2.attemptResize(QgsLayoutSize(10, 19, QgsUnitTypes.LayoutMillimeters))
view.resizeSelectedItems(QgsLayoutAligner.ResizeToSquare)
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 18, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(19, 19, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.8, QgsUnitTypes.LayoutCentimeters))


if __name__ == '__main__':
Expand Down

0 comments on commit c022bc8

Please sign in to comment.