29#include <QGraphicsView>
30#include <QGraphicsSceneHoverEvent>
38 : QgsGraphicsViewMouseHandles( view )
43 connect( mLayout, &QGraphicsScene::selectionChanged,
this, &QgsLayoutMouseHandles::selectionChanged );
45 mHorizontalSnapLine = mView->createSnapLine();
46 mHorizontalSnapLine->hide();
47 layout->addItem( mHorizontalSnapLine );
48 mVerticalSnapLine = mView->createSnapLine();
49 mVerticalSnapLine->hide();
50 layout->addItem( mVerticalSnapLine );
53void QgsLayoutMouseHandles::paint( QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget )
55 paintInternal( painter, mLayout->renderContext().isPreviewRender(),
56 mLayout->renderContext().boundingBoxesVisible(),
true, option, widget );
59void QgsLayoutMouseHandles::selectionChanged()
62 const QList<QGraphicsItem *> itemList = layout()->items();
63 for ( QGraphicsItem *graphicsItem : itemList )
69 if ( item->isSelected() )
89void QgsLayoutMouseHandles::setViewportCursor( Qt::CursorShape cursor )
94 if ( qobject_cast< QgsLayoutViewToolSelect *>( mView->tool() ) )
96 mView->viewport()->setCursor( cursor );
100QList<QGraphicsItem *> QgsLayoutMouseHandles::sceneItemsAtPoint( QPointF scenePoint )
102 QList< QGraphicsItem * > items = mLayout->items( scenePoint );
103 items.erase( std::remove_if( items.begin(), items.end(), []( QGraphicsItem * item )
105 return !dynamic_cast<QgsLayoutItem *>( item );
111QList<QGraphicsItem *> QgsLayoutMouseHandles::selectedSceneItems(
bool includeLockedItems )
const
113 QList<QGraphicsItem *> res;
114 const QList<QgsLayoutItem *> layoutItems = mLayout->selectedLayoutItems( includeLockedItems );
115 res.reserve( layoutItems.size() );
121bool QgsLayoutMouseHandles::itemIsLocked( QGraphicsItem *item )
129bool QgsLayoutMouseHandles::itemIsGroupMember( QGraphicsItem *item )
132 return layoutItem->isGroupMember();
137QRectF QgsLayoutMouseHandles::itemRect( QGraphicsItem *item )
const
140 return layoutItem->rectWithFrame();
145QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode,
bool snapHorizontal,
bool snapVertical )
147 bool snapped =
false;
149 const QList< QGraphicsItem * > selectedItems = selectedSceneItems();
150 QList< QGraphicsItem * > itemsToExclude;
151 expandItemList( selectedItems, itemsToExclude );
153 QList< QgsLayoutItem * > layoutItemsToExclude;
154 for ( QGraphicsItem *item : itemsToExclude )
155 layoutItemsToExclude << dynamic_cast< QgsLayoutItem * >( item );
158 QPointF snappedPoint;
162 snappedPoint = mLayout->snapper().snapRect( rect().translated( originalPoint ), mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr,
163 snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude ).topLeft();
166 snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr,
167 snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude );
171 return snapped ? snappedPoint : originalPoint;
174void QgsLayoutMouseHandles::createItemCommand( QGraphicsItem *item )
176 mItemCommand.reset( qgis::down_cast< QgsLayoutItem * >( item )->createCommand( QString(), 0 ) );
177 mItemCommand->saveBeforeState();
180void QgsLayoutMouseHandles::endItemCommand( QGraphicsItem * )
182 mItemCommand->saveAfterState();
183 mLayout->undoStack()->push( mItemCommand.release() );
186void QgsLayoutMouseHandles::startMacroCommand(
const QString &text )
188 mLayout->undoStack()->beginMacro( text );
192void QgsLayoutMouseHandles::endMacroCommand()
194 mLayout->undoStack()->endMacro();
197void QgsLayoutMouseHandles::hideAlignItems()
199 mHorizontalSnapLine->hide();
200 mVerticalSnapLine->hide();
203void QgsLayoutMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
205 for ( QGraphicsItem *item : items )
210 const QList<QgsLayoutItem *> groupItems =
static_cast< QgsLayoutItemGroup *
>( item )->items();
211 collected.reserve( collected.size() + groupItems.size() );
213 collected.append( groupItem );
222void QgsLayoutMouseHandles::moveItem( QGraphicsItem *item,
double deltaX,
double deltaY )
224 qgis::down_cast< QgsLayoutItem * >( item )->
attemptMoveBy( deltaX, deltaY );
227void QgsLayoutMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
233void QgsLayoutMouseHandles::showStatusMessage(
const QString &message )
238 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.