void QgsComposerView::mouseMoveEvent( QMouseEvent* e ) { if ( !composition() ) { return; } mMouseCurrentXY = e->pos(); //update cursor position in composer status bar emit cursorPosChanged( mapToScene( e->pos() ) ); updateRulers(); if ( mHorizontalRuler ) { mHorizontalRuler->updateMarker( e->posF() ); } if ( mVerticalRuler ) { mVerticalRuler->updateMarker( e->posF() ); } if ( mPanning ) { //panning, so scroll view horizontalScrollBar()->setValue( horizontalScrollBar()->value() - ( e->x() - mMouseLastXY.x() ) ); verticalScrollBar()->setValue( verticalScrollBar()->value() - ( e->y() - mMouseLastXY.y() ) ); mMouseLastXY = e->pos(); return; } else if ( e->buttons() == Qt::NoButton ) { if ( mCurrentTool == Select ) { QGraphicsView::mouseMoveEvent( e ); } } else { QPointF scenePoint = mapToScene( e->pos() ); if ( mMarqueeSelect || mMarqueeZoom ) { updateRubberBand( scenePoint ); return; } switch ( mCurrentTool ) { case Select: QGraphicsView::mouseMoveEvent( e ); break; case AddArrow: { if ( mRubberBandLineItem ) { mRubberBandLineItem->setLine( mRubberBandStartPos.x(), mRubberBandStartPos.y(), scenePoint.x(), scenePoint.y() ); } break; } case AddMap: case AddRectangle: case AddTriangle: case AddEllipse: case AddHtml: //adjust rubber band item { updateRubberBand( scenePoint ); break; } case MoveItemContent: { //update map preview if composer map QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap *>( mMoveContentItem ); if ( composerMap ) { composerMap->setOffset( scenePoint.x() - mMoveContentStartPos.x(), scenePoint.y() - mMoveContentStartPos.y() ); composerMap->update(); } break; } default: break; } } }
void QgsComposerView::mouseReleaseEvent( QMouseEvent* e ) { if ( !composition() ) { return; } QPoint mousePressStopPoint = e->pos(); int diffX = mousePressStopPoint.x() - mMousePressStartPos.x(); int diffY = mousePressStopPoint.y() - mMousePressStartPos.y(); //was this just a click? or a click and drag? bool clickOnly = false; if ( qAbs( diffX ) < 2 && qAbs( diffY ) < 2 ) { clickOnly = true; } QPointF scenePoint = mapToScene( e->pos() ); if ( mPanning ) { mPanning = false; if ( clickOnly && e->button() == Qt::MidButton ) { //middle mouse button click = recenter on point //get current visible part of scene QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() ); QgsRectangle visibleRect = QgsRectangle( mapToScene( viewportRect ).boundingRect() ); visibleRect.scale( 1, scenePoint.x(), scenePoint.y() ); QRectF boundsRect = visibleRect.toRectF(); //zoom view to fit desired bounds fitInView( boundsRect, Qt::KeepAspectRatio ); } //set new cursor if ( mCurrentTool == Pan ) { viewport()->setCursor( Qt::OpenHandCursor ); } else { if ( composition() ) { //allow composer items to change cursor composition()->setPreventCursorChange( false ); } viewport()->setCursor( Qt::ArrowCursor ); } } if ( mMarqueeSelect ) { endMarqueeSelect( e ); return; } switch ( mCurrentTool ) { case Select: { QGraphicsView::mouseReleaseEvent( e ); break; } case Zoom: { if ( mMarqueeZoom ) { endMarqueeZoom( e ); } break; } case MoveItemContent: { if ( mMoveContentItem ) { //update map preview if composer map QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap *>( mMoveContentItem ); if ( composerMap ) { composerMap->setOffset( 0, 0 ); } double moveX = scenePoint.x() - mMoveContentStartPos.x(); double moveY = scenePoint.y() - mMoveContentStartPos.y(); composition()->beginCommand( mMoveContentItem, tr( "Move item content" ) ); mMoveContentItem->moveContent( -moveX, -moveY ); composition()->endCommand(); mMoveContentItem = 0; } break; } case AddArrow: if ( composition() ) { QPointF scenePoint = mapToScene( e->pos() ); QPointF snappedScenePoint = composition()->snapPointToGrid( scenePoint ); QgsComposerArrow* composerArrow = new QgsComposerArrow( mRubberBandStartPos, QPointF( snappedScenePoint.x(), snappedScenePoint.y() ), composition() ); composition()->addComposerArrow( composerArrow ); composition()->clearSelection(); composerArrow->setSelected( true ); emit selectedItemChanged( composerArrow ); scene()->removeItem( mRubberBandLineItem ); delete mRubberBandLineItem; mRubberBandLineItem = 0; emit actionFinished(); composition()->pushAddRemoveCommand( composerArrow, tr( "Arrow added" ) ); } break; case AddRectangle: case AddTriangle: case AddEllipse: addShape( mCurrentTool ); break; case AddMap: if ( !mRubberBandItem || ( mRubberBandItem->rect().width() < 0.1 && mRubberBandItem->rect().height() < 0.1 ) ) { removeRubberBand(); return; } if ( composition() ) { QgsComposerMap* composerMap = new QgsComposerMap( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ); composition()->addComposerMap( composerMap ); composition()->clearSelection(); composerMap->setSelected( true ); emit selectedItemChanged( composerMap ); removeRubberBand(); emit actionFinished(); composition()->pushAddRemoveCommand( composerMap, tr( "Map added" ) ); } break; case AddHtml: if ( composition() ) { QgsComposerHtml* composerHtml = new QgsComposerHtml( composition(), true ); QgsAddRemoveMultiFrameCommand* command = new QgsAddRemoveMultiFrameCommand( QgsAddRemoveMultiFrameCommand::Added, composerHtml, composition(), tr( "Html item added" ) ); composition()->undoStack()->push( command ); QgsComposerFrame* frame = new QgsComposerFrame( composition(), composerHtml, mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ); composition()->beginMultiFrameCommand( composerHtml, tr( "Html frame added" ) ); composerHtml->addFrame( frame ); composition()->endMultiFrameCommand(); composition()->clearSelection(); frame->setSelected( true ); emit selectedItemChanged( frame ); removeRubberBand(); emit actionFinished(); } default: break; } }
void QgsComposerView::mouseMoveEvent( QMouseEvent* e ) { if ( !composition() ) { return; } if ( e->buttons() == Qt::NoButton ) { if ( mCurrentTool == Select ) { QGraphicsView::mouseMoveEvent( e ); } } else { QPointF scenePoint = mapToScene( e->pos() ); switch ( mCurrentTool ) { case Select: QGraphicsView::mouseMoveEvent( e ); break; case AddArrow: { if ( mRubberBandLineItem ) { mRubberBandLineItem->setLine( mRubberBandStartPos.x(), mRubberBandStartPos.y(), scenePoint.x(), scenePoint.y() ); } break; } case AddMap: case AddShape: //adjust rubber band item { double x = 0; double y = 0; double width = 0; double height = 0; double dx = scenePoint.x() - mRubberBandStartPos.x(); double dy = scenePoint.y() - mRubberBandStartPos.y(); if ( dx < 0 ) { x = scenePoint.x(); width = -dx; } else { x = mRubberBandStartPos.x(); width = dx; } if ( dy < 0 ) { y = scenePoint.y(); height = -dy; } else { y = mRubberBandStartPos.y(); height = dy; } if ( mRubberBandItem ) { mRubberBandItem->setRect( 0, 0, width, height ); QTransform t; t.translate( x, y ); mRubberBandItem->setTransform( t ); } break; } case MoveItemContent: { //update map preview if composer map QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap *>( mMoveContentItem ); if ( composerMap ) { composerMap->setOffset( scenePoint.x() - mMoveContentStartPos.x(), scenePoint.y() - mMoveContentStartPos.y() ); composerMap->update(); } break; } default: break; } } }
void QgsComposerView::mouseReleaseEvent( QMouseEvent* e ) { if ( !composition() ) { return; } QPointF scenePoint = mapToScene( e->pos() ); switch ( mCurrentTool ) { case Select: { QGraphicsView::mouseReleaseEvent( e ); break; } case MoveItemContent: { if ( mMoveContentItem ) { //update map preview if composer map QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap *>( mMoveContentItem ); if ( composerMap ) { composerMap->setOffset( 0, 0 ); } double moveX = scenePoint.x() - mMoveContentStartPos.x(); double moveY = scenePoint.y() - mMoveContentStartPos.y(); composition()->beginCommand( mMoveContentItem, tr( "Move item content" ) ); mMoveContentItem->moveContent( -moveX, -moveY ); composition()->endCommand(); mMoveContentItem = 0; } break; } case AddArrow: if ( composition() ) { QPointF scenePoint = mapToScene( e->pos() ); QPointF snappedScenePoint = composition()->snapPointToGrid( scenePoint ); QgsComposerArrow* composerArrow = new QgsComposerArrow( mRubberBandStartPos, QPointF( snappedScenePoint.x(), snappedScenePoint.y() ), composition() ); composition()->addComposerArrow( composerArrow ); scene()->removeItem( mRubberBandLineItem ); delete mRubberBandLineItem; mRubberBandLineItem = 0; emit actionFinished(); composition()->pushAddRemoveCommand( composerArrow, tr( "Arrow added" ) ); } break; case AddShape: if ( !mRubberBandItem || mRubberBandItem->rect().width() < 0.1 || mRubberBandItem->rect().width() < 0.1 ) { scene()->removeItem( mRubberBandItem ); delete mRubberBandItem; mRubberBandItem = 0; return; } if ( composition() ) { QgsComposerShape* composerShape = new QgsComposerShape( mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height(), composition() ); composition()->addComposerShape( composerShape ); scene()->removeItem( mRubberBandItem ); delete mRubberBandItem; mRubberBandItem = 0; emit actionFinished(); composition()->pushAddRemoveCommand( composerShape, tr( "Shape added" ) ); } break; case AddMap: if ( !mRubberBandItem || mRubberBandItem->rect().width() < 0.1 || mRubberBandItem->rect().width() < 0.1 ) { if ( mRubberBandItem ) { scene()->removeItem( mRubberBandItem ); delete mRubberBandItem; mRubberBandItem = 0; } return; } if ( composition() ) { QgsComposerMap* composerMap = new QgsComposerMap( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ); composition()->addComposerMap( composerMap ); scene()->removeItem( mRubberBandItem ); delete mRubberBandItem; mRubberBandItem = 0; emit actionFinished(); composition()->pushAddRemoveCommand( composerMap, tr( "Map added" ) ); } break; default: break; } }
void QgsComposerView::mouseReleaseEvent( QMouseEvent* e ) { if ( !composition() ) { return; } QPointF scenePoint = mapToScene( e->pos() ); if ( mPanning ) { mPanning = false; //set new cursor if ( mCurrentTool == Pan ) { viewport()->setCursor( Qt::OpenHandCursor ); } else { if ( composition() ) { //allow composer items to change cursor composition()->setPreventCursorChange( false ); } viewport()->setCursor( Qt::ArrowCursor ); } } if ( mMarqueeSelect ) { endMarqueeSelect( e ); return; } switch ( mCurrentTool ) { case Select: { QGraphicsView::mouseReleaseEvent( e ); break; } case MoveItemContent: { if ( mMoveContentItem ) { //update map preview if composer map QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap *>( mMoveContentItem ); if ( composerMap ) { composerMap->setOffset( 0, 0 ); } double moveX = scenePoint.x() - mMoveContentStartPos.x(); double moveY = scenePoint.y() - mMoveContentStartPos.y(); composition()->beginCommand( mMoveContentItem, tr( "Move item content" ) ); mMoveContentItem->moveContent( -moveX, -moveY ); composition()->endCommand(); mMoveContentItem = 0; } break; } case AddArrow: if ( composition() ) { QPointF scenePoint = mapToScene( e->pos() ); QPointF snappedScenePoint = composition()->snapPointToGrid( scenePoint ); QgsComposerArrow* composerArrow = new QgsComposerArrow( mRubberBandStartPos, QPointF( snappedScenePoint.x(), snappedScenePoint.y() ), composition() ); composition()->addComposerArrow( composerArrow ); scene()->removeItem( mRubberBandLineItem ); delete mRubberBandLineItem; mRubberBandLineItem = 0; emit actionFinished(); composition()->pushAddRemoveCommand( composerArrow, tr( "Arrow added" ) ); } break; case AddRectangle: case AddTriangle: case AddEllipse: addShape( mCurrentTool ); break; case AddMap: if ( !mRubberBandItem || mRubberBandItem->rect().width() < 0.1 || mRubberBandItem->rect().width() < 0.1 ) { removeRubberBand(); return; } if ( composition() ) { QgsComposerMap* composerMap = new QgsComposerMap( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ); composition()->addComposerMap( composerMap ); removeRubberBand(); emit actionFinished(); composition()->pushAddRemoveCommand( composerMap, tr( "Map added" ) ); } break; case AddHtml: if ( composition() ) { QgsComposerHtml* composerHtml = new QgsComposerHtml( composition(), true ); QgsAddRemoveMultiFrameCommand* command = new QgsAddRemoveMultiFrameCommand( QgsAddRemoveMultiFrameCommand::Added, composerHtml, composition(), tr( "Html item added" ) ); composition()->undoStack()->push( command ); QgsComposerFrame* frame = new QgsComposerFrame( composition(), composerHtml, mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ); composition()->beginMultiFrameCommand( composerHtml, tr( "Html frame added" ) ); composerHtml->addFrame( frame ); composition()->endMultiFrameCommand(); removeRubberBand(); emit actionFinished(); } default: break; } }
void QgsComposerView::mouseReleaseEvent( QMouseEvent* e ) { if ( !composition() ) { return; } if ( e->button() != Qt::LeftButton && ( composition()->selectionHandles()->isDragging() || composition()->selectionHandles()->isResizing() ) ) { //ignore clicks while dragging/resizing items return; } QPoint mousePressStopPoint = e->pos(); int diffX = mousePressStopPoint.x() - mMousePressStartPos.x(); int diffY = mousePressStopPoint.y() - mMousePressStartPos.y(); //was this just a click? or a click and drag? bool clickOnly = false; if ( qAbs( diffX ) < 2 && qAbs( diffY ) < 2 ) { clickOnly = true; } QPointF scenePoint = mapToScene( e->pos() ); if ( mMousePanning || mToolPanning ) { mMousePanning = false; mToolPanning = false; if ( clickOnly && e->button() == Qt::MidButton ) { //middle mouse button click = recenter on point //get current visible part of scene QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() ); QgsRectangle visibleRect = QgsRectangle( mapToScene( viewportRect ).boundingRect() ); visibleRect.scale( 1, scenePoint.x(), scenePoint.y() ); QRectF boundsRect = visibleRect.toRectF(); //zoom view to fit desired bounds fitInView( boundsRect, Qt::KeepAspectRatio ); } //set new cursor if ( mCurrentTool != Pan ) { if ( composition() ) { //allow composer items to change cursor composition()->setPreventCursorChange( false ); } } viewport()->setCursor( defaultCursorForTool( mCurrentTool ) ); } //for every other tool, ignore clicks of non-left button if ( e->button() != Qt::LeftButton ) { return; } if ( mMarqueeSelect ) { endMarqueeSelect( e ); return; } switch ( mCurrentTool ) { case Select: { QGraphicsView::mouseReleaseEvent( e ); break; } case Zoom: { if ( mMarqueeZoom ) { endMarqueeZoom( e ); } break; } case MoveItemContent: { if ( mMoveContentItem ) { //update map preview if composer map QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap *>( mMoveContentItem ); if ( composerMap ) { composerMap->setOffset( 0, 0 ); } double moveX = scenePoint.x() - mMoveContentStartPos.x(); double moveY = scenePoint.y() - mMoveContentStartPos.y(); composition()->beginCommand( mMoveContentItem, tr( "Move item content" ) ); mMoveContentItem->moveContent( -moveX, -moveY ); composition()->endCommand(); mMoveContentItem = 0; mMovingItemContent = false; } break; } case AddArrow: if ( !composition() || !mRubberBandLineItem ) { scene()->removeItem( mRubberBandLineItem ); delete mRubberBandLineItem; mRubberBandLineItem = 0; return; } else { QgsComposerArrow* composerArrow = new QgsComposerArrow( mRubberBandLineItem->line().p1(), mRubberBandLineItem->line().p2(), composition() ); composition()->addComposerArrow( composerArrow ); composition()->clearSelection(); composerArrow->setSelected( true ); emit selectedItemChanged( composerArrow ); scene()->removeItem( mRubberBandLineItem ); delete mRubberBandLineItem; mRubberBandLineItem = 0; emit actionFinished(); composition()->pushAddRemoveCommand( composerArrow, tr( "Arrow added" ) ); } break; case AddRectangle: case AddTriangle: case AddEllipse: addShape( mCurrentTool ); break; case AddMap: if ( !composition() || !mRubberBandItem || ( mRubberBandItem->rect().width() < 0.1 && mRubberBandItem->rect().height() < 0.1 ) ) { removeRubberBand(); return; } else { QgsComposerMap* composerMap = new QgsComposerMap( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ); composition()->addComposerMap( composerMap ); composition()->clearSelection(); composerMap->setSelected( true ); emit selectedItemChanged( composerMap ); removeRubberBand(); emit actionFinished(); composition()->pushAddRemoveCommand( composerMap, tr( "Map added" ) ); } break; case AddPicture: if ( !composition() || !mRubberBandItem || ( mRubberBandItem->rect().width() < 0.1 && mRubberBandItem->rect().height() < 0.1 ) ) { removeRubberBand(); return; } else { QgsComposerPicture* newPicture = new QgsComposerPicture( composition() ); newPicture->setSceneRect( QRectF( mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ) ); composition()->addComposerPicture( newPicture ); composition()->clearSelection(); newPicture->setSelected( true ); emit selectedItemChanged( newPicture ); removeRubberBand(); emit actionFinished(); composition()->pushAddRemoveCommand( newPicture, tr( "Picture added" ) ); } break; case AddLabel: if ( !composition() || !mRubberBandItem ) { removeRubberBand(); return; } else { QgsComposerLabel* newLabelItem = new QgsComposerLabel( composition() ); newLabelItem->setText( tr( "QGIS" ) ); newLabelItem->adjustSizeToText(); //make sure label size is sufficient to fit text double labelWidth = qMax( mRubberBandItem->rect().width(), newLabelItem->rect().width() ); double labelHeight = qMax( mRubberBandItem->rect().height(), newLabelItem->rect().height() ); newLabelItem->setSceneRect( QRectF( mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), labelWidth, labelHeight ) ); composition()->addComposerLabel( newLabelItem ); composition()->clearSelection(); newLabelItem->setSelected( true ); emit selectedItemChanged( newLabelItem ); removeRubberBand(); emit actionFinished(); composition()->pushAddRemoveCommand( newLabelItem, tr( "Label added" ) ); } break; case AddLegend: if ( !composition() || !mRubberBandItem ) { removeRubberBand(); return; } else { QgsComposerLegend* newLegend = new QgsComposerLegend( composition() ); newLegend->setSceneRect( QRectF( mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ) ); composition()->addComposerLegend( newLegend ); newLegend->updateLegend(); composition()->clearSelection(); newLegend->setSelected( true ); emit selectedItemChanged( newLegend ); removeRubberBand(); emit actionFinished(); composition()->pushAddRemoveCommand( newLegend, tr( "Legend added" ) ); } break; case AddTable: if ( !composition() || !mRubberBandItem ) { removeRubberBand(); return; } else { QgsComposerAttributeTable* newTable = new QgsComposerAttributeTable( composition() ); newTable->setSceneRect( QRectF( mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), qMax( mRubberBandItem->rect().height(), 15.0 ) ) ); QList<const QgsComposerMap*> mapItemList = composition()->composerMapItems(); if ( mapItemList.size() > 0 ) { newTable->setComposerMap( mapItemList.at( 0 ) ); } composition()->addComposerTable( newTable ); composition()->clearSelection(); newTable->setSelected( true ); emit selectedItemChanged( newTable ); removeRubberBand(); emit actionFinished(); composition()->pushAddRemoveCommand( newTable, tr( "Table added" ) ); } break; case AddHtml: if ( !composition() || !mRubberBandItem || ( mRubberBandItem->rect().width() < 0.1 && mRubberBandItem->rect().height() < 0.1 ) ) { removeRubberBand(); return; } else { QgsComposerHtml* composerHtml = new QgsComposerHtml( composition(), true ); QgsAddRemoveMultiFrameCommand* command = new QgsAddRemoveMultiFrameCommand( QgsAddRemoveMultiFrameCommand::Added, composerHtml, composition(), tr( "Html item added" ) ); composition()->undoStack()->push( command ); QgsComposerFrame* frame = new QgsComposerFrame( composition(), composerHtml, mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() ); composition()->beginMultiFrameCommand( composerHtml, tr( "Html frame added" ) ); composerHtml->addFrame( frame ); composition()->endMultiFrameCommand(); composition()->clearSelection(); frame->setSelected( true ); emit selectedItemChanged( frame ); removeRubberBand(); emit actionFinished(); } default: break; } }