def drawLineSection(self, painter, firstP, secondP, firstCorner, secondCorner): direction = self.getPointToPointDirection(firstP, secondP) thickness = self.CONNECTION_THICKNESS * self.zoomFactor() halfthick = thickness / 2 cornerRoundness = halfthick**0.5 cornerOffset = halfthick * cornerRoundness * (4 * self.zoomFactor()**2) innerCorner = halfthick * (cornerRoundness + 1) outerCorner = halfthick * (cornerRoundness - 1) rect = self.getRectBetweenTwoPoints(firstP, secondP, firstCorner, secondCorner) # Paint witch color gradient (PyQt4) if direction == self.ConnectionDirection.LEFT: # horizontal, negative brush = QLinearGradient(rect.x(), rect.y(), rect.x(), rect.y() + halfthick) elif direction == self.ConnectionDirection.RIGHT: # horizontal, positive brush = QLinearGradient(rect.x(), rect.y(), rect.x(), rect.y() + halfthick) elif direction == self.ConnectionDirection.UP: # vertical, negative brush = QLinearGradient(rect.x(), rect.y(), rect.x() + halfthick, rect.y()) elif direction == self.ConnectionDirection.DOWN: # vertical, positive brush = QLinearGradient(rect.x(), rect.y(), rect.x() + halfthick, rect.y()) brush.setSpread(QLinearGradient.ReflectSpread) brush.setColorAt(0, self.FILL_COLOR1) brush.setColorAt(1, self.FILL_COLOR2) painter.setBrush(brush) painter.drawRect(rect)
def drawIcon(self, colorRamp): # input color ramp object: QgsVectorColorRampV2 object. # QgsVectorColorRampV2 is a virtual object, the real object name in # this case is QgsVectorGradientColorRampV2 object. mStops = colorRamp.stops() linearGradient = QLinearGradient(0.0, 50.0, 200.0, 50.0) for item in mStops: linearGradient.setColorAt(item.offset, item.color) linearGradient.setSpread(QGradient.PadSpread) svgName = os.path.join(os.path.dirname(__file__), self.text() + '.svg') pix = QSvgGenerator() pix.setFileName(svgName) pix.setSize(QSize(200, 100)) painter = QPainter() painter.begin(pix) br = QBrush(linearGradient) painter.setBrush(br) painter.drawRect(QRect(0, 0, 200, 100)) painter.end() pixmap = QPixmap(svgName) icon = QIcon(pixmap) self.svgName = svgName return icon
def drawLineSection(self, painter, firstP, secondP, firstCorner, secondCorner): direction = self.getPointToPointDirection(firstP, secondP) thickness = self.CONNECTION_THICKNESS * self.zoomFactor() halfthick = thickness / 2 cornerRoundness = halfthick ** 0.5 cornerOffset = halfthick * cornerRoundness * (4 * self.zoomFactor()**2) innerCorner = halfthick * (cornerRoundness + 1) outerCorner = halfthick * (cornerRoundness - 1) rect = self.getRectBetweenTwoPoints(firstP, secondP, firstCorner, secondCorner) # Paint witch color gradient (PyQt4) if direction == self.ConnectionDirection.LEFT: # horizontal, negative brush = QLinearGradient(rect.x(), rect.y(), rect.x(), rect.y() + halfthick) elif direction == self.ConnectionDirection.RIGHT: # horizontal, positive brush = QLinearGradient(rect.x(), rect.y(), rect.x(), rect.y() + halfthick) elif direction == self.ConnectionDirection.UP: # vertical, negative brush = QLinearGradient(rect.x(), rect.y(), rect.x() + halfthick, rect.y()) elif direction == self.ConnectionDirection.DOWN: # vertical, positive brush = QLinearGradient(rect.x(), rect.y(), rect.x() + halfthick, rect.y()) brush.setSpread(QLinearGradient.ReflectSpread) brush.setColorAt(0, self.FILL_COLOR1) brush.setColorAt(1, self.FILL_COLOR2) painter.setBrush(brush) painter.drawRect(rect)
def _gen_striped_gradient(basecolor): """Just generate a standard gradient, for use as a background""" g = QLinearGradient() g.setSpread(g.ReflectSpread) g.setStart(0,0) g.setFinalStop(2, 2) g.setColorAt(0.0, QColor(basecolor)) g.setColorAt(0.35, QColor(0,0,0,0)) g.setColorAt(0.75, QColor(0,0,0,0)) g.setColorAt(1.0, QColor(basecolor)) return g
def drawSection(self, painter, sectionIndex): """ This is going to replace drawLineSection """ firstP = self.mapFromParent(self._route[sectionIndex]) secondP = self.mapFromParent(self._route[sectionIndex + 1]) direction = self.getPointToPointDirection(firstP, secondP) if self.CONNECTION_TYPE != "ORTHOGONAL" or direction == self.ConnectionDirection.UNDEFINED: self.drawStraightLine(painter, firstP, secondP) return previousP = None nextP = None if sectionIndex == 0: lastDirection = self.sourceDirection() else: previousP = self.mapFromParent(self._route[sectionIndex - 1]) lastDirection = self.getPointToPointDirection(previousP, firstP) if sectionIndex > len(self._route) - 3: nextDirection = self.targetDirection() else: nextP = self.mapFromParent(self._route[sectionIndex + 2]) nextDirection = self.getPointToPointDirection(secondP, nextP) firstCorner = self.getCornerType(lastDirection, direction) secondCorner = self.getCornerType(direction, nextDirection) minDist = self.CONNECTION_THICKNESS * self.zoomFactor() * 4 maxRadius = None if previousP: xDist = abs(firstP.x() - previousP.x()) yDist = abs(firstP.y() - previousP.y()) if xDist > 0 and xDist < minDist: maxRadius = 0.5 * xDist elif yDist > 0 and yDist < minDist: maxRadius = 0.5 * yDist xDist = abs(firstP.x() - secondP.x()) yDist = abs(firstP.y() - secondP.y()) if xDist > 0 and xDist < minDist: maxRadius = 0.5 * xDist elif yDist > 0 and yDist < minDist: maxRadius = 0.5 * yDist #if maxRadius: self.drawCorner(painter, firstP, firstCorner, maxRadius) # print "_____________________ darawSection _______________________" # print "firstP", firstP # print "secondP", secondP # print "lastDirection", self.connectionDirectionString(lastDirection) # print " firstCorner", self.cornerTypeString(firstCorner) # print "direction", self.connectionDirectionString(direction) # print " secondCorner", self.cornerTypeString(secondCorner) # print "nextDirection", self.connectionDirectionString(nextDirection) # print "\n\n" thickness = self.CONNECTION_THICKNESS * self.zoomFactor() halfthick = thickness / 2 cornerRoundness = halfthick**0.5 cornerOffset = halfthick * cornerRoundness * (4 * self.zoomFactor()**2) innerCorner = halfthick * (cornerRoundness + 1) outerCorner = halfthick * (cornerRoundness - 1) rect = self.getRectBetweenTwoPoints(firstP, secondP, firstCorner, secondCorner) # Paint witch color gradient (PyQt4) if direction == self.ConnectionDirection.LEFT: # horizontal, negative brush = QLinearGradient(rect.x(), rect.y(), rect.x(), rect.y() + halfthick) elif direction == self.ConnectionDirection.RIGHT: # horizontal, positive brush = QLinearGradient(rect.x(), rect.y(), rect.x(), rect.y() + halfthick) elif direction == self.ConnectionDirection.UP: # vertical, negative brush = QLinearGradient(rect.x(), rect.y(), rect.x() + halfthick, rect.y()) elif direction == self.ConnectionDirection.DOWN: # vertical, positive brush = QLinearGradient(rect.x(), rect.y(), rect.x() + halfthick, rect.y()) else: # Should already be drawn above --> direction == self.ConnectionDirection.UNDEFINED return if painter.redirected(painter.device()): # e.q. QPixmap.grabWidget() painter.setBrush(self.FILL_COLOR1) else: brush.setSpread(QLinearGradient.ReflectSpread) brush.setColorAt(0, self.FILL_COLOR1) brush.setColorAt(1, self.FILL_COLOR2) painter.setBrush(brush) painter.drawRect(rect)
def drawSection(self, painter, sectionIndex): """ This is going to replace drawLineSection """ firstP = self.mapFromParent(self._route[sectionIndex]) secondP = self.mapFromParent(self._route[sectionIndex +1]) direction = self.getPointToPointDirection(firstP, secondP) if self.CONNECTION_TYPE!="ORTHOGONAL" or direction == self.ConnectionDirection.UNDEFINED: self.drawStraightLine(painter, firstP, secondP) return previousP = None nextP = None if sectionIndex == 0: lastDirection = self.sourceDirection() else: previousP = self.mapFromParent(self._route[sectionIndex -1]) lastDirection = self.getPointToPointDirection(previousP, firstP) if sectionIndex > len(self._route) -3: nextDirection = self.targetDirection() else: nextP = self.mapFromParent(self._route[sectionIndex +2]) nextDirection = self.getPointToPointDirection(secondP, nextP) firstCorner = self.getCornerType(lastDirection, direction) secondCorner = self.getCornerType(direction, nextDirection) minDist = self.CONNECTION_THICKNESS * self.zoomFactor() * 4 maxRadius = None if previousP: xDist = abs(firstP.x() - previousP.x()) yDist = abs(firstP.y() - previousP.y()) if xDist > 0 and xDist < minDist: maxRadius = 0.5 * xDist elif yDist > 0 and yDist < minDist: maxRadius = 0.5 * yDist xDist = abs(firstP.x() - secondP.x()) yDist = abs(firstP.y() - secondP.y()) if xDist > 0 and xDist < minDist: maxRadius = 0.5 * xDist elif yDist > 0 and yDist < minDist: maxRadius = 0.5 * yDist #if maxRadius: self.drawCorner(painter, firstP, firstCorner, maxRadius) # print "_____________________ darawSection _______________________" # print "firstP", firstP # print "secondP", secondP # print "lastDirection", self.connectionDirectionString(lastDirection) # print " firstCorner", self.cornerTypeString(firstCorner) # print "direction", self.connectionDirectionString(direction) # print " secondCorner", self.cornerTypeString(secondCorner) # print "nextDirection", self.connectionDirectionString(nextDirection) # print "\n\n" thickness = self.CONNECTION_THICKNESS * self.zoomFactor() halfthick = thickness / 2 cornerRoundness = halfthick ** 0.5 cornerOffset = halfthick * cornerRoundness * (4 * self.zoomFactor()**2) innerCorner = halfthick * (cornerRoundness + 1) outerCorner = halfthick * (cornerRoundness - 1) rect = self.getRectBetweenTwoPoints(firstP, secondP, firstCorner, secondCorner) # Paint witch color gradient (PyQt4) if direction == self.ConnectionDirection.LEFT: # horizontal, negative brush = QLinearGradient(rect.x(), rect.y(), rect.x(), rect.y() + halfthick) elif direction == self.ConnectionDirection.RIGHT: # horizontal, positive brush = QLinearGradient(rect.x(), rect.y(), rect.x(), rect.y() + halfthick) elif direction == self.ConnectionDirection.UP: # vertical, negative brush = QLinearGradient(rect.x(), rect.y(), rect.x() + halfthick, rect.y()) elif direction == self.ConnectionDirection.DOWN: # vertical, positive brush = QLinearGradient(rect.x(), rect.y(), rect.x() + halfthick, rect.y()) else: # Should already be drawn above --> direction == self.ConnectionDirection.UNDEFINED return if painter.redirected(painter.device()): # e.q. QPixmap.grabWidget() painter.setBrush(self.FILL_COLOR1) else: brush.setSpread(QLinearGradient.ReflectSpread) brush.setColorAt(0, self.FILL_COLOR1) brush.setColorAt(1, self.FILL_COLOR2) painter.setBrush(brush) painter.drawRect(rect)