Skip to content

Commit

Permalink
Move last part of ModelerGraphicItem to c++, generalize handling of
Browse files Browse the repository at this point in the history
folding buttons
  • Loading branch information
nyalldawson committed Mar 3, 2020
1 parent b9edcbd commit d80bdd3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 51 deletions.
51 changes: 0 additions & 51 deletions python/plugins/processing/modeler/ModelerGraphicItem.py
Expand Up @@ -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):

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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(),
Expand Down
41 changes: 41 additions & 0 deletions src/gui/processing/models/qgsmodelcomponentgraphicitem.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/gui/processing/models/qgsmodelcomponentgraphicitem.h
Expand Up @@ -27,6 +27,7 @@ class QgsProcessingModelChildAlgorithm;
class QgsProcessingModelOutput;
class QgsProcessingModelAlgorithm;
class QgsModelDesignerFlatButtonGraphicItem;
class QgsModelDesignerFoldButtonGraphicItem;

///@cond NOT_STABLE

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d80bdd3

Please sign in to comment.