def cueMessage(self): if self.paused: return message = self.pendingMessage self.pendingMessage = None while True: if message: if not isinstance(message, mido.MetaMessage): if message.type == 'control_change': self.accum[message.control] = message.value else: self.output.send(message) lineNo = ((self.accum[110]<<14) +(self.accum[111]<<7) + self.accum[112]) colNo = ((self.accum[113]<<7) + self.accum[114]) self.lineAndCol.emit(lineNo, colNo) #if Common.abcEditor.widget: # Common.abcEditor.widget.moveToRowCol(lineNo, colNo) try: message = self.messages.next() except StopIteration: dbg_print('cue_msg; StopIteration') return dbg_print(message.type, message) #if pendingMessage.type != 'control_change': if message.time != 0: break #dbg_print(dir(pendingMessage), pendingMessage.type) self.pendingMessage = message milliseconds = int(message.time * 1000) QtCore.QTimer.singleShot(milliseconds, self.cueMessage)
def setFileName(self, fileName=None): if fileName is not None: self.fileName = fileName title = "%s - %s" % (self.headerText, os.path.abspath(self.fileName)) dbg_print (title) if self.dock: self.dock.setWindowTitle(title)
def process(self, inFileName, **kw): dbg_print ("External", self.cmd, kw, "run on", inFileName, self.fmtNameIn) baseName = os.path.splitext(inFileName)[0] if inFileName != (self.fmtNameIn % baseName): raise TypeError, ("%s cannot handle this filetype: %s" % (self.fmtNameIn, baseName, self.__class__.__name__, inFileName)) self.outFileName = self.fmtNameOut % baseName if self.cmd is not None: cmd1 = self.cmd(inFileName, self.outFileName, **kw) dbg_print (cmd1) if self.errOnOut: stderr = subprocess.STDOUT else: stderr = subprocess.PIPE process = subprocess.Popen(cmd1, stdout=subprocess.PIPE, stderr=stderr, shell=True) output, error = process.communicate() _retcode = process.poll() if _retcode: pass # raise subprocess.CalledProcessError(retcode, # cmd1, output=output) if self.errOnOut: self.postProcess(output) return None else: self.postProcess(error) return output
def __init__(self, commander=None): QtGui.QPlainTextEdit.__init__(self) dbg_print (self.__class__.__name__+':__init__... commander.reMsg =', commander.reMsg) self.creMsg = re.compile(commander.reMsg) self.rowColOrigin = commander.rowColOrigin self.quiet = False self.cursorPositionChanged.connect(self.handleCursorMove)
def __init__(self): dbg_print ("External __init__", self.fmtNameIn, self.fmtNameOut) if Common.stdBook is None: dbg_print ("what? no Common.stdBook") self.stdTab = None else: self.stdTab = StdTab(self) Common.stdBook.widget.addTab(self.stdTab, self.__class__.__name__)
def moveToRowCol(self, row=0, col=0): block = self.document().findBlockByLineNumber (row) desiredPosition = block.position() + col dbg_print ('AbcEditor.moveToRowCol', row, col, 'desiredPosition', desiredPosition) tc = self.textCursor() tc.setPosition(desiredPosition) self.setTextCursor(tc) self.setFocus()
def postProcess(self, error): External.postProcess(self, error) svgList = [] for line in error.split('\n'): match = self.outFile_CRE.match(line) if match: svgList.append(match.group(1)) dbg_print (svgList) if svgList and Common.score: Common.score.useFiles(svgList)
def showAtRowAndCol(self, row, col): dbg_print ('showAtRowAndCol', row, col) l = len(self.svgDigests) for i in range(l): j = (i +self.which) % l abcEltAtCursor = self.svgDigests[j].buildQuickDic(row, col) if abcEltAtCursor is not None: break else: dbg_print ("can't find svg graphics correspond to row : col...", row, ':', col) return self.showWhichPage(j, force=True)
def loadFile(self, fileName, row=0, col=0): dbg_print ("AbcEditor.loadFile", fileName) f = QtCore.QFile(fileName) if not f.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text): return self.readAll(f) f.close() dbg_print ("Loaded %s" % fileName) self.setFileName(fileName) self.moveToRowCol(row, col) # primarily to gain focus! self.document().setModified(True) # forc rewrite of Score
def rowColAtXY(self, x, y): if not self.quickDic: self.buildQuickDic() # dbg_print('x,y', x, y, [(a, self.quickDic[a][:8]) # for a in self.quickDic.keys()]) x_dist = x/self.scale - self.quickDic['x'] y_dist = y/self.scale - self.quickDic['y'] a_dist = x_dist*x_dist + y_dist*y_dist # dbg_print('a_dist', a_dist[:8]) am = np.argmin(a_dist) row = self.quickDic['row'][am] - 1 col = self.quickDic['col'][am] dbg_print(am, row, col, self.quickDic['x'][am], self.quickDic['y'][am], ) return row, col
def mousePressEvent(self, event): scP = event.scenePos() x = scP.x() y = scP.y() dbg_print ("MyScene.mousePressEvent", #event.pos(), event.scenePos(), event.screenPos() 'scenePos x,y =', x, y, 'button =', event.button(), 'scene width =', self.width(), 'scene height =', self.height(), ) if event.button() == 1: Common.score.locateXY(x, y) event.accept() else: event.ignore()
def __init__(self, parent=None): dbg_print ("Score.__init__", parent) widgetWithMenu.__init__(self) QtGui.QGraphicsView.__init__(self, parent) self.svgItem = None self.backgroundItem = None self.outlineItem = None self.image = QtGui.QImage() self.setScene(MyScene(self)) self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse) self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag) self.which = 0 # default to show first generated svg until we know better. self.svgDigests = [] dbg_print ("!Score.__init__", parent)
def handleCursorMove(self): dbg_print (self.__class__.__name__+':handleCursorMove... self.quiet =', self.quiet) if (Common.abcEditor is None) or self.quiet: return match = self.creMsg.match(self.textCursor().block().text()) dbg_print (self.__class__.__name__+':handleCursorMove... match =', match) if match is None: return location = [o1+o2 for (o1, o2) in zip( map(lambda s: int(s), match.groups()), self.rowColOrigin)] print ("Autolocating error in ABC", location ) Common.abcEditor.widget.moveToRowCol(*location)
def __init__(self, dock=None): dbg_print ("AbcEditor.__init__", dock) widgetWithMenu.__init__(self) Editor.__init__(self) #font = QtGui.QFont() font = self.font() font.setPointSize(10) self.setFont(font) self.setCursorWidth(2) self.setWindowTitle('title') self.textChanged.connect(self.handleTextChanged) self.setLineWrapMode(QtGui.QPlainTextEdit.NoWrap) self.dock = dock Common.timer.timeout.connect(self.countDown) self.cursorPositionChanged.connect( self.handleCursorMove) self.originalText = None
def saveFile(self, fileName=None): if fileName is None: fileName = self.fileName if fileName is None: return #f = QtCore.QFile(fileName) out = open(fileName, 'w') if not out: return self.writeAll(out) out.close() dbg_print ("Saved %s " % fileName) self.document().setModified(False) if Common.abcm2svg: Common.abcm2svg.process(fileName) if Common.abc2midi: Common.abc2midi.process(fileName)
def showWhichPage(self, which=0, force=False): dbg_print ('----- showWhichPage', which) which %= len(self.svgDigests) if (not force) and (which==self.which): return svg_file = self.svgDigests[which].svg_file if not svg_file.exists(): raise IOError, ("'%s' does not exist!" % svg_file.filename()) self.which = which s = self.scene() s.clear() self.svgItem = QtSvg.QGraphicsSvgItem(svg_file.fileName()) self.svgItem.setFlags(QtGui.QGraphicsItem.ItemClipsToShape) self.svgItem.setCacheMode(QtGui.QGraphicsItem.NoCache) self.svgItem.setZValue(0) self.backgroundItem = QtGui.QGraphicsRectItem(self.svgItem.boundingRect()) self.backgroundItem.setBrush(QtCore.Qt.white) self.backgroundItem.setPen(QtGui.QPen(QtCore.Qt.NoPen)) self.backgroundItem.setVisible(True) self.backgroundItem.setZValue(-1) self.outlineItem = QtGui.QGraphicsRectItem(self.svgItem.boundingRect()) outline = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.DashLine) outline.setCosmetic(True) self.outlineItem.setPen(outline) self.outlineItem.setBrush(QtGui.QBrush(QtCore.Qt.NoBrush)) self.outlineItem.setVisible(True) self.outlineItem.setZValue(1) s.addItem(self.backgroundItem) s.addItem(self.svgItem) s.addItem(self.outlineItem) rect = self.outlineItem.boundingRect() # dbg_print (rect) s.setSceneRect(rect) # .adjusted(-10, -10, 10, 10)) self.setViewport(QtGui.QWidget()) self.backgroundItem.setVisible(True) self.outlineItem.setVisible(False)
def writeAll(self, out): text = self.toPlainText() dbg_print('len(text)=', len(text)) out.write(text)
def showNextPage(self): dbg_print ('showNextPage') return self.showWhichPage(self.which + 1)
def showPreviousPage(self): dbg_print ('showPreviousPage') return self.showWhichPage(self.which - 1)
def handleTextChanged(self): self.counted = self.latency dbg_print ('textChanged', self.counted)
def locateXY(self, x, y): row, col = self.svgDigests[self.which].rowColAtXY(x, y) dbg_print ("locateXY(", x, y, " > row,co", row, col) if Common.abcEditor: Common.abcEditor.widget.moveToRowCol(row, col)
def postProcess(self, error): if self.stdTab is None: dbg_print ("self.stdTab is None!") else: self.stdTab.setPlainText(error)
def wheelEvent(self, event): factor = 1.2**( event.delta() / 120.0) self.scale(factor, factor) # self.mustApplyTransform = self.transform() dbg_print ("Score.wheelEvent, delta = ", event.delta()) event.accept()
def readAll(self, f): dbg_print ('readAll', self, f) stream = QtCore.QTextStream(f) text = stream.readAll() self.setPlainText(text)
def loadAnyFile(self): fileName = QtGui.QFileDialog.getOpenFileName(self, "Choose a data file", '', '*.abc')[0] dbg_print ("loadAnyFile 2", fileName) self.loadFile(fileName)
def reloadFile(self): dbg_print ("ReloadFile", self.fileName) self.loadFile(self.fileName)
def buildQuickDic(self, row=None, col=None): """ extract the all-imortant information from the .svg file which enables us to correlate locations within the image with locations within the source abc file. """ fileName = str(self.svg_file.fileName()) if self.svg_tree is None: # dbg_print("building 'quick dictionary' from '%s'" % fileName) # dbg_print('self.eltCursor =', self.eltCursor) for attr, func, dtype in self.attrTab: self.quickDic[attr] = np.array([], dtype=dtype) self.svg_tree = lxml.etree.parse(fileName) # root = svg_tree.getroot() else: self.removeCursor() eltCursor = lxml.etree.Element('circle', r='7', stroke='red', fill="none") eltCursor.set('stroke-width', '2') self.abcEltAtCursor = useEltAtCursor = None justHadAbc = False latest = {} for i,elt in enumerate(self.svg_tree.iter()): # dbg_print (i, elt.tag, type(elt.tag), str(elt.tag)) if callable(elt.tag): continue if elt.tag.endswith('abc'): justHadAbc = True # dbg_print ("got abc tag!") for attr, func, dtype in self.attrTab: latest[attr] = func(elt.get(attr)) self.quickDic[attr] = np.append(self.quickDic[attr], latest[attr]) if (row is not None and # presumably col is not None and ... latest['type'] == 'N' and latest['row'] == (row+1) and latest['col'] == col): dbg_print (i, 'put cursor around notehead here') self.abcEltAtCursor = elt else: pass # dbg_print (i, "not this abc statement") elif justHadAbc and elt.tag.endswith('use'): justHadAbc = False if ((self.abcEltAtCursor is not None) and (useEltAtCursor is None)): useEltAtCursor = elt # dbg_print ("found 'use' right after cursor abc" ) for attr in ('x', 'y'): try: # dbg_print ("adjusting", attr) headCoord = elt.get(attr) if useEltAtCursor is elt: dbg_print ("adjusting eltCursor", 'c'+attr) eltCursor.set('c' + attr, headCoord) self.quickDic[attr][-1] = self.scaledFloat(headCoord) except TypeError: dbg_print ("failed to adjust", attr) break if self.abcEltAtCursor is None: dbg_print ("can't find cursor position!") else: self.cursorsDad = self.abcEltAtCursor.getparent() # dbg_print (dad) # self.removeCursor() self.cursorsDad.insert(0, eltCursor) # test only: self.cursorsDad.remove(eltCursor) outFile = open(fileName, 'w') dbg_print ('written', fileName) self.svg_tree.write(outFile) self.eltCursor = eltCursor return self.abcEltAtCursor
def __init__(self): QtGui.QWidget.__init__(self) self.output = mido.open_output(self.outputPort) dbg_print('MidiPlayer:__init__', self.output)