Work on interactive moving of items

This commit is contained in:
Nyall Dawson 2017-09-29 17:09:41 +10:00
parent 2d6cbd6bba
commit d7bd44d9e7
6 changed files with 74 additions and 3 deletions

View File

@ -127,6 +127,8 @@ class QgsLayoutView: QGraphicsView
:rtype: list of int :rtype: list of int
%End %End
public slots: public slots:
void zoomFull(); void zoomFull();

View File

@ -38,6 +38,7 @@
#include "qgsdockwidget.h" #include "qgsdockwidget.h"
#include "qgslayoutpagepropertieswidget.h" #include "qgslayoutpagepropertieswidget.h"
#include "qgslayoutguidewidget.h" #include "qgslayoutguidewidget.h"
#include "qgslayoutmousehandles.h"
#include <QShortcut> #include <QShortcut>
#include <QComboBox> #include <QComboBox>
#include <QLineEdit> #include <QLineEdit>

View File

@ -511,6 +511,11 @@ QgsLayoutMouseHandles::MouseAction QgsLayoutMouseHandles::mouseActionForScenePos
return mouseActionForPosition( itemPos ); return mouseActionForPosition( itemPos );
} }
bool QgsLayoutMouseHandles::shouldBlockEvent( QInputEvent * ) const
{
return mIsDragging || mIsResizing;
}
void QgsLayoutMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event ) void QgsLayoutMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event )
{ {
setViewportCursor( cursorForPosition( event->pos() ) ); setViewportCursor( cursorForPosition( event->pos() ) );
@ -575,6 +580,9 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
QPointF mEndHandleMovePos = scenePos(); QPointF mEndHandleMovePos = scenePos();
double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
//move all selected items //move all selected items
const QList<QgsLayoutItem *> selectedItems = mLayout->selectedLayoutItems( false ); const QList<QgsLayoutItem *> selectedItems = mLayout->selectedLayoutItems( false );
for ( QgsLayoutItem *item : selectedItems ) for ( QgsLayoutItem *item : selectedItems )
@ -587,7 +595,15 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
#if 0 //TODO #if 0 //TODO
QgsComposerItemCommand *subcommand = new QgsComposerItemCommand( item, QLatin1String( "" ), parentCommand ); QgsComposerItemCommand *subcommand = new QgsComposerItemCommand( item, QLatin1String( "" ), parentCommand );
subcommand->savePreviousState(); 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(); subcommand->saveAfterState();
#endif #endif
} }
@ -629,6 +645,7 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
itemRect = itemRect.normalized(); itemRect = itemRect.normalized();
QPointF newPos = mapToScene( itemRect.topLeft() ); QPointF newPos = mapToScene( itemRect.topLeft() );
#if 0 //TODO - convert to existing units #if 0 //TODO - convert to existing units
item->attemptMove( newPos.x(), newPos.y(), itemRect.width(), itemRect.height(), QgsComposerItem::UpperLeft, true ); item->attemptMove( newPos.x(), newPos.y(), itemRect.width(), itemRect.height(), QgsComposerItem::UpperLeft, true );
#endif #endif
@ -782,6 +799,9 @@ void QgsLayoutMouseHandles::dragMouseMove( QPointF currentPosition, bool lockMov
QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY ); QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
QPointF snappedLeftPoint; QPointF snappedLeftPoint;
//TODO
snappedLeftPoint = upperLeftPoint;
#if 0 #if 0
//no snapping for rotated items for now //no snapping for rotated items for now
if ( !preventSnap && qgsDoubleNear( rotation(), 0.0 ) ) if ( !preventSnap && qgsDoubleNear( rotation(), 0.0 ) )

View File

@ -28,6 +28,7 @@
class QgsLayout; class QgsLayout;
class QGraphicsView; class QGraphicsView;
class QgsLayoutView; class QgsLayoutView;
class QInputEvent;
///@cond PRIVATE ///@cond PRIVATE
@ -101,10 +102,12 @@ class GUI_EXPORT QgsLayoutMouseHandles: public QObject, public QGraphicsRectItem
QgsLayoutMouseHandles::MouseAction mouseActionForScenePos( QPointF sceneCoordPos ); QgsLayoutMouseHandles::MouseAction mouseActionForScenePos( QPointF sceneCoordPos );
//! Returns true is user is currently dragging the handles //! 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 //! 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: protected:

View File

@ -220,6 +220,13 @@ QList<int> QgsLayoutView::visiblePageNumbers() const
return currentLayout()->pageCollection()->visiblePageNumbers( visibleRect ); return currentLayout()->pageCollection()->visiblePageNumbers( visibleRect );
} }
///@cond PRIVATE
QgsLayoutMouseHandles *QgsLayoutView::mouseHandles()
{
return mMouseHandles;
}
///@endcond
void QgsLayoutView::zoomFull() void QgsLayoutView::zoomFull()
{ {
fitInView( scene()->sceneRect(), Qt::KeepAspectRatio ); fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
@ -273,6 +280,17 @@ void QgsLayoutView::mousePressEvent( QMouseEvent *event )
{ {
mSnapMarker->setVisible( false ); mSnapMarker->setVisible( false );
if ( mMouseHandles->shouldBlockEvent( event ) )
{
//ignore clicks while dragging/resizing items
return;
}
else if ( mMouseHandles->isVisible() )
{
QGraphicsView::mousePressEvent( event );
return;
}
if ( mTool ) if ( mTool )
{ {
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) ); std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
@ -306,6 +324,13 @@ void QgsLayoutView::mousePressEvent( QMouseEvent *event )
void QgsLayoutView::mouseReleaseEvent( QMouseEvent *event ) void QgsLayoutView::mouseReleaseEvent( QMouseEvent *event )
{ {
if ( event->button() != Qt::LeftButton &&
mMouseHandles->shouldBlockEvent( event ) )
{
//ignore clicks while dragging/resizing items
return;
}
if ( mTool ) if ( mTool )
{ {
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) ); std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
@ -363,6 +388,12 @@ void QgsLayoutView::mouseDoubleClickEvent( QMouseEvent *event )
void QgsLayoutView::wheelEvent( QWheelEvent *event ) void QgsLayoutView::wheelEvent( QWheelEvent *event )
{ {
if ( mMouseHandles->shouldBlockEvent( event ) )
{
//ignore wheel events while dragging/resizing items
return;
}
if ( mTool ) if ( mTool )
{ {
mTool->wheelEvent( event ); mTool->wheelEvent( event );
@ -377,6 +408,11 @@ void QgsLayoutView::wheelEvent( QWheelEvent *event )
void QgsLayoutView::keyPressEvent( QKeyEvent *event ) void QgsLayoutView::keyPressEvent( QKeyEvent *event )
{ {
if ( mMouseHandles->isDragging() || mMouseHandles->isResizing() )
{
return;
}
if ( mTool ) if ( mTool )
{ {
mTool->keyPressEvent( event ); mTool->keyPressEvent( event );

View File

@ -158,6 +158,15 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
*/ */
QList< int > visiblePageNumbers() const; 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: public slots:
/** /**