Skip to content

Commit 606ad21

Browse files
committed
Port group actions from composer
1 parent 5aa9a15 commit 606ad21

File tree

9 files changed

+141
-10
lines changed

9 files changed

+141
-10
lines changed

python/core/layout/qgslayoutitemgroup.sip

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class QgsLayoutItemGroup: QgsLayoutItem
2727

2828
virtual QString stringType() const;
2929

30+
virtual QString displayName() const;
31+
3032

3133
void addItem( QgsLayoutItem *item /Transfer/ );
3234
%Docstring

python/gui/layout/qgslayoutview.sip

+12
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ class QgsLayoutView: QGraphicsView
314314
Deletes all selected items.
315315
%End
316316

317+
void groupSelectedItems();
318+
%Docstring
319+
Groups all selected items.
320+
.. seealso:: ungroupSelectedItems()
321+
%End
322+
323+
void ungroupSelectedItems();
324+
%Docstring
325+
Ungroups all selected items.
326+
.. seealso:: groupSelectedItems()
327+
%End
328+
317329
void viewChanged();
318330
%Docstring
319331
Updates associated rulers and other widgets after view extent or zoom has changed.

src/app/layout/qgslayoutdesignerdialog.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
372372
{
373373
mView->deleteSelectedItems();
374374
} );
375+
connect( mActionGroupItems, &QAction::triggered, this, [ = ]
376+
{
377+
mView->groupSelectedItems();
378+
} );
379+
connect( mActionUngroupItems, &QAction::triggered, this, [ = ]
380+
{
381+
mView->ungroupSelectedItems();
382+
} );
375383

376384
//create status bar labels
377385
mStatusCursorXLabel = new QLabel( mStatusBar );

src/core/layout/qgslayoutitemgroup.cpp

+14-8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ QString QgsLayoutItemGroup::stringType() const
5252
return QStringLiteral( "ItemGroup" );
5353
}
5454

55+
QString QgsLayoutItemGroup::displayName() const
56+
{
57+
//return id, if it's not empty
58+
if ( !id().isEmpty() )
59+
{
60+
return id();
61+
}
62+
return tr( "<Group>" );
63+
}
64+
5565
void QgsLayoutItemGroup::addItem( QgsLayoutItem *item )
5666
{
5767
if ( !item )
@@ -67,10 +77,6 @@ void QgsLayoutItemGroup::addItem( QgsLayoutItem *item )
6777
mItems << QPointer< QgsLayoutItem >( item );
6878
item->setParentGroup( this );
6979

70-
#if 0 //TODO - move to gui
71-
item->setSelected( false );
72-
#endif
73-
7480
updateBoundingRect();
7581
}
7682

@@ -82,10 +88,6 @@ void QgsLayoutItemGroup::removeItems()
8288
continue;
8389

8490
item->setParentGroup( nullptr );
85-
86-
#if 0 //TODO - move to GUI
87-
item->setSelected( true );
88-
#endif
8991
}
9092
mItems.clear();
9193
}
@@ -104,6 +106,8 @@ QList<QgsLayoutItem *> QgsLayoutItemGroup::items() const
104106

105107
void QgsLayoutItemGroup::setVisibility( const bool visible )
106108
{
109+
if ( mLayout )
110+
mLayout->undoStack()->beginMacro( tr( "Set group visibility" ) );
107111
//also set visibility for all items within the group
108112
for ( QgsLayoutItem *item : qgsAsConst( mItems ) )
109113
{
@@ -113,6 +117,8 @@ void QgsLayoutItemGroup::setVisibility( const bool visible )
113117
}
114118
//lastly set visibility for group item itself
115119
QgsLayoutItem::setVisibility( visible );
120+
if ( mLayout )
121+
mLayout->undoStack()->endMacro();
116122
}
117123

118124
void QgsLayoutItemGroup::draw( QgsRenderContext &, const QStyleOptionGraphicsItem * )

src/core/layout/qgslayoutitemgroup.h

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CORE_EXPORT QgsLayoutItemGroup: public QgsLayoutItem
3636

3737
int type() const override;
3838
QString stringType() const override;
39+
QString displayName() const override;
3940

4041
/**
4142
* Adds an \a item to the group. Ownership of the item

src/core/layout/qgslayoutmodel.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ QgsLayoutItem *QgsLayoutModel::findItemAbove( QgsLayoutItem *item ) const
853853
while ( it.hasPrevious() )
854854
{
855855
//now find previous item, since list is sorted from lowest->highest items
856-
if ( it.hasPrevious() /* TODO && !it.peekPrevious()->isGroupMember() */ )
856+
if ( it.hasPrevious() && !it.peekPrevious()->isGroupMember() )
857857
{
858858
return it.previous();
859859
}
@@ -872,7 +872,7 @@ QgsLayoutItem *QgsLayoutModel::findItemBelow( QgsLayoutItem *item ) const
872872
//return next item (list is sorted from lowest->highest items)
873873
while ( it.hasNext() )
874874
{
875-
if ( true /* TODO !it.peekNext()->isGroupMember()*/ )
875+
if ( !it.peekNext()->isGroupMember() )
876876
{
877877
return it.next();
878878
}

src/gui/layout/qgslayoutview.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "qgsapplication.h"
3131
#include "qgslayoutitemundocommand.h"
3232
#include "qgsproject.h"
33+
#include "qgslayoutitemgroup.h"
3334
#include <memory>
3435
#include <QDesktopWidget>
3536
#include <QMenu>
@@ -614,6 +615,60 @@ void QgsLayoutView::deleteSelectedItems()
614615
#endif
615616
}
616617

618+
void QgsLayoutView::groupSelectedItems()
619+
{
620+
if ( !currentLayout() )
621+
{
622+
return;
623+
}
624+
625+
//group selected items
626+
const QList<QgsLayoutItem *> selectionList = currentLayout()->selectedLayoutItems();
627+
QgsLayoutItemGroup *itemGroup = currentLayout()->groupItems( selectionList );
628+
629+
if ( !itemGroup )
630+
{
631+
//group could not be created
632+
return;
633+
}
634+
635+
for ( QgsLayoutItem *item : selectionList )
636+
{
637+
item->setSelected( false );
638+
}
639+
640+
currentLayout()->setSelectedItem( itemGroup );
641+
}
642+
643+
void QgsLayoutView::ungroupSelectedItems()
644+
{
645+
if ( !currentLayout() )
646+
{
647+
return;
648+
}
649+
650+
QList< QgsLayoutItem * > ungroupedItems;
651+
//hunt through selection for any groups, and ungroup them
652+
const QList<QgsLayoutItem *> selectionList = currentLayout()->selectedLayoutItems();
653+
for ( QgsLayoutItem *item : selectionList )
654+
{
655+
if ( item->type() == QgsLayoutItemRegistry::LayoutGroup )
656+
{
657+
QgsLayoutItemGroup *itemGroup = static_cast<QgsLayoutItemGroup *>( item );
658+
ungroupedItems.append( currentLayout()->ungroupItems( itemGroup ) );
659+
}
660+
}
661+
662+
if ( !ungroupedItems.empty() )
663+
{
664+
for ( QgsLayoutItem *item : qgsAsConst( ungroupedItems ) )
665+
{
666+
item->setSelected( true );
667+
}
668+
emit itemFocused( ungroupedItems.at( 0 ) );
669+
}
670+
}
671+
617672
void QgsLayoutView::mousePressEvent( QMouseEvent *event )
618673
{
619674
mSnapMarker->setVisible( false );

src/gui/layout/qgslayoutview.h

+12
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,18 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
356356
*/
357357
void deleteSelectedItems();
358358

359+
/**
360+
* Groups all selected items.
361+
* \see ungroupSelectedItems()
362+
*/
363+
void groupSelectedItems();
364+
365+
/**
366+
* Ungroups all selected items.
367+
* \see groupSelectedItems()
368+
*/
369+
void ungroupSelectedItems();
370+
359371
/**
360372
* Updates associated rulers and other widgets after view extent or zoom has changed.
361373
* This should be called after calling any of the QGraphicsView

src/ui/layout/qgslayoutdesignerbase.ui

+35
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@
207207
<addaction name="separator"/>
208208
<addaction name="mActionResizeToSquare"/>
209209
</widget>
210+
<addaction name="mActionGroupItems"/>
211+
<addaction name="mActionUngroupItems"/>
212+
<addaction name="separator"/>
210213
<addaction name="mActionRaiseItems"/>
211214
<addaction name="mActionLowerItems"/>
212215
<addaction name="mActionMoveItemsToTop"/>
@@ -252,6 +255,8 @@
252255
</attribute>
253256
<addaction name="mActionLockItems"/>
254257
<addaction name="mActionUnlockAll"/>
258+
<addaction name="mActionGroupItems"/>
259+
<addaction name="mActionUngroupItems"/>
255260
</widget>
256261
<action name="mActionClose">
257262
<property name="text">
@@ -992,6 +997,36 @@
992997
<string>Show pages</string>
993998
</property>
994999
</action>
1000+
<action name="mActionGroupItems">
1001+
<property name="icon">
1002+
<iconset resource="../../../images/images.qrc">
1003+
<normaloff>:/images/themes/default/mActionGroupItems.svg</normaloff>:/images/themes/default/mActionGroupItems.svg</iconset>
1004+
</property>
1005+
<property name="text">
1006+
<string>&amp;Group</string>
1007+
</property>
1008+
<property name="toolTip">
1009+
<string>Group items</string>
1010+
</property>
1011+
<property name="shortcut">
1012+
<string>Ctrl+G</string>
1013+
</property>
1014+
</action>
1015+
<action name="mActionUngroupItems">
1016+
<property name="icon">
1017+
<iconset resource="../../../images/images.qrc">
1018+
<normaloff>:/images/themes/default/mActionUngroupItems.svg</normaloff>:/images/themes/default/mActionUngroupItems.svg</iconset>
1019+
</property>
1020+
<property name="text">
1021+
<string>&amp;Ungroup</string>
1022+
</property>
1023+
<property name="toolTip">
1024+
<string>Ungroup items</string>
1025+
</property>
1026+
<property name="shortcut">
1027+
<string>Ctrl+Shift+G</string>
1028+
</property>
1029+
</action>
9951030
</widget>
9961031
<resources>
9971032
<include location="../../../images/images.qrc"/>

0 commit comments

Comments
 (0)