19#include "moc_qgslayoutmousehandles.cpp" 
   34#include <QGraphicsView> 
   35#include <QGraphicsSceneHoverEvent> 
   43  : QgsGraphicsViewMouseHandles( view )
 
   47  setRotationEnabled( 
true );
 
   50  connect( mLayout, &QGraphicsScene::selectionChanged, 
this, &QgsLayoutMouseHandles::selectionChanged );
 
   52  mHorizontalSnapLine = mView->createSnapLine();
 
   53  mHorizontalSnapLine->hide();
 
   54  layout->addItem( mHorizontalSnapLine );
 
   55  mVerticalSnapLine = mView->createSnapLine();
 
   56  mVerticalSnapLine->hide();
 
   57  layout->addItem( mVerticalSnapLine );
 
   60void QgsLayoutMouseHandles::paint( QPainter *painter, 
const QStyleOptionGraphicsItem *option, QWidget *widget )
 
   62  paintInternal( painter, mLayout->renderContext().isPreviewRender(), mLayout->renderContext().boundingBoxesVisible(), 
true, option, widget );
 
   65void QgsLayoutMouseHandles::selectionChanged()
 
   68  const QList<QGraphicsItem *> itemList = layout()->items();
 
   69  for ( QGraphicsItem *graphicsItem : itemList )
 
   75    if ( item->isSelected() )
 
   95void QgsLayoutMouseHandles::setViewportCursor( Qt::CursorShape cursor )
 
  100  if ( qobject_cast<QgsLayoutViewToolSelect *>( mView->tool() ) )
 
  102    mView->viewport()->setCursor( cursor );
 
  106QList<QGraphicsItem *> QgsLayoutMouseHandles::sceneItemsAtPoint( QPointF scenePoint )
 
  108  QList<QGraphicsItem *> items;
 
  111    const double searchTolerance = tool->searchToleranceInLayoutUnits();
 
  112    const QRectF area( scenePoint.x() - searchTolerance, scenePoint.y() - searchTolerance, 2 * searchTolerance, 2 * searchTolerance );
 
  113    items = mLayout->items( area );
 
  117    items = mLayout->items( scenePoint );
 
  119  items.erase( std::remove_if( items.begin(), items.end(), []( QGraphicsItem *item ) {
 
  120                 return !dynamic_cast<QgsLayoutItem *>( item );
 
  127QList<QGraphicsItem *> QgsLayoutMouseHandles::selectedSceneItems( 
bool includeLockedItems )
 const 
  129  QList<QGraphicsItem *> res;
 
  130  const QList<QgsLayoutItem *> layoutItems = mLayout->selectedLayoutItems( includeLockedItems );
 
  131  res.reserve( layoutItems.size() );
 
  137bool QgsLayoutMouseHandles::itemIsLocked( QGraphicsItem *item )
 
  145bool QgsLayoutMouseHandles::itemIsGroupMember( QGraphicsItem *item )
 
  148    return layoutItem->isGroupMember();
 
  153QRectF QgsLayoutMouseHandles::itemRect( QGraphicsItem *item )
 const 
  156    return layoutItem->rectWithFrame();
 
  161QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode, 
bool snapHorizontal, 
bool snapVertical )
 
  163  bool snapped = 
false;
 
  165  const QList<QGraphicsItem *> selectedItems = selectedSceneItems();
 
  166  QList<QGraphicsItem *> itemsToExclude;
 
  167  expandItemList( selectedItems, itemsToExclude );
 
  169  QList<QgsLayoutItem *> layoutItemsToExclude;
 
  170  for ( QGraphicsItem *item : itemsToExclude )
 
  171    layoutItemsToExclude << dynamic_cast<
QgsLayoutItem *>( item );
 
  174  QPointF snappedPoint;
 
  178      snappedPoint = mLayout->snapper().snapRect( rect().translated( originalPoint ), mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine : nullptr, snapVertical ? mVerticalSnapLine : nullptr, &layoutItemsToExclude ).topLeft();
 
  181      snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine : nullptr, snapVertical ? mVerticalSnapLine : nullptr, &layoutItemsToExclude );
 
  185  return snapped ? snappedPoint : originalPoint;
 
  188void QgsLayoutMouseHandles::createItemCommand( QGraphicsItem *item )
 
  190  mItemCommand.reset( qgis::down_cast<QgsLayoutItem *>( item )->createCommand( QString(), 0 ) );
 
  191  mItemCommand->saveBeforeState();
 
  194void QgsLayoutMouseHandles::endItemCommand( QGraphicsItem * )
 
  196  mItemCommand->saveAfterState();
 
  197  mLayout->undoStack()->push( mItemCommand.release() );
 
  200void QgsLayoutMouseHandles::startMacroCommand( 
const QString &text )
 
  202  mLayout->undoStack()->beginMacro( text );
 
  205void QgsLayoutMouseHandles::endMacroCommand()
 
  207  mLayout->undoStack()->endMacro();
 
  210void QgsLayoutMouseHandles::hideAlignItems()
 
  212  mHorizontalSnapLine->hide();
 
  213  mVerticalSnapLine->hide();
 
  216void QgsLayoutMouseHandles::expandItemList( 
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
 const 
  218  for ( QGraphicsItem *item : items )
 
  223      const QList<QgsLayoutItem *> groupItems = 
static_cast<QgsLayoutItemGroup *
>( item )->items();
 
  224      expandItemList( groupItems, collected );
 
  234void QgsLayoutMouseHandles::expandItemList( 
const QList<QgsLayoutItem *> &items, QList<QGraphicsItem *> &collected )
 const 
  236  for ( QGraphicsItem *item : items )
 
  241      const QList<QgsLayoutItem *> groupItems = 
static_cast<QgsLayoutItemGroup *
>( item )->items();
 
  242      expandItemList( groupItems, collected );
 
  251void QgsLayoutMouseHandles::moveItem( QGraphicsItem *item, 
double deltaX, 
double deltaY )
 
  253  qgis::down_cast<QgsLayoutItem *>( item )->attemptMoveBy( deltaX, deltaY );
 
  256void QgsLayoutMouseHandles::rotateItem( QGraphicsItem *item, 
double deltaDegree, 
double deltaCenterX, 
double deltaCenterY )
 
  258  QgsLayoutItem *itm = qgis::down_cast<QgsLayoutItem *>( item );
 
  266void QgsLayoutMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
 
  272void QgsLayoutMouseHandles::showStatusMessage( 
const QString &message )
 
  277  mView->pushStatusMessage( message );
 
  281void QgsLayoutMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
 
  283  QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( event );
 
  285  QList<QGraphicsItem *> items = selectedSceneItems();
 
  286  if ( items.isEmpty() )
 
  290  if ( item == 
nullptr )
 
  294  int itemtype = item->
type();
 
  297      itemtype = multiFrame->type();
 
  300  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.
 
bool isLocked() const
Returns true if the item is locked, and cannot be interacted with using the mouse.
 
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.