mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-22 00:14:55 -05:00
[sextante] improved modeler appearance with Bezier curves
This commit is contained in:
parent
41932dfcca
commit
4c1bdfbccb
@ -45,7 +45,7 @@ from sextante.core.GeoAlgorithm import GeoAlgorithm
|
|||||||
##
|
##
|
||||||
#############################################################################
|
#############################################################################
|
||||||
|
|
||||||
class ModelerArrowItem(QtGui.QGraphicsLineItem):
|
class ModelerArrowItem(QtGui.QGraphicsPathItem):
|
||||||
|
|
||||||
def __init__(self, startItem, outputIndex, endItem, paramIndex ,parent=None, scene=None):
|
def __init__(self, startItem, outputIndex, endItem, paramIndex ,parent=None, scene=None):
|
||||||
super(ModelerArrowItem, self).__init__(parent, scene)
|
super(ModelerArrowItem, self).__init__(parent, scene)
|
||||||
@ -61,6 +61,33 @@ class ModelerArrowItem(QtGui.QGraphicsLineItem):
|
|||||||
self.setZValue(0)
|
self.setZValue(0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def contains_point(self, x, y, epsilon):
|
||||||
|
p = (x, y)
|
||||||
|
min_distance = float(0x7fffffff)
|
||||||
|
t = 0.0
|
||||||
|
while t < 1.0:
|
||||||
|
point = self.path.pointAtPercent(t)
|
||||||
|
spline_point = (point.x(), point.y())
|
||||||
|
print p, spline_point
|
||||||
|
distance = self.distance(p, spline_point)
|
||||||
|
if distance < min_distance:
|
||||||
|
min_distance = distance
|
||||||
|
t += 0.1
|
||||||
|
print min_distance, epsilon
|
||||||
|
return (min_distance <= epsilon)
|
||||||
|
|
||||||
|
#===========================================================================
|
||||||
|
# def boundingRect(self):
|
||||||
|
# return self.path.boundingRect()
|
||||||
|
#===========================================================================
|
||||||
|
|
||||||
|
|
||||||
|
def distance(self, p0, p1):
|
||||||
|
a = p1[0] - p0[0]
|
||||||
|
b = p1[1] - p0[1]
|
||||||
|
return math.sqrt(a * a + b * b)
|
||||||
|
|
||||||
def startItem(self):
|
def startItem(self):
|
||||||
return self.myStartItem
|
return self.myStartItem
|
||||||
|
|
||||||
@ -71,95 +98,55 @@ class ModelerArrowItem(QtGui.QGraphicsLineItem):
|
|||||||
#this is a quick fix to avoid arrows not being drawn
|
#this is a quick fix to avoid arrows not being drawn
|
||||||
return QtCore.QRectF(0, 0, 4000,4000)
|
return QtCore.QRectF(0, 0, 4000,4000)
|
||||||
|
|
||||||
def shape(self):
|
#===============================================================================
|
||||||
path = super(ModelerArrowItem, self).shape()
|
# def shape(self):
|
||||||
path.addPolygon(self.arrowHead)
|
# path = super(ModelerArrowItem, self).shape()
|
||||||
return path
|
# path.addPolygon(self.arrowHead)
|
||||||
|
# return path
|
||||||
def updatePosition(self):
|
#
|
||||||
line = QtCore.QLineF(self.mapFromItem(self.myStartItem, 0, 0), self.mapFromItem(self.myEndItem, 0, 0))
|
# def updatePosition(self):
|
||||||
self.setLine(line)
|
# line = QtCore.QLineF(self.mapFromItem(self.myStartItem, 0, 0), self.mapFromItem(self.myEndItem, 0, 0))
|
||||||
|
# self.setLine(line)
|
||||||
|
#===============================================================================
|
||||||
|
|
||||||
def paint(self, painter, option, widget=None):
|
def paint(self, painter, option, widget=None):
|
||||||
myStartItem = self.myStartItem
|
startItem = self.myStartItem
|
||||||
myEndItem = self.myEndItem
|
endItem = self.myEndItem
|
||||||
myPen = self.pen()
|
myPen = self.pen()
|
||||||
myPen.setColor(self.myColor)
|
myPen.setColor(self.myColor)
|
||||||
arrowSize = 6.0
|
|
||||||
painter.setPen(myPen)
|
painter.setPen(myPen)
|
||||||
painter.setBrush(self.myColor)
|
painter.setBrush(self.myColor)
|
||||||
|
|
||||||
if isinstance(self.startItem().element, GeoAlgorithm):
|
controlPoints = []
|
||||||
if self.startItem().element.outputs:
|
|
||||||
endPt = self.endItem().getLinkPointForParameter(self.paramIndex)
|
endPt = self.endItem().getLinkPointForParameter(self.paramIndex)
|
||||||
startPt = self.startItem().getLinkPointForOutput(self.outputIndex)
|
startPt = self.startItem().getLinkPointForOutput(self.outputIndex)
|
||||||
arrowLine = QtCore.QLineF(myEndItem.pos() + endPt - QtCore.QPointF(endPt.x() + ModelerGraphicItem.BOX_WIDTH /2, 0), myEndItem.pos() + endPt)
|
if isinstance(self.startItem().element, GeoAlgorithm):
|
||||||
painter.drawLine(arrowLine)
|
if self.startItem().element.outputs:
|
||||||
tailLine = QtCore.QLineF(myStartItem.pos() + startPt + QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH /2 - startPt.x(),0), myStartItem.pos() + startPt)
|
controlPoints.append(startItem.pos() + startPt)
|
||||||
painter.drawLine(tailLine)
|
controlPoints.append(startItem.pos() + startPt + QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH /2,0))
|
||||||
pt = QtCore.QPointF(myStartItem.pos() + startPt + QtCore.QPointF(- 2, -2))
|
controlPoints.append(endItem.pos() + endPt - QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH /2, 0))
|
||||||
rect = QtCore.QRectF(pt.x(), pt.y(), 4, 4)
|
controlPoints.append(endItem.pos() + endPt)
|
||||||
painter.fillRect(rect, QtCore.Qt.gray)
|
pt = QtCore.QPointF(startItem.pos() + startPt + QtCore.QPointF(-3, -3))
|
||||||
line = QtCore.QLineF(myStartItem.pos() + startPt + QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH /2 - startPt.x(),0),
|
painter.drawEllipse(pt.x(), pt.y(), 6, 6)
|
||||||
myEndItem.pos() + endPt - QtCore.QPointF(endPt.x() + ModelerGraphicItem.BOX_WIDTH /2, 0))
|
pt = QtCore.QPointF(endItem.pos() + endPt + QtCore.QPointF(-3, -3))
|
||||||
|
painter.drawEllipse(pt.x(), pt.y(), 6, 6)
|
||||||
else: # case where there is a dependency on an algorithm not on an output
|
else: # case where there is a dependency on an algorithm not on an output
|
||||||
endPolygon = myEndItem.polygon()
|
controlPoints.append(startItem.pos() + startPt)
|
||||||
p1 = endPolygon.first() + myEndItem.pos()
|
controlPoints.append(startItem.pos() + startPt + QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH /2,0))
|
||||||
line = QtCore.QLineF(myStartItem.pos(), myEndItem.pos())
|
controlPoints.append(endItem.pos() + endPt - QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH /2, 0))
|
||||||
intersectPoint = QtCore.QPointF()
|
controlPoints.append(endItem.pos() + endPt)
|
||||||
for i in endPolygon:
|
|
||||||
p2 = i + myEndItem.pos()
|
|
||||||
polyLine = QtCore.QLineF(p1, p2)
|
|
||||||
intersectType = polyLine.intersect(line, intersectPoint)
|
|
||||||
if intersectType == QtCore.QLineF.BoundedIntersection:
|
|
||||||
break
|
|
||||||
p1 = p2
|
|
||||||
|
|
||||||
self.setLine(QtCore.QLineF(intersectPoint, myStartItem.pos()))
|
|
||||||
line = self.line()
|
|
||||||
if line.length() == 0: #division by zero might occur if arrow has no length
|
|
||||||
return
|
|
||||||
angle = math.acos(line.dx() / line.length())
|
|
||||||
if line.dy() >= 0:
|
|
||||||
angle = (math.pi * 2.0) - angle
|
|
||||||
|
|
||||||
arrowP1 = line.p1() + QtCore.QPointF(math.sin(angle + math.pi / 3.0) * arrowSize,
|
|
||||||
math.cos(angle + math.pi / 3) * arrowSize)
|
|
||||||
arrowP2 = line.p1() + QtCore.QPointF(math.sin(angle + math.pi - math.pi / 3.0) * arrowSize,
|
|
||||||
math.cos(angle + math.pi - math.pi / 3.0) * arrowSize)
|
|
||||||
|
|
||||||
self.arrowHead.clear()
|
|
||||||
for point in [line.p1(), arrowP1, arrowP2]:
|
|
||||||
self.arrowHead.append(point)
|
|
||||||
|
|
||||||
painter.drawLine(line)
|
|
||||||
painter.drawPolygon(self.arrowHead)
|
|
||||||
return;
|
|
||||||
else:
|
else:
|
||||||
endPt = self.endItem().getLinkPointForParameter(self.paramIndex)
|
controlPoints.append(startItem.pos())
|
||||||
arrowLine = QtCore.QLineF(myEndItem.pos() + endPt - QtCore.QPointF(endPt.x() + ModelerGraphicItem.BOX_WIDTH /2, 0), myEndItem.pos() + endPt)
|
controlPoints.append(startItem.pos() + QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH /2,0))
|
||||||
painter.drawLine(arrowLine)
|
controlPoints.append(endItem.pos() + endPt - QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH /2, 0))
|
||||||
line = QtCore.QLineF(myStartItem.pos(),
|
controlPoints.append(endItem.pos() + endPt)
|
||||||
myEndItem.pos() + endPt - QtCore.QPointF(endPt.x() + ModelerGraphicItem.BOX_WIDTH /2, 0))
|
pt = QtCore.QPointF(endItem.pos() + endPt + QtCore.QPointF(-3, -3))
|
||||||
|
painter.drawEllipse(pt.x(), pt.y(), 6, 6)
|
||||||
|
|
||||||
self.setLine(line);
|
path = QtGui.QPainterPath()
|
||||||
|
path.moveTo(controlPoints[0])
|
||||||
|
path.cubicTo(*controlPoints[1:])
|
||||||
|
painter.strokePath(path, painter.pen())
|
||||||
|
self.setPath(path)
|
||||||
|
|
||||||
if line.length() == 0: #division by zero might occur if arrow has no length
|
|
||||||
return
|
|
||||||
|
|
||||||
angle = math.acos(line.dx() / line.length())
|
|
||||||
if line.dy() >= 0:
|
|
||||||
angle = (math.pi * 2.0) - angle
|
|
||||||
|
|
||||||
arrowP1 = arrowLine.p2() + QtCore.QPointF(-math.cos(math.pi / 9.0) * arrowSize,
|
|
||||||
math.sin(math.pi / 9.0) * arrowSize)
|
|
||||||
arrowP2 = arrowLine.p2() + QtCore.QPointF(-math.cos(math.pi / 9.0) * arrowSize,
|
|
||||||
-math.sin(math.pi / 9.0) * arrowSize)
|
|
||||||
|
|
||||||
self.arrowHead.clear()
|
|
||||||
for point in [arrowLine.p2(), arrowP1, arrowP2]:
|
|
||||||
self.arrowHead.append(point)
|
|
||||||
|
|
||||||
painter.drawLine(line)
|
|
||||||
painter.drawPolygon(self.arrowHead)
|
|
||||||
|
|
||||||
|
|||||||
@ -238,14 +238,14 @@ class ModelerGraphicItem(QtGui.QGraphicsItem):
|
|||||||
painter.drawPixmap(-(ModelerGraphicItem.BOX_WIDTH / 2.0) + 3, -8, self.pixmap)
|
painter.drawPixmap(-(ModelerGraphicItem.BOX_WIDTH / 2.0) + 3, -8, self.pixmap)
|
||||||
|
|
||||||
def getLinkPointForParameter(self, paramIndex):
|
def getLinkPointForParameter(self, paramIndex):
|
||||||
offsetX = 30
|
offsetX = 25
|
||||||
if self.inputFolded:
|
if self.inputFolded:
|
||||||
paramIndex = -1
|
paramIndex = -1
|
||||||
offsetX = 22
|
offsetX = 17
|
||||||
font = QtGui.QFont("Verdana", 8)
|
font = QtGui.QFont("Verdana", 8)
|
||||||
fm = QtGui.QFontMetricsF(font)
|
fm = QtGui.QFontMetricsF(font)
|
||||||
if isinstance(self.element, GeoAlgorithm):
|
if isinstance(self.element, GeoAlgorithm):
|
||||||
h = (fm.height() * 1.2) * (paramIndex + 2) - fm.height() / 2.0
|
h = (fm.height() * 1.2) * (paramIndex + 2) - fm.height() / 2.0 + 2
|
||||||
h = h + ModelerGraphicItem.BOX_HEIGHT / 2.0
|
h = h + ModelerGraphicItem.BOX_HEIGHT / 2.0
|
||||||
else:
|
else:
|
||||||
h = 0
|
h = 0
|
||||||
@ -264,7 +264,7 @@ class ModelerGraphicItem(QtGui.QGraphicsItem):
|
|||||||
fm = QtGui.QFontMetricsF(font)
|
fm = QtGui.QFontMetricsF(font)
|
||||||
w = fm.width(QtCore.QString(text))
|
w = fm.width(QtCore.QString(text))
|
||||||
h = (fm.height() * 1.2) * (outputIndex + 3 + numParams) - fm.height() / 2.0
|
h = (fm.height() * 1.2) * (outputIndex + 3 + numParams) - fm.height() / 2.0
|
||||||
y = h + ModelerGraphicItem.BOX_HEIGHT / 2.0
|
y = h + ModelerGraphicItem.BOX_HEIGHT / 2.0 + 2
|
||||||
x = -(ModelerGraphicItem.BOX_WIDTH)/2 + 33 + w + 5 if not self.outputFolded else 10
|
x = -(ModelerGraphicItem.BOX_WIDTH)/2 + 33 + w + 5 if not self.outputFolded else 10
|
||||||
return QtCore.QPointF(x, y)
|
return QtCore.QPointF(x, y)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user