def paintGhost(self, painter: QtGui.QPainter) -> None: if self.__ghost_pos is None: return ymid = self.height() // 2 tool = self.track_editor.currentToolType() pos = self.__ghost_pos painter.setOpacity(0.4) if tool.is_rest: sym = { 1: 'rest-whole', 2: 'rest-half', 4: 'rest-quarter', 8: 'rest-8th', 16: 'rest-16th', 32: 'rest-32th', }[self._tool_duration_map[tool].denominator] svg_symbol.paintSymbol(painter, sym, QtCore.QPoint(pos.x(), ymid)) elif tool.is_note: duration = self._tool_duration_map[tool] if duration >= audioproc.MusicalDuration(1, 2): svg_symbol.paintSymbol(painter, 'note-head-void', pos) else: svg_symbol.paintSymbol(painter, 'note-head-black', pos) if duration <= audioproc.MusicalDuration(1, 2): painter.fillRect(pos.x() + 8, pos.y() - 63, 3, 60, Qt.black) if duration == audioproc.MusicalDuration(1, 8): flags = 1 elif duration == audioproc.MusicalDuration(1, 16): flags = 2 elif duration == audioproc.MusicalDuration(1, 32): flags = 3 else: flags = 0 for f in range(flags): svg_symbol.paintSymbol( painter, 'note-flag-down', QtCore.QPoint(pos.x() + 11, pos.y() - 63 + 12 * f)) elif tool.is_accidental: accidental = { tools.ToolType.ACCIDENTAL_NATURAL: '', tools.ToolType.ACCIDENTAL_FLAT: 'b', tools.ToolType.ACCIDENTAL_SHARP: '#', tools.ToolType.ACCIDENTAL_DOUBLE_FLAT: 'bb', tools.ToolType.ACCIDENTAL_DOUBLE_SHARP: '##', }[tool] sym = self._accidental_map[accidental] svg_symbol.paintSymbol(painter, sym, pos) else: painter.setPen(Qt.NoPen) painter.setBrush(Qt.black) painter.drawEllipse(pos.x() - 15, pos.y() - 15, 31, 31)
def paintForeground(self, painter: QtGui.QPainter) -> None: assert self._note_area is not None self._edit_areas.clear() ymid = self.height() // 2 base_stave_line = self.measure.clef.center_pitch.stave_line base_octave = self.measure.clef.base_octave acc_map = { 'C#': 'C%d' % (base_octave + 1), 'D#': 'D%d' % (base_octave + 1), 'E#': 'E%d' % (base_octave + 1), 'F#': 'F%d' % (base_octave + 1), 'G#': 'G%d' % (base_octave + 1), 'A#': 'A%d' % base_octave, 'B#': 'B%d' % base_octave, 'Cb': 'C%d' % (base_octave + 1), 'Db': 'D%d' % (base_octave + 1), 'Eb': 'E%d' % (base_octave + 1), 'Fb': 'F%d' % base_octave, 'Gb': 'G%d' % base_octave, 'Ab': 'A%d' % base_octave, 'Bb': 'B%d' % base_octave, } active_accidentals = {} for acc in self.measure.key_signature.accidentals: value = acc_map[acc] active_accidentals[value[:1]] = acc[1:] x, note_area_width = self._note_area if note_area_width > 80: px = x - 20 note_time = audioproc.MusicalDuration(0) for idx, note in enumerate(self.measure.notes): overflow = note_time + note.duration > self.measure.duration if note.is_rest: sym = { 1: 'rest-whole', 2: 'rest-half', 4: 'rest-quarter', 8: 'rest-8th', 16: 'rest-16th', 32: 'rest-32th', }[note.base_duration.denominator] svg_symbol.paintSymbol(painter, sym, QtCore.QPoint(x, ymid)) if note.base_duration >= audioproc.MusicalDuration(1, 2): dx = 25 dy = -10 else: dx = 12 dy = 0 for d in range(note.dots): painter.setPen(Qt.NoPen) painter.setBrush(Qt.black) painter.drawEllipse(dx - 4 + 10 * d, dy - 4, 9, 9) if note.tuplet != 0: painter.setPen(Qt.black) painter.drawText(-5, -45, '%d' % note.tuplet) # if overflow: # n.setOpacity(0.4) elif len(note.pitches) > 0: min_stave_line = 1000 max_stave_line = -1000 for pitch in note.pitches: stave_line = pitch.stave_line - base_stave_line min_stave_line = min(min_stave_line, stave_line) max_stave_line = max(max_stave_line, stave_line) painter.setPen(Qt.black) painter.setOpacity(0.4 if overflow else 0.8) # Ledger lines above stave. for l in range(6, max_stave_line + 1, 2): painter.drawLine(x - 20, ymid - 10 * l, x + 20, ymid - 10 * l) # Ledger lines below stave. for l in range(-6, min_stave_line - 1, -2): painter.drawLine(x - 20, ymid - 10 * l, x + 20, ymid - 10 * l) painter.setOpacity(1.0) for pitch in note.pitches: stave_line = pitch.stave_line - base_stave_line y = ymid - 10 * stave_line active_accidental = active_accidentals.get( pitch.value, '') if pitch.accidental != active_accidental: sym = self._accidental_map[pitch.accidental] svg_symbol.paintSymbol(painter, sym, QtCore.QPoint(x - 12, y)) active_accidentals[pitch.value] = pitch.accidental if note.base_duration >= audioproc.MusicalDuration( 1, 2): svg_symbol.paintSymbol(painter, 'note-head-void', QtCore.QPoint(x, y)) else: svg_symbol.paintSymbol(painter, 'note-head-black', QtCore.QPoint(x, y)) if note.base_duration <= audioproc.MusicalDuration( 1, 2): painter.fillRect(x + 8, y - 63, 3, 60, Qt.black) if note.base_duration == audioproc.MusicalDuration( 1, 8): flags = 1 elif note.base_duration == audioproc.MusicalDuration( 1, 16): flags = 2 elif note.base_duration == audioproc.MusicalDuration( 1, 32): flags = 3 else: flags = 0 for f in range(flags): svg_symbol.paintSymbol( painter, 'note-flag-down', QtCore.QPoint(x + 11, y - 63 + 12 * f)) for d in range(note.dots): painter.setPen(Qt.NoPen) painter.setBrush(Qt.black) painter.drawEllipse(x + 12 + 10 * d, y - 4, 9, 9) if note.tuplet != 0: painter.drawText(x - 5, y - 85, '%d' % note.tuplet) # if overflow: # n.setOpacity(0.4) x1 = max(x - 12, px) x2 = max(x + 13, x1) if x1 > px: self._edit_areas.append((px, x1, idx, False)) px = x1 if x2 > x1: self._edit_areas.append((x1, x2, idx, True)) px = x2 note_time += note.duration x += int(note_area_width * note.duration.fraction) if px < self.width(): self._edit_areas.append( (px, self.width(), len(self.measure.notes), False)) else: self._note_area = (0, self.width())
def paintBackground(self, painter: QtGui.QPainter) -> None: ymid = self.height() // 2 painter.setPen(Qt.black) painter.setBrush(Qt.black) for l in range(-2, 3): painter.drawLine(0, ymid + 20 * l, self.width() - 1, ymid + 20 * l) if self.is_first: painter.fillRect(0, ymid - 40, 2, 20 * 4, Qt.black) painter.drawLine(self.width() - 1, ymid - 40, self.width() - 1, ymid + 40) if not self.measure_reference.is_first: prev_sibling = down_cast( model.ScoreMeasure, self.measure_reference.prev_sibling.measure) else: prev_sibling = None base_stave_line = self.measure.clef.center_pitch.stave_line base_octave = self.measure.clef.base_octave x = 0 paint_clef = prev_sibling is None or self.measure.clef != prev_sibling.clef if paint_clef and self.width() - x > 200: svg_symbol.paintSymbol( painter, 'clef-%s' % self.measure.clef.symbol, QtCore.QPoint( x + 30, ymid - 10 * (self.measure.clef.base_pitch.stave_line - base_stave_line) )) x += 60 acc_map = { 'C#': 'C%d' % (base_octave + 1), 'D#': 'D%d' % (base_octave + 1), 'E#': 'E%d' % (base_octave + 1), 'F#': 'F%d' % (base_octave + 1), 'G#': 'G%d' % (base_octave + 1), 'A#': 'A%d' % base_octave, 'B#': 'B%d' % base_octave, 'Cb': 'C%d' % (base_octave + 1), 'Db': 'D%d' % (base_octave + 1), 'Eb': 'E%d' % (base_octave + 1), 'Fb': 'F%d' % base_octave, 'Gb': 'G%d' % base_octave, 'Ab': 'A%d' % base_octave, 'Bb': 'B%d' % base_octave, } paint_key_signature = ( prev_sibling is None or self.measure.key_signature != prev_sibling.key_signature) if paint_key_signature and self.width() - x > 200: for acc in self.measure.key_signature.accidentals: value = acc_map[acc] stave_line = value_types.Pitch( value).stave_line - base_stave_line svg_symbol.paintSymbol( painter, self._accidental_map[acc[1:]], QtCore.QPoint(x + 10, ymid - 10 * stave_line)) x += 10 if self.measure.key_signature.accidentals: x += 10 paint_time_signature = ( prev_sibling is None or self.measure.time_signature != prev_sibling.time_signature) if paint_time_signature and self.width() - x > 200: font = QtGui.QFont('FreeSerif', 30, QtGui.QFont.Black) font.setStretch(120) painter.setFont(font) painter.drawText(x, ymid - 5, '%d' % self.measure.time_signature.upper) painter.drawText(x, ymid + 32, '%d' % self.measure.time_signature.lower) x += 40 if self.width() - x > 100: self._note_area = (x + 20, self.width() - x - 20) else: self._note_area = (0, self.width())
def paintBackground(self, painter): ymid = self.height() // 2 painter.setPen(Qt.black) painter.setBrush(Qt.black) for l in range(-2, 3): painter.drawLine(0, ymid + 20 * l, self.width() - 1, ymid + 20 * l) if self.is_first: painter.fillRect(0, ymid - 40, 2, 20 * 4, Qt.black) painter.drawLine(self.width() - 1, ymid - 40, self.width() - 1, ymid + 40) base_stave_line = self.measure.clef.center_pitch.stave_line base_octave = self.measure.clef.base_octave x = 0 if self.width() - x > 200: svg_symbol.paintSymbol( painter, 'clef-%s' % self.measure.clef.symbol, QtCore.QPoint( x + 30, ymid - 10 * (self.measure.clef.base_pitch.stave_line - base_stave_line))) x += 60 acc_map = { 'C#': 'C%d' % (base_octave + 1), 'D#': 'D%d' % (base_octave + 1), 'E#': 'E%d' % (base_octave + 1), 'F#': 'F%d' % (base_octave + 1), 'G#': 'G%d' % (base_octave + 1), 'A#': 'A%d' % base_octave, 'B#': 'B%d' % base_octave, 'Cb': 'C%d' % (base_octave + 1), 'Db': 'D%d' % (base_octave + 1), 'Eb': 'E%d' % (base_octave + 1), 'Fb': 'F%d' % base_octave, 'Gb': 'G%d' % base_octave, 'Ab': 'A%d' % base_octave, 'Bb': 'B%d' % base_octave, } active_accidentals = {} for acc in self.measure.key_signature.accidentals: value = acc_map[acc] active_accidentals[value[:1]] = acc[1:] if self.width() - x > 200: for acc in self.measure.key_signature.accidentals: value = acc_map[acc] stave_line = music.Pitch(value).stave_line - base_stave_line svg_symbol.paintSymbol( painter, self._accidental_map[acc[1:]], QtCore.QPoint( x + 10, ymid - 10 * stave_line)) x += 10 if self.measure.key_signature.accidentals: x += 10 if self.width() - x > 200: font = QtGui.QFont('FreeSerif', 30, QtGui.QFont.Black) font.setStretch(120) painter.setFont(font) painter.drawText( x, ymid - 5, '%d' % self.measure.time_signature.upper) painter.drawText( x, ymid + 32, '%d' % self.measure.time_signature.lower) x += 40 if self.width() - x > 100: self._note_area = (x + 20, self.width() - x - 20) else: self._note_area = (0, self.width())
def paintGhost(self, painter): if self.__ghost_pos is None: return ymid = self.height() // 2 tool = self.track_item.currentTool() pos = self.__ghost_pos painter.setOpacity(0.4) if tool.is_rest: sym = { music.Duration(1, 1): 'rest-whole', music.Duration(1, 2): 'rest-half', music.Duration(1, 4): 'rest-quarter', music.Duration(1, 8): 'rest-8th', music.Duration(1, 16): 'rest-16th', music.Duration(1, 32): 'rest-32th', }[self._tool_duration_map[tool]] svg_symbol.paintSymbol( painter, sym, QtCore.QPoint(pos.x(), ymid)) elif tool.is_note: duration = self._tool_duration_map[tool] if duration >= music.Duration(1, 2): svg_symbol.paintSymbol( painter, 'note-head-void', pos) else: svg_symbol.paintSymbol( painter, 'note-head-black', pos) if duration <= music.Duration(1, 2): painter.fillRect(pos.x() + 8, pos.y() - 63, 3, 60, Qt.black) if duration == music.Duration(1, 8): flags = 1 elif duration == music.Duration(1, 16): flags = 2 elif duration == music.Duration(1, 32): flags = 3 else: flags = 0 for f in range(flags): svg_symbol.paintSymbol( painter, 'note-flag-down', QtCore.QPoint(pos.x() + 11, pos.y() - 63 + 12 * f)) elif tool.is_accidental: accidental = { tools.Tool.ACCIDENTAL_NATURAL: '', tools.Tool.ACCIDENTAL_FLAT: 'b', tools.Tool.ACCIDENTAL_SHARP: '#', tools.Tool.ACCIDENTAL_DOUBLE_FLAT: 'bb', tools.Tool.ACCIDENTAL_DOUBLE_SHARP: '##', }[tool] sym = self._accidental_map[accidental] svg_symbol.paintSymbol(painter, sym, pos) else: painter.setPen(Qt.NoPen) painter.setBrush(Qt.black) painter.drawEllipse(pos.x() - 15, pos.y() - 15, 31, 31, Qt.black)
def paintForeground(self, painter): assert self._note_area is not None self._edit_areas.clear() ymid = self.height() // 2 base_stave_line = self.measure.clef.center_pitch.stave_line base_octave = self.measure.clef.base_octave acc_map = { 'C#': 'C%d' % (base_octave + 1), 'D#': 'D%d' % (base_octave + 1), 'E#': 'E%d' % (base_octave + 1), 'F#': 'F%d' % (base_octave + 1), 'G#': 'G%d' % (base_octave + 1), 'A#': 'A%d' % base_octave, 'B#': 'B%d' % base_octave, 'Cb': 'C%d' % (base_octave + 1), 'Db': 'D%d' % (base_octave + 1), 'Eb': 'E%d' % (base_octave + 1), 'Fb': 'F%d' % base_octave, 'Gb': 'G%d' % base_octave, 'Ab': 'A%d' % base_octave, 'Bb': 'B%d' % base_octave, } active_accidentals = {} for acc in self.measure.key_signature.accidentals: value = acc_map[acc] active_accidentals[value[:1]] = acc[1:] x, note_area_width = self._note_area if note_area_width > 80: px = x - 20 note_time = music.Duration(0) for idx, note in enumerate(self.measure.notes): overflow = note_time + note.duration > self.measure.duration if note.is_rest: sym = { music.Duration(1, 1): 'rest-whole', music.Duration(1, 2): 'rest-half', music.Duration(1, 4): 'rest-quarter', music.Duration(1, 8): 'rest-8th', music.Duration(1, 16): 'rest-16th', music.Duration(1, 32): 'rest-32th', }[note.base_duration] svg_symbol.paintSymbol( painter, sym, QtCore.QPoint(x, ymid)) if note.base_duration >= music.Duration(1, 2): dx = 25 dy = -10 else: dx = 12 dy = 0 for d in range(note.dots): painter.setPen(Qt.NoPen) painter.setBrush(Qt.black) painter.drawEllipse(dx - 4 + 10*d, dy - 4, 9, 9) if note.tuplet != 0: painter.setPen(Qt.black) painter.drawText(-5, -45, '%d' % note.tuplet) # if overflow: # n.setOpacity(0.4) elif len(note.pitches) > 0: min_stave_line = 1000 max_stave_line = -1000 for pitch in note.pitches: stave_line = pitch.stave_line - base_stave_line min_stave_line = min(min_stave_line, stave_line) max_stave_line = max(max_stave_line, stave_line) painter.setPen(Qt.black) painter.setOpacity(0.4 if overflow else 0.8) # Ledger lines above stave. for l in range(6, max_stave_line + 1, 2): painter.drawLine( x - 20, ymid - 10 * l, x + 20, ymid - 10 * l) # Ledger lines below stave. for l in range(-6, min_stave_line - 1, -2): painter.drawLine( x - 20, ymid - 10 * l, x + 20, ymid - 10 * l) painter.setOpacity(1.0) for pitch in note.pitches: stave_line = pitch.stave_line - base_stave_line y = ymid - 10 * stave_line active_accidental = active_accidentals.get(pitch.value, '') if pitch.accidental != active_accidental: sym = self._accidental_map[pitch.accidental] svg_symbol.paintSymbol( painter, sym, QtCore.QPoint(x - 12, y)) active_accidentals[pitch.value] = pitch.accidental if note.base_duration >= music.Duration(1, 2): svg_symbol.paintSymbol( painter, 'note-head-void', QtCore.QPoint(x, y)) else: svg_symbol.paintSymbol( painter, 'note-head-black', QtCore.QPoint(x, y)) if note.base_duration <= music.Duration(1, 2): painter.fillRect(x + 8, y - 63, 3, 60, Qt.black) if note.base_duration == music.Duration(1, 8): flags = 1 elif note.base_duration == music.Duration(1, 16): flags = 2 elif note.base_duration == music.Duration(1, 32): flags = 3 else: flags = 0 for f in range(flags): svg_symbol.paintSymbol( painter, 'note-flag-down', QtCore.QPoint(x + 11, y - 63 + 12 * f)) for d in range(note.dots): painter.setPen(Qt.NoPen) painter.setBrush(Qt.black) painter.drawEllipse(x + 12 + 10*d, y - 4, 9, 9) if note.tuplet != 0: painter.drawText(x - 5, y - 85, '%d' % note.tuplet) # if overflow: # n.setOpacity(0.4) # if self.app.showEditAreas: # info = QtWidgets.QGraphicsSimpleTextItem(self) # info.setText( # '%d/%d' % (min_stave_line, max_stave_line)) # info.setPos(x - 10, 0) x1 = max(x - 12, px) x2 = max(x + 13, x1) if x1 > px: self._edit_areas.append((px, x1, idx, False)) px = x1 if x2 > x1: self._edit_areas.append((x1, x2, idx, True)) px = x2 note_time += note.duration x += int(note_area_width * note.duration) if px < self.width(): self._edit_areas.append( (px, self.width(), len(self.measure.notes), False)) else: self._note_area = (0, self.width())