Skip to content

Commit

Permalink
Move mouse handles to select tool
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 6, 2017
1 parent bfa9c1d commit e0089fb
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 60 deletions.
2 changes: 0 additions & 2 deletions python/gui/layout/qgslayoutview.sip
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ class QgsLayoutView: QGraphicsView
:rtype: list of int
%End



public slots:

void zoomFull();
Expand Down
9 changes: 9 additions & 0 deletions python/gui/layout/qgslayoutviewtoolselect.sip
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@




class QgsLayoutViewToolSelect : QgsLayoutViewTool
{
%Docstring
Expand All @@ -31,9 +32,17 @@ class QgsLayoutViewToolSelect : QgsLayoutViewTool

virtual void layoutReleaseEvent( QgsLayoutViewMouseEvent *event );

virtual void wheelEvent( QWheelEvent *event );

virtual void keyPressEvent( QKeyEvent *event );

virtual void deactivate();




void setLayout( QgsLayout *layout );

};

/************************************************************************
Expand Down
4 changes: 3 additions & 1 deletion src/app/layout/qgslayoutdesignerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ void QgsLayoutDesignerDialog::setCurrentLayout( QgsLayout *layout )
connect( mActionRedo, &QAction::triggered, mLayout->undoStack()->stack(), &QUndoStack::redo );
mUndoView->setStack( mLayout->undoStack()->stack() );

mSelectTool->setLayout( layout );

createLayoutPropertiesWidget();
}

Expand Down Expand Up @@ -440,7 +442,7 @@ void QgsLayoutDesignerDialog::showGrid( bool visible )
void QgsLayoutDesignerDialog::showBoxes( bool visible )
{
mLayout->context().setBoundingBoxesVisible( visible );
mView->mouseHandles()->update();
mSelectTool->mouseHandles()->update();
}

void QgsLayoutDesignerDialog::snapToGrid( bool enabled )
Expand Down
42 changes: 0 additions & 42 deletions src/gui/layout/qgslayoutview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ void QgsLayoutView::setCurrentLayout( QgsLayout *layout )
connect( &layout->guides(), &QAbstractItemModel::modelReset, mVerticalRuler, [ = ] { mVerticalRuler->update(); } );
}

//add mouse selection handles to layout, and initially hide
mMouseHandles = new QgsLayoutMouseHandles( layout, this );
mMouseHandles->hide();
mMouseHandles->setZValue( QgsLayout::ZMouseHandles );
layout->addItem( mMouseHandles );

//emit layoutSet, so that designer dialogs can update for the new layout
emit layoutSet( layout );
}
Expand Down Expand Up @@ -220,13 +214,6 @@ 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 @@ -280,17 +267,6 @@ 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 @@ -324,13 +300,6 @@ 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 @@ -388,12 +357,6 @@ 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 @@ -408,11 +371,6 @@ void QgsLayoutView::wheelEvent( QWheelEvent *event )

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

if ( mTool )
{
mTool->keyPressEvent( event );
Expand Down
12 changes: 0 additions & 12 deletions src/gui/layout/qgslayoutview.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class QgsLayoutViewToolTemporaryMousePan;
class QgsLayoutRuler;
class QgsLayoutViewMenuProvider;
class QgsLayoutViewSnapMarker;
class QgsLayoutMouseHandles;

/**
* \ingroup gui
Expand Down Expand Up @@ -158,15 +157,6 @@ 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 Expand Up @@ -310,8 +300,6 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView

std::unique_ptr< QgsLayoutViewSnapMarker > mSnapMarker;

QgsLayoutMouseHandles *mMouseHandles = nullptr; //owned by scene

int mCurrentPage = 0;

friend class TestQgsLayoutView;
Expand Down
143 changes: 140 additions & 3 deletions src/gui/layout/qgslayoutviewtoolselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgslayoutview.h"
#include "qgslayout.h"
#include "qgslayoutitempage.h"
#include "qgslayoutmousehandles.h"

QgsLayoutViewToolSelect::QgsLayoutViewToolSelect( QgsLayoutView *view )
: QgsLayoutViewTool( view, tr( "Select" ) )
Expand All @@ -31,15 +32,102 @@ QgsLayoutViewToolSelect::QgsLayoutViewToolSelect( QgsLayoutView *view )

void QgsLayoutViewToolSelect::layoutPressEvent( QgsLayoutViewMouseEvent *event )
{
if ( mMouseHandles->shouldBlockEvent( event ) )
{
//swallow clicks while dragging/resizing items
return;
}

if ( mMouseHandles->isVisible() )
{
//selection handles are being shown, get mouse action for current cursor position
QgsLayoutMouseHandles::MouseAction mouseAction = mMouseHandles->mouseActionForScenePos( event->layoutPoint() );

if ( mouseAction != QgsLayoutMouseHandles::MoveItem
&& mouseAction != QgsLayoutMouseHandles::NoAction
&& mouseAction != QgsLayoutMouseHandles::SelectItem )
{
//mouse is over a resize handle, so propagate event onward
event->ignore();
return;
}
}

if ( event->button() != Qt::LeftButton )
{
event->ignore();
return;
}

mIsSelecting = true;
mMousePressStartPos = event->pos();
mRubberBand->start( event->layoutPoint(), 0 );
QgsLayoutItem *selectedItem = nullptr;
QgsLayoutItem *previousSelectedItem = nullptr;

if ( event->modifiers() & Qt::ControlModifier )
{
//CTRL modifier, so we are trying to select the next item below the current one
//first, find currently selected item
QList<QgsLayoutItem *> selectedItems = layout()->selectedLayoutItems();
if ( !selectedItems.isEmpty() )
{
previousSelectedItem = selectedItems.at( 0 );
}
}

if ( previousSelectedItem )
{
//select highest item just below previously selected item at position of event
selectedItem = layout()->layoutItemAt( event->layoutPoint(), previousSelectedItem, true );

//if we didn't find a lower item we'll use the top-most as fall-back
//this duplicates mapinfo/illustrator/etc behavior where ctrl-clicks are "cyclic"
if ( !selectedItem )
{
selectedItem = layout()->layoutItemAt( event->layoutPoint(), true );
}
}
else
{
//select topmost item at position of event
selectedItem = layout()->layoutItemAt( event->layoutPoint(), true );
}

if ( !selectedItem )
{
//not clicking over an item, so start marquee selection
mIsSelecting = true;
mMousePressStartPos = event->pos();
mRubberBand->start( event->layoutPoint(), 0 );
return;
}

if ( ( !selectedItem->isSelected() ) && //keep selection if an already selected item pressed
!( event->modifiers() & Qt::ShiftModifier ) ) //keep selection if shift key pressed
{
layout()->deselectAll();
}

if ( ( event->modifiers() & Qt::ShiftModifier ) && ( selectedItem->isSelected() ) )
{
//SHIFT-clicking a selected item deselects it
selectedItem->setSelected( false );

//Check if we have any remaining selected items, and if so, update the item panel
QList<QgsLayoutItem *> selectedItems = layout()->selectedLayoutItems();
if ( !selectedItems.isEmpty() )
{
#if 0 //TODO
emit selectedItemChanged( selectedItems.at( 0 ) );
#endif
}
}
else
{
selectedItem->setSelected( true );
event->ignore();
#if 0 //TODO
emit selectedItemChanged( selectedItem );
#endif
}
}

void QgsLayoutViewToolSelect::layoutMoveEvent( QgsLayoutViewMouseEvent *event )
Expand All @@ -56,6 +144,12 @@ void QgsLayoutViewToolSelect::layoutMoveEvent( QgsLayoutViewMouseEvent *event )

void QgsLayoutViewToolSelect::layoutReleaseEvent( QgsLayoutViewMouseEvent *event )
{
if ( event->button() != Qt::LeftButton && mMouseHandles->shouldBlockEvent( event ) )
{
//swallow clicks while dragging/resizing items
return;
}

if ( !mIsSelecting || event->button() != Qt::LeftButton )
{
event->ignore();
Expand Down Expand Up @@ -132,6 +226,31 @@ void QgsLayoutViewToolSelect::layoutReleaseEvent( QgsLayoutViewMouseEvent *event
#endif
}

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

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

void QgsLayoutViewToolSelect::deactivate()
{
if ( mIsSelecting )
Expand All @@ -141,3 +260,21 @@ void QgsLayoutViewToolSelect::deactivate()
}
QgsLayoutViewTool::deactivate();
}

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

void QgsLayoutViewToolSelect::setLayout( QgsLayout *layout )
{
// existing handles are owned by previous layout

//add mouse selection handles to layout, and initially hide
mMouseHandles = new QgsLayoutMouseHandles( layout, view() );
mMouseHandles->hide();
mMouseHandles->setZValue( QgsLayout::ZMouseHandles );
layout->addItem( mMouseHandles );
}
///@endcond
16 changes: 16 additions & 0 deletions src/gui/layout/qgslayoutviewtoolselect.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "qgslayoutviewrubberband.h"
#include <memory>

class QgsLayoutMouseHandles;

/**
* \ingroup gui
* Layout view tool for selecting items in the layout.
Expand All @@ -42,8 +44,21 @@ class GUI_EXPORT QgsLayoutViewToolSelect : public QgsLayoutViewTool
void layoutPressEvent( QgsLayoutViewMouseEvent *event ) override;
void layoutMoveEvent( QgsLayoutViewMouseEvent *event ) override;
void layoutReleaseEvent( QgsLayoutViewMouseEvent *event ) override;
void wheelEvent( QWheelEvent *event ) override;
void keyPressEvent( QKeyEvent *event ) override;
void deactivate() override;

///@cond PRIVATE

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

void setLayout( QgsLayout *layout );

private:

bool mIsSelecting = false;
Expand All @@ -57,6 +72,7 @@ class GUI_EXPORT QgsLayoutViewToolSelect : public QgsLayoutViewTool
//! Start of rubber band creation
QPointF mRubberBandStartPos;

QgsLayoutMouseHandles *mMouseHandles = nullptr; //owned by scene
};

#endif // QGSLAYOUTVIEWTOOLSELECT_H

0 comments on commit e0089fb

Please sign in to comment.