def __init__(self, brown_object, pos, pen=None, brush=None):
super().__init__(brown_object)
self.qt_object = QtWidgets.QGraphicsRectItem(
0, 0, 10, 10)
self.pos = pos
if pen:
self.pen = pen
else:
self.pen = PenInterface(
None, Color('#000000'), 0,
PenPattern.SOLID, PenJoinStyle.BEVEL, PenCapStyle.SQUARE)
if brush:
self.brush = brush
else:
self.brush = BrushInterface(
None, Color('#000000'), BrushPattern.SOLID)
python类QGraphicsRectItem()的实例源码
def mousePressEvent(self, event):
if event.button() == 1: # QtCore.Qt.MouseButton.LeftButton
self.add(event.scenePos()) #create a new tempo point by telling the api a position and then reacting to "delete all, recreate" from the callback.
event.accept()
else:
super().mousePressEvent(event) #call default implementation from QGraphicsRectItem
#self.parentView.verticalScrollBar().valueChanged.connect(self.repositionAfterScroll)
#self.parentView.horizontalScrollBar().valueChanged.connect(self.repositionAfterScroll)
#def repositionAfterScroll(self):
# Dont use it. Only in here as later template for handling scrollbars.
#
#print(self, self.parentView.mapToScene(0, 0).y())
# #self.setY(self.parentView.mapToScene(0, 0).y())
# #self.setX(self.parentView.mapToScene(0, 0).x())
# self.outline.setX(self.parentView.mapToScene(0, 0).x())
# self.setY(-1*constantsAndConfigs.trackHeight)
def addGraphicsItems(self):
self.mainRect = QGraphicsRectItem(QRectF(-15, -15, 30, 30), self)
self.nodeTitle = QGraphicsTextItem(type(self).name, self)
titleFont = QFont()
titleFont.setBold(True)
self.nodeTitle.setFont(titleFont)
self.selectedChanged(self.isSelected())
def __init__(self, ID = 0, x = 0, y = 0):
super(self.__class__, self).__init__(ID, x, y)
self._kind_name = 'pickingStation'
self._items = []
self._graphics_item = QGraphicsRectItem(self)
self._items.append(QGraphicsRectItem(self._graphics_item))
self._items.append(QGraphicsRectItem(self._graphics_item))
self._text = QGraphicsTextItem(self._graphics_item)
def __init__(self, ID = 0, x = 0, y = 0):
super(self.__class__, self).__init__(ID, x, y)
self._kind_name = 'robot'
self._carries = None
self._initial_carries = None
self._tasks = []
self._graphics_item = QGraphicsRectItem(self)
self._text = QGraphicsTextItem(self)
self.setZValue(1.0)
def __init__(self, ID = 0, x = 0, y = 0):
super(self.__class__, self).__init__(ID, x, y)
self._kind_name = 'checkpoint'
self._ids = {}
self._graphics_item = QGraphicsRectItem(self)
self._text = QGraphicsTextItem(self._graphics_item)
self._shine = False
def paintBlockBackgroundColors(self, staticBlocksRepresentation):
"""This gets not called by self.createGraphicItemsFromData but only by
a score callback for blocksChanged"""
for bg in self.backgroundBlockColors:
self.parentScore.removeWhenIdle(bg)
for th in self.transparentBlockHandles:
self.parentScore.removeWhenIdle(th)
self.backgroundBlockColors = []
self.transparentBlockHandles = []
for block in staticBlocksRepresentation:
if self.staticExportItem["double"]: #double track, twice as high
h = 10 * constantsAndConfigs.stafflineGap
else:
h = 4 * constantsAndConfigs.stafflineGap
bgItem = QtWidgets.QGraphicsRectItem(0, 0, block["completeDuration"] / constantsAndConfigs.ticksToPixelRatio, h) #x, y, w, h
bgItem.setFlag(QtWidgets.QGraphicsItem.ItemIgnoresParentOpacity)
bgItem.setPen(QtGui.QColor("transparent"))
color = self.stringToColor(block["name"])
bgItem.setBrush(color)
bgItem.setParentItem(self)
self.backgroundBlockColors.append(bgItem)
bgItem.setPos(block["tickindex"] / constantsAndConfigs.ticksToPixelRatio, -2*constantsAndConfigs.stafflineGap)
bgItem.setZValue(-10) #This is the z value within GuiTrack
bgItem.setEnabled(False)
transparentBlockHandle = GuiBlockHandle(self, block, 0, -2 * constantsAndConfigs.stafflineGap, block["completeDuration"] / constantsAndConfigs.ticksToPixelRatio, h + 4 * constantsAndConfigs.stafflineGap) #x, y, w, h
transparentBlockHandle.color = color
self.transparentBlockHandles.append(transparentBlockHandle)
transparentBlockHandle.setPos(block["tickindex"] / constantsAndConfigs.ticksToPixelRatio, -2*constantsAndConfigs.stafflineGap)
def createBeams(self, beamList):
for b in self.beams:
self.parentScore.removeWhenIdle(b)
self.beams = []
for startTick, endTick, beamtype, positionAsStaffline, direction in beamList:
numberOfBeams = int(log(beamtype, 2)-2)
assert numberOfBeams == log(beamtype, 2)-2
for x in range(numberOfBeams):
if direction > 0: #stem/beam upwards
beamNumberOffset = 4*x
xOffset = 7
else:
beamNumberOffset = -4*x
xOffset = 1
#non-scalable X-Offsets are not possible via setPos. zoom and stretch go haywire.
#Instead we use one item for strechting and for the offset position and wrap it in an item group that is used for positioing.
shifterAnchor = QtWidgets.QGraphicsItemGroup()
shifterAnchor.setParentItem(self)
rectangle = QtWidgets.QGraphicsRectItem(0, 0, (endTick-startTick) / constantsAndConfigs.ticksToPixelRatio, constantsAndConfigs.beamHeight) #x, y, w, h
rectangle.setBrush(QtGui.QColor("black"))
shifterAnchor.addToGroup(rectangle)
shifterAnchor.rectangle = rectangle
rectangle.setPos(xOffset, 0)
if constantsAndConfigs.noteHeadMode:
rectangle.show()
else:
rectangle.hide()
self.beams.append(shifterAnchor)
x = startTick/ constantsAndConfigs.ticksToPixelRatio
y = beamNumberOffset + positionAsStaffline * constantsAndConfigs.stafflineGap / 2 - 1
shifterAnchor.setPos(x, y)
def stretchXCoordinates(self, factor):
"""Reposition the items on the X axis.
Call goes through all parents/children, starting from ScoreView._stretchXCoordinates.
Docstring there."""
#The rects and lines here are just a QGraphicsRectItem and have no custom subclass. So they have no stretchXCoordinates itself.
for item in self.items:
item.setX(item.pos().x() * factor)
item.stretchXCoordinates(factor)
for staticItem in self.staticItems: #labels etc.
staticItem.setX(staticItem.pos().x() * factor)
for barline in self.barLines:
barline.setX(barline.pos().x() * factor)
for staffline in self.staffLines: #stafflines start from x=0. 0*factor=0 so we omit setPos
stretchLine(staffline, factor)
for beam in self.beams: #beams are part of the track, not of the note items.
beam.setX(beam.pos().x() * factor )
stretchRect(beam.rectangle, factor)
for backgroundBlockColor in self.backgroundBlockColors:
backgroundBlockColor.setX(backgroundBlockColor.pos().x() * factor)
stretchRect(backgroundBlockColor, factor)
for transparentBlockHandle in self.transparentBlockHandles:
transparentBlockHandle.setX(transparentBlockHandle.pos().x() * factor)
transparentBlockHandle.stretchXCoordinates(factor)
for ccPath in self.ccPaths.values():
ccPath.stretchXCoordinates(factor)
self.lengthInPixel = self.lengthInPixel * factor #this also gets updated in createGraphicItemsFromData
def createGraphicItemsFromData(self):
w = self.staticItem["completeDuration"] / constantsAndConfigs.ticksToPixelRatio - constantsAndConfigs.magicPixel*2
if int(self.staticItem["numberOfMeasures"]) == self.staticItem["numberOfMeasures"]: #just an int?
numberOfMeasuresString = str(int(self.staticItem["numberOfMeasures"]))
else:
numberOfMeasuresString = str(self.staticItem["numberOfMeasures"]) #include the decimal dot
rect = QtWidgets.QGraphicsRectItem(constantsAndConfigs.magicPixel,0, w, constantsAndConfigs.stafflineGap) #x,y, w, h
rect.setBrush(QtCore.Qt.black)
rect.setParentItem(self)
self.rect = rect #for stretching
leftBlock = QtWidgets.QGraphicsRectItem(2, -1* constantsAndConfigs.stafflineGap, constantsAndConfigs.magicPixel, 3*constantsAndConfigs.stafflineGap)
leftBlock.setBrush(QtCore.Qt.black)
leftBlock.setParentItem(self)
rightBlock = QtWidgets.QGraphicsRectItem(0, -1* constantsAndConfigs.stafflineGap, constantsAndConfigs.magicPixel, 3*constantsAndConfigs.stafflineGap) #x, y, w, h
rightBlock.setBrush(QtCore.Qt.black)
#rightBlock.setPos(w,0)
rightBlock.setPos(rect.rect().right(),0)
rightBlock.setParentItem(self)
self.rightBlock = rightBlock #for stretching
for i, char in enumerate(numberOfMeasuresString):
if char == ".":
continue #TODO: Maybe warn about that? MMR below 1 are not valid but they are needed for editing purposes. Sometimes you need to go through such a value.
else:
n = GuiTimeSignature.digits[int(char)]
char = QtSvg.QGraphicsSvgItem()
char.setSharedRenderer(GuiTimeSignature.renderer)
char.setElementId(n)
#char.setPos(i*4*constantsAndConfigs.magicPixel + 4* constantsAndConfigs.magicPixel, -3*constantsAndConfigs.stafflineGap)
char.setPos(leftBlock.rect().width(), -3*constantsAndConfigs.stafflineGap)
char.setScale(1.5)
char.setParentItem(self)
self.setPos(0, -3 * constantsAndConfigs.stafflineGap)
def draw_sep_area(self, y_mid):
x = self.sceneRect().x()
y = self.sceneRect().y()
h = self.sceneRect().height()
w = self.sceneRect().width()
if self.noise_area is not None:
self.noise_area.hide()
if self.ones_area is None:
self.ones_area = QGraphicsRectItem(x, y, w, h / 2 + y_mid)
self.ones_area.setBrush(constants.ONES_AREA_COLOR)
self.ones_area.setOpacity(constants.SEPARATION_OPACITY)
self.ones_area.setPen(QPen(constants.TRANSPARENT_COLOR, Qt.FlatCap))
self.addItem(self.ones_area)
else:
self.ones_area.show()
self.ones_area.setRect(x, y, w, h / 2 + y_mid)
start = y + h / 2 + y_mid
if self.zeros_area is None:
self.zeros_area = QGraphicsRectItem(x, start, w, (y + h) - start)
self.zeros_area.setBrush(constants.ZEROS_AREA_COLOR)
self.zeros_area.setOpacity(constants.SEPARATION_OPACITY)
self.zeros_area.setPen(QPen(constants.TRANSPARENT_COLOR, Qt.FlatCap))
self.addItem(self.zeros_area)
else:
self.zeros_area.show()
self.zeros_area.setRect(x, start, w, (y + h) - start)