def draw(self):
lengthInPixel = self.noteExportObject["tieDistanceInTicks"] / constantsAndConfigs.ticksToPixelRatio
path = QtGui.QPainterPath()
path.cubicTo(0, 0, lengthInPixel/2, constantsAndConfigs.stafflineGap, lengthInPixel, 0) # ctrlPt1x, ctrlPt1y, ctrlPt2x, ctrlPt2y, endPtx, endPty
self.setPath(path) #set path edits the old one in place
python类QPainterPath()的实例源码
def shape(self):
"""Return a more accurate shape for this item so that
mouse hovering is more accurate"""
path = QtGui.QPainterPath()
path.addRect(QtCore.QRectF(-2, -2, 5, self.parentTransparentBlock.rect().height()+2 )) #this is directly related to inits parameter x, y, w, h
return path
def modify(self, img: QtGui.QPixmap, mult: float=1.0, rounding: int=0, cut: int=5):
if img.width() == 0 or img.height() == 0:
log.info("FATAL ERROR: IMG NOT LOADED")
return img
img = QtGui.QPixmap(img)
h = img.height()
w = img.width()
color = QtGui.QColor(0, 0, 0, 0) # (r,g,b, density)
pix = QtGui.QPixmap(QtCore.QSize(w, h))
pix.fill(color)
if not rounding > 0:
cut = 0
rect = QtCore.QRectF(-1 + cut, -1 + cut, 2 + w - cut * 2, 2 + h - cut * 2)
painter = QtGui.QPainter()
painter.begin(pix)
painter.setRenderHints(QtGui.QPainter.Antialiasing, True)
path = QtGui.QPainterPath()
path.addRoundedRect(rect, rounding, rounding)
painter.drawPath(path)
brush = QtGui.QBrush()
brush.setTexture(img)
painter.fillPath(path, brush)
painter.end()
return pix.scaledToWidth(pix.width() * mult)
# credits to: http://python.6.x6.nabble.com/Rounded-corners-td4716825.html
def __init__(self, parent, graphic_view=None):
self.peak = []
super().__init__(parent)
self.scene = GridScene(parent=graphic_view)
self.scene.setBackgroundBrush(constants.BGCOLOR)
self.peak_item = self.scene.addPath(QPainterPath(),
QPen(constants.PEAK_COLOR, Qt.FlatCap)) # type: QGraphicsPathItem
def clear_peak(self):
self.peak = []
if self.peak_item:
self.peak_item.setPath(QPainterPath())
def __init__(self, parent=None):
super(RenderArea, self).__init__(parent)
newFont = self.font()
newFont.setPixelSize(12)
self.setFont(newFont)
fontMetrics = QFontMetrics(newFont)
self.xBoundingRect = fontMetrics.boundingRect("x")
self.yBoundingRect = fontMetrics.boundingRect("y")
self.shape = QPainterPath()
self.operations = []
def createPath(self):
self.path = QPainterPath()
angle = 2*math.pi/self._sides
self.path.moveTo(self._outerRadius, 0)
for step in range(1, self._sides + 1):
self.path.lineTo(
self._innerRadius * math.cos((step - 0.5) * angle),
self._innerRadius * math.sin((step - 0.5) * angle)
)
self.path.lineTo(
self._outerRadius * math.cos(step * angle),
self._outerRadius * math.sin(step * angle)
)
self.path.closeSubpath()
def __init__(self):
self.myPath = QPainterPath()
self.myPosition = QPoint()
self.myColor = QColor()
self.myToolTip = ''
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
self._optionsRects = {}
w, h = self.width(), self.height()
metrics = self.fontMetrics()
hphp = 2 * _hPad
painter.save()
path = QPainterPath()
path.addRoundedRect(.5, .5, w - 1, h - 1, 4, 4)
painter.fillPath(path, QColor(250, 250, 250))
x = 0
linePath = QPainterPath()
for text in self._options[:-1]:
x += hphp + metrics.width(text)
linePath.moveTo(x, 0)
linePath.lineTo(x, h)
pen = painter.pen()
pen.setColor(QColor(218, 218, 218))
pen.setWidth(0)
painter.setPen(pen)
painter.drawPath(path)
painter.setRenderHint(QPainter.Antialiasing, False)
painter.drawPath(linePath)
painter.restore()
painter.translate(_hPad, _vPad + metrics.ascent())
left = 0
for index, text in enumerate(self._options):
if index in self._selection:
color = QColor(20, 146, 230)
else:
color = QColor(63, 63, 63)
painter.setPen(color)
painter.drawText(0, 0, text)
textWidth = metrics.width(text)
rectWidth = textWidth + hphp
rect = (left, 0, rectWidth, h)
self._optionsRects[index] = rect
painter.translate(rectWidth, 0)
left += rectWidth
def setupShapes(self):
truck = QPainterPath()
truck.setFillRule(Qt.WindingFill)
truck.moveTo(0.0, 87.0)
truck.lineTo(0.0, 60.0)
truck.lineTo(10.0, 60.0)
truck.lineTo(35.0, 35.0)
truck.lineTo(100.0, 35.0)
truck.lineTo(100.0, 87.0)
truck.lineTo(0.0, 87.0)
truck.moveTo(17.0, 60.0)
truck.lineTo(55.0, 60.0)
truck.lineTo(55.0, 40.0)
truck.lineTo(37.0, 40.0)
truck.lineTo(17.0, 60.0)
truck.addEllipse(17.0, 75.0, 25.0, 25.0)
truck.addEllipse(63.0, 75.0, 25.0, 25.0)
clock = QPainterPath()
clock.addEllipse(-50.0, -50.0, 100.0, 100.0)
clock.addEllipse(-48.0, -48.0, 96.0, 96.0)
clock.moveTo(0.0, 0.0)
clock.lineTo(-2.0, -2.0)
clock.lineTo(0.0, -42.0)
clock.lineTo(2.0, -2.0)
clock.lineTo(0.0, 0.0)
clock.moveTo(0.0, 0.0)
clock.lineTo(2.732, -0.732)
clock.lineTo(24.495, 14.142)
clock.lineTo(0.732, 2.732)
clock.lineTo(0.0, 0.0)
house = QPainterPath()
house.moveTo(-45.0, -20.0)
house.lineTo(0.0, -45.0)
house.lineTo(45.0, -20.0)
house.lineTo(45.0, 45.0)
house.lineTo(-45.0, 45.0)
house.lineTo(-45.0, -20.0)
house.addRect(15.0, 5.0, 20.0, 35.0)
house.addRect(-35.0, -15.0, 25.0, 25.0)
text = QPainterPath()
font = QFont()
font.setPixelSize(50)
fontBoundingRect = QFontMetrics(font).boundingRect("Qt")
text.addText(-QPointF(fontBoundingRect.center()), font, "Qt")
self.shapes = (clock, house, text, truck)
self.shapeComboBox.activated.connect(self.shapeSelected)
def __init__(self):
super(SortingBox, self).__init__()
self.circlePath = QPainterPath()
self.squarePath = QPainterPath()
self.trianglePath = QPainterPath()
self.shapeItems = []
self.previousPosition = QPoint()
self.setMouseTracking(True)
self.setBackgroundRole(QPalette.Base)
self.itemInMotion = None
self.newCircleButton = self.createToolButton("New Circle",
QIcon(':/images/circle.png'), self.createNewCircle)
self.newSquareButton = self.createToolButton("New Square",
QIcon(':/images/square.png'), self.createNewSquare)
self.newTriangleButton = self.createToolButton("New Triangle",
QIcon(':/images/triangle.png'), self.createNewTriangle)
self.circlePath.addEllipse(0, 0, 100, 100)
self.squarePath.addRect(0, 0, 100, 100)
x = self.trianglePath.currentPosition().x()
y = self.trianglePath.currentPosition().y()
self.trianglePath.moveTo(x + 120 / 2, y)
self.trianglePath.lineTo(0, 100)
self.trianglePath.lineTo(120, 100)
self.trianglePath.lineTo(x + 120 / 2, y)
self.setWindowTitle("Tooltips")
self.resize(500, 300)
self.createShapeItem(self.circlePath, "Circle",
self.initialItemPosition(self.circlePath),
self.initialItemColor())
self.createShapeItem(self.squarePath, "Square",
self.initialItemPosition(self.squarePath),
self.initialItemColor())
self.createShapeItem(self.trianglePath, "Triangle",
self.initialItemPosition(self.trianglePath),
self.initialItemColor())
def setupShapes(self):
truck = QPainterPath()
truck.setFillRule(Qt.WindingFill)
truck.moveTo(0.0, 87.0)
truck.lineTo(0.0, 60.0)
truck.lineTo(10.0, 60.0)
truck.lineTo(35.0, 35.0)
truck.lineTo(100.0, 35.0)
truck.lineTo(100.0, 87.0)
truck.lineTo(0.0, 87.0)
truck.moveTo(17.0, 60.0)
truck.lineTo(55.0, 60.0)
truck.lineTo(55.0, 40.0)
truck.lineTo(37.0, 40.0)
truck.lineTo(17.0, 60.0)
truck.addEllipse(17.0, 75.0, 25.0, 25.0)
truck.addEllipse(63.0, 75.0, 25.0, 25.0)
clock = QPainterPath()
clock.addEllipse(-50.0, -50.0, 100.0, 100.0)
clock.addEllipse(-48.0, -48.0, 96.0, 96.0)
clock.moveTo(0.0, 0.0)
clock.lineTo(-2.0, -2.0)
clock.lineTo(0.0, -42.0)
clock.lineTo(2.0, -2.0)
clock.lineTo(0.0, 0.0)
clock.moveTo(0.0, 0.0)
clock.lineTo(2.732, -0.732)
clock.lineTo(24.495, 14.142)
clock.lineTo(0.732, 2.732)
clock.lineTo(0.0, 0.0)
house = QPainterPath()
house.moveTo(-45.0, -20.0)
house.lineTo(0.0, -45.0)
house.lineTo(45.0, -20.0)
house.lineTo(45.0, 45.0)
house.lineTo(-45.0, 45.0)
house.lineTo(-45.0, -20.0)
house.addRect(15.0, 5.0, 20.0, 35.0)
house.addRect(-35.0, -15.0, 25.0, 25.0)
text = QPainterPath()
font = QFont()
font.setPixelSize(50)
fontBoundingRect = QFontMetrics(font).boundingRect("Qt")
text.addText(-QPointF(fontBoundingRect.center()), font, "Qt")
self.shapes = (clock, house, text, truck)
self.shapeComboBox.activated.connect(self.shapeSelected)
def __init__(self):
super(SortingBox, self).__init__()
self.circlePath = QPainterPath()
self.squarePath = QPainterPath()
self.trianglePath = QPainterPath()
self.shapeItems = []
self.previousPosition = QPoint()
self.setMouseTracking(True)
self.setBackgroundRole(QPalette.Base)
self.itemInMotion = None
self.newCircleButton = self.createToolButton("New Circle",
QIcon(':/images/circle.png'), self.createNewCircle)
self.newSquareButton = self.createToolButton("New Square",
QIcon(':/images/square.png'), self.createNewSquare)
self.newTriangleButton = self.createToolButton("New Triangle",
QIcon(':/images/triangle.png'), self.createNewTriangle)
self.circlePath.addEllipse(0, 0, 100, 100)
self.squarePath.addRect(0, 0, 100, 100)
x = self.trianglePath.currentPosition().x()
y = self.trianglePath.currentPosition().y()
self.trianglePath.moveTo(x + 120 / 2, y)
self.trianglePath.lineTo(0, 100)
self.trianglePath.lineTo(120, 100)
self.trianglePath.lineTo(x + 120 / 2, y)
self.setWindowTitle("Tooltips")
self.resize(500, 300)
self.createShapeItem(self.circlePath, "Circle",
self.initialItemPosition(self.circlePath),
self.initialItemColor())
self.createShapeItem(self.squarePath, "Square",
self.initialItemPosition(self.squarePath),
self.initialItemColor())
self.createShapeItem(self.trianglePath, "Triangle",
self.initialItemPosition(self.trianglePath),
self.initialItemColor())
def __init__(self, parent, noteExportObject):
#Dimensions and Position
x = noteExportObject["leftModInTicks"] / constantsAndConfigs.ticksToPixelRatio
y = -1 * constantsAndConfigs.stafflineGap / 2 + 1
w = (noteExportObject["rightModInTicks"] + noteExportObject["completeDuration"] - noteExportObject["leftModInTicks"]) / constantsAndConfigs.ticksToPixelRatio
h = constantsAndConfigs.stafflineGap - 2 #2 pixel to make room for the pen-width.
super().__init__(x, y, w, h)
self.setPos(0, constantsAndConfigs.stafflineGap * noteExportObject["dotOnLine"] / 2)
self.setParentItem(parent)
#Prepare self.shape()
#NOTE: this was from a time when mouse modification was possible. Leave for later use.
#self.path = QtGui.QPainterPath()
#self.pathRect = QtCore.QRectF(x, y-1, w, h+4) #this is directly related to inits parameter x, y, w, h
#self.path.addRect(self.pathRect)
#self.setCursor(QtCore.Qt.SizeHorCursor)
#Different color when the duration was modified by the user
inactive = "green" if noteExportObject["manualOverride"] else "black"
self.inactiveColor = QtGui.QColor(inactive)
self.setBrush(self.inactiveColor)
#Pen for the borders
pen = QtGui.QPen()
pen.setCapStyle(QtCore.Qt.RoundCap)
pen.setJoinStyle(QtCore.Qt.RoundJoin)
pen.setWidth(1)
pen.setColor(QtGui.QColor("darkGrey"))
self.setPen(pen)
#Since accidentals are part of the notehead we need one here. It doesn't matter if the traditional notehead already has one
if noteExportObject["accidental"]: #0 means no difference to keysig
self.accidental = GuiNote.createAccidentalGraphicsItem(self, noteExportObject["accidental"])
self.accidental.setPos(0, 0) #not analogue to the notehead acciental position because here we choose the rectangle as parent
self.accidental.setParentItem(self)
#NOTE: this was from a time when mouse modification was possible. Leave for later use.
#def shape(self):
# """Qt Function
# Return a more accurate shape for this item so that
# mouse hovering is more accurate.
#
# Must be within the bounding rect."""
# return self.path
#def boundingRect(self, *args):
# """Keep in syn with self.shape()"""
# return self.pathRect
#def paint(self, *args):
#Do NOT implement this. This actually works already. The parent item needs this.
def keyPressEvent(self, event):
self.keylist.append(event.key())
if Qt.Key_Control in self.keylist and Qt.Key_A in self.keylist:
paintRect = QtGui.QPainterPath()
paintRect.addRect(0, -(26.5 * globals.TileWidth), 241 * globals.TileWidth, 27.5 * globals.TileWidth)
self.scene.setSelectionArea(paintRect)
event.accept()
return
elif event.key() in [Qt.Key_Delete, Qt.Key_Backspace]:
try:
sel = self.scene.selectedItems()
except RuntimeError:
return
if len(sel) > 0:
for obj in sel:
index = globals.Area.objects.index(obj)
del globals.Area.objects[index]
self.scene.update(obj.boundRect)
obj.setSelected(False)
obj.delete()
self.scene.removeItem(obj)
self.scene.update()
del obj
event.accept()
return
elif event.key() == Qt.Key_G:
globals.GridShown = not globals.GridShown
self.scene.update()
event.accept()
return
super().keyPressEvent(event)
def setupShapes(self):
truck = QPainterPath()
truck.setFillRule(Qt.WindingFill)
truck.moveTo(0.0, 87.0)
truck.lineTo(0.0, 60.0)
truck.lineTo(10.0, 60.0)
truck.lineTo(35.0, 35.0)
truck.lineTo(100.0, 35.0)
truck.lineTo(100.0, 87.0)
truck.lineTo(0.0, 87.0)
truck.moveTo(17.0, 60.0)
truck.lineTo(55.0, 60.0)
truck.lineTo(55.0, 40.0)
truck.lineTo(37.0, 40.0)
truck.lineTo(17.0, 60.0)
truck.addEllipse(17.0, 75.0, 25.0, 25.0)
truck.addEllipse(63.0, 75.0, 25.0, 25.0)
clock = QPainterPath()
clock.addEllipse(-50.0, -50.0, 100.0, 100.0)
clock.addEllipse(-48.0, -48.0, 96.0, 96.0)
clock.moveTo(0.0, 0.0)
clock.lineTo(-2.0, -2.0)
clock.lineTo(0.0, -42.0)
clock.lineTo(2.0, -2.0)
clock.lineTo(0.0, 0.0)
clock.moveTo(0.0, 0.0)
clock.lineTo(2.732, -0.732)
clock.lineTo(24.495, 14.142)
clock.lineTo(0.732, 2.732)
clock.lineTo(0.0, 0.0)
house = QPainterPath()
house.moveTo(-45.0, -20.0)
house.lineTo(0.0, -45.0)
house.lineTo(45.0, -20.0)
house.lineTo(45.0, 45.0)
house.lineTo(-45.0, 45.0)
house.lineTo(-45.0, -20.0)
house.addRect(15.0, 5.0, 20.0, 35.0)
house.addRect(-35.0, -15.0, 25.0, 25.0)
text = QPainterPath()
font = QFont()
font.setPixelSize(50)
fontBoundingRect = QFontMetrics(font).boundingRect("Qt")
text.addText(-QPointF(fontBoundingRect.center()), font, "Qt")
self.shapes = (clock, house, text, truck)
self.shapeComboBox.activated.connect(self.shapeSelected)
def __init__(self):
super(SortingBox, self).__init__()
self.circlePath = QPainterPath()
self.squarePath = QPainterPath()
self.trianglePath = QPainterPath()
self.shapeItems = []
self.previousPosition = QPoint()
self.setMouseTracking(True)
self.setBackgroundRole(QPalette.Base)
self.itemInMotion = None
self.newCircleButton = self.createToolButton("New Circle",
QIcon(':/images/circle.png'), self.createNewCircle)
self.newSquareButton = self.createToolButton("New Square",
QIcon(':/images/square.png'), self.createNewSquare)
self.newTriangleButton = self.createToolButton("New Triangle",
QIcon(':/images/triangle.png'), self.createNewTriangle)
self.circlePath.addEllipse(0, 0, 100, 100)
self.squarePath.addRect(0, 0, 100, 100)
x = self.trianglePath.currentPosition().x()
y = self.trianglePath.currentPosition().y()
self.trianglePath.moveTo(x + 120 / 2, y)
self.trianglePath.lineTo(0, 100)
self.trianglePath.lineTo(120, 100)
self.trianglePath.lineTo(x + 120 / 2, y)
self.setWindowTitle("Tooltips")
self.resize(500, 300)
self.createShapeItem(self.circlePath, "Circle",
self.initialItemPosition(self.circlePath),
self.initialItemColor())
self.createShapeItem(self.squarePath, "Square",
self.initialItemPosition(self.squarePath),
self.initialItemColor())
self.createShapeItem(self.trianglePath, "Triangle",
self.initialItemPosition(self.trianglePath),
self.initialItemColor())