本文整理汇总了Python中PyQt4.QtGui.QTextDocument类的典型用法代码示例。如果您正苦于以下问题:Python QTextDocument类的具体用法?Python QTextDocument怎么用?Python QTextDocument使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QTextDocument类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: displayAnnotation
def displayAnnotation(self, geom, message):
''' Display a specific message in the centroid of a specific geometry
'''
centroid = geom.centroid()
# clean previous annotations:
for annotation in self.annotations:
try:
scene = annotation.scene()
if scene:
scene.removeItem(annotation)
except:
# annotation can be erased by QGIS interface
pass
self.annotations = []
# build annotation
textDoc = QTextDocument(message)
item = QgsTextAnnotationItem(self.iface.mapCanvas())
item.setMapPosition(centroid.asPoint())
item.setFrameSize(textDoc.size())
item.setDocument(textDoc)
item.update()
# add to annotations
self.annotations.append(item)
# center in the centroid
self.iface.mapCanvas().setCenter(centroid.asPoint())
self.iface.mapCanvas().zoomScale(float(self.defaultZoomScale))
self.iface.mapCanvas().refresh()
示例2: paint
def paint(self, painter, option, index):
optionV4 = QStyleOptionViewItemV4(option)
self.initStyleOption(optionV4, index)
style = optionV4.widget.style() if optionV4.widget else QApplication.style()
doc = QTextDocument()
doc.setHtml(optionV4.text)
# painting item without text
optionV4.text = QString()
style.drawControl(QStyle.CE_ItemViewItem, optionV4, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
# Hilight text if item is selected
if optionV4.state & QStyle.State_Selected:
ctx.palette.setColor(QPalette.Text, optionV4.palette.color(QPalette.Active, QPalette.HighlightedText))
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, optionV4)
painter.save()
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
doc.documentLayout().draw(painter, ctx)
painter.restore()
示例3: paint
def paint(self, painter, option, index):
"""Render the delegate for the item."""
if index.column() != self._html_column:
return QStyledItemDelegate.paint(self, painter, option, index)
options = QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
if options.widget is None:
style = QApplication.style()
else:
style = options.widget.style()
doc = QTextDocument()
doc.setHtml(options.text)
options.text = ""
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
painter.save()
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
doc.documentLayout().draw(painter, ctx)
painter.restore()
示例4: _copy
def _copy(self, index):
doc = QTextDocument()
doc.setHtml(index.data(Qt.DisplayRole).toString())
clipboard = QApplication.clipboard()
richTextData = QMimeData()
richTextData.setHtml(index.data(Qt.DisplayRole).toString())
richTextData.setText(doc.toPlainText())
clipboard.setMimeData(richTextData)
示例5: htmlCopy
def htmlCopy(document, type='editor'):
"""Returns a new QTextDocument with highlighting set as HTML textcharformats."""
data = textformats.formatData(type)
doc = QTextDocument()
doc.setDefaultFont(data.font)
doc.setPlainText(document.toPlainText())
highlight(doc, HighlightFormats(data), ly.lex.state(documentinfo.mode(document)))
return doc
示例6: writePdf
def writePdf(dir_):
newbody = originHTML.format(**htmlpara)
printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(dir_+'.pdf')
printer.setPageSize(QPrinter.A4)
text = QTextDocument()
text.setHtml(newbody.decode('utf-8'))
text.print_(printer)
示例7: __render_status_message
def __render_status_message(self, width, index):
message = unicode(index.data(self.MessageRole).toPyObject())
urls = index.data(self.URLsEntitiesRole).toPyObject()
for url in urls:
pretty_url = "<a href='%s'>%s</a>" % (url.url, url.display_text)
message = message.replace(url.search_for, pretty_url)
doc = QTextDocument()
doc.setHtml(message)
doc.setTextWidth(self.__calculate_text_width(width))
return doc
示例8: __getDocument
def __getDocument(self, content):
"""
Returns a `QTextDocument <http://doc.qt.nokia.com/qtextdocument.html>`_ class instance
with given content.
:return: Document.
:rtype: QTextDocument
"""
document = QTextDocument(QString(content))
document.clearUndoRedoStacks()
document.setModified(False)
return document
示例9: __render_username
def __render_username(self, width, index):
username_string = index.data(self.UsernameRole).toPyObject()
username = QTextDocument()
if username_string != '':
username.setHtml("<span style='color: #666;'>@%s</span>" % username_string)
else:
username.setHtml("<span style='color: #666;'></span>" % username_string)
username.setDefaultFont(USERNAME_FONT)
username.setTextWidth(self.__calculate_text_width(width))
return username
示例10: writePdfabs
def writePdfabs(dir_):
updates = PDF_PARAMETER
htmlpara.update(updates)
newbody = originHTML.format(**htmlpara)
# with open('t.html', 'wb') as f:
# f.write(newbody)
# f.close()
printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(dir_)
printer.setPageSize(QPrinter.A4)
text = QTextDocument()
text.setHtml(newbody.decode('utf-8'))
text.print_(printer)
示例11: documentForScript
def documentForScript(self, script=0):
if type(script) != Script:
script = self.libraryList[script]
if script not in self._cachedDocuments:
doc = QTextDocument(self)
doc.setDocumentLayout(QPlainTextDocumentLayout(doc))
doc.setPlainText(script.script)
doc.setDefaultFont(QFont(self.defaultFont))
doc.highlighter = PythonSyntaxHighlighter(doc)
doc.modificationChanged[bool].connect(self.onModificationChanged)
doc.setModified(False)
self._cachedDocuments[script] = doc
return self._cachedDocuments[script]
示例12: init_print
def init_print(self, linenos=True, style="default"):
app = QApplication(sys.argv) # noqa
doc = QTextDocument()
doc.setHtml(
self.highlight_file(linenos=linenos, style=style)
)
printer = QPrinter()
printer.setOutputFileName(self.pdf_file)
printer.setOutputFormat(QPrinter.PdfFormat)
page_size_dict = {"a2": QPrinter.A2, "a3": QPrinter.A3, "a4": QPrinter.A4}
printer.setPageSize(page_size_dict.get(self.size.lower(), QPrinter.A4))
printer.setPageMargins(15, 15, 15, 15, QPrinter.Millimeter)
doc.print_(printer)
logging.info("PDF created at %s" % (self.pdf_file))
示例13: show
def show(cursor, pos=None, num_lines=6):
"""Displays a tooltip showing part of the cursor's Document.
If the cursor has a selection, those blocks are displayed.
Otherwise, num_lines lines are displayed.
If pos is not given, the global mouse position is used.
"""
block = cursor.document().findBlock(cursor.selectionStart())
c2 = QTextCursor(block)
if cursor.hasSelection():
c2.setPosition(cursor.selectionEnd(), QTextCursor.KeepAnchor)
c2.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
else:
c2.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor, num_lines)
data = textformats.formatData('editor')
doc = QTextDocument()
font = QFont(data.font)
font.setPointSizeF(font.pointSizeF() * .8)
doc.setDefaultFont(font)
doc.setPlainText(c2.selection().toPlainText())
if metainfo.info(cursor.document()).highlighting:
highlighter.highlight(doc, state=tokeniter.state(block))
size = doc.size().toSize() + QSize(8, -4)
pix = QPixmap(size)
pix.fill(data.baseColors['background'])
doc.drawContents(QPainter(pix))
label = QLabel()
label.setPixmap(pix)
label.setStyleSheet("QLabel { border: 1px solid #777; }")
label.resize(size)
widgets.customtooltip.show(label, pos)
示例14: printDocument1
def printDocument1(self):
html = u""
date = QDate.currentDate().toString(self.DATE_FORMAT)
address = Qt.escape("Bario francisco mesa").replace(",","<br>")
contact = Qt.escape("Luis Mejia")
balance = 5000
html += ("<p align=right><img src=':/logo.png'></p>"
"<p> align = right>Greasy hands ltd."
"<br>New Lombard Street"
"<br>London<br>WC13 4PX<br>%s</p>"
"<p>%s</p><p>Dear %s, </p>"
"<p>The balance of your account is %s.")% (
date, address, contact, QString("$ %L1").arg(float(balance),0,"f",2))
if balance <0 :
html += ("<p><font color =red><b> Please remit the amount owing immediately.</b></font>")
else:
html += "We are delighted to have done business with you."
html += ("</p><p> </p><p>"
"<table border=1 cellpadding=2 cellspacing=2><tr><td colspan=3>Transaction</td></tr>")
transactions = [
(QDate.currentDate(),500),
(QDate.currentDate(),500),
(QDate.currentDate(),-500),
(QDate.currentDate(),500)
]
for date, amount in transactions:
color, status = "black", "Credit"
if amount <0:
color, status = "red", "Debid"
html += ("<tr>"
"<td align= right>%s</td>"
"<td>%s</td><td align=right><font color=%s>%s</font></td></tr>" % (
date.toString(self.DATE_FORMAT), status,color, QString("$ %L1").arg(float(abs(amount)), 0, "f",2)))
html += ("</table></p><p style='page-break-after=always;'>"
"We hope to continue doing business with you</p>")
pdialog = QPrintDialog()
if pdialog.exec_() == QDialog.Accepted:
printer = pdialog.printer()
document = QTextDocument()
document.setHtml(html)
document.print_(printer)
示例15: paint
def paint(self, painter, option, index):
"""QStyledItemDelegate.paint implementation
"""
option.state &= ~QStyle.State_HasFocus # never draw focus rect
options = QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
style = QApplication.style() if options.widget is None else options.widget.style()
doc = QTextDocument()
doc.setDocumentMargin(1)
doc.setHtml(options.text)
# bad long (multiline) strings processing doc.setTextWidth(options.rect.width())
options.text = ""
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
# Highlighting text if item is selected
if option.state & QStyle.State_Selected:
ctx.palette.setColor(QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText))
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
painter.save()
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
doc.documentLayout().draw(painter, ctx)
painter.restore()