Skip to content
Permalink
Browse files

[processing] Constrain model item resizing to prevent items being res…

…ized

to a too small size
  • Loading branch information
nyalldawson committed Mar 17, 2020
1 parent d64cafe commit 7614de451e4df0bf0d3a4c5cc037609c40781d85
@@ -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
@@ -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 )
@@ -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

@@ -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;
@@ -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 )
@@ -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;

@@ -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 & )
@@ -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() ) );

@@ -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 );

0 comments on commit 7614de4

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