def insertRow(self, row): self.insertingFrames = True document = self.document() rootFrame = document.rootFrame() rows = rootFrame.childFrames() if row > len(rows): traceLogger.critical("invalid row %d > %d" % (row, len(rows))) row = len(rows) if row == len(rows): cursor = QTextCursor(document) cursor.movePosition(QTextCursor.End) elif rows: cursor = QTextCursor(rows[row].firstCursorPosition()) cursor.movePosition(QTextCursor.PreviousBlock) cursor.insertFrame(QtReduceRowFormat()) cursor.insertFrame(QtReduceInput().frameFormat) position = cursor.position() cursor.clearSelection() cursor.setBlockFormat(QtReduceInput().blockFormat) cursor.setBlockCharFormat(QtReduceInput().charFormat) cursor.movePosition(QTextCursor.NextBlock) cursor.insertFrame(QtReduceNoResult().frameFormat) cursor.setBlockFormat(QtReduceNoResult().blockFormat) cursor.setBlockCharFormat(QtReduceNoResult().charFormat) cursor.insertText(QtReduceFrameView.NotEvaluated) cursor.setPosition(position) self.insertingFrames = False
def insertRow(self,row): self.insertingFrames = True document = self.document() rootFrame = document.rootFrame() rows = rootFrame.childFrames() if row > len(rows): traceLogger.critical("invalid row %d > %d" % (row,len(rows))) row = len(rows) if row == len(rows): cursor = QTextCursor(document) cursor.movePosition(QTextCursor.End) elif rows: cursor = QTextCursor(rows[row].firstCursorPosition()) cursor.movePosition(QTextCursor.PreviousBlock) cursor.insertFrame(QtReduceRowFormat()) cursor.insertFrame(QtReduceInput().frameFormat) position = cursor.position() cursor.clearSelection() cursor.setBlockFormat(QtReduceInput().blockFormat) cursor.setBlockCharFormat(QtReduceInput().charFormat) cursor.movePosition(QTextCursor.NextBlock) cursor.insertFrame(QtReduceNoResult().frameFormat) cursor.setBlockFormat(QtReduceNoResult().blockFormat) cursor.setBlockCharFormat(QtReduceNoResult().charFormat) cursor.insertText(QtReduceFrameView.NotEvaluated) cursor.setPosition(position) self.insertingFrames = False
def __positionPair(self, pos): cursor = QTextCursor(self.document()) cursor.setPosition(pos) rootFrame = self.document().rootFrame() frames = rootFrame.childFrames() frame = cursor.currentFrame() if frame == rootFrame: # pos is outside any group return [pos, pos] p = frame.parentFrame() if p != rootFrame: # frame is an input or output frame containing pos return [frame.firstPosition(), frame.lastPosition()] # frame is a group frame containing pos in a comment position localFrames = frame.childFrames() inputStart = localFrames[0].firstPosition() if pos < inputStart: return [frame.firstPosition(), inputStart - 1] inputEnd = localFrames[0].lastPosition() outputStart = localFrames[1].firstPosition() if inputEnd < pos and pos < outputStart: return [inputEnd + 1, outputStart - 1] outputEnd = localFrames[1].lastPosition() if outputEnd < pos: return [outputEnd + 1, frame.lastPosition()] traceLogger.critical("bad position %d (%d %d) (%d %d)" % (pos, ifr.firstPosition(), ifr.lastPosition(), ofr.firstPosition(), ofr.lastPosition())) return [-1, -1]
def __positionPair(self,pos): cursor = QTextCursor(self.document()) cursor.setPosition(pos) rootFrame = self.document().rootFrame() frames = rootFrame.childFrames() frame = cursor.currentFrame() if frame == rootFrame: # pos is outside any group return [pos,pos] p = frame.parentFrame() if p != rootFrame: # frame is an input or output frame containing pos return [frame.firstPosition(),frame.lastPosition()] # frame is a group frame containing pos in a comment position localFrames = frame.childFrames() inputStart = localFrames[0].firstPosition() if pos < inputStart: return [frame.firstPosition(),inputStart-1] inputEnd = localFrames[0].lastPosition() outputStart = localFrames[1].firstPosition() if inputEnd < pos and pos < outputStart: return [inputEnd+1,outputStart-1] outputEnd = localFrames[1].lastPosition() if outputEnd < pos: return [outputEnd+1,frame.lastPosition()] traceLogger.critical("bad position %d (%d %d) (%d %d)" % (pos, ifr.firstPosition(), ifr.lastPosition(), ofr.firstPosition(), ofr.lastPosition())) return [-1,-1]
def removeRows(self,start,end): document = self.document() rootFrame = document.rootFrame() rows = rootFrame.childFrames() if end >= len(rows): traceLogger.critical("invalid row %d >= %d" % (end,len(rows))) cursor = rows[start].firstCursorPosition() cursor.setPosition(rows[end].lastPosition()+1,QTextCursor.KeepAnchor) cursor.deleteChar()
def insertAbove(self): if self.view.atEnd(): self.model.insertRow(self.model.rowCount()) return row = self.view.rowOrNextRow() if row < 0: traceLogger.critical("unxepected row %s" % row) self.model.insertRow(row) self.view.gotoRow(row)
def insertBelow(self): if self.view.atStart(): self.model.insertRow(0) return row = self.view.rowOrPreviousRow() if row < 0: traceLogger.critical("unxepected row %s" % row) self.model.insertRow(row + 1) self.view.gotoRow(row + 1)
def removeRows(self, start, end): document = self.document() rootFrame = document.rootFrame() rows = rootFrame.childFrames() if end >= len(rows): traceLogger.critical("invalid row %d >= %d" % (end, len(rows))) cursor = rows[start].firstCursorPosition() cursor.setPosition(rows[end].lastPosition() + 1, QTextCursor.KeepAnchor) cursor.deleteChar()
def gotoRow(self, row): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row, len(rows) - 1)) row = len(rows) cursor = QTextCursor(rows[row].lastCursorPosition()) cursor.movePosition(QTextCursor.NextBlock) self.setTextCursor(cursor) self.ensureCursorVisible() cursor = QTextCursor(rows[row].childFrames()[0]) self.setTextCursor(cursor) self.ensureCursorVisible()
def gotoRow(self,row): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row,len(rows)-1)) row = len(rows) cursor = QTextCursor(rows[row].lastCursorPosition()) cursor.movePosition(QTextCursor.NextBlock) self.setTextCursor(cursor) self.ensureCursorVisible() cursor = QTextCursor(rows[row].childFrames()[0]) self.setTextCursor(cursor) self.ensureCursorVisible()
def rowOrPreviousRow(self, cursor=None): if not cursor: cursor = self.textCursor() row = self.row(cursor) if row >= 0: return row if cursor.atStart(): return -1 cursor.movePosition(QTextCursor.Left) row = self.row(cursor) if row >= 0: return row traceLogger.critical("unexpected negative row index")
def rowOrPreviousRow(self,cursor=None): if not cursor: cursor = self.textCursor() row = self.row(cursor) if row >= 0: return row if cursor.atStart(): return -1 cursor.movePosition(QTextCursor.Left) row = self.row(cursor) if row >= 0: return row traceLogger.critical("unexpected negative row index")
def endComputationHandler(self,computation): row = computation.row if row == -1: traceLogger.critical("invalid row -1") # row = -2 is be used to initialize the status bar. It is # ignored in the model but computation takes place and signals # are processed. if row >= 0: if row == len(self.model) - 1: self.insertRow(row+1) self.setData(self.index(row,0),computation, QtReduceModel.RawDataRole) self.endComputation.emit(computation)
def rowOrNextRow(self, cursor=None): if not cursor: cursor = self.textCursor() row = self.row(cursor) if row >= 0: return row if cursor.atEnd(): return -1 cursor.movePosition(QTextCursor.Right) row = self.row(cursor) if row >= 0: return row traceLogger.critical("unexpected negative row index")
def rowOrNextRow(self,cursor=None): if not cursor: cursor = self.textCursor() row = self.row(cursor) if row >= 0: return row if cursor.atEnd(): return -1 cursor.movePosition(QTextCursor.Right) row = self.row(cursor) if row >= 0: return row traceLogger.critical("unexpected negative row index")
def subCell(self): rootFrame = self.document().rootFrame() rows = rootFrame.childFrames() cursor = self.textCursor() pos = cursor.position() frame = cursor.currentFrame() if frame == rootFrame: # pos is outside any group return SubCell(-1, pos, pos, SubCell.Root, '') p = frame.parentFrame() if p != rootFrame: # frame is an input or output frame containing pos row = rows.index(p) i = p.childFrames().index(frame) if i == 0: typ = SubCell.Input elif i == 1: typ = SubCell.Output else: traceLogger.critical("bad subframe %d" % i) startPos = frame.firstPosition() endPos = frame.lastPosition() else: # frame is a group frame containing pos in a comment position row = rows.index(frame) localFrames = frame.childFrames() inputStart = localFrames[0].firstPosition() if pos < inputStart: typ = SubCell.C1 startPos = frame.firstPosition() endPos = inputStart - 1 else: inputEnd = localFrames[0].lastPosition() outputStart = localFrames[1].firstPosition() if inputEnd < pos and pos < outputStart: typ = SubCell.C2 startPos = inputEnd + 1 endPos = outputStart - 1 else: outputEnd = localFrames[1].lastPosition() if pos <= outputEnd: traceLogger.critical("pos could not be identified %d" % pos) typ = SubCell.C3 startPos = outputEnd + 1 endPos = frame.lastPosition() cursor.setPosition(startPos) cursor.setPosition(endPos, QTextCursor.KeepAnchor) content = cursor.selection().toPlainText() return SubCell(row, startPos, endPos, typ, content)
def currentField(self,row): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row,len(rows)-1)) row = len(rows) inputFrame = rows[row].childFrames()[0] cursor = QTextCursor(inputFrame) cursor.setPosition(inputFrame.lastPosition(),QTextCursor.KeepAnchor) command = cursor.selectedText() command = command.replace(u'\u2028',u'\n') command = command.replace(u'\u2029',u'\n') if command != '' and not command[-1] in [';','$']: command += ';' return command
def currentField(self, row): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row, len(rows) - 1)) row = len(rows) inputFrame = rows[row].childFrames()[0] cursor = QTextCursor(inputFrame) cursor.setPosition(inputFrame.lastPosition(), QTextCursor.KeepAnchor) command = cursor.selectedText() command = command.replace(u'\u2028', u'\n') command = command.replace(u'\u2029', u'\n') if command != '' and not command[-1] in [';', '$']: command += ';' return command
def subCell(self): rootFrame = self.document().rootFrame() rows = rootFrame.childFrames() cursor = self.textCursor() pos = cursor.position() frame = cursor.currentFrame() if frame == rootFrame: # pos is outside any group return SubCell(-1,pos,pos,SubCell.Root,'') p = frame.parentFrame() if p != rootFrame: # frame is an input or output frame containing pos row = rows.index(p) i = p.childFrames().index(frame) if i == 0: typ = SubCell.Input elif i == 1: typ = SubCell.Output else: traceLogger.critical("bad subframe %d" % i) startPos = frame.firstPosition() endPos = frame.lastPosition() else: # frame is a group frame containing pos in a comment position row = rows.index(frame) localFrames = frame.childFrames() inputStart = localFrames[0].firstPosition() if pos < inputStart: typ = SubCell.C1 startPos = frame.firstPosition() endPos = inputStart-1 else: inputEnd = localFrames[0].lastPosition() outputStart = localFrames[1].firstPosition() if inputEnd < pos and pos < outputStart: typ = SubCell.C2 startPos = inputEnd+1 endPos = outputStart-1 else: outputEnd = localFrames[1].lastPosition() if pos <= outputEnd: traceLogger.critical("pos could not be identified %d" % pos) typ = SubCell.C3 startPos = outputEnd+1 endPos = frame.lastPosition() cursor.setPosition(startPos) cursor.setPosition(endPos,QTextCursor.KeepAnchor) content = cursor.selection().toPlainText() return SubCell(row,startPos,endPos,typ,content)
def __setOutput(self, row, text, reduceBlockFormat): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row, len(rows) - 1)) return rowFrames = rows[row].childFrames() if len(rowFrames) != 2: traceLogger.critical("%d subframes in row %d", len(rowFrames), row) return outputFrame = rowFrames[1] cursor = outputFrame.firstCursorPosition() cursor.setPosition(outputFrame.lastPosition(), QTextCursor.KeepAnchor) cursor.setBlockFormat(reduceBlockFormat.blockFormat) traceLogger.warning("text=%s" % text) cursor.insertText(text.decode('utf-8'), reduceBlockFormat.charFormat)
def setInput(self, row, text): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row, len(rows) - 1)) return rowFrames = rows[row].childFrames() if len(rowFrames) != 2: traceLogger.critical("%d subframes in row %d", len(rowFrames), row) return #text = text.rstrip(';') inputFrame = rowFrames[0] cursor = inputFrame.firstCursorPosition() cursor.setPosition(inputFrame.lastPosition(), QTextCursor.KeepAnchor) cursor.setBlockFormat(QtReduceInput().blockFormat) cursor.insertText(text, QtReduceInput().charFormat)
def setInput(self,row,text): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row,len(rows)-1)) return rowFrames = rows[row].childFrames() if len(rowFrames) != 2: traceLogger.critical("%d subframes in row %d", len(rowFrames), row) return #text = text.rstrip(';') inputFrame = rowFrames[0] cursor = inputFrame.firstCursorPosition() cursor.setPosition(inputFrame.lastPosition(),QTextCursor.KeepAnchor) cursor.setBlockFormat(QtReduceInput().blockFormat) cursor.insertText(text,QtReduceInput().charFormat)
def __setOutput(self,row,text,reduceBlockFormat): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row,len(rows)-1)) return rowFrames = rows[row].childFrames() if len(rowFrames) != 2: traceLogger.critical("%d subframes in row %d", len(rowFrames), row) return outputFrame = rowFrames[1] cursor = outputFrame.firstCursorPosition() cursor.setPosition(outputFrame.lastPosition(),QTextCursor.KeepAnchor) cursor.setBlockFormat(reduceBlockFormat.blockFormat) traceLogger.warning("text=%s" % text) cursor.insertText(text.decode('utf-8'),reduceBlockFormat.charFormat)
def setCell(self, row, c1, input, c2, output, c3): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row, len(rows) - 1)) return cell = rows[row] rowFrames = cell.childFrames() if len(rowFrames) != 2: traceLogger.critical("%d subframes in row %d", len(rowFrames), row) return inputFrame = rowFrames[0] outputFrame = rowFrames[1] # C1: cursor = cell.firstCursorPosition() cursor.setPosition(inputFrame.firstPosition(), QTextCursor.KeepAnchor) cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor) cursor.insertText(c1) # Input: cursor.setPosition(inputFrame.firstPosition()) cursor.setPosition(inputFrame.lastPosition(), QTextCursor.KeepAnchor) cursor.setBlockFormat(QtReduceInput().blockFormat) #cursor.insertText(input,QtReduceInput().charFormat) cursor.insertText(input) # C2: cursor.setPosition(inputFrame.lastPosition()) cursor.movePosition(QTextCursor.Right) cursor.setPosition(outputFrame.firstPosition(), QTextCursor.KeepAnchor) cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor) cursor.insertText(c2) # Output: cursor.setPosition(outputFrame.firstPosition()) cursor.setPosition(outputFrame.lastPosition(), QTextCursor.KeepAnchor) # ... # C3 cursor.setPosition(outputFrame.lastPosition()) cursor.movePosition(QTextCursor.Right) cursor.setPosition(cell.lastPosition(), QTextCursor.KeepAnchor) cursor.insertText(c3)
def setCell(self,row,c1,input,c2,output,c3): rows = self.document().rootFrame().childFrames() if row >= len(rows): traceLogger.critical("invalid row %d > %d" % (row,len(rows)-1)) return cell = rows[row] rowFrames = cell.childFrames() if len(rowFrames) != 2: traceLogger.critical("%d subframes in row %d", len(rowFrames), row) return inputFrame = rowFrames[0] outputFrame = rowFrames[1] # C1: cursor = cell.firstCursorPosition() cursor.setPosition(inputFrame.firstPosition(),QTextCursor.KeepAnchor) cursor.movePosition(QTextCursor.Left,QTextCursor.KeepAnchor) cursor.insertText(c1) # Input: cursor.setPosition(inputFrame.firstPosition()) cursor.setPosition(inputFrame.lastPosition(),QTextCursor.KeepAnchor) cursor.setBlockFormat(QtReduceInput().blockFormat) #cursor.insertText(input,QtReduceInput().charFormat) cursor.insertText(input) # C2: cursor.setPosition(inputFrame.lastPosition()) cursor.movePosition(QTextCursor.Right) cursor.setPosition(outputFrame.firstPosition(),QTextCursor.KeepAnchor) cursor.movePosition(QTextCursor.Left,QTextCursor.KeepAnchor) cursor.insertText(c2) # Output: cursor.setPosition(outputFrame.firstPosition()) cursor.setPosition(outputFrame.lastPosition(),QTextCursor.KeepAnchor) # ... # C3 cursor.setPosition(outputFrame.lastPosition()) cursor.movePosition(QTextCursor.Right) cursor.setPosition(cell.lastPosition(),QTextCursor.KeepAnchor) cursor.insertText(c3)
def __abortComputationBox(self): tit = self.tr("Aborting Batch Computation") txt = self.tr("Reduce is currently evaluating several groups in " "batch. Do you want to interrupt only the current " "computation or all?") diag = QMessageBox(self.model.controller.mainWindow) diag.setWindowTitle(tit) diag.setIcon(QMessageBox.Question) diag.setText(txt) abortAll = diag.addButton("Abort All",QMessageBox.YesRole) abortCurrent = diag.addButton("Abort Current",QMessageBox.YesRole) cancel = diag.addButton(QMessageBox.StandardButton.Cancel) diag.setDefaultButton(abortAll) diag.setEscapeButton(cancel) diag.setWindowModality(Qt.WindowModal) diag.exec_() ans = diag.clickedButton() if ans == abortAll: return 'all' elif ans == abortCurrent: return 'current' elif ans == cancel: return 'cancel' traceLogger.critical("unidentifiable button %s" % ans)
def save(self): if not self.__fileName: traceLogger.critical('empty file name') self.__saveAs(self.__fileName)