Skip to content

Commit

Permalink
[processing] Constrain model item resizing to prevent items being res…
Browse files Browse the repository at this point in the history
…ized

to a too small size
  • Loading branch information
nyalldawson committed Mar 18, 2020
1 parent d64cafe commit 7614de4
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
Expand Up @@ -108,7 +108,7 @@ Shows a preview of moving the item from its stored position by ``dx``, ``dy``.
Sets a new scene ``rect`` for the item.
%End

void previewItemRectChange( QRectF rect );
QRectF previewItemRectChange( QRectF rect );
%Docstring
Shows a preview of setting a new ``rect`` for the item.
%End
Expand Down
11 changes: 10 additions & 1 deletion src/gui/processing/models/qgsmodelcomponentgraphicitem.cpp
Expand Up @@ -146,15 +146,24 @@ void QgsModelComponentGraphicItem::setItemRect( QRectF )
emit updateArrowPaths();
}

void QgsModelComponentGraphicItem::previewItemRectChange( QRectF rect )
QRectF QgsModelComponentGraphicItem::previewItemRectChange( QRectF rect )
{
rect = rect.normalized();

if ( rect.width() < MIN_COMPONENT_WIDTH )
rect.setWidth( MIN_COMPONENT_WIDTH );
if ( rect.height() < MIN_COMPONENT_HEIGHT )
rect.setHeight( MIN_COMPONENT_HEIGHT );

setPos( rect.center() );
prepareGeometryChange();

mTempSize = rect.size();

updateButtonPositions();
emit updateArrowPaths();

return rect;
}

void QgsModelComponentGraphicItem::modelHoverEnterEvent( QgsModelViewMouseEvent *event )
Expand Down
5 changes: 4 additions & 1 deletion src/gui/processing/models/qgsmodelcomponentgraphicitem.h
Expand Up @@ -133,7 +133,7 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
/**
* Shows a preview of setting a new \a rect for the item.
*/
void previewItemRectChange( QRectF rect );
QRectF previewItemRectChange( QRectF rect );

#ifndef SIP_RUN

Expand Down Expand Up @@ -358,6 +358,9 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;

static constexpr double MIN_COMPONENT_WIDTH = 70;
static constexpr double MIN_COMPONENT_HEIGHT = 50;

static constexpr double DEFAULT_BUTTON_WIDTH = 16;
static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
static constexpr double BUTTON_MARGIN = 2;
Expand Down
5 changes: 3 additions & 2 deletions src/gui/processing/models/qgsmodelviewmousehandles.cpp
Expand Up @@ -141,12 +141,13 @@ void QgsModelViewMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
}
}

void QgsModelViewMouseHandles::previewSetItemRect( QGraphicsItem *item, QRectF rect )
QRectF QgsModelViewMouseHandles::previewSetItemRect( QGraphicsItem *item, QRectF rect )
{
if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
{
componentItem->previewItemRectChange( rect );
return componentItem->previewItemRectChange( rect );
}
return rect;
}

void QgsModelViewMouseHandles::startMacroCommand( const QString &text )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/processing/models/qgsmodelviewmousehandles.h
Expand Up @@ -62,7 +62,7 @@ class GUI_EXPORT QgsModelViewMouseHandles: public QgsGraphicsViewMouseHandles
void moveItem( QGraphicsItem *item, double deltaX, double deltaY ) override;
void previewItemMove( QGraphicsItem *item, double deltaX, double deltaY ) override;
void setItemRect( QGraphicsItem *item, QRectF rect ) override;
void previewSetItemRect( QGraphicsItem *item, QRectF rect ) override;
QRectF previewSetItemRect( QGraphicsItem *item, QRectF rect ) override;
void startMacroCommand( const QString &text ) override;
void endMacroCommand() override;

Expand Down
12 changes: 7 additions & 5 deletions src/gui/qgsgraphicsviewmousehandles.cpp
Expand Up @@ -65,9 +65,9 @@ void QgsGraphicsViewMouseHandles::previewItemMove( QGraphicsItem *, double, doub

}

void QgsGraphicsViewMouseHandles::previewSetItemRect( QGraphicsItem *, QRectF )
QRectF QgsGraphicsViewMouseHandles::previewSetItemRect( QGraphicsItem *, QRectF )
{

return QRectF();
}

void QgsGraphicsViewMouseHandles::startMacroCommand( const QString & )
Expand Down Expand Up @@ -1025,19 +1025,21 @@ void QgsGraphicsViewMouseHandles::resizeMouseMove( QPointF currentPosition, bool
mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), -( mBeginHandleHeight + ry ) ), QPointF( 0, 0 ) );
}

setRect( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) );

const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
QRectF newHandleBounds;
for ( QGraphicsItem *item : selectedItems )
{
//get stored item bounds in mouse handle item's coordinate system
QRectF thisItemRect = mapRectFromScene( storedItemRect( item ) );
//now, resize it relative to the current resized dimensions of the mouse handles
relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );

previewSetItemRect( item, mapRectToScene( thisItemRect ) );
thisItemRect = mapRectFromScene( previewSetItemRect( item, mapRectToScene( thisItemRect ) ) );
newHandleBounds = newHandleBounds.isValid() ? newHandleBounds.united( thisItemRect ) : thisItemRect;
}

setRect( newHandleBounds.isValid() ? newHandleBounds : QRectF( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) ) );

//show current size of selection in status bar
showStatusMessage( tr( "width: %1 mm height: %2 mm" ).arg( rect().width() ).arg( rect().height() ) );

Expand Down
12 changes: 11 additions & 1 deletion src/gui/qgsgraphicsviewmousehandles.h
Expand Up @@ -120,7 +120,17 @@ class GUI_EXPORT QgsGraphicsViewMouseHandles: public QObject, public QGraphicsRe
virtual void moveItem( QGraphicsItem *item, double deltaX, double deltaY ) = 0;
virtual void previewItemMove( QGraphicsItem *item, double deltaX, double deltaY );
virtual void setItemRect( QGraphicsItem *item, QRectF rect ) = 0;
virtual void previewSetItemRect( QGraphicsItem *item, QRectF rect );

/**
* Called when a resize or move action is in progress and the effects can be previewed for the specified \a item. The
* \a rect argument gives the new "transient" rectangular bounds of \a item (in item coordinates).
*
* If implemented, the method should return the item's calculated desired rect given the specified \a rect. This allows
* an item to override the rect results, e.g. by applying a minimum size constraint. The returned value
* should be in the item's coordinates.
*/
virtual QRectF previewSetItemRect( QGraphicsItem *item, QRectF rect );

virtual void startMacroCommand( const QString &text );
virtual void endMacroCommand();
virtual void createItemCommand( QGraphicsItem *item );
Expand Down

0 comments on commit 7614de4

Please sign in to comment.