19#include "moc_qgslayoutmousehandles.cpp"
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;
106 const double searchTolerance = tool->searchToleranceInLayoutUnits();
107 const QRectF area( scenePoint.x() - searchTolerance, scenePoint.y() - searchTolerance, 2 * searchTolerance, 2 * searchTolerance );
108 items = mLayout->items( area );
112 items = mLayout->items( scenePoint );
114 items.erase( std::remove_if( items.begin(), items.end(), []( QGraphicsItem * item )
116 return !dynamic_cast<QgsLayoutItem *>( item );
122QList<QGraphicsItem *> QgsLayoutMouseHandles::selectedSceneItems(
bool includeLockedItems )
const
124 QList<QGraphicsItem *> res;
125 const QList<QgsLayoutItem *> layoutItems = mLayout->selectedLayoutItems( includeLockedItems );
126 res.reserve( layoutItems.size() );
132bool QgsLayoutMouseHandles::itemIsLocked( QGraphicsItem *item )
140bool QgsLayoutMouseHandles::itemIsGroupMember( QGraphicsItem *item )
143 return layoutItem->isGroupMember();
148QRectF QgsLayoutMouseHandles::itemRect( QGraphicsItem *item )
const
151 return layoutItem->rectWithFrame();
156QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode,
bool snapHorizontal,
bool snapVertical )
158 bool snapped =
false;
160 const QList< QGraphicsItem * > selectedItems = selectedSceneItems();
161 QList< QGraphicsItem * > itemsToExclude;
162 expandItemList( selectedItems, itemsToExclude );
164 QList< QgsLayoutItem * > layoutItemsToExclude;
165 for ( QGraphicsItem *item : itemsToExclude )
166 layoutItemsToExclude << dynamic_cast<
QgsLayoutItem * >( item );
169 QPointF snappedPoint;
173 snappedPoint = mLayout->snapper().snapRect( rect().translated( originalPoint ), mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine : nullptr,
174 snapVertical ? mVerticalSnapLine : nullptr, &layoutItemsToExclude ).topLeft();
177 snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine : nullptr,
178 snapVertical ? mVerticalSnapLine : nullptr, &layoutItemsToExclude );
182 return snapped ? snappedPoint : originalPoint;
185void QgsLayoutMouseHandles::createItemCommand( QGraphicsItem *item )
187 mItemCommand.reset( qgis::down_cast< QgsLayoutItem * >( item )->createCommand( QString(), 0 ) );
188 mItemCommand->saveBeforeState();
191void QgsLayoutMouseHandles::endItemCommand( QGraphicsItem * )
193 mItemCommand->saveAfterState();
194 mLayout->undoStack()->push( mItemCommand.release() );
197void QgsLayoutMouseHandles::startMacroCommand(
const QString &text )
199 mLayout->undoStack()->beginMacro( text );
203void QgsLayoutMouseHandles::endMacroCommand()
205 mLayout->undoStack()->endMacro();
208void QgsLayoutMouseHandles::hideAlignItems()
210 mHorizontalSnapLine->hide();
211 mVerticalSnapLine->hide();
214void QgsLayoutMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
216 for ( QGraphicsItem *item : items )
221 const QList<QgsLayoutItem *> groupItems =
static_cast< QgsLayoutItemGroup *
>( item )->items();
222 expandItemList( groupItems, collected );
232void QgsLayoutMouseHandles::expandItemList(
const QList<QgsLayoutItem *> &items, QList<QGraphicsItem *> &collected )
const
234 for ( QGraphicsItem *item : items )
239 const QList<QgsLayoutItem *> groupItems =
static_cast< QgsLayoutItemGroup *
>( item )->items();
240 expandItemList( groupItems, collected );
249void QgsLayoutMouseHandles::moveItem( QGraphicsItem *item,
double deltaX,
double deltaY )
251 qgis::down_cast< QgsLayoutItem * >( item )->attemptMoveBy( deltaX, deltaY );
254void QgsLayoutMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
260void QgsLayoutMouseHandles::showStatusMessage(
const QString &message )
265 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.