30#include <QGraphicsView>
31#include <QGraphicsSceneHoverEvent>
39 : QgsGraphicsViewMouseHandles( view )
44 connect( mLayout, &QGraphicsScene::selectionChanged,
this, &QgsLayoutMouseHandles::selectionChanged );
46 mHorizontalSnapLine = mView->createSnapLine();
47 mHorizontalSnapLine->hide();
48 layout->addItem( mHorizontalSnapLine );
49 mVerticalSnapLine = mView->createSnapLine();
50 mVerticalSnapLine->hide();
51 layout->addItem( mVerticalSnapLine );
54void QgsLayoutMouseHandles::paint( QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget )
56 paintInternal( painter, mLayout->renderContext().isPreviewRender(),
57 mLayout->renderContext().boundingBoxesVisible(),
true, option, widget );
60void QgsLayoutMouseHandles::selectionChanged()
63 const QList<QGraphicsItem *> itemList = layout()->items();
64 for ( QGraphicsItem *graphicsItem : itemList )
70 if ( item->isSelected() )
90void QgsLayoutMouseHandles::setViewportCursor( Qt::CursorShape cursor )
95 if ( qobject_cast< QgsLayoutViewToolSelect *>( mView->tool() ) )
97 mView->viewport()->setCursor( cursor );
101QList<QGraphicsItem *> QgsLayoutMouseHandles::sceneItemsAtPoint( QPointF scenePoint )
103 QList< QGraphicsItem * > items = mLayout->items( scenePoint );
104 items.erase( std::remove_if( items.begin(), items.end(), []( QGraphicsItem * item )
106 return !dynamic_cast<QgsLayoutItem *>( item );
112QList<QGraphicsItem *> QgsLayoutMouseHandles::selectedSceneItems(
bool includeLockedItems )
const
114 QList<QGraphicsItem *> res;
115 const QList<QgsLayoutItem *> layoutItems = mLayout->selectedLayoutItems( includeLockedItems );
116 res.reserve( layoutItems.size() );
122bool QgsLayoutMouseHandles::itemIsLocked( QGraphicsItem *item )
130bool QgsLayoutMouseHandles::itemIsGroupMember( QGraphicsItem *item )
133 return layoutItem->isGroupMember();
138QRectF QgsLayoutMouseHandles::itemRect( QGraphicsItem *item )
const
141 return layoutItem->rectWithFrame();
146QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode,
bool snapHorizontal,
bool snapVertical )
148 bool snapped =
false;
150 const QList< QGraphicsItem * > selectedItems = selectedSceneItems();
151 QList< QGraphicsItem * > itemsToExclude;
152 expandItemList( selectedItems, itemsToExclude );
154 QList< QgsLayoutItem * > layoutItemsToExclude;
155 for ( QGraphicsItem *item : itemsToExclude )
156 layoutItemsToExclude << dynamic_cast< QgsLayoutItem * >( item );
159 QPointF snappedPoint;
163 snappedPoint = mLayout->snapper().snapRect( rect().translated( originalPoint ), mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr,
164 snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude ).topLeft();
167 snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr,
168 snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude );
172 return snapped ? snappedPoint : originalPoint;
175void QgsLayoutMouseHandles::createItemCommand( QGraphicsItem *item )
177 mItemCommand.reset( qgis::down_cast< QgsLayoutItem * >( item )->createCommand( QString(), 0 ) );
178 mItemCommand->saveBeforeState();
181void QgsLayoutMouseHandles::endItemCommand( QGraphicsItem * )
183 mItemCommand->saveAfterState();
184 mLayout->undoStack()->push( mItemCommand.release() );
187void QgsLayoutMouseHandles::startMacroCommand(
const QString &text )
189 mLayout->undoStack()->beginMacro( text );
193void QgsLayoutMouseHandles::endMacroCommand()
195 mLayout->undoStack()->endMacro();
198void QgsLayoutMouseHandles::hideAlignItems()
200 mHorizontalSnapLine->hide();
201 mVerticalSnapLine->hide();
204void QgsLayoutMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
206 for ( QGraphicsItem *item : items )
211 const QList<QgsLayoutItem *> groupItems =
static_cast< QgsLayoutItemGroup *
>( item )->items();
212 collected.reserve( collected.size() + groupItems.size() );
214 collected.append( groupItem );
223void QgsLayoutMouseHandles::moveItem( QGraphicsItem *item,
double deltaX,
double deltaY )
225 qgis::down_cast< QgsLayoutItem * >( item )->
attemptMoveBy( deltaX, deltaY );
228void QgsLayoutMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
234void QgsLayoutMouseHandles::showStatusMessage(
const QString &message )
239 mView->pushStatusMessage( message );
A container for grouping several QgsLayoutItems.
@ LayoutGroup
Grouped item.
Base class for graphical items within a QgsLayout.
void rotationChanged(double newRotation)
Emitted on item rotation change.
bool isLocked() const
Returns true if the item is locked, and cannot be interacted with using the mouse.
void sizePositionChanged()
Emitted when the item's size or position changes.
void lockChanged()
Emitted if the item's lock status changes.
void frameChanged()
Emitted if the item's frame style changes.
void attemptMoveBy(double deltaX, double deltaY)
Attempts to shift the item's position by a specified deltaX and deltaY, in layout units.
void attemptSetSceneRect(const QRectF &rect, bool includesFrame=false)
Attempts to update the item's position and size to match the passed rect in layout coordinates.
A graphical widget to display and interact with QgsLayouts.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.