Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Nicer comment text appearance
  • Loading branch information
nyalldawson committed Mar 5, 2020
1 parent 961557d commit 0af98f9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 30 deletions.
Expand Up @@ -34,6 +34,13 @@ Base class for graphic items representing model components in the model designer
Hover,
};

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


QgsModelComponentGraphicItem( QgsProcessingModelComponent *component /Transfer/,
QgsProcessingModelAlgorithm *model,
QGraphicsItem *parent /TransferThis/ );
Expand All @@ -48,6 +55,11 @@ Ownership of ``component`` is transferred to the item.

~QgsModelComponentGraphicItem();

virtual Flags flags() const;
%Docstring
Returns item flags.
%End

QgsProcessingModelComponent *component();
%Docstring
Returns the model component associated with this item.
Expand Down Expand Up @@ -238,6 +250,8 @@ Updates the position stored in the model for the associated comment
%End

};
QFlags<QgsModelComponentGraphicItem::Flag> operator|(QgsModelComponentGraphicItem::Flag f1, QFlags<QgsModelComponentGraphicItem::Flag> f2);


class QgsModelParameterGraphicItem : QgsModelComponentGraphicItem
{
Expand Down Expand Up @@ -433,6 +447,7 @@ Ownership of ``output`` is transferred to the item.
~QgsModelCommentGraphicItem();
virtual void contextMenuEvent( QGraphicsSceneContextMenuEvent *event );

virtual Flags flags() const;

protected:

Expand Down
23 changes: 4 additions & 19 deletions python/plugins/processing/modeler/ModelerGraphicItem.py
Expand Up @@ -144,15 +144,15 @@ def updateAlgorithm(self, alg):
alg.setPosition(existing_child.position())
alg.setLinksCollapsed(Qt.TopEdge, existing_child.linksCollapsed(Qt.TopEdge))
alg.setLinksCollapsed(Qt.BottomEdge, existing_child.linksCollapsed(Qt.BottomEdge))
alg.comment().setPosition(existing_child.comment().position() or
alg.position() + QPointF(
alg.comment().setPosition(existing_child.comment().position()
or alg.position() + QPointF(
self.component().size().width(),
-1.5 * self.component().size().height())
)
alg.comment().setSize(existing_child.comment().size())
for i, out in enumerate(alg.modelOutputs().keys()):
alg.modelOutput(out).setPosition(existing_child.modelOutput(out).position() or
alg.position() + QPointF(
alg.modelOutput(out).setPosition(existing_child.modelOutput(out).position()
or alg.position() + QPointF(
self.component().size().width(),
(i + 1.5) * self.component().size().height()))
alg.modelOutput(out).comment().setDescription(existing_child.modelOutput(out).comment().description())
Expand Down Expand Up @@ -196,18 +196,3 @@ def editComponent(self):

def editComment(self):
self.edit(edit_comment=True)


class ModelerCommentGraphicItem(QgsModelCommentGraphicItem):
"""
IMPORTANT! This is intentionally a MINIMAL class, only containing code which HAS TO BE HERE
because it contains Python code for compatibility with deprecated methods ONLY.
Don't add anything here -- edit the c++ base class instead!
"""

def __init__(self, model, element, parent_component):
super().__init__(element, parent_component, model, None)

def editComponent(self):
pass
6 changes: 1 addition & 5 deletions python/plugins/processing/modeler/ModelerScene.py
Expand Up @@ -25,8 +25,7 @@
from processing.modeler.ModelerGraphicItem import (
ModelerInputGraphicItem,
ModelerOutputGraphicItem,
ModelerChildAlgorithmGraphicItem,
ModelerCommentGraphicItem
ModelerChildAlgorithmGraphicItem
)


Expand All @@ -49,6 +48,3 @@ def createChildAlgGraphicItem(self, model, child):

def createOutputGraphicItem(self, model, output):
return ModelerOutputGraphicItem(output.clone(), model)

#def createCommentGraphicItem(self, model, comment, parent):
# return ModelerCommentGraphicItem(model, comment.clone(), parent)
38 changes: 33 additions & 5 deletions src/gui/processing/models/qgsmodelcomponentgraphicitem.cpp
Expand Up @@ -67,6 +67,11 @@ QgsModelComponentGraphicItem::QgsModelComponentGraphicItem( QgsProcessingModelCo
connect( mDeleteButton, &QgsModelDesignerFlatButtonGraphicItem::clicked, this, &QgsModelComponentGraphicItem::deleteComponent );
}

QgsModelComponentGraphicItem::Flags QgsModelComponentGraphicItem::flags() const
{
return nullptr;
}

QgsModelComponentGraphicItem::~QgsModelComponentGraphicItem() = default;

QgsProcessingModelComponent *QgsModelComponentGraphicItem::component()
Expand Down Expand Up @@ -198,14 +203,26 @@ void QgsModelComponentGraphicItem::paint( QPainter *painter, const QStyleOptionG
painter->setFont( font() );
painter->setPen( QPen( textColor( state() ) ) );

QString text = truncatedTextForItem( label() );
QString text;

const QSizeF componentSize = mComponent->size();

QFontMetricsF fm( font() );
double h = fm.ascent();
QPointF pt( -componentSize.width() / 2 + 25, componentSize.height() / 2.0 - h + 1 );
painter->drawText( pt, text );

if ( flags() & FlagMultilineText )
{
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 );
}

painter->setPen( QPen( QApplication::palette().color( QPalette::WindowText ) ) );

if ( linkPointCount( Qt::TopEdge ) || linkPointCount( Qt::BottomEdge ) )
Expand Down Expand Up @@ -512,8 +529,11 @@ void QgsModelParameterGraphicItem::contextMenuEvent( QGraphicsSceneContextMenuEv
QMenu *popupmenu = new QMenu( event->widget() );
QAction *removeAction = popupmenu->addAction( QObject::tr( "Remove" ) );
connect( removeAction, &QAction::triggered, this, &QgsModelParameterGraphicItem::deleteComponent );
QAction *editAction = popupmenu->addAction( QObject::tr( "Edit" ) );
QAction *editAction = popupmenu->addAction( QObject::tr( "Edit" ) );
connect( editAction, &QAction::triggered, this, &QgsModelParameterGraphicItem::editComponent );
QAction *editCommentAction = popupmenu->addAction( component()->comment()->description().isEmpty() ? QObject::tr( "Add Comment…" ) : QObject::tr( "Edit Comment…" ) );
connect( editCommentAction, &QAction::triggered, this, &QgsModelParameterGraphicItem::editComment );

popupmenu->exec( event->screenPos() );
}

Expand Down Expand Up @@ -616,8 +636,11 @@ void QgsModelChildAlgorithmGraphicItem::contextMenuEvent( QGraphicsSceneContextM
QMenu *popupmenu = new QMenu( event->widget() );
QAction *removeAction = popupmenu->addAction( QObject::tr( "Remove" ) );
connect( removeAction, &QAction::triggered, this, &QgsModelChildAlgorithmGraphicItem::deleteComponent );
QAction *editAction = popupmenu->addAction( QObject::tr( "Edit" ) );
QAction *editAction = popupmenu->addAction( QObject::tr( "Edit" ) );
connect( editAction, &QAction::triggered, this, &QgsModelChildAlgorithmGraphicItem::editComponent );
QAction *editCommentAction = popupmenu->addAction( component()->comment()->description().isEmpty() ? QObject::tr( "Add Comment…" ) : QObject::tr( "Edit Comment…" ) );
connect( editCommentAction, &QAction::triggered, this, &QgsModelParameterGraphicItem::editComment );
popupmenu->addSeparator();

if ( const QgsProcessingModelChildAlgorithm *child = dynamic_cast< const QgsProcessingModelChildAlgorithm * >( component() ) )
{
Expand Down Expand Up @@ -879,11 +902,16 @@ void QgsModelCommentGraphicItem::contextMenuEvent( QGraphicsSceneContextMenuEven
QMenu *popupmenu = new QMenu( event->widget() );
QAction *removeAction = popupmenu->addAction( QObject::tr( "Remove" ) );
connect( removeAction, &QAction::triggered, this, &QgsModelCommentGraphicItem::deleteComponent );
QAction *editAction = popupmenu->addAction( QObject::tr( "Edit" ) );
QAction *editAction = popupmenu->addAction( QObject::tr( "Edit" ) );
connect( editAction, &QAction::triggered, this, &QgsModelCommentGraphicItem::editComponent );
popupmenu->exec( event->screenPos() );
}

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

QgsModelCommentGraphicItem::~QgsModelCommentGraphicItem() = default;

QColor QgsModelCommentGraphicItem::fillColor( QgsModelComponentGraphicItem::State state ) const
Expand Down
15 changes: 14 additions & 1 deletion src/gui/processing/models/qgsmodelcomponentgraphicitem.h
Expand Up @@ -54,6 +54,13 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
Hover, //!< Cursor is hovering over an deselected item
};

//! Available flags
enum Flag
{
FlagMultilineText = 1 << 0, //!< Show multiline text in label
};
Q_DECLARE_FLAGS( Flags, Flag )

/**
* Constructor for QgsModelComponentGraphicItem for the specified \a component, with the specified \a parent item.
*
Expand All @@ -68,6 +75,11 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject

~QgsModelComponentGraphicItem() override;

/**
* Returns item flags.
*/
virtual Flags flags() const;

/**
* Returns the model component associated with this item.
*/
Expand Down Expand Up @@ -278,6 +290,7 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
bool mIsHovering = false;

};
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )

/**
* \ingroup gui
Expand Down Expand Up @@ -442,7 +455,7 @@ 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

0 comments on commit 0af98f9

Please sign in to comment.