Beispiel #1
0
 def apply_changes(self):
     """Apply the changes and update the tokens."""
     c = QTextCursor(self._d)
     # record a sensible position for undo
     c.setPosition(self._changes_list[-1][0])
     c.joinPreviousEditBlock() if self.combine_undo else c.beginEditBlock()
     try:
         for start, end, text in self._changes_list:
             c.movePosition(QTextCursor.End) if end is None else c.setPosition(end)
             c.setPosition(start, QTextCursor.KeepAnchor)
             c.insertText(text)
     finally:
         c.endEditBlock()
         if self.combine_undo is None:
             self.combine_undo = True
Beispiel #2
0
 def apply_changes(self):
     """Apply the changes and update the tokens."""
     c = QTextCursor(self._d)
     # record a sensible position for undo
     c.setPosition(self._changes_list[-1][0])
     c.joinPreviousEditBlock() if self.combine_undo else c.beginEditBlock()
     try:
         for start, end, text in self._changes_list:
             c.movePosition(
                 QTextCursor.End) if end is None else c.setPosition(end)
             c.setPosition(start, QTextCursor.KeepAnchor)
             c.insertText(text)
     finally:
         c.endEditBlock()
         if self.combine_undo is None:
             self.combine_undo = True
Beispiel #3
0
 def indent_line(self, line_no, indent_length):
     block = self._get_block(line_no)
     cursor = QTextCursor(block)
     cursor.joinPreviousEditBlock()
     cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
     if indent_length < 0:
         for i in range(-indent_length):
             cursor.deleteChar()
     else:
         cursor.insertText(" " * indent_length)
     if indent_length:
         cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
         line = unicode(cursor.block().text())
         if len(line) and line[0] == " ":
             cursor.movePosition(QTextCursor.NextWord, QTextCursor.MoveAnchor)
         self.editview.setTextCursor(cursor)
     cursor.endEditBlock()
Beispiel #4
0
 def indent_line(self, line_no, indent_length):
     block = self._get_block(line_no)
     cursor = QTextCursor(block)
     cursor.joinPreviousEditBlock()
     cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
     if indent_length <  0:
         for i in range(-indent_length):
             cursor.deleteChar()
     else:
         cursor.insertText(" " * indent_length)
     if indent_length:
         cursor.movePosition(
             QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
         line = unicode(cursor.block().text())
         if len(line) and line[0] == " ":
             cursor.movePosition(
                 QTextCursor.NextWord, QTextCursor.MoveAnchor)
         self.editview.setTextCursor(cursor)
     cursor.endEditBlock()
Beispiel #5
0
 def insertMarkers(self):
     # Copy the text and if it is empty, complain and exit.
     qi = QString(self.insertText.text())
     if qi.isEmpty():
         pqMsgs.warningMsg("No insert text specified")
         return
     # See how many pages are involved: all the ones that aren't marked skip
     n = 0
     for i in range(IMC.pageTable.size()):
         if IMC.pageTable.getAction(i) != IMC.FolioRuleSkip:
             n += 1
     if n == 0:  # page table empty or all rows marked skip
         pqMsgs.warningMsg("No pages to give folios to")
         return
     m = "Insert this string at the top of {0} pages?".format(n)
     b = pqMsgs.okCancelMsg(QString(m), pqMsgs.trunc(qi, 35))
     if b:
         # Convert any '\n' in the text to the QT line delimiter char
         # we do this in the copy so the lineEdit text doesn't change
         qi.replace(QString(u'\\n'), QString(IMC.QtLineDelim))
         # get a cursor on the edit document
         tc = QTextCursor(IMC.editWidget.textCursor())
         tc.beginEditBlock()  # start single undoable operation
         # Working from the end of the document backward, go to the
         # top of each page and insert the string
         for i in reversed(range(IMC.pageTable.size())):
             if IMC.pageTable.getAction(i) != IMC.FolioRuleSkip:
                 # Note the page's start position and set our work cursor to it
                 pos = IMC.pageTable.getCursor(i).position()
                 tc.setPosition(pos)
                 # Make a copy of the insert string replacing %f with this folio
                 f = IMC.pageTable.getDisplay(i)
                 qf = QString(qi)
                 qf.replace(QString(u'%f'), f, Qt.CaseInsensitive)
                 tc.insertText(qf)
                 # The insertion goes in ahead of the saved cursor position so now
                 # it points after the inserted string. Put it back where it was.
                 IMC.pageTable.setPosition(i, pos)
         tc.endEditBlock()  # wrap up the undo op
Beispiel #6
0
 def insertMarkers(self):
     # Copy the text and if it is empty, complain and exit.
     qi = QString(self.insertText.text())
     if qi.isEmpty() :
         pqMsgs.warningMsg("No insert text specified")
         return
     # See how many pages are involved: all the ones that aren't marked skip
     n = 0
     for i in range(IMC.pageTable.size()):
         if IMC.pageTable.getAction(i) != IMC.FolioRuleSkip :
             n += 1
     if n == 0 : # page table empty or all rows marked skip
         pqMsgs.warningMsg("No pages to give folios to")
         return
     m = "Insert this string at the top of {0} pages?".format(n)
     b = pqMsgs.okCancelMsg(QString(m),pqMsgs.trunc(qi,35))
     if b :
         # Convert any '\n' in the text to the QT line delimiter char
         # we do this in the copy so the lineEdit text doesn't change
         qi.replace(QString(u'\\n'),QString(IMC.QtLineDelim))
         # get a cursor on the edit document
         tc = QTextCursor(IMC.editWidget.textCursor())
         tc.beginEditBlock() # start single undoable operation
         # Working from the end of the document backward, go to the
         # top of each page and insert the string
         for i in reversed( range( IMC.pageTable.size() ) ) :
             if IMC.pageTable.getAction(i) != IMC.FolioRuleSkip :
                 # Note the page's start position and set our work cursor to it
                 pos = IMC.pageTable.getCursor(i).position()
                 tc.setPosition(pos)
                 # Make a copy of the insert string replacing %f with this folio
                 f = IMC.pageTable.getDisplay(i)
                 qf = QString(qi)
                 qf.replace(QString(u'%f'),f,Qt.CaseInsensitive)
                 tc.insertText(qf)
                 # The insertion goes in ahead of the saved cursor position so now
                 # it points after the inserted string. Put it back where it was.
                 IMC.pageTable.setPosition(i, pos)
         tc.endEditBlock() # wrap up the undo op
Beispiel #7
0
	def __replaceWithinDocument(self, document, occurrences, replacementPattern):
		"""
		Replaces given pattern occurrences in given document using given settings.

		:param document: Document.
		:type document: QTextDocument
		:param replacementPattern: Replacement pattern.
		:type replacementPattern: unicode
		:return: Replaced occurrences count.
		:rtype: int
		"""

		cursor = QTextCursor(document)
		cursor.beginEditBlock()
		offset = count = 0
		for occurence in sorted(occurrences, key=lambda x: x.position):
			cursor.setPosition(offset + occurence.position, QTextCursor.MoveAnchor)
			cursor.setPosition(offset + occurence.position + occurence.length, QTextCursor.KeepAnchor)
			cursor.insertText(replacementPattern)
			offset += len(replacementPattern) - occurence.length
			count += 1
		cursor.endEditBlock()
		return count