Skip to content
Permalink
Browse files

Start work on model designer mouse handles

  • Loading branch information
nyalldawson committed Mar 11, 2020
1 parent 28aea7e commit 9044acd149c66dfacfc7b544010a2bdeee2ce74d
@@ -31,8 +31,10 @@ QGraphicsScene subclass representing the model designer.
{
ArrowLink,
ModelComponent,
MouseHandles,
RubberBand,
ZSnapIndicator,

};

enum Flag
@@ -285,6 +285,7 @@ SET(QGIS_GUI_SRCS
processing/models/qgsmodelsnapper.cpp
processing/models/qgsmodelundocommand.cpp
processing/models/qgsmodelviewmouseevent.cpp
processing/models/qgsmodelviewmousehandles.cpp
processing/models/qgsmodelviewrubberband.cpp
processing/models/qgsmodelviewtool.cpp
processing/models/qgsmodelviewtoolselect.cpp
@@ -957,6 +958,7 @@ SET(QGIS_GUI_HDRS
processing/models/qgsmodelsnapper.h
processing/models/qgsmodelundocommand.h
processing/models/qgsmodelviewmouseevent.h
processing/models/qgsmodelviewmousehandles.h
processing/models/qgsmodelviewrubberband.h
processing/models/qgsmodelviewtool.h
processing/models/qgsmodelviewtoolselect.h
@@ -89,9 +89,6 @@ class GUI_EXPORT QgsLayoutMouseHandles: public QgsGraphicsViewMouseHandles
//! Sets up listeners to sizeChanged signal for all selected items
void selectionChanged();

//! Redraws handles when selected item rotation changes
void selectedItemRotationChanged();

private:

QgsLayout *mLayout = nullptr;
@@ -48,8 +48,10 @@ class GUI_EXPORT QgsModelGraphicsScene : public QGraphicsScene
{
ArrowLink = 0, //!< An arrow linking model items
ModelComponent = 1, //!< Model components (e.g. algorithms, inputs and outputs)
MouseHandles = 99, //!< Mouse handles
RubberBand = 100, //!< Rubber band item
ZSnapIndicator = 101, //!< Z-value for snapping indicator

};

//! Flags for controlling how the scene is rendered and scene behavior
@@ -0,0 +1,136 @@
/***************************************************************************
qgsmodelviewmousehandles.cpp
------------------------
begin : March 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall.dawson@gmail.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 "qgsmodelviewmousehandles.h"
#include "qgis.h"
#include "qgslogger.h"
#include "qgsproject.h"
#include "qgsmodelgraphicsview.h"
#include "qgsmodelgraphicsscene.h"
#include "qgsmodelcomponentgraphicitem.h"
#include "qgsmodelviewtool.h"
#include <QGraphicsSceneHoverEvent>
#include <QPainter>
#include <QWidget>
#include <limits>

///@cond PRIVATE


QgsModelViewMouseHandles::QgsModelViewMouseHandles( QgsModelGraphicsView *view )
: QgsGraphicsViewMouseHandles( view )
, mView( view )
{
//listen for selection changes, and update handles accordingly
connect( modelScene(), &QGraphicsScene::selectionChanged, this, &QgsModelViewMouseHandles::selectionChanged );
}

QgsModelGraphicsScene *QgsModelViewMouseHandles::modelScene() const
{
return qobject_cast< QgsModelGraphicsScene * >( mView->scene() );
}

void QgsModelViewMouseHandles::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
paintInternal( painter, !( modelScene()->flags() & QgsModelGraphicsScene::FlagHideControls ),
!( modelScene()->flags() & QgsModelGraphicsScene::FlagHideControls ), option, widget );
}

void QgsModelViewMouseHandles::selectionChanged()
{
#if 0
//listen out for selected items' size and rotation changed signals
const QList<QGraphicsItem *> itemList = layout()->items();
for ( QGraphicsItem *graphicsItem : itemList )
{
QgsLayoutItem *item = dynamic_cast<QgsLayoutItem *>( graphicsItem );
if ( !item )
continue;

if ( item->isSelected() )
{
connect( item, &QgsLayoutItem::sizePositionChanged, this, &QgsModelViewMouseHandles::selectedItemSizeChanged );
connect( item, &QgsLayoutItem::rotationChanged, this, &QgsModelViewMouseHandles::selectedItemRotationChanged );
connect( item, &QgsLayoutItem::frameChanged, this, &QgsModelViewMouseHandles::selectedItemSizeChanged );
connect( item, &QgsLayoutItem::lockChanged, this, &QgsModelViewMouseHandles::selectedItemSizeChanged );
}
else
{
disconnect( item, &QgsLayoutItem::sizePositionChanged, this, &QgsModelViewMouseHandles::selectedItemSizeChanged );
disconnect( item, &QgsLayoutItem::rotationChanged, this, &QgsModelViewMouseHandles::selectedItemRotationChanged );
disconnect( item, &QgsLayoutItem::frameChanged, this, &QgsModelViewMouseHandles::selectedItemSizeChanged );
disconnect( item, &QgsLayoutItem::lockChanged, this, &QgsModelViewMouseHandles::selectedItemSizeChanged );
}
}

#endif
resetStatusBar();
updateHandles();
}

void QgsModelViewMouseHandles::setViewportCursor( Qt::CursorShape cursor )
{
//workaround qt bug #3732 by setting cursor for QGraphicsView viewport,
//rather then setting it directly here

if ( qobject_cast< QgsModelViewTool *>( mView->tool() ) )
{
mView->viewport()->setCursor( cursor );
}
}

QList<QGraphicsItem *> QgsModelViewMouseHandles::sceneItemsAtPoint( QPointF scenePoint )
{
return modelScene()->items( scenePoint );
}

QList<QGraphicsItem *> QgsModelViewMouseHandles::selectedSceneItems( bool ) const
{
QList<QGraphicsItem *> res;
const QList<QgsModelComponentGraphicItem *> componentItems = modelScene()->selectedComponentItems();
res.reserve( componentItems.size() );
for ( QgsModelComponentGraphicItem *item : componentItems )
res << item;
return res;
}

QRectF QgsModelViewMouseHandles::itemRect( QGraphicsItem *item ) const
{
if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
return componentItem->itemRect();
else
return QRectF();
}

void QgsModelViewMouseHandles::moveItem( QGraphicsItem *item, double deltaX, double deltaY )
{
if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
{
componentItem->moveComponentBy( deltaX, deltaY );
}
}

void QgsModelViewMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
{
if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
{

}
}


///@endcond PRIVATE
@@ -0,0 +1,77 @@
/***************************************************************************
qgsmodelviewmousehandles.h
-----------------------
begin : March 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall.dawson@gmail.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 QGSMODELVIEWMOUSEHANDLES_H
#define QGSMODELVIEWMOUSEHANDLES_H

#define SIP_NO_FILE

#include "qgsgraphicsviewmousehandles.h"
#include <QPointer>
#include <memory>

#include "qgis_gui.h"

class QgsModelGraphicsView;
class QgsModelGraphicsScene;
class QInputEvent;

class QgsLayoutItem;

///@cond PRIVATE

/**
* \ingroup gui
* Handles drawing of selection outlines and mouse handles in a QgsModelGraphicsView
*
* Also is responsible for mouse interactions such as resizing and moving selected items.
*
* \note not available in Python bindings
*
* \since QGIS 3.14
*/
class GUI_EXPORT QgsModelViewMouseHandles: public QgsGraphicsViewMouseHandles
{
Q_OBJECT
public:

QgsModelViewMouseHandles( QgsModelGraphicsView *view );

void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;

protected:

void setViewportCursor( Qt::CursorShape cursor ) override;
QList<QGraphicsItem *> sceneItemsAtPoint( QPointF scenePoint ) override;
QList<QGraphicsItem *> selectedSceneItems( bool includeLockedItems = true ) const override;
QRectF itemRect( QGraphicsItem *item ) const override;
void moveItem( QGraphicsItem *item, double deltaX, double deltaY ) override;
void setItemRect( QGraphicsItem *item, QRectF rect ) override;

public slots:

//! Sets up listeners to sizeChanged signal for all selected items
void selectionChanged();

private:
QgsModelGraphicsScene *modelScene() const;

QPointer< QgsModelGraphicsView > mView;
};

///@endcond PRIVATE

#endif // QGSMODELVIEWMOUSEHANDLES_H
@@ -19,6 +19,7 @@
#include "qgsprocessingmodelalgorithm.h"
#include "qgsmodelgraphicsscene.h"
#include "qgsmodelcomponentgraphicitem.h"
#include "qgsmodelviewmousehandles.h"

QgsModelViewToolSelect::QgsModelViewToolSelect( QgsModelGraphicsView *view )
: QgsModelViewTool( view, tr( "Select" ) )
@@ -32,20 +33,17 @@ QgsModelViewToolSelect::QgsModelViewToolSelect( QgsModelGraphicsView *view )

QgsModelViewToolSelect::~QgsModelViewToolSelect()
{
#if 0
if ( mMouseHandles )
{
// want to force them to be removed from the scene
if ( mMouseHandles->scene() )
mMouseHandles->scene()->removeItem( mMouseHandles );
mMouseHandles->deleteLater();
}
#endif
}

void QgsModelViewToolSelect::modelPressEvent( QgsModelViewMouseEvent *event )
{
#if 0
if ( mMouseHandles->shouldBlockEvent( event ) )
{
//swallow clicks while dragging/resizing items
@@ -55,18 +53,17 @@ void QgsModelViewToolSelect::modelPressEvent( QgsModelViewMouseEvent *event )
if ( mMouseHandles->isVisible() )
{
//selection handles are being shown, get mouse action for current cursor position
QgsLayoutMouseHandles::MouseAction mouseAction = mMouseHandles->mouseActionForScenePos( event->layoutPoint() );
QgsGraphicsViewMouseHandles::MouseAction mouseAction = mMouseHandles->mouseActionForScenePos( event->modelPoint() );

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

if ( event->button() != Qt::LeftButton )
{
@@ -139,13 +136,11 @@ void QgsModelViewToolSelect::modelMoveEvent( QgsModelViewMouseEvent *event )

void QgsModelViewToolSelect::modelReleaseEvent( QgsModelViewMouseEvent *event )
{
#if 0
if ( event->button() != Qt::LeftButton && mMouseHandles->shouldBlockEvent( event ) )
{
//swallow clicks while dragging/resizing items
return;
}
#endif

if ( !mIsSelecting || event->button() != Qt::LeftButton )
{
@@ -220,36 +215,29 @@ void QgsModelViewToolSelect::modelReleaseEvent( QgsModelViewMouseEvent *event )
{
emit itemFocused( nullptr );
}
#if 0
mMouseHandles->selectionChanged();
#endif
}

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

void QgsModelViewToolSelect::keyPressEvent( QKeyEvent *event )
{

#if 0
if ( mMouseHandles->isDragging() || mMouseHandles->isResizing() )
{
return;
}
else
#endif
{
event->ignore();
}
@@ -272,21 +260,19 @@ bool QgsModelViewToolSelect::allowItemInteraction()

QgsModelViewMouseHandles *QgsModelViewToolSelect::mouseHandles()
{
return nullptr; //mMouseHandles;
return mMouseHandles;
}

void QgsModelViewToolSelect::setScene( QgsModelGraphicsScene *model )
void QgsModelViewToolSelect::setScene( QgsModelGraphicsScene *scene )
{
#if 0
// existing handles are owned by previous layout
if ( mMouseHandles )
mMouseHandles->deleteLater();

//add mouse selection handles to layout, and initially hide
mMouseHandles = new QgsLayoutMouseHandles( layout, view() );
//add mouse selection handles to scene, and initially hide
mMouseHandles = new QgsModelViewMouseHandles( view() );
mMouseHandles->hide();
mMouseHandles->setZValue( QgsLayout::ZMouseHandles );
layout->addItem( mMouseHandles );
#endif
mMouseHandles->setZValue( QgsModelGraphicsScene::MouseHandles );
scene->addItem( mMouseHandles );
}
///@endcond

0 comments on commit 9044acd

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