Skip to content

Commit

Permalink
Less Python, more c++
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 2, 2020
1 parent 72d7305 commit 167a8d3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 40 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ QGraphicsScene subclass representing the model designer.
%End %End
public: public:


enum ZValues
{
ArrowLink,
ModelComponent,
};

QgsModelGraphicsScene( QObject *parent /TransferThis/ = 0 ); QgsModelGraphicsScene( QObject *parent /TransferThis/ = 0 );
%Docstring %Docstring
Constructor for QgsModelGraphicsScene with the specified ``parent`` object. Constructor for QgsModelGraphicsScene with the specified ``parent`` object.
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/modeler/ModelerArrowItem.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
*************************************************************************** ***************************************************************************
""" """


from qgis.core import (QgsProcessingModelAlgorithm, from qgis.core import (QgsProcessingModelChildAlgorithm,
QgsProcessingModelChildAlgorithm,
QgsProcessingModelParameter) QgsProcessingModelParameter)
from qgis.gui import QgsModelGraphicsScene
from qgis.PyQt.QtCore import Qt, QPointF from qgis.PyQt.QtCore import Qt, QPointF
from qgis.PyQt.QtWidgets import QApplication, QGraphicsPathItem, QGraphicsItem from qgis.PyQt.QtWidgets import QApplication, QGraphicsPathItem, QGraphicsItem
from qgis.PyQt.QtGui import QPen, QPainterPath, QPolygonF, QPainter, QPalette from qgis.PyQt.QtGui import QPen, QPainterPath, QPolygonF, QPainter, QPalette
Expand All @@ -71,7 +71,7 @@ def __init__(self, startItem, startIndex, endItem, endIndex,
self.myColor.setAlpha(150) self.myColor.setAlpha(150)
self.setPen(QPen(self.myColor, 1, Qt.SolidLine, self.setPen(QPen(self.myColor, 1, Qt.SolidLine,
Qt.RoundCap, Qt.RoundJoin)) Qt.RoundCap, Qt.RoundJoin))
self.setZValue(0) self.setZValue(QgsModelGraphicsScene.ArrowLink)


def setPenStyle(self, style): def setPenStyle(self, style):
pen = self.pen() pen = self.pen()
Expand Down
47 changes: 11 additions & 36 deletions python/plugins/processing/modeler/ModelerGraphicItem.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@
__copyright__ = '(C) 2012, Victor Olaya' __copyright__ = '(C) 2012, Victor Olaya'


import os import os
import math


from qgis.PyQt.QtCore import Qt, QPointF, QRectF from qgis.PyQt.QtCore import Qt, QPointF, QRectF
from qgis.PyQt.QtGui import QFont, QFontMetricsF, QPen, QBrush, QColor, QPolygonF, QPicture, QPainter, QPalette from qgis.PyQt.QtGui import QFont, QFontMetricsF, QPen, QBrush, QColor, QPicture, QPainter, QPalette
from qgis.PyQt.QtWidgets import QApplication, QGraphicsItem, QMessageBox, QMenu from qgis.PyQt.QtWidgets import QApplication, QGraphicsItem, QMessageBox, QMenu
from qgis.PyQt.QtSvg import QSvgRenderer from qgis.PyQt.QtSvg import QSvgRenderer
from qgis.core import (QgsProcessingParameterDefinition, from qgis.core import (QgsProcessingParameterDefinition,
QgsProcessingModelParameter, QgsProcessingModelParameter,
QgsProcessingModelOutput, QgsProcessingModelOutput,
QgsProcessingModelChildAlgorithm, QgsProcessingModelChildAlgorithm,
QgsProcessingModelAlgorithm,
QgsProject) QgsProject)
from qgis.gui import ( from qgis.gui import (
QgsProcessingParameterDefinitionDialog, QgsProcessingParameterDefinitionDialog,
Expand All @@ -60,7 +58,6 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):


def __init__(self, element, model, controls, scene=None): def __init__(self, element, model, controls, scene=None):
super().__init__(element, None) super().__init__(element, None)
self.setAcceptHoverEvents(True)
self.controls = controls self.controls = controls
self.model = model self.model = model
self.scene = scene self.scene = scene
Expand Down Expand Up @@ -104,32 +101,28 @@ def __init__(self, element, model, controls, scene=None):
self.pixmap = element.algorithm().icon().pixmap(15, 15) self.pixmap = element.algorithm().icon().pixmap(15, 15)
self.text = element.description() self.text = element.description()
self.arrows = [] self.arrows = []
self.setFlag(QGraphicsItem.ItemIsMovable, True)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
self.setZValue(1000)


if controls: if controls:
svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'edit.svg')) svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'edit.svg'))
picture = QPicture() picture = QPicture()
painter = QPainter(picture) painter = QPainter(picture)
svg.render(painter) svg.render(painter)
painter.end() painter.end()
pt = QPointF(self.box_width / 2 pt = QPointF(self.box_width / 2 -
- ModelerGraphicItem.BUTTON_WIDTH / 2, ModelerGraphicItem.BUTTON_WIDTH / 2,
self.box_height / 2 self.box_height / 2 -
- ModelerGraphicItem.BUTTON_HEIGHT / 2) ModelerGraphicItem.BUTTON_HEIGHT / 2)
self.editButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt) self.editButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt)
self.editButton.clicked.connect(self.editElement) self.editButton.clicked.connect(self.editElement)
svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'delete.svg')) svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'delete.svg'))
picture = QPicture() picture = QPicture()
painter = QPainter(picture) painter = QPainter(picture)
svg.render(painter) svg.render(painter)
painter.end() painter.end()
pt = QPointF(self.box_width / 2 pt = QPointF(self.box_width / 2 -
- ModelerGraphicItem.BUTTON_WIDTH / 2, ModelerGraphicItem.BUTTON_WIDTH / 2,
ModelerGraphicItem.BUTTON_HEIGHT / 2 ModelerGraphicItem.BUTTON_HEIGHT / 2 -
- self.box_height / 2) self.box_height / 2)
self.deleteButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt) self.deleteButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt)
self.deleteButton.clicked.connect(self.removeElement) self.deleteButton.clicked.connect(self.removeElement)


Expand Down Expand Up @@ -312,8 +305,8 @@ def updateAlgorithm(self, alg):
alg.setParametersCollapsed(existing_child.parametersCollapsed()) alg.setParametersCollapsed(existing_child.parametersCollapsed())
alg.setOutputsCollapsed(existing_child.outputsCollapsed()) alg.setOutputsCollapsed(existing_child.outputsCollapsed())
for i, out in enumerate(alg.modelOutputs().keys()): for i, out in enumerate(alg.modelOutputs().keys()):
alg.modelOutput(out).setPosition(alg.modelOutput(out).position() alg.modelOutput(out).setPosition(alg.modelOutput(out).position() or
or alg.position() + QPointF( alg.position() + QPointF(
self.box_width, self.box_width,
(i + 1.5) * self.box_height)) (i + 1.5) * self.box_height))
self.model.setChildAlgorithm(alg) self.model.setChildAlgorithm(alg)
Expand Down Expand Up @@ -485,21 +478,3 @@ def itemChange(self, change, value):
self.repaintArrows() self.repaintArrows()


return super().itemChange(change, value) return super().itemChange(change, value)

def polygon(self):
fm = QFontMetricsF(self.item_font)
hUp = fm.height() * 1.2 * (len(self.component().parameters) + 2)
hDown = fm.height() * 1.2 * (len(self.component().outputs) + 2)
pol = QPolygonF([
QPointF(-(self.box_width + 2) / 2,
-(self.box_height + 2) / 2 - hUp),
QPointF(-(self.box_width + 2) / 2,
(self.box_height + 2) / 2 + hDown),
QPointF((self.box_width + 2) / 2,
(self.box_height + 2) / 2 + hDown),
QPointF((self.box_width + 2) / 2,
-(self.box_height + 2) / 2 - hUp),
QPointF(-(self.box_width + 2) / 2,
-(self.box_height + 2) / 2 - hUp)
])
return pol
7 changes: 6 additions & 1 deletion src/gui/processing/models/qgsmodelcomponentgraphicitem.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@


#include "qgsmodelcomponentgraphicitem.h" #include "qgsmodelcomponentgraphicitem.h"
#include "qgsprocessingmodelcomponent.h" #include "qgsprocessingmodelcomponent.h"
#include "qgsmodelgraphicsscene.h"


///@cond NOT_STABLE ///@cond NOT_STABLE


QgsModelComponentGraphicItem::QgsModelComponentGraphicItem( QgsProcessingModelComponent *component, QGraphicsItem *parent ) QgsModelComponentGraphicItem::QgsModelComponentGraphicItem( QgsProcessingModelComponent *component, QGraphicsItem *parent )
: QGraphicsObject( parent ) : QGraphicsObject( parent )
, mComponent( component ) , mComponent( component )
{ {

setAcceptHoverEvents( true );
setFlag( QGraphicsItem::ItemIsMovable, true );
setFlag( QGraphicsItem::ItemIsSelectable, true );
setFlag( QGraphicsItem::ItemSendsGeometryChanges, true );
setZValue( QgsModelGraphicsScene::ZValues::ModelComponent );
} }


QgsProcessingModelComponent *QgsModelComponentGraphicItem::component() QgsProcessingModelComponent *QgsModelComponentGraphicItem::component()
Expand Down
7 changes: 7 additions & 0 deletions src/gui/processing/models/qgsmodelgraphicsscene.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class GUI_EXPORT QgsModelGraphicsScene : public QGraphicsScene


public: public:


//! Z values for scene items
enum ZValues
{
ArrowLink = 0, //!< An arrow linking model items
ModelComponent = 1, //!< Model components (e.g. algorithms, inputs and outputs)
};

/** /**
* Constructor for QgsModelGraphicsScene with the specified \a parent object. * Constructor for QgsModelGraphicsScene with the specified \a parent object.
*/ */
Expand Down

0 comments on commit 167a8d3

Please sign in to comment.