Esempio n. 1
0
void QgsLayoutViewToolZoom::layoutReleaseEvent( QgsLayoutViewMouseEvent *event )
{
  if ( !mMarqueeZoom || event->button() != Qt::LeftButton )
  {
    event->ignore();
    return;
  }

  mMarqueeZoom = false;
  QRectF newBoundsRect = mRubberBand->finish( event->layoutPoint() );

  // click? or click-and-drag?
  if ( !isClickAndDrag( mMousePressStartPos, event->pos() ) )
  {
    //just a click, so zoom to clicked point and recenter
    double scaleFactor = 0.5;
    //get current visible part of scene
    QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
    QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );

    visibleRect.scale( scaleFactor, event->layoutPoint().x(), event->layoutPoint().y() );
    newBoundsRect = visibleRect.toRectF();
  }

  //zoom view to fit desired bounds
  view()->fitInView( newBoundsRect, Qt::KeepAspectRatio );
  view()->emitZoomLevelChanged();
  view()->updateRulers();
}
Esempio n. 2
0
void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *event )
{
  if ( event->button() != Qt::LeftButton || !mDrawing )
  {
    event->ignore();
    return;
  }
  mDrawing = false;

  QRectF rect = mRubberBand->finish( event->snappedPoint(), event->modifiers() );

  QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( mItemType, layout() );

  // click? or click-and-drag?
  bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
  if ( clickOnly )
  {
    QgsLayoutItemPropertiesDialog dlg( view() );
    dlg.setLayout( layout() );
    dlg.setItemPosition( QgsLayoutPoint( event->snappedPoint(), layout()->units() ) );
    if ( dlg.exec() )
    {
      item->setReferencePoint( dlg.referencePoint() );
      item->attemptResize( dlg.itemSize() );
      item->attemptMove( dlg.itemPosition() );
    }
    else
    {
      delete item;
      return;
    }
  }
  else
  {
    item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), QgsUnitTypes::LayoutMillimeters ) );
    item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), QgsUnitTypes::LayoutMillimeters ) );
  }

  // record last created item size
  QgsSettings settings;
  settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
  settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );
  settings.setValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), static_cast< int >( item->sizeWithUnits().units() ) );

  layout()->addLayoutItem( item );
  layout()->setSelectedItem( item );
}
void QgsLayoutViewToolSelect::layoutReleaseEvent( QgsLayoutViewMouseEvent *event )
{
  if ( event->button() != Qt::LeftButton && mMouseHandles->shouldBlockEvent( event ) )
  {
    //swallow clicks while dragging/resizing items
    return;
  }

  if ( !mIsSelecting || event->button() != Qt::LeftButton )
  {
    event->ignore();
    return;
  }

  mIsSelecting = false;
  bool wasClick = !isClickAndDrag( mMousePressStartPos, event->pos() );

  QRectF rect = mRubberBand->finish( event->layoutPoint(), event->modifiers() );

  bool subtractingSelection = false;
  if ( event->modifiers() & Qt::ShiftModifier )
  {
    //shift modifier means adding to selection, nothing required here
  }
  else if ( event->modifiers() & Qt::ControlModifier )
  {
    //control modifier means subtract from current selection
    subtractingSelection = true;
  }
  else
  {
    //not adding to or removing from selection, so clear current selection
    whileBlocking( layout() )->deselectAll();
  }

  //determine item selection mode, default to intersection
  Qt::ItemSelectionMode selectionMode = Qt::IntersectsItemShape;
  if ( event->modifiers() & Qt::AltModifier )
  {
    //alt modifier switches to contains selection mode
    selectionMode = Qt::ContainsItemShape;
  }

  //find all items in rect
  QList<QGraphicsItem *> itemList;
  if ( wasClick )
    itemList = layout()->items( rect.center(), selectionMode );
  else
    itemList = layout()->items( rect, selectionMode );
  for ( QGraphicsItem *item : qgis::as_const( itemList ) )
  {
    QgsLayoutItem *layoutItem = dynamic_cast<QgsLayoutItem *>( item );
    QgsLayoutItemPage *paperItem = dynamic_cast<QgsLayoutItemPage *>( item );
    if ( layoutItem && !paperItem )
    {
      if ( !layoutItem->isLocked() )
      {
        if ( subtractingSelection )
        {
          layoutItem->setSelected( false );
        }
        else
        {
          layoutItem->setSelected( true );
        }
        if ( wasClick )
        {
          // found an item, and only a click - nothing more to do
          break;
        }
      }
    }
  }

  //update item panel
  const QList<QgsLayoutItem *> selectedItemList = layout()->selectedLayoutItems();
  if ( !selectedItemList.isEmpty() )
  {
    emit itemFocused( selectedItemList.at( 0 ) );
  }
  else
  {
    emit itemFocused( nullptr );
  }
  mMouseHandles->selectionChanged();
}
Esempio n. 4
0
void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *event )
{
  if ( event->button() != Qt::LeftButton || !mDrawing )
  {
    event->ignore();
    return;
  }
  mDrawing = false;

  QRectF rect = mRubberBand->finish( event->snappedPoint(), event->modifiers() );

  QString undoText;
  if ( QgsLayoutItemAbstractGuiMetadata *metadata = QgsGui::layoutItemGuiRegistry()->itemMetadata( mItemMetadataId ) )
  {
    undoText = tr( "Create %1" ).arg( metadata->visibleName() );
  }
  else
  {
    undoText = tr( "Create Item" );
  }
  layout()->undoStack()->beginMacro( undoText );

  QgsLayoutItem *item = QgsGui::layoutItemGuiRegistry()->createItem( mItemMetadataId, layout() );
  if ( !item )
  {
    layout()->undoStack()->endMacro();
    return;
  }

  // click? or click-and-drag?
  bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
  if ( clickOnly )
  {
    QgsLayoutItemPropertiesDialog dlg( view() );
    dlg.setLayout( layout() );
    dlg.setItemPosition( QgsLayoutPoint( event->snappedPoint(), layout()->units() ) );
    if ( dlg.exec() )
    {
      item->setReferencePoint( dlg.referencePoint() );
      item->attemptResize( dlg.itemSize() );
      item->attemptMove( dlg.itemPosition(), true, false, dlg.page() );
    }
    else
    {
      delete item;
      layout()->undoStack()->endMacro();
      return;
    }
  }
  else
  {
    item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), QgsUnitTypes::LayoutMillimeters ) );
    item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), QgsUnitTypes::LayoutMillimeters ) );
  }

  // record last created item size
  QgsSettings settings;
  settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
  settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );
  settings.setValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), static_cast< int >( item->sizeWithUnits().units() ) );

  QgsGui::layoutItemGuiRegistry()->newItemAddedToLayout( mItemMetadataId, item );

  // it's possible (in certain circumstances, e.g. when adding frame items) that this item
  // has already been added to the layout
  if ( item->scene() != layout() )
    layout()->addLayoutItem( item );
  layout()->setSelectedItem( item );

  layout()->undoStack()->endMacro();
  emit createdItem();
}