def correct(self,last_char,next_char,cursor): ch_list = [u'\n',u' ',u'\u00A0',self.language.quotes[1],'!','?'] if last_char in'!?' and (next_char not in ch_list): COTextCursorFunctions.insertText(cursor,u' ') return True return False
def correct(self,last_char,next_char,cursor): ch_list = [u'\n',u' ',u'\u00A0',self.language.quotes[1],u'.']+[str(i) for i in range(10)] if last_char in '.,;:' and (next_char not in ch_list): COTextCursorFunctions.insertText(cursor,u' ') return True return False
def SLOT_actionInsertImage(self): if self.parent()!=None: dft_opening_site = self.parent().get_default_opening_saving_site() local_dir = self.get_local_dir() else: dft_opening_site ='.' local_dir = False filepath = FMTextFileManagement.open_gui_filepath( dft_opening_site , self,filter="Image Files (*.png *.jpg *.bmp *.gif)") if filepath: d,f = os.path.split(filepath) assert os.path.isabs(d) if local_dir and d!=os.path.abspath(local_dir): assert os.path.isabs(local_dir) r = QtWidgets.QMessageBox.question(self, "Local importation", "Do you want to import the file locally ?", QtWidgets.QMessageBox.Yes|QtWidgets.QMessageBox.No) if r== QtWidgets.QMessageBox.Yes: newfilepath = os.path.join(local_dir,f) newfilepath = FMTextFileManagement.exists(newfilepath) if not newfilepath: return False shutil.copyfile(filepath,newfilepath) tmp,newfilepath = os.path.split(newfilepath) #local path elif not local_dir: newfilepath = filepath r = QtWidgets.QMessageBox.question(self, "Local importation", "Since you did not saved the file, the absolute path "+\ "will be displayed. For import the file localy, save "+\ "first", QtWidgets.QMessageBox.Yes|QtWidgets.QMessageBox.Cancel) if r== QtWidgets.QMessageBox.Cancel: return False else : tmp,newfilepath = os.path.split(filepath) #local path # self.SLOT_setFormating(TSStyleImage.userPropertyId) self.blockSignals(True) cursor=self.textCursor() cursor.clearSelection() block_id = cursor.blockFormat().property( QtGui.QTextFormat.UserProperty) # # block_id = block_id.toPyObject() cursor.beginEditBlock() if block_id==TSStyleImage.userPropertyId : # if there is an image, we remove the previous one TSManager.inverseStyle(cursor,block_id) res,cursor1 = TSManager.inverseStyle(cursor,TSStyleImage.userPropertyId) COTextCursorFunctions.insertText(cursor1,newfilepath) cursor.endEditBlock() self.blockSignals(False) self.somethingChanged .emit() return filepath return False
def correct(self,last_char,next_char,cursor): if last_char==u'.' and next_char==u'.': if self.language.lastChar(cursor,n=2)==u'.': cursor.deleteChar() cursor.deletePreviousChar() cursor.deletePreviousChar() COTextCursorFunctions.insertText(cursor,u'\u2026') return True return False
def correct(self,last_char,next_char,cursor): if last_char in self.diff_quotes[0]: cursor.deletePreviousChar() COTextCursorFunctions.insertText(cursor,self.language.quotes[0]) return True elif last_char in self.diff_quotes[1]: cursor.deletePreviousChar() COTextCursorFunctions.insertText(cursor,self.language.quotes[1]) return True return False
def correct(self,last_char,next_char,cursor): if next_char==u'"': if last_char in [u' ',u'\n',u'\u00A0']: cursor.deleteChar() COTextCursorFunctions.insertText(cursor,self.language.quotes[0]) else : cursor.deleteChar() COTextCursorFunctions.insertText(cursor,self.language.quotes[1]) return True return False
def SLOT_correctWord(self,word): """ Replaces the selected text with word. """ cursor = self.textCursor() cursor.beginEditBlock() cursor.removeSelectedText() COTextCursorFunctions.insertText(cursor,word) self.language.check_after_paste(cursor,len(word) ) cursor.endEditBlock()
def insert_delimiters(self, delim_key, cursor): assert delim_key in self.delimiters if cursor.hasSelection(): selectionStart = cursor.selectionStart() selectionEnd = cursor.selectionEnd() position = cursor.position() l1 = len(self.delimiters[delim_key][0]) l2 = len(self.delimiters[delim_key][1]) cursor.beginEditBlock() cursor.setPosition(selectionStart) COTextCursorFunctions.insertText(cursor, self.delimiters[delim_key][0]) cursor.setPosition(selectionEnd + l1) COTextCursorFunctions.insertText(cursor, self.delimiters[delim_key][1]) cursor.setPosition(selectionStart + l1, QtGui.QTextCursor.MoveAnchor) cursor.setPosition(selectionEnd + l1, QtGui.QTextCursor.KeepAnchor) cursor.endEditBlock() # cursor.setPosition(position+l1,QtGui.QTextCursor.KeepAnchor) else: cursor.beginEditBlock() COTextCursorFunctions.insertText(cursor, self.delimiters[delim_key][0]) COTextCursorFunctions.insertText(cursor, self.delimiters[delim_key][1]) cursor.movePosition(QtGui.QTextCursor.Left, QtGui.QTextCursor.MoveAnchor, len(self.delimiters[delim_key][1])) cursor.endEditBlock() return cursor
def insertFromMimeData(self,source ): """A re-implementation of insertFromMimeData. We have to check the typography of what we have just paste. TODO : some summary window of all the corrections. """ self.blockSignals (True) cursor=self.textCursor() cursor.beginEditBlock() cursor_pos=cursor.position() if source.hasFormat("text/athena"): xml = source.data("text/athena") # 106 for the utf-8 xml = QtCore.QTextCodec.codecForMib(106).toUnicode(xml) document = QtGui.QTextDocument() document.setPlainText(xml) TSManager.fromXml(document) cur = QtGui.QTextCursor(document) cur.select(QtGui.QTextCursor.Document) sel = cur.selection() cursor.insertFragment(cur.selection()) size = len(sel.toPlainText()) # if source.html()==self.lastCopy[0].html(): # # if the pasted thing comes from the document itself # cursor.insertFragment(self.lastCopy[1]) # size = len(self.lastCopy[1].toPlainText()) else : text=source.text() text.replace("\t", " ") # for k,v in TEDictCharReplace.items(): # text = text.replace(k,v) COTextCursorFunctions.insertText(cursor,text) size = len(text) cursor.setPosition(cursor_pos) if TEPreferences["DO_TYPOGRAPHY"]: self.language.check_after_paste(cursor,size) cursor.setPosition(cursor_pos) cursor.movePosition( QtGui.QTextCursor.Right, QtGui.QTextCursor.KeepAnchor, n = size) # self.setTextCursor(cursor) TSManager.recheckBlockStyle(cursor) TSManager.recheckCharStyle(cursor) cursor.endEditBlock() self.blockSignals (False)
def correct(self,last_char,next_char,cursor): if self.language.inside_quotes==None: if last_char==self.language.quotes[0] and next_char in all_spaces: cursor.deleteChar() return True elif last_char in all_spaces and next_char==self.language.quotes[1]: cursor.deletePreviousChar() return True else: if last_char==self.language.quotes[0] and next_char!=self.language.inside_quotes: COTextCursorFunctions.insertText(cursor,self.language.inside_quotes) return True elif last_char!=self.language.inside_quotes and next_char==self.language.quotes[1]: COTextCursorFunctions.insertText(cursor,self.language.inside_quotes) return True return False
def resetStyle(self,cursor): """Will reset all the formating of the cursor, that is to say: - the char format of the selection - the block format of the whole block """ res1 = False res2 = False ## Let's remove the style of all the selected blocks for block,cursor1 in COTextCursorFunctions.yieldBlock(cursor): qtBlockFormat_id = getBlockId(cursor1) if qtBlockFormat_id in list(self.dictBlockStyle.keys()): self.inverseStyle(cursor1,qtBlockFormat_id) res2 = True ## Let's remove then all the styles in the selection if cursor.hasSelection(): cursor1 = QtGui.QTextCursor(cursor.document()) cursor1.setPosition(cursor.selectionStart()) cursor1.movePosition(QtGui.QTextCursor.Right) last_id = None st_pos = cursor1.position() while cursor1.position()<=cursor.selectionEnd(): new_id = getCharId(cursor1) if new_id!=last_id: if last_id!=None: print("cursor1.position() : ",cursor1.position()) print("st_pos : ",st_pos) cursor2 = QtGui.QTextCursor(cursor1.document()) cursor2.setPosition(st_pos) cursor2.setPosition(cursor1.position()-1, QtGui.QTextCursor.KeepAnchor) # self.textedit.setTextCursor(cursor2) self.inverseStyle(cursor2,last_id) last_id = new_id st_pos = cursor1.position()-1 # it take the style of the # previous char res1=True cursor1.movePosition(QtGui.QTextCursor.Right) if cursor1.atEnd(): break if last_id!=None: cursor2 = QtGui.QTextCursor(cursor1) cursor2.setPosition(st_pos, QtGui.QTextCursor.KeepAnchor) self.inverseStyle(cursor2,last_id) # if qtCharFormat_id in self.dictCharStyle.keys(): # print "coucou1" # self.inverseStyle(cursor1,qtCharFormat_id) # res1 = True return res1,res2
def correct_between_chars(self, cursor): """Function that will be called everytime the cursor moves. It check the respect of all the typography rules of the two char of both sides of the position that the cursor has just left.""" block_id = cursor.blockFormat().property( QtGui.QTextFormat.UserProperty) # # block_id = block_id.toPyObject() if block_id != None and TSManager.dictStyle[block_id].protected: return False last_char = COTextCursorFunctions.lastChar(cursor) next_char = COTextCursorFunctions.nextChar(cursor) for rule in self.afterCharRules: if self.profile <= rule.profile: res = rule.correct(last_char, next_char, cursor) if res: return (rule, cursor.position()) return False
def afterWordWritten(self, cursor): """Function which is called after a word has just been witten. It replaces some things that are specific to the language. It also correct the word if there has some auto-correction to make.""" cur_tmp = QtGui.QTextCursor(cursor) cur_tmp.clearSelection() block_id = cur_tmp.blockFormat().property( QtGui.QTextFormat.UserProperty) # # block_id = block_id.toPyObject() if block_id != None and TSManager.dictStyle[block_id].protected: return False if cur_tmp.movePosition(QtGui.QTextCursor.Left, QtGui.QTextCursor.MoveAnchor, 1): word, cur_tmp = self.getWordUnderCursor(cur_tmp) replace = False # We replace things due to the language : 'oe' as a elision for # instance word_replace = str(word) id = COWordTools.whatID(word_replace) word_replace = word_replace.lower() for rule in self.afterWordRules: word_replace = rule.correct(word_replace, cursor) if word_replace: word = COWordTools.toID(word_replace, id) replace = True # We replace things due to the language user settings. # Example: 'gvt' --> 'government' word_replace = self.dict_autocorrection.get(word, False) if word_replace: word = word_replace replace = True if replace: COTextCursorFunctions.insertText(cur_tmp, word) return replace return False
def inverseStyle(self,cursor,style_id): """ Will inverse the style on the cursor's selection according to the syle corresponding to style_id returns: - res : True if the style has been applied, False is it has been removed - cursor1 : the cursor1 where have taken place the modification Also, cursor will be moved where to correspond to the new place where should be the textCursor. """ if style_id in list(self.dictBlockStyle.keys()): # cursor1 = QtGui.QTextCursor(cursor) style = self.dictBlockStyle[style_id] res,cursor1 = style.inverseId(cursor) self.recheckBlockStyle(cursor1) for bl,cursor2 in COTextCursorFunctions.yieldBlock(cursor1): cursor2.select(QtGui.QTextCursor.BlockUnderCursor) self.recheckCharStyle(cursor2) return res,cursor1 elif style_id in list(self.dictCharStyle.keys()): style = self.dictCharStyle[style_id] res,cursor1 = style.inverseId(cursor) if cursor1.hasSelection(): for block,cursor2 in COTextCursorFunctions.yieldBlock(cursor1): self.recheckCharStyle(cursor2) else : qtBlockFormat,qtCharFormat = self.getDefaultFormat(cursor1) if res : # if we have to put the syle: style.setStyleToQtFormating(qtCharFormat,cursor1.document()) cursor1.setCharFormat(qtCharFormat) return res,cursor1 # self.inverseCharStyle(cursor,style_id) else: raise ValueError('Format id '+str(style_id)+' unknown !')
def recheckBlockStyle(self,cursor): for block,_ in COTextCursorFunctions.yieldBlock(cursor): cursor1 = QtGui.QTextCursor(block) qtBlockFormat,qtCharFormat = self.getDefaultFormat(cursor1) cursor1.movePosition(QtGui.QTextCursor.EndOfBlock, QtGui.QTextCursor.KeepAnchor) # Why not cursor1.select(QtGui.QTextCursor.BlockUnderCursor): # because it applies the format also at the previous block cursor1.setBlockCharFormat(qtCharFormat) cursor1.setBlockFormat(qtBlockFormat) # For the color to work (for some reason) : qtCharFormat.setForeground(qtCharFormat.foreground()) cursor1.mergeCharFormat(qtCharFormat) # merge in order to keep the id
def correct(self,last_char,next_char,cursor): if next_char==u"'": cursor.deleteChar() COTextCursorFunctions.insertText(cursor,u'\u2019') return True return False