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;
105 const double searchTolerance = tool->searchToleranceInLayoutUnits();
106 const QRectF area( scenePoint.x() - searchTolerance, scenePoint.y() - searchTolerance, 2 * searchTolerance, 2 * searchTolerance );
107 items = mLayout->items( area );
111 items = mLayout->items( scenePoint );
113 items.erase( std::remove_if( items.begin(), items.end(), []( QGraphicsItem * item )
115 return !dynamic_cast<QgsLayoutItem *>( item );
121QList<QGraphicsItem *> QgsLayoutMouseHandles::selectedSceneItems(
bool includeLockedItems )
const
123 QList<QGraphicsItem *> res;
124 const QList<QgsLayoutItem *> layoutItems = mLayout->selectedLayoutItems( includeLockedItems );
125 res.reserve( layoutItems.size() );
131bool QgsLayoutMouseHandles::itemIsLocked( QGraphicsItem *item )
139bool QgsLayoutMouseHandles::itemIsGroupMember( QGraphicsItem *item )
142 return layoutItem->isGroupMember();
147QRectF QgsLayoutMouseHandles::itemRect( QGraphicsItem *item )
const
150 return layoutItem->rectWithFrame();
155QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode,
bool snapHorizontal,
bool snapVertical )
157 bool snapped =
false;
159 const QList< QGraphicsItem * > selectedItems = selectedSceneItems();
160 QList< QGraphicsItem * > itemsToExclude;
161 expandItemList( selectedItems, itemsToExclude );
163 QList< QgsLayoutItem * > layoutItemsToExclude;
164 for ( QGraphicsItem *item : itemsToExclude )
165 layoutItemsToExclude << dynamic_cast<
QgsLayoutItem * >( item );
168 QPointF snappedPoint;
172 snappedPoint = mLayout->snapper().snapRect( rect().translated( originalPoint ), mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine : nullptr,
173 snapVertical ? mVerticalSnapLine : nullptr, &layoutItemsToExclude ).topLeft();
176 snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine : nullptr,
177 snapVertical ? mVerticalSnapLine : nullptr, &layoutItemsToExclude );
181 return snapped ? snappedPoint : originalPoint;
184void QgsLayoutMouseHandles::createItemCommand( QGraphicsItem *item )
186 mItemCommand.reset( qgis::down_cast< QgsLayoutItem * >( item )->createCommand( QString(), 0 ) );
187 mItemCommand->saveBeforeState();
190void QgsLayoutMouseHandles::endItemCommand( QGraphicsItem * )
192 mItemCommand->saveAfterState();
193 mLayout->undoStack()->push( mItemCommand.release() );
196void QgsLayoutMouseHandles::startMacroCommand(
const QString &text )
198 mLayout->undoStack()->beginMacro( text );
202void QgsLayoutMouseHandles::endMacroCommand()
204 mLayout->undoStack()->endMacro();
207void QgsLayoutMouseHandles::hideAlignItems()
209 mHorizontalSnapLine->hide();
210 mVerticalSnapLine->hide();
213void QgsLayoutMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
215 for ( QGraphicsItem *item : items )
220 const QList<QgsLayoutItem *> groupItems =
static_cast< QgsLayoutItemGroup *
>( item )->items();
221 expandItemList( groupItems, collected );
231void QgsLayoutMouseHandles::expandItemList(
const QList<QgsLayoutItem *> &items, QList<QGraphicsItem *> &collected )
const
233 for ( QGraphicsItem *item : items )
238 const QList<QgsLayoutItem *> groupItems =
static_cast< QgsLayoutItemGroup *
>( item )->items();
239 expandItemList( groupItems, collected );
248void QgsLayoutMouseHandles::moveItem( QGraphicsItem *item,
double deltaX,
double deltaY )
250 qgis::down_cast< QgsLayoutItem * >( item )->attemptMoveBy( deltaX, deltaY );
253void QgsLayoutMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
259void QgsLayoutMouseHandles::showStatusMessage(
const QString &message )
264 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 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.