Skip to content
Permalink
Browse files

[processing] Add a dedicated pan tool for navigating models

Fixes #40714
  • Loading branch information
nyalldawson committed Dec 23, 2020
1 parent 0758e57 commit 590f8d140d76e5b5c6eaa001f7e5f654a03f3fea
@@ -336,6 +336,7 @@ set(QGIS_GUI_SRCS
processing/models/qgsmodelviewmousehandles.cpp
processing/models/qgsmodelviewrubberband.cpp
processing/models/qgsmodelviewtool.cpp
processing/models/qgsmodelviewtoolpan.cpp
processing/models/qgsmodelviewtoolselect.cpp
processing/models/qgsmodelviewtooltemporarykeypan.cpp
processing/models/qgsmodelviewtooltemporarykeyzoom.cpp
@@ -1117,6 +1118,7 @@ set(QGIS_GUI_HDRS
processing/models/qgsmodelviewmousehandles.h
processing/models/qgsmodelviewrubberband.h
processing/models/qgsmodelviewtool.h
processing/models/qgsmodelviewtoolpan.h
processing/models/qgsmodelviewtoolselect.h
processing/models/qgsmodelviewtooltemporarykeypan.h
processing/models/qgsmodelviewtooltemporarykeyzoom.h
@@ -25,6 +25,7 @@
#include "qgsprocessingparametertype.h"
#include "qgsmodelundocommand.h"
#include "qgsmodelviewtoolselect.h"
#include "qgsmodelviewtoolpan.h"
#include "qgsmodelgraphicsscene.h"
#include "qgsmodelcomponentgraphicitem.h"
#include "processing/models/qgsprocessingmodelgroupbox.h"
@@ -75,6 +76,7 @@ Qt::DropActions QgsModelerToolboxModel::supportedDragActions() const

QgsModelDesignerDialog::QgsModelDesignerDialog( QWidget *parent, Qt::WindowFlags flags )
: QMainWindow( parent, flags )
, mToolsActionGroup( new QActionGroup( this ) )
{
setupUi( this );

@@ -308,10 +310,18 @@ QgsModelDesignerDialog::QgsModelDesignerDialog( QWidget *parent, Qt::WindowFlags
mActionShowComments->setChecked( settings.value( QStringLiteral( "/Processing/Modeler/ShowComments" ), true ).toBool() );
connect( mActionShowComments, &QAction::toggled, this, &QgsModelDesignerDialog::toggleComments );

mPanTool = new QgsModelViewToolPan( mView );
mPanTool->setAction( mActionPan );

mToolsActionGroup->addAction( mActionPan );
connect( mActionPan, &QAction::triggered, mPanTool, [ = ] { mView->setTool( mPanTool ); } );

mSelectTool = new QgsModelViewToolSelect( mView );
#if 0
mSelectTool->setAction( mActionSelectMoveItem );
#endif

mToolsActionGroup->addAction( mActionSelectMoveItem );
connect( mActionSelectMoveItem, &QAction::triggered, mSelectTool, [ = ] { mView->setTool( mSelectTool ); } );

mView->setTool( mSelectTool );
mView->setFocus();

@@ -27,6 +27,7 @@ class QgsMessageBar;
class QgsProcessingModelAlgorithm;
class QgsModelUndoCommand;
class QUndoView;
class QgsModelViewToolPan;
class QgsModelViewToolSelect;

///@cond NOT_STABLE
@@ -164,6 +165,10 @@ class GUI_EXPORT QgsModelDesignerDialog : public QMainWindow, public Ui::QgsMode

QgsMessageBar *mMessageBar = nullptr;
QgsModelerToolboxModel *mAlgorithmsModel = nullptr;

QActionGroup *mToolsActionGroup = nullptr;

QgsModelViewToolPan *mPanTool = nullptr;
QgsModelViewToolSelect *mSelectTool = nullptr;
QgsModelGraphicsScene *mScene = nullptr;

@@ -0,0 +1,89 @@
/***************************************************************************
qgsmodelviewtoolpan.cpp
------------------------------------
Date : March 2020
Copyright : (C) 2020 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmodelviewtoolpan.h"
#include "qgsmodelviewmouseevent.h"
#include "qgsmodelgraphicsview.h"
#include <QScrollBar>

QgsModelViewToolPan::QgsModelViewToolPan( QgsModelGraphicsView *view )
: QgsModelViewTool( view, tr( "Pan" ) )
{
setCursor( Qt::OpenHandCursor );
}

void QgsModelViewToolPan::modelPressEvent( QgsModelViewMouseEvent *event )
{
mMousePressStartPos = event->pos();

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

mIsPanning = true;
mLastMousePos = event->pos();
view()->setCursor( Qt::ClosedHandCursor );
}

void QgsModelViewToolPan::modelMoveEvent( QgsModelViewMouseEvent *event )
{
if ( !mIsPanning )
{
event->ignore();
return;
}

view()->horizontalScrollBar()->setValue( view()->horizontalScrollBar()->value() - ( event->x() - mLastMousePos.x() ) );
view()->verticalScrollBar()->setValue( view()->verticalScrollBar()->value() - ( event->y() - mLastMousePos.y() ) );
mLastMousePos = event->pos();
}

void QgsModelViewToolPan::modelReleaseEvent( QgsModelViewMouseEvent *event )
{
bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );

if ( event->button() == Qt::MiddleButton && clickOnly )
{
//middle mouse button click = recenter on point

//get current visible part of scene
QRect viewportRect( 0, 0, view()->viewport()->width(), view()->viewport()->height() );
QgsRectangle visibleRect = QgsRectangle( view()->mapToScene( viewportRect ).boundingRect() );
QPointF scenePoint = event->modelPoint();
visibleRect.scale( 1, scenePoint.x(), scenePoint.y() );
QRectF boundsRect = visibleRect.toRectF();

//zoom view to fit desired bounds
view()->fitInView( boundsRect, Qt::KeepAspectRatio );
return;
}

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

mIsPanning = false;
view()->setCursor( Qt::OpenHandCursor );
}

void QgsModelViewToolPan::deactivate()
{
mIsPanning = false;
QgsModelViewTool::deactivate();
}
@@ -0,0 +1,55 @@
/***************************************************************************
qgsmodelviewtoolpan.h
----------------------------------
Date : December 2020
Copyright : (C) 2020 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSMODELVIEWTOOLPAN_H
#define QGSMODELVIEWTOOLPAN_H

#include "qgis_sip.h"
#include "qgis_gui.h"
#include "qgsmodelviewtool.h"

#define SIP_NO_FILE

/**
* \ingroup gui
* Model designer view tool for panning a model.
* \since QGIS 3.18
*/
class GUI_EXPORT QgsModelViewToolPan : public QgsModelViewTool
{

Q_OBJECT

public:

/**
* Constructor for QgsModelViewToolPan.
*/
QgsModelViewToolPan( QgsModelGraphicsView *view SIP_TRANSFERTHIS );

void modelPressEvent( QgsModelViewMouseEvent *event ) override;
void modelMoveEvent( QgsModelViewMouseEvent *event ) override;
void modelReleaseEvent( QgsModelViewMouseEvent *event ) override;
void deactivate() override;

private:

bool mIsPanning = false;
QPoint mLastMousePos;
//! Start position for mouse press
QPoint mMousePressStartPos;

};
#endif // QGSMODELVIEWTOOLPAN_H
@@ -117,6 +117,9 @@
<addaction name="mActionSaveAs"/>
<addaction name="mActionSaveInProject"/>
<addaction name="separator"/>
<addaction name="mActionPan"/>
<addaction name="mActionSelectMoveItem"/>
<addaction name="separator"/>
<addaction name="mActionZoomIn"/>
<addaction name="mActionZoomOut"/>
<addaction name="mActionZoomActual"/>
@@ -709,6 +712,39 @@
<string>Ctrl+Tab</string>
</property>
</action>
<action name="mActionPan">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionPan.svg</normaloff>:/images/themes/default/mActionPan.svg</iconset>
</property>
<property name="text">
<string>Pan Layout</string>
</property>
<property name="shortcut">
<string>P</string>
</property>
</action>
<action name="mActionSelectMoveItem">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionSelect.svg</normaloff>:/images/themes/default/mActionSelect.svg</iconset>
</property>
<property name="text">
<string>Select/Move Item</string>
</property>
<property name="toolTip">
<string>Select/Move item</string>
</property>
<property name="shortcut">
<string>V</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@@ -752,8 +788,6 @@
</customwidgets>
<resources>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit 590f8d1

Please sign in to comment.
You can’t perform that action at this time.