19#include "moc_qgslayoutmousehandles.cpp"
34#include <QGraphicsView>
35#include <QGraphicsSceneHoverEvent>
43 : QgsGraphicsViewMouseHandles( view )
48 connect( mLayout, &QGraphicsScene::selectionChanged,
this, &QgsLayoutMouseHandles::selectionChanged );
50 mHorizontalSnapLine = mView->createSnapLine();
51 mHorizontalSnapLine->hide();
52 layout->addItem( mHorizontalSnapLine );
53 mVerticalSnapLine = mView->createSnapLine();
54 mVerticalSnapLine->hide();
55 layout->addItem( mVerticalSnapLine );
58void QgsLayoutMouseHandles::paint( QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget )
60 paintInternal( painter, mLayout->renderContext().isPreviewRender(), mLayout->renderContext().boundingBoxesVisible(),
true, option, widget );
63void QgsLayoutMouseHandles::selectionChanged()
66 const QList<QGraphicsItem *> itemList = layout()->items();
67 for ( QGraphicsItem *graphicsItem : itemList )
73 if ( item->isSelected() )
93void QgsLayoutMouseHandles::setViewportCursor( Qt::CursorShape cursor )
98 if ( qobject_cast<QgsLayoutViewToolSelect *>( mView->tool() ) )
100 mView->viewport()->setCursor( cursor );
104QList<QGraphicsItem *> QgsLayoutMouseHandles::sceneItemsAtPoint( QPointF scenePoint )
106 QList<QGraphicsItem *> items;
109 const double searchTolerance = tool->searchToleranceInLayoutUnits();
110 const QRectF area( scenePoint.x() - searchTolerance, scenePoint.y() - searchTolerance, 2 * searchTolerance, 2 * searchTolerance );
111 items = mLayout->items( area );
115 items = mLayout->items( scenePoint );
117 items.erase( std::remove_if( items.begin(), items.end(), []( QGraphicsItem *item ) {
118 return !dynamic_cast<QgsLayoutItem *>( item );
125QList<QGraphicsItem *> QgsLayoutMouseHandles::selectedSceneItems(
bool includeLockedItems )
const
127 QList<QGraphicsItem *> res;
128 const QList<QgsLayoutItem *> layoutItems = mLayout->selectedLayoutItems( includeLockedItems );
129 res.reserve( layoutItems.size() );
135bool QgsLayoutMouseHandles::itemIsLocked( QGraphicsItem *item )
143bool QgsLayoutMouseHandles::itemIsGroupMember( QGraphicsItem *item )
146 return layoutItem->isGroupMember();
151QRectF QgsLayoutMouseHandles::itemRect( QGraphicsItem *item )
const
154 return layoutItem->rectWithFrame();
159QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode,
bool snapHorizontal,
bool snapVertical )
161 bool snapped =
false;
163 const QList<QGraphicsItem *> selectedItems = selectedSceneItems();
164 QList<QGraphicsItem *> itemsToExclude;
165 expandItemList( selectedItems, itemsToExclude );
167 QList<QgsLayoutItem *> layoutItemsToExclude;
168 for ( QGraphicsItem *item : itemsToExclude )
169 layoutItemsToExclude << dynamic_cast<
QgsLayoutItem *>( item );
172 QPointF snappedPoint;
176 snappedPoint = mLayout->snapper().snapRect( rect().translated( originalPoint ), mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine : nullptr, snapVertical ? mVerticalSnapLine : nullptr, &layoutItemsToExclude ).topLeft();
179 snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine : nullptr, snapVertical ? mVerticalSnapLine : nullptr, &layoutItemsToExclude );
183 return snapped ? snappedPoint : originalPoint;
186void QgsLayoutMouseHandles::createItemCommand( QGraphicsItem *item )
188 mItemCommand.reset( qgis::down_cast<QgsLayoutItem *>( item )->createCommand( QString(), 0 ) );
189 mItemCommand->saveBeforeState();
192void QgsLayoutMouseHandles::endItemCommand( QGraphicsItem * )
194 mItemCommand->saveAfterState();
195 mLayout->undoStack()->push( mItemCommand.release() );
198void QgsLayoutMouseHandles::startMacroCommand(
const QString &text )
200 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 );
269void QgsLayoutMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
271 QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( event );
273 QList<QGraphicsItem *> items = selectedSceneItems();
274 if ( items.isEmpty() )
278 if ( item ==
nullptr )
282 int itemtype = item->
type();
285 itemtype = multiFrame->type();
288 if ( metadataId == -1 )
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
Base class for frame items, which form a layout multiframe item.
A container for grouping several QgsLayoutItems.
int metadataIdForItemType(int type) const
Returns the GUI item metadata ID which corresponds to the specified layout item type.
QgsLayoutItemAbstractGuiMetadata * itemMetadata(int metadataId) const
Returns the metadata for the specified item metadataId.
@ 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.
int type() const override
Returns a unique graphics item type identifier.
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.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
A graphical widget to display and interact with QgsLayouts.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.