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 ) {
124 return !dynamic_cast<QgsLayoutItem *>( item );
131QList<QGraphicsItem *> QgsLayoutMouseHandles::selectedSceneItems(
bool includeLockedItems )
const
133 QList<QGraphicsItem *> res;
134 const QList<QgsLayoutItem *> layoutItems = mLayout->selectedLayoutItems( includeLockedItems );
135 res.reserve( layoutItems.size() );
141bool QgsLayoutMouseHandles::itemIsLocked( QGraphicsItem *item )
144 return layoutItem->isLocked();
149bool QgsLayoutMouseHandles::itemIsGroupMember( QGraphicsItem *item )
152 return layoutItem->isGroupMember();
157QRectF QgsLayoutMouseHandles::itemRect( QGraphicsItem *item )
const
160 return layoutItem->rectWithFrame();
165QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode,
bool snapHorizontal,
bool snapVertical )
167 bool snapped =
false;
169 const QList<QGraphicsItem *> selectedItems = selectedSceneItems();
170 QList<QGraphicsItem *> itemsToExclude;
171 expandItemList( selectedItems, itemsToExclude );
173 QList<QgsLayoutItem *> layoutItemsToExclude;
174 for ( QGraphicsItem *item : itemsToExclude )
175 layoutItemsToExclude << dynamic_cast<QgsLayoutItem *>( item );
178 QPointF snappedPoint;
182 snappedPoint = mLayout->snapper().snapRect( rect().translated( originalPoint ), mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr, snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude ).topLeft();
185 snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr, snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude );
189 return snapped ? snappedPoint : originalPoint;
192void QgsLayoutMouseHandles::createItemCommand( QGraphicsItem *item )
194 mItemCommand.reset( qgis::down_cast<QgsLayoutItem *>( item )->createCommand( QString(), 0 ) );
195 mItemCommand->saveBeforeState();
198void QgsLayoutMouseHandles::endItemCommand( QGraphicsItem * )
200 mItemCommand->saveAfterState();
201 mLayout->undoStack()->push( mItemCommand.release() );
204void QgsLayoutMouseHandles::startMacroCommand(
const QString &text )
206 mLayout->undoStack()->beginMacro( text );
209void QgsLayoutMouseHandles::endMacroCommand()
211 mLayout->undoStack()->endMacro();
214void QgsLayoutMouseHandles::hideAlignItems()
216 mHorizontalSnapLine->hide();
217 mVerticalSnapLine->hide();
220void QgsLayoutMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
222 for ( QGraphicsItem *item : items )
227 const QList<QgsLayoutItem *> groupItems =
static_cast<QgsLayoutItemGroup *
>( item )->items();
228 expandItemList( groupItems, collected );
238void QgsLayoutMouseHandles::expandItemList(
const QList<QgsLayoutItem *> &items, QList<QGraphicsItem *> &collected )
const
240 for ( QGraphicsItem *item : items )
245 const QList<QgsLayoutItem *> groupItems =
static_cast<QgsLayoutItemGroup *
>( item )->items();
246 expandItemList( groupItems, collected );
255void QgsLayoutMouseHandles::moveItem( QGraphicsItem *item,
double deltaX,
double deltaY )
257 qgis::down_cast<QgsLayoutItem *>( item )->attemptMoveBy( deltaX, deltaY );
260void QgsLayoutMouseHandles::rotateItem( QGraphicsItem *item,
double deltaDegree,
double deltaCenterX,
double deltaCenterY )
262 QgsLayoutItem *itm = qgis::down_cast<QgsLayoutItem *>( item );
270void QgsLayoutMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
272 QgsLayoutItem *layoutItem = qgis::down_cast<QgsLayoutItem *>( item );
276void QgsLayoutMouseHandles::showStatusMessage(
const QString &message )
281 mView->pushStatusMessage( message );
285void QgsLayoutMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
287 QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( event );
289 QList<QGraphicsItem *> items = selectedSceneItems();
290 if ( items.isEmpty() )
294 if ( item ==
nullptr )
298 int itemtype = item->
type();
301 itemtype = multiFrame->type();
304 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.