test

[size=medium]
digwtx

[b]国家崛起[/b]
最新战报显示,大国的崛起都会经历一个漫长的过程。
[url]http://www.google.com[/url]

[/size]


#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from highlighter import MyHighlighter
from highlighter import HighlightingRule

class CodeEditor(QPlainTextEdit):
'''代码编辑器主类'''

def __init__(self, parent = None):
QPlainTextEdit.__init__(self, parent)
self.lineNumberArea = LineNumberArea(self)

self.connect( \
self, \
SIGNAL('blockCountChanged(int)'), \
self.updateLineNumberAreaWidth \
)
self.connect( \
self, \
SIGNAL('updateRequest(const QRect &, int)'), \
self.updateLineNumberArea \
)
self.connect( \
self, \
SIGNAL('cursorPositionChanged()'), \
self.highlightCurrentLine \
)

self.updateLineNumberAreaWidth(0)
self.highlightCurrentLine()

def highlightCurrentLine(self):
'''高亮显示当前行'''
extraSelections = QTextEdit.extraSelections(QTextEdit())
if not self.isReadOnly():
selection = QTextEdit.ExtraSelection()
lineColor = QColor(Qt.yellow).lighter(160)
selection.format.setBackground(lineColor)
selection.format.setProperty( \
QTextFormat.FullWidthSelection, \
True \
)
selection.cursor = self.textCursor()
selection.cursor.clearSelection()
extraSelections.append(selection)
self.setExtraSelections(extraSelections)

def lineNumberAreaWidth(self):
'''计算行号显示区域的宽度'''
digits = 1
Max = max(1, self.blockCount())
while (Max >= 10) :
Max /= 10
digits += 1
space = 3 + self.fontMetrics().width('9') * digits
return space

def updateLineNumberAreaWidth(self, newBlockCount):
'''更新行号显示区域宽度'''
self.setViewportMargins( \
self.lineNumberAreaWidth(), \
0, \
0, \
0 \
)

def updateLineNumberArea(self, rect, dy):
'''更新行号显示区域内容'''
if dy:
self.lineNumberArea.scroll(0, dy)
else:
self.lineNumberArea.update( \
0, \
rect.y(), \
self.lineNumberAreaWidth(), \
rect.height() \
)
if (rect.contains(self.viewport().rect())):
self.updateLineNumberAreaWidth(0)

def resizeEvent(self, e):
'''控件大小改变事件'''
QPlainTextEdit.resizeEvent(self, e)
cr = self.contentsRect()
self.lineNumberArea.setGeometry( \
QRect(cr.left(), \
cr.top(), \
self.lineNumberAreaWidth(), \
cr.height()) \
)

def lineNumberAreaPaintEvent(self, event):
'''行号改变时触发的事件'''
painter = QPainter(self.lineNumberArea)
rect = event.rect()
painter.fillRect(rect, Qt.lightGray)
block = self.firstVisibleBlock()
blockNumber = block.blockNumber()
top = int(self.blockBoundingGeometry(block).translated( \
self.contentOffset()).top() \
)
bottom = top + int(self.blockBoundingRect(block).height())
while (block.isValid() and top <= rect.bottom()):
if (block.isVisible() and bottom >= rect.top()):
number = QString.number(blockNumber + 1)
painter.setPen(Qt.black)
painter.drawText( \
0, \
top, \
self.lineNumberArea.width(), \
self.fontMetrics().height(), \
Qt.AlignRight, \
number \
)
block = block.next()
top = bottom
bottom = top + int(self.blockBoundingRect(block).height())
blockNumber += 1

class LineNumberArea(QWidget):
'''显示行号的类'''

def __init__(self, editor):
QWidget.__init__(self, editor)
self.codeEditor = editor

def sizeHint(self):
return QSize(codeEditor.lineNumberAreaWidth(), 0)

def paintEvent(self, event):
self.codeEditor.lineNumberAreaPaintEvent(event)

def main():
app = QApplication(sys.argv)
editor = CodeEditor()
editor.setWindowTitle('Code Editor Example')
editor.show()
app.exec_()

if __name__ == '__main__':
main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值