Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved appearance of resized components
  • Loading branch information
nyalldawson committed Mar 12, 2020
1 parent 3b61938 commit 4169c7b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
Expand Up @@ -36,7 +36,6 @@ Base class for graphic items representing model components in the model designer

enum Flag
{
FlagMultilineText,
};
typedef QFlags<QgsModelComponentGraphicItem::Flag> Flags;

Expand Down Expand Up @@ -285,6 +284,11 @@ Returns a QPixmap version of the item's icon, if available.
virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;
%Docstring
Updates the position and size stored in the model for the associated comment
%End

void updateButtonPositions();
%Docstring
Updates the item's button positions, based on the current item rect.
%End

};
Expand Down Expand Up @@ -485,8 +489,6 @@ Ownership of ``output`` is transferred to the item.
~QgsModelCommentGraphicItem();
virtual void contextMenuEvent( QGraphicsSceneContextMenuEvent *event );

virtual Flags flags() const;

protected:

virtual QColor fillColor( State state ) const;
Expand Down
Expand Up @@ -48,6 +48,11 @@ for the button. The button will be rendered at the specified ``position`` and ``
virtual void mousePressEvent( QGraphicsSceneMouseEvent *event );


void setPosition( const QPointF &position );
%Docstring
Sets the button's ``position``.
%End

QgsModelGraphicsView *view();
%Docstring
Returns the associated model view.
Expand Down
35 changes: 27 additions & 8 deletions src/gui/processing/models/qgsmodelcomponentgraphicitem.cpp
Expand Up @@ -137,6 +137,9 @@ void QgsModelComponentGraphicItem::setItemRect( QRectF )

emit aboutToChange( tr( "Resize %1" ).arg( mComponent->description() ) );
updateStoredComponentPosition( pos(), mComponent->size() );

updateButtonPositions();

emit changed();

emit updateArrowPaths();
Expand All @@ -148,6 +151,7 @@ void QgsModelComponentGraphicItem::previewItemRectChange( QRectF rect )
prepareGeometryChange();
mTempSize = rect.size();

updateButtonPositions();
emit updateArrowPaths();
}

Expand Down Expand Up @@ -260,16 +264,17 @@ void QgsModelComponentGraphicItem::paint( QPainter *painter, const QStyleOptionG
double h = fm.ascent();
QPointF pt( -componentSize.width() / 2 + 25, componentSize.height() / 2.0 - h + 1 );

if ( flags() & FlagMultilineText )
if ( iconPicture().isNull() && iconPixmap().isNull() )
{
QRectF labelRect = QRectF( rect.left() + 4, rect.top() + 4, rect.width() - 8 - mButtonSize.width(), rect.height() - 8 );
text = label();
painter->drawText( labelRect, Qt::TextWordWrap, text );
}
else
{
text = truncatedTextForItem( label() );
painter->drawText( pt, text );
QRectF labelRect = QRectF( rect.left() + 25, rect.top() + 4, rect.width() - 8 - mButtonSize.width(), rect.height() - 8 );
text = label();
painter->drawText( labelRect, Qt::TextWordWrap | Qt::AlignVCenter, text );
}

painter->setPen( QPen( QApplication::palette().color( QPalette::WindowText ) ) );
Expand Down Expand Up @@ -378,6 +383,25 @@ QPixmap QgsModelComponentGraphicItem::iconPixmap() const
return QPixmap();
}

void QgsModelComponentGraphicItem::updateButtonPositions()
{
mEditButton->setPosition( QPointF( itemSize().width() / 2.0 - mButtonSize.width() / 2.0,
itemSize().height() / 2.0 - mButtonSize.height() / 2.0 ) );
mDeleteButton->setPosition( QPointF( itemSize().width() / 2.0 - mButtonSize.width() / 2.0,
mButtonSize.height() / 2.0 - itemSize().height() / 2.0 ) );

if ( mExpandTopButton )
{
QPointF pt = linkPoint( Qt::TopEdge, -1 );
mExpandTopButton->setPosition( QPointF( 0, pt.y() ) );
}
if ( mExpandBottomButton )
{
QPointF pt = linkPoint( Qt::BottomEdge, -1 );
mExpandBottomButton->setPosition( QPointF( 0, pt.y() ) );
}
}

QSizeF QgsModelComponentGraphicItem::itemSize() const
{
return !mTempSize.isValid() ? mComponent->size() : mTempSize;
Expand Down Expand Up @@ -986,11 +1010,6 @@ void QgsModelCommentGraphicItem::contextMenuEvent( QGraphicsSceneContextMenuEven
popupmenu->exec( event->screenPos() );
}

QgsModelCommentGraphicItem::Flags QgsModelCommentGraphicItem::flags() const
{
return FlagMultilineText;
}

QgsModelCommentGraphicItem::~QgsModelCommentGraphicItem() = default;

QColor QgsModelCommentGraphicItem::fillColor( QgsModelComponentGraphicItem::State state ) const
Expand Down
7 changes: 5 additions & 2 deletions src/gui/processing/models/qgsmodelcomponentgraphicitem.h
Expand Up @@ -58,7 +58,6 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
//! Available flags
enum Flag
{
FlagMultilineText = 1 << 0, //!< Show multiline text in label
};
Q_DECLARE_FLAGS( Flags, Flag )

Expand Down Expand Up @@ -300,6 +299,11 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
*/
virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;

/**
* Updates the item's button positions, based on the current item rect.
*/
void updateButtonPositions();

private:

QSizeF itemSize() const;
Expand Down Expand Up @@ -496,7 +500,6 @@ class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicIte
QGraphicsItem *parent SIP_TRANSFERTHIS );
~QgsModelCommentGraphicItem() override;
void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
Flags flags() const override;
protected:

QColor fillColor( State state ) const override;
Expand Down
7 changes: 7 additions & 0 deletions src/gui/processing/models/qgsmodelgraphicitem.cpp
Expand Up @@ -89,6 +89,13 @@ void QgsModelDesignerFlatButtonGraphicItem::mousePressEvent( QGraphicsSceneMouse
emit clicked();
}

void QgsModelDesignerFlatButtonGraphicItem::setPosition( const QPointF &position )
{
mPosition = position;
prepareGeometryChange();
update();
}

QgsModelGraphicsView *QgsModelDesignerFlatButtonGraphicItem::view()
{
return qobject_cast< QgsModelGraphicsView * >( scene()->views().first() );
Expand Down
5 changes: 5 additions & 0 deletions src/gui/processing/models/qgsmodelgraphicitem.h
Expand Up @@ -52,6 +52,11 @@ class GUI_EXPORT QgsModelDesignerFlatButtonGraphicItem : public QGraphicsObject
void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
void mousePressEvent( QGraphicsSceneMouseEvent *event ) override;

/**
* Sets the button's \a position.
*/
void setPosition( const QPointF &position );

/**
* Returns the associated model view.
*/
Expand Down

0 comments on commit 4169c7b

Please sign in to comment.