Skip to content

Commit

Permalink
Work on interactive moving of items
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 6, 2017
1 parent 2d6cbd6 commit d7bd44d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 3 deletions.
2 changes: 2 additions & 0 deletions python/gui/layout/qgslayoutview.sip
Expand Up @@ -127,6 +127,8 @@ class QgsLayoutView: QGraphicsView
:rtype: list of int
%End



public slots:

void zoomFull();
Expand Down
1 change: 1 addition & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -38,6 +38,7 @@
#include "qgsdockwidget.h"
#include "qgslayoutpagepropertieswidget.h"
#include "qgslayoutguidewidget.h"
#include "qgslayoutmousehandles.h"
#include <QShortcut>
#include <QComboBox>
#include <QLineEdit>
Expand Down
22 changes: 21 additions & 1 deletion src/gui/layout/qgslayoutmousehandles.cpp
Expand Up @@ -511,6 +511,11 @@ QgsLayoutMouseHandles::MouseAction QgsLayoutMouseHandles::mouseActionForScenePos
return mouseActionForPosition( itemPos );
}

bool QgsLayoutMouseHandles::shouldBlockEvent( QInputEvent * ) const
{
return mIsDragging || mIsResizing;
}

void QgsLayoutMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event )
{
setViewportCursor( cursorForPosition( event->pos() ) );
Expand Down Expand Up @@ -575,6 +580,9 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )

QPointF mEndHandleMovePos = scenePos();

double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();

//move all selected items
const QList<QgsLayoutItem *> selectedItems = mLayout->selectedLayoutItems( false );
for ( QgsLayoutItem *item : selectedItems )
Expand All @@ -587,7 +595,15 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
#if 0 //TODO
QgsComposerItemCommand *subcommand = new QgsComposerItemCommand( item, QLatin1String( "" ), parentCommand );
subcommand->savePreviousState();
item->move( mEndHandleMovePos.x() - mBeginHandlePos.x(), mEndHandleMovePos.y() - mBeginHandlePos.y() );
#endif

// need to convert delta from layout units -> item units
QgsLayoutPoint itemPos = item->positionWithUnits();
QgsLayoutPoint deltaPos = mLayout->convertFromLayoutUnits( QPointF( deltaX, deltaY ), itemPos.units() );
itemPos.setX( itemPos.x() + deltaPos.x() );
itemPos.setY( itemPos.y() + deltaPos.y() );
item->attemptMove( itemPos );
#if 0
subcommand->saveAfterState();
#endif
}
Expand Down Expand Up @@ -629,6 +645,7 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )

itemRect = itemRect.normalized();
QPointF newPos = mapToScene( itemRect.topLeft() );

#if 0 //TODO - convert to existing units
item->attemptMove( newPos.x(), newPos.y(), itemRect.width(), itemRect.height(), QgsComposerItem::UpperLeft, true );
#endif
Expand Down Expand Up @@ -782,6 +799,9 @@ void QgsLayoutMouseHandles::dragMouseMove( QPointF currentPosition, bool lockMov
QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );

QPointF snappedLeftPoint;

//TODO
snappedLeftPoint = upperLeftPoint;
#if 0
//no snapping for rotated items for now
if ( !preventSnap && qgsDoubleNear( rotation(), 0.0 ) )
Expand Down
7 changes: 5 additions & 2 deletions src/gui/layout/qgslayoutmousehandles.h
Expand Up @@ -28,6 +28,7 @@
class QgsLayout;
class QGraphicsView;
class QgsLayoutView;
class QInputEvent;

///@cond PRIVATE

Expand Down Expand Up @@ -101,10 +102,12 @@ class GUI_EXPORT QgsLayoutMouseHandles: public QObject, public QGraphicsRectItem
QgsLayoutMouseHandles::MouseAction mouseActionForScenePos( QPointF sceneCoordPos );

//! Returns true is user is currently dragging the handles
bool isDragging() { return mIsDragging; }
bool isDragging() const { return mIsDragging; }

//! Returns true is user is currently resizing with the handles
bool isResizing() { return mIsResizing; }
bool isResizing() const { return mIsResizing; }

bool shouldBlockEvent( QInputEvent *event ) const;

protected:

Expand Down
36 changes: 36 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -220,6 +220,13 @@ QList<int> QgsLayoutView::visiblePageNumbers() const
return currentLayout()->pageCollection()->visiblePageNumbers( visibleRect );
}

///@cond PRIVATE
QgsLayoutMouseHandles *QgsLayoutView::mouseHandles()
{
return mMouseHandles;
}
///@endcond

void QgsLayoutView::zoomFull()
{
fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
Expand Down Expand Up @@ -273,6 +280,17 @@ void QgsLayoutView::mousePressEvent( QMouseEvent *event )
{
mSnapMarker->setVisible( false );

if ( mMouseHandles->shouldBlockEvent( event ) )
{
//ignore clicks while dragging/resizing items
return;
}
else if ( mMouseHandles->isVisible() )
{
QGraphicsView::mousePressEvent( event );
return;
}

if ( mTool )
{
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
Expand Down Expand Up @@ -306,6 +324,13 @@ void QgsLayoutView::mousePressEvent( QMouseEvent *event )

void QgsLayoutView::mouseReleaseEvent( QMouseEvent *event )
{
if ( event->button() != Qt::LeftButton &&
mMouseHandles->shouldBlockEvent( event ) )
{
//ignore clicks while dragging/resizing items
return;
}

if ( mTool )
{
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
Expand Down Expand Up @@ -363,6 +388,12 @@ void QgsLayoutView::mouseDoubleClickEvent( QMouseEvent *event )

void QgsLayoutView::wheelEvent( QWheelEvent *event )
{
if ( mMouseHandles->shouldBlockEvent( event ) )
{
//ignore wheel events while dragging/resizing items
return;
}

if ( mTool )
{
mTool->wheelEvent( event );
Expand All @@ -377,6 +408,11 @@ void QgsLayoutView::wheelEvent( QWheelEvent *event )

void QgsLayoutView::keyPressEvent( QKeyEvent *event )
{
if ( mMouseHandles->isDragging() || mMouseHandles->isResizing() )
{
return;
}

if ( mTool )
{
mTool->keyPressEvent( event );
Expand Down
9 changes: 9 additions & 0 deletions src/gui/layout/qgslayoutview.h
Expand Up @@ -158,6 +158,15 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
*/
QList< int > visiblePageNumbers() const;

///@cond PRIVATE

/**
* Returns the view's mouse handles.
* \note Not available in Python bindings.
*/
SIP_SKIP QgsLayoutMouseHandles *mouseHandles();
///@endcond

public slots:

/**
Expand Down

0 comments on commit d7bd44d

Please sign in to comment.