Skip to content

Commit c022bc8

Browse files
committed
[FEATURE][layouts] Add 'resize to square' action
Resizes all selected items so that they are square
1 parent 73077c4 commit c022bc8

File tree

7 files changed

+53
-1
lines changed

7 files changed

+53
-1
lines changed

python/core/layout/qgslayoutaligner.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class QgsLayoutAligner
5050
ResizeWidest,
5151
ResizeShortest,
5252
ResizeTallest,
53+
ResizeToSquare,
5354
};
5455

5556
static void alignItems( QgsLayout *layout, const QList< QgsLayoutItem * > &items, Alignment alignment );

src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
213213
resizeToolButton->addAction( mActionResizeWidest );
214214
resizeToolButton->addAction( mActionResizeShortest );
215215
resizeToolButton->addAction( mActionResizeTallest );
216+
resizeToolButton->addAction( mActionResizeToSquare );
216217
resizeToolButton->setDefaultAction( mActionResizeNarrowest );
217218
mActionsToolbar->addWidget( resizeToolButton );
218219

@@ -318,6 +319,10 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
318319
{
319320
mView->resizeSelectedItems( QgsLayoutAligner::ResizeTallest );
320321
} );
322+
connect( mActionResizeToSquare, &QAction::triggered, this, [ = ]
323+
{
324+
mView->resizeSelectedItems( QgsLayoutAligner::ResizeToSquare );
325+
} );
321326

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

src/core/layout/qgslayoutaligner.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ void QgsLayoutAligner::distributeItems( QgsLayout *layout, const QList<QgsLayout
178178

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

184184
auto collectSize = [resize]( QgsLayoutItem * item )->double
@@ -188,6 +188,7 @@ void QgsLayoutAligner::resizeItems( QgsLayout *layout, const QList<QgsLayoutItem
188188
{
189189
case ResizeNarrowest:
190190
case ResizeWidest:
191+
case ResizeToSquare:
191192
return itemBBox.width();
192193
case ResizeShortest:
193194
case ResizeTallest:
@@ -211,6 +212,8 @@ void QgsLayoutAligner::resizeItems( QgsLayout *layout, const QList<QgsLayoutItem
211212
case ResizeWidest:
212213
newSize = std::max( size, newSize );
213214
break;
215+
case ResizeToSquare:
216+
break;
214217
}
215218
}
216219

@@ -227,6 +230,14 @@ void QgsLayoutAligner::resizeItems( QgsLayout *layout, const QList<QgsLayoutItem
227230
case ResizeShortest:
228231
newSize.setHeight( size );
229232
break;
233+
case ResizeToSquare:
234+
{
235+
if ( newSize.width() > newSize.height() )
236+
newSize.setHeight( newSize.width() );
237+
else
238+
newSize.setWidth( newSize.height() );
239+
break;
240+
}
230241
}
231242

232243
// need to keep item units
@@ -315,6 +326,8 @@ QString QgsLayoutAligner::undoText( QgsLayoutAligner::Resize resize )
315326
return QObject::tr( "Resized items to shortest" );
316327
case ResizeTallest:
317328
return QObject::tr( "Resized items to tallest" );
329+
case ResizeToSquare:
330+
return QObject::tr( "Resized items to square" );
318331
}
319332
return QString(); //no warnings
320333
}

src/core/layout/qgslayoutaligner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class CORE_EXPORT QgsLayoutAligner
6767
ResizeWidest, //!< Resize width to match widest width
6868
ResizeShortest, //!< Resize height to match shortest height
6969
ResizeTallest, //!< Resize height to match tallest height
70+
ResizeToSquare, //!< Resize items to square
7071
};
7172

7273
/**

src/ui/layout/qgslayoutdesignerbase.ui

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@
191191
<addaction name="separator"/>
192192
<addaction name="mActionResizeShortest"/>
193193
<addaction name="mActionResizeTallest"/>
194+
<addaction name="separator"/>
195+
<addaction name="mActionResizeToSquare"/>
194196
</widget>
195197
<addaction name="mActionRaiseItems"/>
196198
<addaction name="mActionLowerItems"/>
@@ -911,6 +913,18 @@
911913
<string>Del</string>
912914
</property>
913915
</action>
916+
<action name="mActionResizeToSquare">
917+
<property name="icon">
918+
<iconset resource="../../../images/images.qrc">
919+
<normaloff>:/images/themes/default/mActionResizeTallest.svg</normaloff>:/images/themes/default/mActionResizeTallest.svg</iconset>
920+
</property>
921+
<property name="text">
922+
<string>To S&amp;quare</string>
923+
</property>
924+
<property name="toolTip">
925+
<string>Resizes items to squares</string>
926+
</property>
927+
</action>
914928
</widget>
915929
<resources>
916930
<include location="../../../images/images.qrc"/>

tests/src/python/test_qgslayoutaligner.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,17 @@ def testResize(self):
228228
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 16, QgsUnitTypes.LayoutMillimeters))
229229
self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 16, QgsUnitTypes.LayoutMillimeters))
230230
self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters))
231+
l.undoStack().stack().undo()
232+
233+
item2.attemptResize(QgsLayoutSize(10, 19, QgsUnitTypes.LayoutMillimeters))
234+
QgsLayoutAligner.resizeItems(l, [item1, item2, item3], QgsLayoutAligner.ResizeToSquare)
235+
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 18, QgsUnitTypes.LayoutMillimeters))
236+
self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(19, 19, QgsUnitTypes.LayoutMillimeters))
237+
self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.8, QgsUnitTypes.LayoutCentimeters))
238+
239+
l.undoStack().stack().undo()
240+
QgsLayoutAligner.resizeItems(l, [item1], QgsLayoutAligner.ResizeToSquare)
241+
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 18, QgsUnitTypes.LayoutMillimeters))
231242

232243

233244
if __name__ == '__main__':

tests/src/python/test_qgslayoutview.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,13 @@ def testResize(self):
626626
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 16, QgsUnitTypes.LayoutMillimeters))
627627
self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 16, QgsUnitTypes.LayoutMillimeters))
628628
self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters))
629+
l.undoStack().stack().undo()
630+
631+
item2.attemptResize(QgsLayoutSize(10, 19, QgsUnitTypes.LayoutMillimeters))
632+
view.resizeSelectedItems(QgsLayoutAligner.ResizeToSquare)
633+
self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 18, QgsUnitTypes.LayoutMillimeters))
634+
self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(19, 19, QgsUnitTypes.LayoutMillimeters))
635+
self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.8, QgsUnitTypes.LayoutCentimeters))
629636

630637

631638
if __name__ == '__main__':

0 commit comments

Comments
 (0)