Example #1
0
void QgsComposerItem::deleteAlignItems()
{
  deleteHAlignSnapItem();
  deleteVAlignSnapItem();
}
void QgsComposerItem::changeItemRectangle( const QPointF& currentPosition,
    const QPointF& mouseMoveStartPos,
    const QGraphicsRectItem* originalItem,
    double dx, double dy,
    QGraphicsRectItem* changeItem )
{
  Q_UNUSED( dx );
  Q_UNUSED( dy );
  if ( !changeItem || !originalItem || !mComposition )
  {
    return;
  }

  //test if change item is a composer item. If so, prefer call to  setSceneRect() instead of setTransform() and setRect()
  QgsComposerItem* changeComposerItem = dynamic_cast<QgsComposerItem *>( changeItem );

  double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
  QPointF snappedPosition = mComposition->snapPointToGrid( currentPosition );

  //snap to grid and align to other items
  if ( mComposition->alignmentSnap() && mCurrentMouseMoveAction != QgsComposerItem::MoveItem )
  {
    double alignX = 0;
    double alignY = 0;
    snappedPosition = mComposition->alignPos( snappedPosition, dynamic_cast<const QgsComposerItem*>( originalItem ), alignX, alignY );
    if ( alignX != -1 )
    {
      QGraphicsLineItem* item = hAlignSnapItem();
      item->setLine( QLineF( alignX, 0, alignX,  mComposition->paperHeight() ) );
      item->show();
    }
    else
    {
      deleteHAlignSnapItem();
    }

    if ( alignY != -1 )
    {
      QGraphicsLineItem* item = vAlignSnapItem();
      item->setLine( QLineF( 0, alignY, mComposition->paperWidth(), alignY ) );
      item->show();
    }
    else
    {
      deleteVAlignSnapItem();
    }
  }

  double diffX = 0;
  double diffY = 0;

  switch ( mCurrentMouseMoveAction )
  {
      //vertical resize
    case QgsComposerItem::ResizeUp:
      diffY = snappedPosition.y() - originalItem->transform().dy();
      mx = 0; my = diffY; rx = 0; ry = -diffY;
      break;

    case QgsComposerItem::ResizeDown:
      diffY = snappedPosition.y() - ( originalItem->transform().dy() + originalItem->rect().height() );
      mx = 0; my = 0; rx = 0; ry = diffY;
      break;

      //horizontal resize
    case QgsComposerItem::ResizeLeft:
      diffX = snappedPosition.x() - originalItem->transform().dx();
      mx = diffX, my = 0; rx = -diffX; ry = 0;
      break;

    case QgsComposerItem::ResizeRight:
      diffX = snappedPosition.x() - ( originalItem->transform().dx() + originalItem->rect().width() );
      mx = 0; my = 0; rx = diffX, ry = 0;
      break;

      //diagonal resize
    case QgsComposerItem::ResizeLeftUp:
      diffX = snappedPosition.x() - originalItem->transform().dx();
      diffY = snappedPosition.y() - originalItem->transform().dy();
      mx = diffX, my = diffY; rx = -diffX; ry = -diffY;
      break;

    case QgsComposerItem::ResizeRightDown:
      diffX = snappedPosition.x() - ( originalItem->transform().dx() + originalItem->rect().width() );
      diffY = snappedPosition.y() - ( originalItem->transform().dy() + originalItem->rect().height() );
      mx = 0; my = 0; rx = diffX, ry = diffY;
      break;

    case QgsComposerItem::ResizeRightUp:
      diffX = snappedPosition.x() - ( originalItem->transform().dx() + originalItem->rect().width() );
      diffY = snappedPosition.y() - originalItem->transform().dy();
      mx = 0; my = diffY, rx = diffX, ry = -diffY;
      break;

    case QgsComposerItem::ResizeLeftDown:
      diffX = snappedPosition.x() - originalItem->transform().dx();
      diffY = snappedPosition.y() - ( originalItem->transform().dy() + originalItem->rect().height() );
      mx = diffX, my = 0; rx = -diffX; ry = diffY;
      break;

    case QgsComposerItem::MoveItem:
    {
      //calculate total move difference
      double moveX = currentPosition.x() - mouseMoveStartPos.x();
      double moveY = currentPosition.y() - mouseMoveStartPos.y();

      QPointF upperLeftPoint( originalItem->transform().dx() + moveX, originalItem->transform().dy() + moveY );
      QPointF snappedLeftPoint = mComposition->snapPointToGrid( upperLeftPoint );

      if ( snappedLeftPoint != upperLeftPoint ) //don't do align snap if grid snap has been done
      {
        deleteAlignItems();
      }
      else if ( mComposition->alignmentSnap() ) //align item
      {
        double alignX = 0;
        double alignY = 0;
        snappedLeftPoint = mComposition->alignItem( dynamic_cast<const QgsComposerItem*>( originalItem ), alignX, alignY, moveX, moveY );
        if ( alignX != -1 )
        {
          QGraphicsLineItem* item = hAlignSnapItem();
          int numPages = mComposition->numPages();
          double yLineCoord = 300; //default in case there is no single page
          if ( numPages > 0 )
          {
            yLineCoord = mComposition->paperHeight() * numPages + mComposition->spaceBetweenPages() * ( numPages - 1 );
          }
          item->setLine( QLineF( alignX, 0, alignX,  yLineCoord ) );
          item->show();
        }
        else
        {
          deleteHAlignSnapItem();
        }
        if ( alignY != -1 )
        {
          QGraphicsLineItem* item = vAlignSnapItem();
          item->setLine( QLineF( 0, alignY, mComposition->paperWidth(), alignY ) );
          item->show();
        }
        else
        {
          deleteVAlignSnapItem();
        }
      }
      double moveRectX = snappedLeftPoint.x() - originalItem->transform().dx();
      double moveRectY = snappedLeftPoint.y() - originalItem->transform().dy();

      if ( !changeComposerItem )
      {
        QTransform moveTransform;
        moveTransform.translate( originalItem->transform().dx() + moveRectX, originalItem->transform().dy() + moveRectY );
        changeItem->setTransform( moveTransform );
      }
      else  //for composer items, we prefer setSceneRect as subclasses can implement custom behaviour (e.g. item group)
      {
        changeComposerItem->setSceneRect( QRectF( originalItem->transform().dx() + moveRectX,
                                          originalItem->transform().dy() + moveRectY,
                                          originalItem->rect().width(), originalItem->rect().height() ) );
        changeComposerItem->updateItem();
      }
    }
    return;
    case QgsComposerItem::NoAction:
      break;
  }

  if ( !changeComposerItem )
  {
    QTransform itemTransform;
    itemTransform.translate( originalItem->transform().dx() + mx, originalItem->transform().dy() + my );
    changeItem->setTransform( itemTransform );
    QRectF itemRect( 0, 0, originalItem->rect().width() + rx,  originalItem->rect().height() + ry );
    changeItem->setRect( itemRect );
  }
  else //for composer items, we prefer setSceneRect as subclasses can implement custom behaviour (e.g. item group)
  {
    changeComposerItem->setSceneRect( QRectF( originalItem->transform().dx() + mx, originalItem->transform().dy() + my,
                                      originalItem->rect().width() + rx, originalItem->rect().height() + ry ) );
    changeComposerItem->updateItem();
  }
}