Skip to content
Permalink
Browse files

Port group actions from composer

  • Loading branch information
nyalldawson committed Oct 9, 2017
1 parent 5aa9a15 commit 606ad21a51f6bea3d52e3ca74b0db006c244327f
@@ -27,6 +27,8 @@ class QgsLayoutItemGroup: QgsLayoutItem

virtual QString stringType() const;

virtual QString displayName() const;


void addItem( QgsLayoutItem *item /Transfer/ );
%Docstring
@@ -314,6 +314,18 @@ class QgsLayoutView: QGraphicsView
Deletes all selected items.
%End

void groupSelectedItems();
%Docstring
Groups all selected items.
.. seealso:: ungroupSelectedItems()
%End

void ungroupSelectedItems();
%Docstring
Ungroups all selected items.
.. seealso:: groupSelectedItems()
%End

void viewChanged();
%Docstring
Updates associated rulers and other widgets after view extent or zoom has changed.
@@ -372,6 +372,14 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
{
mView->deleteSelectedItems();
} );
connect( mActionGroupItems, &QAction::triggered, this, [ = ]
{
mView->groupSelectedItems();
} );
connect( mActionUngroupItems, &QAction::triggered, this, [ = ]
{
mView->ungroupSelectedItems();
} );

//create status bar labels
mStatusCursorXLabel = new QLabel( mStatusBar );
@@ -52,6 +52,16 @@ QString QgsLayoutItemGroup::stringType() const
return QStringLiteral( "ItemGroup" );
}

QString QgsLayoutItemGroup::displayName() const
{
//return id, if it's not empty
if ( !id().isEmpty() )
{
return id();
}
return tr( "<Group>" );
}

void QgsLayoutItemGroup::addItem( QgsLayoutItem *item )
{
if ( !item )
@@ -67,10 +77,6 @@ void QgsLayoutItemGroup::addItem( QgsLayoutItem *item )
mItems << QPointer< QgsLayoutItem >( item );
item->setParentGroup( this );

#if 0 //TODO - move to gui
item->setSelected( false );
#endif

updateBoundingRect();
}

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

item->setParentGroup( nullptr );

#if 0 //TODO - move to GUI
item->setSelected( true );
#endif
}
mItems.clear();
}
@@ -104,6 +106,8 @@ QList<QgsLayoutItem *> QgsLayoutItemGroup::items() const

void QgsLayoutItemGroup::setVisibility( const bool visible )
{
if ( mLayout )
mLayout->undoStack()->beginMacro( tr( "Set group visibility" ) );
//also set visibility for all items within the group
for ( QgsLayoutItem *item : qgsAsConst( mItems ) )
{
@@ -113,6 +117,8 @@ void QgsLayoutItemGroup::setVisibility( const bool visible )
}
//lastly set visibility for group item itself
QgsLayoutItem::setVisibility( visible );
if ( mLayout )
mLayout->undoStack()->endMacro();
}

void QgsLayoutItemGroup::draw( QgsRenderContext &, const QStyleOptionGraphicsItem * )
@@ -36,6 +36,7 @@ class CORE_EXPORT QgsLayoutItemGroup: public QgsLayoutItem

int type() const override;
QString stringType() const override;
QString displayName() const override;

/**
* Adds an \a item to the group. Ownership of the item
@@ -853,7 +853,7 @@ QgsLayoutItem *QgsLayoutModel::findItemAbove( QgsLayoutItem *item ) const
while ( it.hasPrevious() )
{
//now find previous item, since list is sorted from lowest->highest items
if ( it.hasPrevious() /* TODO && !it.peekPrevious()->isGroupMember() */ )
if ( it.hasPrevious() && !it.peekPrevious()->isGroupMember() )
{
return it.previous();
}
@@ -872,7 +872,7 @@ QgsLayoutItem *QgsLayoutModel::findItemBelow( QgsLayoutItem *item ) const
//return next item (list is sorted from lowest->highest items)
while ( it.hasNext() )
{
if ( true /* TODO !it.peekNext()->isGroupMember()*/ )
if ( !it.peekNext()->isGroupMember() )
{
return it.next();
}
@@ -30,6 +30,7 @@
#include "qgsapplication.h"
#include "qgslayoutitemundocommand.h"
#include "qgsproject.h"
#include "qgslayoutitemgroup.h"
#include <memory>
#include <QDesktopWidget>
#include <QMenu>
@@ -614,6 +615,60 @@ void QgsLayoutView::deleteSelectedItems()
#endif
}

void QgsLayoutView::groupSelectedItems()
{
if ( !currentLayout() )
{
return;
}

//group selected items
const QList<QgsLayoutItem *> selectionList = currentLayout()->selectedLayoutItems();
QgsLayoutItemGroup *itemGroup = currentLayout()->groupItems( selectionList );

if ( !itemGroup )
{
//group could not be created
return;
}

for ( QgsLayoutItem *item : selectionList )
{
item->setSelected( false );
}

currentLayout()->setSelectedItem( itemGroup );
}

void QgsLayoutView::ungroupSelectedItems()
{
if ( !currentLayout() )
{
return;
}

QList< QgsLayoutItem * > ungroupedItems;
//hunt through selection for any groups, and ungroup them
const QList<QgsLayoutItem *> selectionList = currentLayout()->selectedLayoutItems();
for ( QgsLayoutItem *item : selectionList )
{
if ( item->type() == QgsLayoutItemRegistry::LayoutGroup )
{
QgsLayoutItemGroup *itemGroup = static_cast<QgsLayoutItemGroup *>( item );
ungroupedItems.append( currentLayout()->ungroupItems( itemGroup ) );
}
}

if ( !ungroupedItems.empty() )
{
for ( QgsLayoutItem *item : qgsAsConst( ungroupedItems ) )
{
item->setSelected( true );
}
emit itemFocused( ungroupedItems.at( 0 ) );
}
}

void QgsLayoutView::mousePressEvent( QMouseEvent *event )
{
mSnapMarker->setVisible( false );
@@ -356,6 +356,18 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
*/
void deleteSelectedItems();

/**
* Groups all selected items.
* \see ungroupSelectedItems()
*/
void groupSelectedItems();

/**
* Ungroups all selected items.
* \see groupSelectedItems()
*/
void ungroupSelectedItems();

/**
* Updates associated rulers and other widgets after view extent or zoom has changed.
* This should be called after calling any of the QGraphicsView
@@ -207,6 +207,9 @@
<addaction name="separator"/>
<addaction name="mActionResizeToSquare"/>
</widget>
<addaction name="mActionGroupItems"/>
<addaction name="mActionUngroupItems"/>
<addaction name="separator"/>
<addaction name="mActionRaiseItems"/>
<addaction name="mActionLowerItems"/>
<addaction name="mActionMoveItemsToTop"/>
@@ -252,6 +255,8 @@
</attribute>
<addaction name="mActionLockItems"/>
<addaction name="mActionUnlockAll"/>
<addaction name="mActionGroupItems"/>
<addaction name="mActionUngroupItems"/>
</widget>
<action name="mActionClose">
<property name="text">
@@ -992,6 +997,36 @@
<string>Show pages</string>
</property>
</action>
<action name="mActionGroupItems">
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionGroupItems.svg</normaloff>:/images/themes/default/mActionGroupItems.svg</iconset>
</property>
<property name="text">
<string>&amp;Group</string>
</property>
<property name="toolTip">
<string>Group items</string>
</property>
<property name="shortcut">
<string>Ctrl+G</string>
</property>
</action>
<action name="mActionUngroupItems">
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionUngroupItems.svg</normaloff>:/images/themes/default/mActionUngroupItems.svg</iconset>
</property>
<property name="text">
<string>&amp;Ungroup</string>
</property>
<property name="toolTip">
<string>Ungroup items</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+G</string>
</property>
</action>
</widget>
<resources>
<include location="../../../images/images.qrc"/>

0 comments on commit 606ad21

Please sign in to comment.
You can’t perform that action at this time.