digwtx
国家崛起
最新战报显示,大国的崛起都会经历一个漫长的过程。
http://www.google.com
#!/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()