Skip to content
Permalink
Browse files

Move last part of ModelerGraphicItem to c++, generalize handling of

folding buttons
  • Loading branch information
nyalldawson committed Mar 3, 2020
1 parent b9edcbd commit d80bdd33713275210772d197299b0e6ff2754757
@@ -50,23 +50,6 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):
def __init__(self, element, model):
super().__init__(element, model, None)

def getLinkPointForParameter(self, paramIndex):
offsetX = 25
if isinstance(self.component(), QgsProcessingModelChildAlgorithm) and self.component().linksCollapsed(
Qt.TopEdge):
paramIndex = -1
offsetX = 17
if isinstance(self.component(), QgsProcessingModelParameter):
paramIndex = -1
offsetX = 0
fm = QFontMetricsF(self.font())
if isinstance(self.component(), QgsProcessingModelChildAlgorithm):
h = -(fm.height() * 1.2) * (paramIndex + 2) - fm.height() / 2.0 + 8
h = h - self.component().size().height() / 2.0
else:
h = 0
return QPointF(-self.component().size().width() / 2 + offsetX, h)


class ModelerInputGraphicItem(ModelerGraphicItem):

@@ -185,19 +168,6 @@ def __init__(self, element, model):
self.pixmap = element.algorithm().icon().pixmap(15, 15)
self.setLabel(element.description())

alg = element.algorithm()
if [a for a in alg.parameterDefinitions() if not a.isDestination()]:
pt = self.getLinkPointForParameter(-1)
pt = QPointF(0, pt.y())
self.inButton = QgsModelDesignerFoldButtonGraphicItem(self, self.component().linksCollapsed(Qt.TopEdge), pt)
self.inButton.folded.connect(self.foldInput)
if alg.outputDefinitions():
pt = self.linkPoint(Qt.BottomEdge, -1)
pt = QPointF(0, pt.y())
self.outButton = QgsModelDesignerFoldButtonGraphicItem(self, self.component().linksCollapsed(Qt.BottomEdge),
pt)
self.outButton.folded.connect(self.foldOutput)

def iconPicture(self):
return self.picture if self.picture is not None else QPicture()

@@ -240,27 +210,6 @@ def linkPointText(self, edge, index):
out = self.component().algorithm().outputDefinitions()[index]
return self.truncatedTextForItem(out.description())

def foldInput(self, folded):
self.component().setLinksCollapsed(Qt.TopEdge, folded)
# also need to update the model's stored component
self.model().childAlgorithm(self.component().childId()).setLinksCollapsed(Qt.TopEdge, folded)
self.prepareGeometryChange()
if self.component().algorithm().outputDefinitions():
pt = self.linkPoint(Qt.BottomEdge, -1)
pt = QPointF(0, pt.y())
self.outButton.position = pt

self.updateArrowPaths.emit()
self.update()

def foldOutput(self, folded):
self.component().setLinksCollapsed(Qt.BottomEdge, folded)
# also need to update the model's stored component
self.model().childAlgorithm(self.component().childId()).setLinksCollapsed(Qt.BottomEdge, folded)
self.prepareGeometryChange()
self.updateArrowPaths.emit()
self.update()

def editComponent(self):
elemAlg = self.component().algorithm()
dlg = ModelerParametersDialog(elemAlg, self.model(), self.component().childId(),
@@ -140,6 +140,29 @@ QVariant QgsModelComponentGraphicItem::itemChange( QGraphicsItem::GraphicsItemCh
break;
}

case QGraphicsItem::ItemSceneChange:
{
if ( !mInitialized )
{
// ideally would be in constructor, but cannot call virtual methods from that...
if ( linkPointCount( Qt::TopEdge ) )
{
QPointF pt = linkPoint( Qt::TopEdge, -1 );
pt = QPointF( 0, pt.y() );
mExpandTopButton = new QgsModelDesignerFoldButtonGraphicItem( this, mComponent->linksCollapsed( Qt::TopEdge ), pt );
connect( mExpandTopButton, &QgsModelDesignerFoldButtonGraphicItem::folded, this, [ = ]( bool folded ) { fold( Qt::TopEdge, folded ); } );
}
if ( linkPointCount( Qt::BottomEdge ) )
{
QPointF pt = linkPoint( Qt::BottomEdge, -1 );
pt = QPointF( 0, pt.y() );
mExpandBottomButton = new QgsModelDesignerFoldButtonGraphicItem( this, mComponent->linksCollapsed( Qt::BottomEdge ), pt );
connect( mExpandBottomButton, &QgsModelDesignerFoldButtonGraphicItem::folded, this, [ = ]( bool folded ) { fold( Qt::BottomEdge, folded ); } );
}
}
break;
}

default:
break;
}
@@ -291,6 +314,24 @@ void QgsModelComponentGraphicItem::updateToolTip( const QPointF &pos )
}
}

void QgsModelComponentGraphicItem::fold( Qt::Edge edge, bool folded )
{
mComponent->setLinksCollapsed( edge, folded );
// also need to update the model's stored component

// TODO - this is not so nice, consider moving this to model class
if ( QgsProcessingModelChildAlgorithm *child = dynamic_cast< QgsProcessingModelChildAlgorithm * >( mComponent.get() ) )
mModel->childAlgorithm( child->childId() ).setLinksCollapsed( edge, folded );
else if ( QgsProcessingModelParameter *param = dynamic_cast< QgsProcessingModelParameter * >( mComponent.get() ) )
mModel->parameterComponent( param->parameterName() ).setLinksCollapsed( edge, folded );
else if ( QgsProcessingModelOutput *output = dynamic_cast< QgsProcessingModelOutput * >( mComponent.get() ) )
mModel->childAlgorithm( output->childId() ).modelOutput( output->name() ).setLinksCollapsed( edge, folded );

prepareGeometryChange();
emit updateArrowPaths();
update();
}

QString QgsModelComponentGraphicItem::label() const
{
return mLabel;
@@ -27,6 +27,7 @@ class QgsProcessingModelChildAlgorithm;
class QgsProcessingModelOutput;
class QgsProcessingModelAlgorithm;
class QgsModelDesignerFlatButtonGraphicItem;
class QgsModelDesignerFoldButtonGraphicItem;

///@cond NOT_STABLE

@@ -229,9 +230,15 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject

void updateToolTip( const QPointF &pos );

void fold( Qt::Edge edge, bool folded );

std::unique_ptr< QgsProcessingModelComponent > mComponent;
QgsProcessingModelAlgorithm *mModel = nullptr;

bool mInitialized = false;
QgsModelDesignerFoldButtonGraphicItem *mExpandTopButton = nullptr;
QgsModelDesignerFoldButtonGraphicItem *mExpandBottomButton = nullptr;

QString mLabel;

QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;

0 comments on commit d80bdd3

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