[FEATURE][layouts] Add 'resize to square' action

Resizes all selected items so that they are square
This commit is contained in:
Nyall Dawson 2017-10-04 17:54:53 +10:00
parent 73077c4ef3
commit c022bc825e
7 changed files with 53 additions and 1 deletions

View File

@ -50,6 +50,7 @@ class QgsLayoutAligner
ResizeWidest,
ResizeShortest,
ResizeTallest,
ResizeToSquare,
};
static void alignItems( QgsLayout *layout, const QList< QgsLayoutItem * > &items, Alignment alignment );

View File

@ -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 );
@ -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 );

View File

@ -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
@ -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:
@ -211,6 +212,8 @@ void QgsLayoutAligner::resizeItems( QgsLayout *layout, const QList<QgsLayoutItem
case ResizeWidest:
newSize = std::max( size, newSize );
break;
case ResizeToSquare:
break;
}
}
@ -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
@ -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
}

View File

@ -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
};
/**

View File

@ -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"/>
@ -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"/>

View File

@ -228,6 +228,17 @@ class TestQgsLayoutAligner(unittest.TestCase):
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__':

View File

@ -626,6 +626,13 @@ class TestQgsLayoutView(unittest.TestCase):
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__':