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 );
54 void QgsLayoutMouseHandles::paint( QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget )
56 paintInternal( painter, mLayout->renderContext().isPreviewRender(),
57 mLayout->renderContext().boundingBoxesVisible(),
true, option, widget );
60 void QgsLayoutMouseHandles::selectionChanged()
63 const QList<QGraphicsItem *> itemList = layout()->items();
64 for ( QGraphicsItem *graphicsItem : itemList )
70 if ( item->isSelected() )
90 void QgsLayoutMouseHandles::setViewportCursor( Qt::CursorShape cursor )
95 if ( qobject_cast< QgsLayoutViewToolSelect *>( mView->tool() ) )
97 mView->viewport()->setCursor( cursor );
101 QList<QGraphicsItem *> QgsLayoutMouseHandles::sceneItemsAtPoint( QPointF scenePoint )
103 QList< QGraphicsItem * > items = mLayout->items( scenePoint );
104 items.erase( std::remove_if( items.begin(), items.end(), []( QGraphicsItem * item )
106 return !dynamic_cast<QgsLayoutItem *>( item );
112 QList<QGraphicsItem *> QgsLayoutMouseHandles::selectedSceneItems(
bool includeLockedItems )
const
114 QList<QGraphicsItem *> res;
115 const QList<QgsLayoutItem *> layoutItems = mLayout->selectedLayoutItems( includeLockedItems );
116 res.reserve( layoutItems.size() );
122 bool QgsLayoutMouseHandles::itemIsLocked( QGraphicsItem *item )
130 bool QgsLayoutMouseHandles::itemIsGroupMember( QGraphicsItem *item )
133 return layoutItem->isGroupMember();
138 QRectF QgsLayoutMouseHandles::itemRect( QGraphicsItem *item )
const
141 return layoutItem->rectWithFrame();
146 QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode,
bool snapHorizontal,
bool snapVertical )
148 bool snapped =
false;
150 const QList< QGraphicsItem * > selectedItems = selectedSceneItems();
151 QList< QGraphicsItem * > itemsToExclude;
152 expandItemList( selectedItems, itemsToExclude );
154 QList< QgsLayoutItem * > layoutItemsToExclude;
155 for ( QGraphicsItem *item : itemsToExclude )
156 layoutItemsToExclude << dynamic_cast< QgsLayoutItem * >( item );
159 QPointF snappedPoint;
163 snappedPoint = mLayout->snapper().snapRect( rect().translated( originalPoint ), mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr,
164 snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude ).topLeft();
167 snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine :
nullptr,
168 snapVertical ? mVerticalSnapLine :
nullptr, &layoutItemsToExclude );
172 return snapped ? snappedPoint : originalPoint;
175 void QgsLayoutMouseHandles::createItemCommand( QGraphicsItem *item )
177 mItemCommand.reset( qgis::down_cast< QgsLayoutItem * >( item )->createCommand( QString(), 0 ) );
178 mItemCommand->saveBeforeState();
181 void QgsLayoutMouseHandles::endItemCommand( QGraphicsItem * )
183 mItemCommand->saveAfterState();
184 mLayout->undoStack()->push( mItemCommand.release() );
187 void QgsLayoutMouseHandles::startMacroCommand(
const QString &text )
189 mLayout->undoStack()->beginMacro( text );
193 void QgsLayoutMouseHandles::endMacroCommand()
195 mLayout->undoStack()->endMacro();
198 void QgsLayoutMouseHandles::hideAlignItems()
200 mHorizontalSnapLine->hide();
201 mVerticalSnapLine->hide();
204 void QgsLayoutMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
206 for ( QGraphicsItem *item : items )
211 const QList<QgsLayoutItem *> groupItems =
static_cast< QgsLayoutItemGroup *
>( item )->items();
212 collected.reserve( collected.size() + groupItems.size() );
214 collected.append( groupItem );
223 void QgsLayoutMouseHandles::moveItem( QGraphicsItem *item,
double deltaX,
double deltaY )
225 qgis::down_cast< QgsLayoutItem * >( item )->
attemptMoveBy( deltaX, deltaY );
228 void QgsLayoutMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
234 void QgsLayoutMouseHandles::showStatusMessage(
const QString &message )
239 mView->pushStatusMessage( message );