Skip to content

Commit d7bd44d

Browse files
committedOct 6, 2017
Work on interactive moving of items
1 parent 2d6cbd6 commit d7bd44d

File tree

6 files changed

+74
-3
lines changed

6 files changed

+74
-3
lines changed
 

‎python/gui/layout/qgslayoutview.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class QgsLayoutView: QGraphicsView
127127
:rtype: list of int
128128
%End
129129

130+
131+
130132
public slots:
131133

132134
void zoomFull();

‎src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "qgsdockwidget.h"
3939
#include "qgslayoutpagepropertieswidget.h"
4040
#include "qgslayoutguidewidget.h"
41+
#include "qgslayoutmousehandles.h"
4142
#include <QShortcut>
4243
#include <QComboBox>
4344
#include <QLineEdit>

‎src/gui/layout/qgslayoutmousehandles.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ QgsLayoutMouseHandles::MouseAction QgsLayoutMouseHandles::mouseActionForScenePos
511511
return mouseActionForPosition( itemPos );
512512
}
513513

514+
bool QgsLayoutMouseHandles::shouldBlockEvent( QInputEvent * ) const
515+
{
516+
return mIsDragging || mIsResizing;
517+
}
518+
514519
void QgsLayoutMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event )
515520
{
516521
setViewportCursor( cursorForPosition( event->pos() ) );
@@ -575,6 +580,9 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
575580

576581
QPointF mEndHandleMovePos = scenePos();
577582

583+
double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
584+
double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
585+
578586
//move all selected items
579587
const QList<QgsLayoutItem *> selectedItems = mLayout->selectedLayoutItems( false );
580588
for ( QgsLayoutItem *item : selectedItems )
@@ -587,7 +595,15 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
587595
#if 0 //TODO
588596
QgsComposerItemCommand *subcommand = new QgsComposerItemCommand( item, QLatin1String( "" ), parentCommand );
589597
subcommand->savePreviousState();
590-
item->move( mEndHandleMovePos.x() - mBeginHandlePos.x(), mEndHandleMovePos.y() - mBeginHandlePos.y() );
598+
#endif
599+
600+
// need to convert delta from layout units -> item units
601+
QgsLayoutPoint itemPos = item->positionWithUnits();
602+
QgsLayoutPoint deltaPos = mLayout->convertFromLayoutUnits( QPointF( deltaX, deltaY ), itemPos.units() );
603+
itemPos.setX( itemPos.x() + deltaPos.x() );
604+
itemPos.setY( itemPos.y() + deltaPos.y() );
605+
item->attemptMove( itemPos );
606+
#if 0
591607
subcommand->saveAfterState();
592608
#endif
593609
}
@@ -629,6 +645,7 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
629645

630646
itemRect = itemRect.normalized();
631647
QPointF newPos = mapToScene( itemRect.topLeft() );
648+
632649
#if 0 //TODO - convert to existing units
633650
item->attemptMove( newPos.x(), newPos.y(), itemRect.width(), itemRect.height(), QgsComposerItem::UpperLeft, true );
634651
#endif
@@ -782,6 +799,9 @@ void QgsLayoutMouseHandles::dragMouseMove( QPointF currentPosition, bool lockMov
782799
QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
783800

784801
QPointF snappedLeftPoint;
802+
803+
//TODO
804+
snappedLeftPoint = upperLeftPoint;
785805
#if 0
786806
//no snapping for rotated items for now
787807
if ( !preventSnap && qgsDoubleNear( rotation(), 0.0 ) )

‎src/gui/layout/qgslayoutmousehandles.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
class QgsLayout;
2929
class QGraphicsView;
3030
class QgsLayoutView;
31+
class QInputEvent;
3132

3233
///@cond PRIVATE
3334

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

103104
//! Returns true is user is currently dragging the handles
104-
bool isDragging() { return mIsDragging; }
105+
bool isDragging() const { return mIsDragging; }
105106

106107
//! Returns true is user is currently resizing with the handles
107-
bool isResizing() { return mIsResizing; }
108+
bool isResizing() const { return mIsResizing; }
109+
110+
bool shouldBlockEvent( QInputEvent *event ) const;
108111

109112
protected:
110113

‎src/gui/layout/qgslayoutview.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ QList<int> QgsLayoutView::visiblePageNumbers() const
220220
return currentLayout()->pageCollection()->visiblePageNumbers( visibleRect );
221221
}
222222

223+
///@cond PRIVATE
224+
QgsLayoutMouseHandles *QgsLayoutView::mouseHandles()
225+
{
226+
return mMouseHandles;
227+
}
228+
///@endcond
229+
223230
void QgsLayoutView::zoomFull()
224231
{
225232
fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
@@ -273,6 +280,17 @@ void QgsLayoutView::mousePressEvent( QMouseEvent *event )
273280
{
274281
mSnapMarker->setVisible( false );
275282

283+
if ( mMouseHandles->shouldBlockEvent( event ) )
284+
{
285+
//ignore clicks while dragging/resizing items
286+
return;
287+
}
288+
else if ( mMouseHandles->isVisible() )
289+
{
290+
QGraphicsView::mousePressEvent( event );
291+
return;
292+
}
293+
276294
if ( mTool )
277295
{
278296
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
@@ -306,6 +324,13 @@ void QgsLayoutView::mousePressEvent( QMouseEvent *event )
306324

307325
void QgsLayoutView::mouseReleaseEvent( QMouseEvent *event )
308326
{
327+
if ( event->button() != Qt::LeftButton &&
328+
mMouseHandles->shouldBlockEvent( event ) )
329+
{
330+
//ignore clicks while dragging/resizing items
331+
return;
332+
}
333+
309334
if ( mTool )
310335
{
311336
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
@@ -363,6 +388,12 @@ void QgsLayoutView::mouseDoubleClickEvent( QMouseEvent *event )
363388

364389
void QgsLayoutView::wheelEvent( QWheelEvent *event )
365390
{
391+
if ( mMouseHandles->shouldBlockEvent( event ) )
392+
{
393+
//ignore wheel events while dragging/resizing items
394+
return;
395+
}
396+
366397
if ( mTool )
367398
{
368399
mTool->wheelEvent( event );
@@ -377,6 +408,11 @@ void QgsLayoutView::wheelEvent( QWheelEvent *event )
377408

378409
void QgsLayoutView::keyPressEvent( QKeyEvent *event )
379410
{
411+
if ( mMouseHandles->isDragging() || mMouseHandles->isResizing() )
412+
{
413+
return;
414+
}
415+
380416
if ( mTool )
381417
{
382418
mTool->keyPressEvent( event );

‎src/gui/layout/qgslayoutview.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
158158
*/
159159
QList< int > visiblePageNumbers() const;
160160

161+
///@cond PRIVATE
162+
163+
/**
164+
* Returns the view's mouse handles.
165+
* \note Not available in Python bindings.
166+
*/
167+
SIP_SKIP QgsLayoutMouseHandles *mouseHandles();
168+
///@endcond
169+
161170
public slots:
162171

163172
/**

0 commit comments

Comments
 (0)
Please sign in to comment.