QgsAnnotationWidget::QgsAnnotationWidget( QgsMapCanvasAnnotationItem *item, QWidget *parent, Qt::WindowFlags f )
  : QWidget( parent, f )
  , mItem( item )
{
  setupUi( this );
  mLayerComboBox->setAllowEmptyLayer( true );

  mMapMarkerButton->setSymbolType( QgsSymbol::Marker );
  mFrameStyleButton->setSymbolType( QgsSymbol::Fill );

  if ( mItem && mItem->annotation() )
  {
    QgsAnnotation *annotation = mItem->annotation();
    blockAllSignals( true );

    if ( annotation->hasFixedMapPosition() )
    {
      mMapPositionFixedCheckBox->setCheckState( Qt::Checked );
    }
    else
    {
      mMapPositionFixedCheckBox->setCheckState( Qt::Unchecked );
    }

    whileBlocking( mSpinTopMargin )->setValue( annotation->contentsMargin().top() );
    whileBlocking( mSpinLeftMargin )->setValue( annotation->contentsMargin().left() );
    whileBlocking( mSpinRightMargin )->setValue( annotation->contentsMargin().right() );
    whileBlocking( mSpinBottomMargin )->setValue( annotation->contentsMargin().bottom() );

    mLayerComboBox->setLayer( annotation->mapLayer() );

    const QgsMarkerSymbol *symbol = annotation->markerSymbol();
    if ( symbol )
    {
      mMapMarkerButton->setSymbol( symbol->clone() );
    }
    const QgsFillSymbol *fill = annotation->fillSymbol();
    if ( fill )
    {
      mFrameStyleButton->setSymbol( fill->clone() );
    }

    blockAllSignals( false );
  }
  mMapMarkerButton->setMapCanvas( QgisApp::instance()->mapCanvas() );
  mFrameStyleButton->setMapCanvas( QgisApp::instance()->mapCanvas() );
}
Esempio n. 2
0
void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
{
  QgsMapCanvasAnnotationItem *item = selectedItem();
  if ( !item || !item->annotation() )
    return;

  QgsAnnotation *annotation = item->annotation();

  if ( e->buttons() & Qt::LeftButton )
  {
    if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::MoveMapPosition )
    {
      QgsPointXY mapPos = transformCanvasToAnnotation( e->snapPoint(), annotation );
      annotation->setMapPosition( mapPos );
      annotation->setRelativePosition( QPointF( e->pos().x() / mCanvas->width(),
                                       e->pos().y() / mCanvas->height() ) );
      item->update();
      QgsProject::instance()->setDirty( true );
    }
    else if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::MoveFramePosition )
    {
      QPointF newCanvasPos = item->pos() + ( e->pos() - mLastMousePosition );
      if ( annotation->hasFixedMapPosition() )
      {
        const double pixelToMmScale = 25.4 / mCanvas->logicalDpiX();
        const double deltaX = pixelToMmScale * ( e->pos().x() - mLastMousePosition.x() );
        const double deltaY = pixelToMmScale * ( e->pos().y() - mLastMousePosition.y() );
        annotation->setFrameOffsetFromReferencePointMm( QPointF( annotation->frameOffsetFromReferencePointMm().x() + deltaX,
            annotation->frameOffsetFromReferencePointMm().y() + deltaY ) );
        annotation->setRelativePosition( QPointF( newCanvasPos.x() / mCanvas->width(),
                                         newCanvasPos.y() / mCanvas->height() ) );
      }
      else
      {
        QgsPointXY mapPos = transformCanvasToAnnotation( toMapCoordinates( newCanvasPos.toPoint() ), annotation );
        annotation->setMapPosition( mapPos );
        annotation->setRelativePosition( QPointF( newCanvasPos.x() / mCanvas->width(),
                                         newCanvasPos.y() / mCanvas->height() ) );
      }
      item->update();
      QgsProject::instance()->setDirty( true );
    }
    else if ( mCurrentMoveAction != QgsMapCanvasAnnotationItem::NoAction )
    {
      //handle the frame resize actions

      const double pixelToMmScale = 25.4 / mCanvas->logicalDpiX();

      QSizeF size = annotation->frameSizeMm();
      double xmin = annotation->frameOffsetFromReferencePointMm().x();
      double ymin = annotation->frameOffsetFromReferencePointMm().y();
      double xmax = xmin + size.width();
      double ymax = ymin + size.height();
      double relPosX = annotation->relativePosition().x();
      double relPosY = annotation->relativePosition().y();

      if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRight ||
           mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightDown ||
           mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightUp )
      {
        xmax += pixelToMmScale * ( e->pos().x() - mLastMousePosition.x() );
      }
      if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeft ||
           mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftDown ||
           mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftUp )
      {
        xmin += pixelToMmScale * ( e->pos().x() - mLastMousePosition.x() );
        relPosX = ( relPosX * mCanvas->width() + e->pos().x() - mLastMousePosition.x() ) / static_cast<double>( mCanvas->width() );
      }
      if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameUp ||
           mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftUp ||
           mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightUp )
      {
        ymin += pixelToMmScale * ( e->pos().y() - mLastMousePosition.y() );
        relPosY = ( relPosY * mCanvas->height() + e->pos().y() - mLastMousePosition.y() ) / static_cast<double>( mCanvas->height() );
      }
      if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameDown ||
           mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftDown ||
           mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightDown )
      {
        ymax += pixelToMmScale * ( e->pos().y() - mLastMousePosition.y() );
      }

      //switch min / max if necessary
      double tmp;
      if ( xmax < xmin )
      {
        tmp = xmax;
        xmax = xmin;
        xmin = tmp;
      }
      if ( ymax < ymin )
      {
        tmp = ymax;
        ymax = ymin;
        ymin = tmp;
      }

      annotation->setFrameOffsetFromReferencePointMm( QPointF( xmin, ymin ) );
      annotation->setFrameSizeMm( QSizeF( xmax - xmin, ymax - ymin ) );
      annotation->setRelativePosition( QPointF( relPosX, relPosY ) );
      item->update();
      QgsProject::instance()->setDirty( true );
    }
  }
  else if ( item )
  {
    QgsMapCanvasAnnotationItem::MouseMoveAction moveAction = item->moveActionForPosition( e->pos() );
    if ( mCanvas )
    {
      mCanvas->setCursor( QCursor( item->cursorShapeForAction( moveAction ) ) );
    }
  }
  mLastMousePosition = e->pos();
}