def drawContents(self, painter): """ Reimplementation of drawContents to limit the drawing inside `textRext`. """ painter.setPen(self.__color) painter.setFont(self.font()) if self.__textRect: rect = self.__textRect else: rect = self.rect().adjusted(5, 5, -5, -5) if Qt.mightBeRichText(self.__message): doc = QTextDocument() doc.setHtml(self.__message) doc.setTextWidth(rect.width()) cursor = QTextCursor(doc) cursor.select(QTextCursor.Document) fmt = QTextBlockFormat() fmt.setAlignment(self.__alignment) cursor.mergeBlockFormat(fmt) painter.save() painter.translate(rect.topLeft()) doc.drawContents(painter) painter.restore() else: painter.drawText(rect, self.__alignment, self.__message)
def alinear(self,item): formato=QTextBlockFormat() formato.setAlignment(Qt.AlignRight) cursor = item.textCursor() cursor.select(QTextCursor.Document) cursor.mergeBlockFormat(formato) cursor.clearSelection() item.setTextCursor(cursor)
def make_plain_text(self): cursor = self.textCursor() char_format = QTextCharFormat() char_format.setFont(self.font) cursor.setCharFormat(char_format) block_format = QTextBlockFormat() block_format.setNonBreakableLines(False) cursor.setBlockFormat(block_format)
def _generate_formats(self): """generate background colors for restedit warnings""" if self._formats: return format = self.document().findBlock(0).blockFormat() self._formats[0] = format hues = (120, 60, 30, 0) for lvl, hue in enumerate(hues): format = QTextBlockFormat(format) color = QColor() color.setHsv(hue, 95, 255) format.setBackground(color) self._formats[lvl+1] = format.toBlockFormat()
def setTextfield (self): tx = 8 self._text_item = GText (self.hookName, self) self._text_item.setDefaultTextColor (Qt.black) self._text_item.setEnabled (False) if self.hookType=='out' : tx=-50 tmp0 = QTextBlockFormat () tmp0.setAlignment (Qt.AlignRight) tmp = QTextCursor () tmp.setBlockFormat (tmp0) self._text_item.setTextCursor (tmp) self._text_item.setPos (QPointF (tx, -5)) self._text_item.setFont (QFont ("Geneva", 8, QFont.AllLowercase, False)) self._text_item.setTextWidth (65)
def __init__(self, outFileName): super(QObject, self).__init__() # 输出文件的文件名 self.outFileName = self.tr(outFileName) # QTextDocument文件对象 self.doc = QtGui.QTextDocument() # 文件光标 self.cursor = QtGui.QTextCursor(self.doc) # -------初始化样式设置 # 列表样式 self.listFormat = QTextListFormat() self.listFormat.setStyle(QTextListFormat.ListUpperAlpha) # 块样式 self.blockFormat = QTextBlockFormat()
def printDocument2(self): dialog = QPrintDialog() if not dialog.exec_(): return self.printer = dialog.printer() headFormat = QTextBlockFormat() headFormat.setAlignment(Qt.AlignLeft) headFormat.setTextIndent( self.printer.pageRect().width()-216) bodyFormat = QTextBlockFormat() bodyFormat.setAlignment(Qt.AlignJustify) lastParaBodyFormat = QTextBlockFormat(bodyFormat) lastParaBodyFormat.setPageBreakPolicy(QTextFormat.PageBreak_AlwaysAfter) rightBodyFormat = QTextBlockFormat() rightBodyFormat.setAlignment(Qt.AlignRight) headCharFormat = QTextCharFormat() headCharFormat.setFont(QFont("Helvetica",10)) bodyCharFormat = QTextCharFormat() bodyCharFormat.setFont(QFont("Times",11)) redBodyCharFormat = QTextCharFormat(bodyCharFormat) redBodyCharFormat.setForeground(Qt.red) tableFormat = QTextTableFormat() tableFormat.setBorder(1) tableFormat.setCellPadding(2) document = QTextDocument() cursor = QTextCursor(document) mainFrame = cursor.currentFrame() page = 1 cursor.insertBlock(headFormat, headCharFormat) for text in ("Greasy Hands Ltd.", "New Lombard Street","London" , "WC13", QDate.currentDate().toString(self.DATE_FORMAT)): cursor.insertBlock(headFormat,headCharFormat) cursor.insertText(text) cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText("Barrio Francisco Meza") cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText("Dear Lyuis") cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText(QString("The balance of your account is $ %L1.").arg(float(500.987),0,"f",2)) cursor.insertBlock(bodyFormat,redBodyCharFormat) cursor.insertText("Please remit the amount") cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText("Transaction") transactions = [ (QDate.currentDate(),500), (QDate.currentDate(),500), (QDate.currentDate(),-500), (QDate.currentDate(),500) ] table = cursor.insertTable(len(transactions), 3, tableFormat) row = 0 for date, amount in transactions: cellCursor = table.cellAt(row,0).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) cellCursor.insertText(date.toString(self.DATE_FORMAT),bodyCharFormat) cellCursor = table.cellAt(row,1).firstCursorPosition() cellCursor.insertText("Credit",bodyCharFormat) cellCursor = table.cellAt(row,2).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) cellCursor.insertText(QString("The balance of your account is $ %L1.").arg(float(amount),0,"f",2),redBodyCharFormat) row += 1 cursor.setPosition(mainFrame.lastPosition()) cursor.insertBlock(bodyFormat,bodyCharFormat) cursor.insertText("We hope") document.print_(self.printer)
def highlight_line(textedit, line_number, color=None): # format.clearBackGround(Qt.yellow) cursor = QTextCursor(textedit.document().findBlockByNumber(line_number)) fmt = QTextBlockFormat() if color is None: fmt.clearBackground() return if color == 'blue': fmt.setBackground(Qt.blue) elif color == 'red': fmt.setBackground(Qt.red) elif color == 'green': fmt.setBackground(Qt.green) if color == 'yellow': fmt.setBackground(Qt.yellow) elif color == 'magenta': fmt.setBackground(Qt.magenta) elif color == 'cyan': fmt.setBackground(Qt.cyan) cursor.setBlockFormat(fmt)
def setFormat(self): format = QTextBlockFormat() format.setBackground(QtCore.Qt.yellow) return format
class OpenOffice(QObject): def __init__(self, outFileName): super(QObject, self).__init__() # 输出文件的文件名 self.outFileName = self.tr(outFileName) # QTextDocument文件对象 self.doc = QtGui.QTextDocument() # 文件光标 self.cursor = QtGui.QTextCursor(self.doc) # -------初始化样式设置 # 列表样式 self.listFormat = QTextListFormat() self.listFormat.setStyle(QTextListFormat.ListUpperAlpha) # 块样式 self.blockFormat = QTextBlockFormat() def done(self): writer = QtGui.QTextDocumentWriter() #writer.setCodec(QTextCodec.codecForName("utf-8")) #print writer.codec().name() print writer.supportedDocumentFormats() #[PyQt4.QtCore.QByteArray(b'HTML'), PyQt4.QtCore.QByteArray(b'ODF'), PyQt4.QtCore.QByteArray(b'plaintext')] odf_format = writer.supportedDocumentFormats()[1] writer.setFormat(odf_format) writer.setFileName(self.outFileName) writer.write(self.doc) # Return True if successful def setListFormat(self, style): # # 可用的风格有: # QTextListFormat.ListDisc -1 a filled circle # QTextListFormat.ListCircle -2 an empty circle # QTextListFormat.ListSquare -3 a filled square # QTextListFormat.ListDecimal -4 decimal values in ascending order # QTextListFormat.ListLowerAlpha -5 lower case Latin characters in alphabetical order # QTextListFormat.ListUpperAlpha -6 upper case Latin characters in alphabetical order # QTextListFormat.ListLowerRoman -7 lower case roman numerals (supports up to 4999 items only) # QTextListFormat.ListUpperRoman -8 upper case roman numerals (supports up to 4999 items only) # self.listFormat.setStyle(style) def setBlockBgColor(self, color): self.blockFormat.setBackground(color) self.cursor.insertBlock(self.blockFormat) def reset(self): self.cursor.insertBlock(self.blockFormat) def setFont(self, font): print "set font..." def insertText(self, text): self.cursor.insertText(self.tr(text)) def insertList(self): self.cursor.insertList(self.listFormat) def insertTable(self, row, column): return self.cursor.insertTable(row, column) # table def tableMergeCells(self, table, x, y, row, column): return table.mergeCells(x, y, row, column) def tableMoveToCell(self, table, x, y): self.cursor = table.cellAt(x, y).firstCursorPosition() def insertImage(self, imagePath, x = 200, y = 200, pos = QTextFrameFormat.FloatLeft): # # QTextFrameFormat.InFlow 0 # QTextFrameFormat.FloatLeft 1 # QTextFrameFormat.FloatRight 2 # img = QImage() ok = img.load(imagePath) self.doc.addResource(QTextDocument.ImageResource, QUrl("myimage"), img) imageFormat = QTextImageFormat() imageFormat.setName("myimage") imageFormat.setWidth(x) imageFormat.setHeight(y) self.cursor.insertImage(imageFormat, pos) def test(self): #QTextCharFormat char_fmt; char_fmt = QTextCharFormat() char_fmt.setBackground(QColor(150, 150, 250)); self.cursor.insertText(self.tr("Ì1\n"),char_fmt); #QImage img; img = QImage() ok = img.load("./123.png") self.doc.addResource(QTextDocument.ImageResource, QUrl("myimage"), img) imageFormat = QTextImageFormat() imageFormat.setName("myimage") imageFormat.setWidth(10) imageFormat.setHeight(10) self.cursor.insertImage(imageFormat) #self.cursor.insertImage("myimage"); self.cursor.insertText(self.tr("Æåñòêèé äèñê\n"),char_fmt);
def printDocument2(self): dialog = QPrintDialog() if not dialog.exec_(): return self.printer = dialog.printer() headFormat = QTextBlockFormat() headFormat.setAlignment(Qt.AlignLeft) headFormat.setTextIndent(self.printer.pageRect().width() - 216) bodyFormat = QTextBlockFormat() bodyFormat.setAlignment(Qt.AlignJustify) lastParaBodyFormat = QTextBlockFormat(bodyFormat) lastParaBodyFormat.setPageBreakPolicy( QTextFormat.PageBreak_AlwaysAfter) rightBodyFormat = QTextBlockFormat() rightBodyFormat.setAlignment(Qt.AlignRight) headCharFormat = QTextCharFormat() headCharFormat.setFont(QFont("Helvetica", 10)) bodyCharFormat = QTextCharFormat() bodyCharFormat.setFont(QFont("Times", 11)) redBodyCharFormat = QTextCharFormat(bodyCharFormat) redBodyCharFormat.setForeground(Qt.red) tableFormat = QTextTableFormat() tableFormat.setBorder(1) tableFormat.setCellPadding(2) document = QTextDocument() cursor = QTextCursor(document) mainFrame = cursor.currentFrame() page = 1 cursor.insertBlock(headFormat, headCharFormat) for text in ("Greasy Hands Ltd.", "New Lombard Street", "London", "WC13", QDate.currentDate().toString(self.DATE_FORMAT)): cursor.insertBlock(headFormat, headCharFormat) cursor.insertText(text) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("Barrio Francisco Meza") cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("Dear Lyuis") cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText( QString("The balance of your account is $ %L1.").arg( float(500.987), 0, "f", 2)) cursor.insertBlock(bodyFormat, redBodyCharFormat) cursor.insertText("Please remit the amount") cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("Transaction") transactions = [(QDate.currentDate(), 500), (QDate.currentDate(), 500), (QDate.currentDate(), -500), (QDate.currentDate(), 500)] table = cursor.insertTable(len(transactions), 3, tableFormat) row = 0 for date, amount in transactions: cellCursor = table.cellAt(row, 0).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) cellCursor.insertText(date.toString(self.DATE_FORMAT), bodyCharFormat) cellCursor = table.cellAt(row, 1).firstCursorPosition() cellCursor.insertText("Credit", bodyCharFormat) cellCursor = table.cellAt(row, 2).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) cellCursor.insertText( QString("The balance of your account is $ %L1.").arg( float(amount), 0, "f", 2), redBodyCharFormat) row += 1 cursor.setPosition(mainFrame.lastPosition()) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("We hope") document.print_(self.printer)