Skip to content
Permalink
Browse files

Nicer comment text appearance

  • Loading branch information
nyalldawson committed Mar 4, 2020
1 parent 961557d commit 0af98f9f5ba6a2399f24f5e604d9e4129db1d742
@@ -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/ );
@@ -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.
@@ -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
{
@@ -433,6 +447,7 @@ Ownership of ``output`` is transferred to the item.
~QgsModelCommentGraphicItem();
virtual void contextMenuEvent( QGraphicsSceneContextMenuEvent *event );

virtual Flags flags() const;

protected:

@@ -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())
@@ -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
@@ -25,8 +25,7 @@
from processing.modeler.ModelerGraphicItem import (
ModelerInputGraphicItem,
ModelerOutputGraphicItem,
ModelerChildAlgorithmGraphicItem,
ModelerCommentGraphicItem
ModelerChildAlgorithmGraphicItem
)


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

@@ -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() ) )
{
@@ -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
@@ -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.
*
@@ -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.
*/
@@ -278,6 +290,7 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
bool mIsHovering = false;

};
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )

/**
* \ingroup gui
@@ -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;

0 comments on commit 0af98f9

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