Skip to content

Commit

Permalink
[processing] Add a dedicated pan tool for navigating models
Browse files Browse the repository at this point in the history
Fixes #40714
  • Loading branch information
nyalldawson committed Jan 15, 2021
1 parent f3413ac commit ac43030
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -327,6 +327,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
Expand Down Expand Up @@ -1082,6 +1083,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
Expand Down
14 changes: 12 additions & 2 deletions src/gui/processing/models/qgsmodeldesignerdialog.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -296,10 +298,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();

Expand Down
5 changes: 5 additions & 0 deletions src/gui/processing/models/qgsmodeldesignerdialog.h
Expand Up @@ -27,6 +27,7 @@ class QgsMessageBar;
class QgsProcessingModelAlgorithm;
class QgsModelUndoCommand;
class QUndoView;
class QgsModelViewToolPan;
class QgsModelViewToolSelect;

///@cond NOT_STABLE
Expand Down Expand Up @@ -163,6 +164,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;

Expand Down
89 changes: 89 additions & 0 deletions src/gui/processing/models/qgsmodelviewtoolpan.cpp
@@ -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();
}
55 changes: 55 additions & 0 deletions src/gui/processing/models/qgsmodelviewtoolpan.h
@@ -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
38 changes: 36 additions & 2 deletions src/ui/processing/qgsmodeldesignerdialogbase.ui
Expand Up @@ -115,6 +115,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"/>
Expand Down Expand Up @@ -696,6 +699,39 @@
<string>Reorder Model Inputs…</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>
Expand Down Expand Up @@ -739,8 +775,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 ac43030

Please sign in to comment.