37#include <QGraphicsSceneHoverEvent>
38#include <QGraphicsView>
42#include "moc_qgslayoutmousehandles.cpp"
47 : QgsGraphicsViewMouseHandles( view )
51 setRotationEnabled(
true );
54 connect( mLayout, &QGraphicsScene::selectionChanged,
this, &QgsLayoutMouseHandles::selectionChanged );
56 mHorizontalSnapLine = mView->createSnapLine();
57 mHorizontalSnapLine->hide();
58 layout->addItem( mHorizontalSnapLine );
59 mVerticalSnapLine = mView->createSnapLine();
60 mVerticalSnapLine->hide();
61 layout->addItem( mVerticalSnapLine );
64void QgsLayoutMouseHandles::paint( QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget )
66 paintInternal( painter, mLayout->renderContext().isPreviewRender(), mLayout->renderContext().boundingBoxesVisible(),
true, option, widget );
69void QgsLayoutMouseHandles::selectionChanged()
72 const QList<QGraphicsItem *> itemList = layout()->items();
73 for ( QGraphicsItem *graphicsItem : itemList )
79 if ( item->isSelected() )
99void QgsLayoutMouseHandles::setViewportCursor( Qt::CursorShape cursor )
104 if ( qobject_cast<QgsLayoutViewToolSelect *>( mView->tool() ) )
106 mView->viewport()->setCursor( cursor );
110QList<QGraphicsItem *> QgsLayoutMouseHandles::sceneItemsAtPoint( QPointF scenePoint )
112 QList<QGraphicsItem *> items;
115 const double searchTolerance = tool->searchToleranceInLayoutUnits();
116 const QRectF area( scenePoint.x() - searchTolerance, scenePoint.y() - searchTolerance, 2 * searchTolerance, 2 * searchTolerance );
117 items = mLayout->items( area );
121 items = mLayout->items( scenePoint );
123 items.erase( std::remove_if( items.begin(), items.end(), []( QGraphicsItem *item ) { return !dynamic_cast<QgsLayoutItem *>( item ); } ), items.end() );
128QList<QGraphicsItem *> QgsLayoutMouseHandles::selectedSceneItems(
bool includeLockedItems )
const
130 QList<QGraphicsItem *> res;
131 const QList<QgsLayoutItem *> layoutItems = mLayout->selectedLayoutItems( includeLockedItems );
132 res.reserve( layoutItems.size() );
138bool QgsLayoutMouseHandles::itemIsLocked( QGraphicsItem *item )
141 return layoutItem->isLocked();
146bool QgsLayoutMouseHandles::itemIsGroupMember( QGraphicsItem *item )
149 return layoutItem->isGroupMember();
154QRectF QgsLayoutMouseHandles::itemRect( QGraphicsItem *item )
const
157 return layoutItem->rectWithFrame();
162QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode,
bool snapHorizontal,
bool snapVertical )
164 bool snapped =
false;
166 const QList<QGraphicsItem *> selectedItems = selectedSceneItems();
167 QList<QGraphicsItem *> itemsToExclude;
168 expandItemList( selectedItems, itemsToExclude );
170 QList<QgsLayoutItem *> layoutItemsToExclude;
171 for ( QGraphicsItem *item : itemsToExclude )
172 layoutItemsToExclude << dynamic_cast<QgsLayoutItem *>( item );
175 QPointF snappedPoint;
181 .snapRect( rect().translated( originalPoint ), mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr, snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude )
186 = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr, snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude );
190 return snapped ? snappedPoint : originalPoint;
193void QgsLayoutMouseHandles::createItemCommand( QGraphicsItem *item )
195 mItemCommand.reset( qgis::down_cast<QgsLayoutItem *>( item )->createCommand( QString(), 0 ) );
196 mItemCommand->saveBeforeState();
199void QgsLayoutMouseHandles::endItemCommand( QGraphicsItem * )
201 mItemCommand->saveAfterState();
202 mLayout->undoStack()->push( mItemCommand.release() );
205void QgsLayoutMouseHandles::startMacroCommand(
const QString &text )
207 mLayout->undoStack()->beginMacro( text );
210void QgsLayoutMouseHandles::endMacroCommand()
212 mLayout->undoStack()->endMacro();
215void QgsLayoutMouseHandles::hideAlignItems()
217 mHorizontalSnapLine->hide();
218 mVerticalSnapLine->hide();
221void QgsLayoutMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
223 for ( QGraphicsItem *item : items )
228 const QList<QgsLayoutItem *> groupItems =
static_cast<QgsLayoutItemGroup *
>( item )->items();
229 expandItemList( groupItems, collected );
239void QgsLayoutMouseHandles::expandItemList(
const QList<QgsLayoutItem *> &items, QList<QGraphicsItem *> &collected )
const
241 for ( QGraphicsItem *item : items )
246 const QList<QgsLayoutItem *> groupItems =
static_cast<QgsLayoutItemGroup *
>( item )->items();
247 expandItemList( groupItems, collected );
256void QgsLayoutMouseHandles::moveItem( QGraphicsItem *item,
double deltaX,
double deltaY )
258 qgis::down_cast<QgsLayoutItem *>( item )->attemptMoveBy( deltaX, deltaY );
261void QgsLayoutMouseHandles::rotateItem( QGraphicsItem *item,
double deltaDegree,
double deltaCenterX,
double deltaCenterY )
263 QgsLayoutItem *itm = qgis::down_cast<QgsLayoutItem *>( item );
271void QgsLayoutMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
273 QgsLayoutItem *layoutItem = qgis::down_cast<QgsLayoutItem *>( item );
277void QgsLayoutMouseHandles::showStatusMessage(
const QString &message )
282 mView->pushStatusMessage( message );
286void QgsLayoutMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
288 QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( event );
290 QList<QGraphicsItem *> items = selectedSceneItems();
291 if ( items.isEmpty() )
295 if ( item ==
nullptr )
299 int itemtype = item->
type();
302 itemtype = multiFrame->type();
305 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.
double itemRotation() const
Returns the current rotation for the item, in degrees clockwise.
virtual void setItemRotation(double rotation, bool adjustPosition=true)
Sets the layout item's rotation, in degrees clockwise.
void rotationChanged(double newRotation)
Emitted on item rotation change.
ReferencePoint referencePoint() const
Returns the reference point for positioning of the layout item.
ReferencePoint
Fixed position reference point.
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 attemptMoveBy(double deltaX, double deltaY)
Attempts to shift the item's position by a specified deltaX and deltaY, in layout units.
void setReferencePoint(ReferencePoint point)
Sets the reference point for positioning of the layout item.
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.