-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQTextEditCS.py
More file actions
executable file
·155 lines (116 loc) · 5.46 KB
/
QTextEditCS.py
File metadata and controls
executable file
·155 lines (116 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
# Python 2.7
import sys, os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
from bibcolorsyntax import ColorSyntax # pour coloration syntaxique
############
from PyQt4.Qt import QFrame, QWidget, QTextEdit, QHBoxLayout, QPainter
#############################################################################
class QTextEditCS(QtGui.QWidget):
class NumberBar(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.edit = None
# This is used to update the width of the control.
# It is the highest line that is currently visibile.
self.highest_line = 0
def setTextEdit(self, edit):
self.edit = edit
def update(self, *args):
'''
Updates the number bar to display the current set of numbers.
Also, adjusts the width of the number bar if necessary.
'''
# The + 4 is used to compensate for the current line being bold.
width = self.fontMetrics().width(str(self.highest_line)) + 4
if self.width() != width:
self.setFixedWidth(width)
QWidget.update(self, *args)
def paintEvent(self, event):
contents_y = self.edit.verticalScrollBar().value()
page_bottom = contents_y + self.edit.viewport().height()
font_metrics = self.fontMetrics()
current_block = self.edit.document().findBlock(self.edit.textCursor().position())
painter = QPainter(self)
line_count = 0
# Iterate over all text blocks in the document.
block = self.edit.document().begin()
while block.isValid():
line_count += 1
# The top left position of the block in the document
position = self.edit.document().documentLayout().blockBoundingRect(block).topLeft()
# Check if the position of the block is out side of the visible
# area.
if position.y() > page_bottom:
break
# We want the line number for the selected line to be bold.
bold = False
if block == current_block:
bold = True
font = painter.font()
font.setBold(True)
painter.setFont(font)
# Draw the line number right justified at the y position of the
# line. 3 is a magic padding number. drawText(x, y, text).
painter.drawText(self.width() - font_metrics.width(str(line_count)) - 3, round(position.y()) - contents_y + font_metrics.ascent()+3, str(line_count))
# Remove the bold style if it was set previously.
if bold:
font = painter.font()
font.setBold(False)
painter.setFont(font)
block = block.next()
self.highest_line = line_count
painter.end()
QWidget.paintEvent(self, event)
def __init__(self, script="", parent=None):
super(QTextEditCS, self).__init__(parent)
self.resize(800, 600)
self.script = script
self.edit = QtGui.QTextEdit(self)
# met une police de caractère même largeur pour tous les caractères
font = QtGui.QFont()
font.setFamily(u"DejaVu Sans Mono") # police de Qt4
font.setStyleHint(QtGui.QFont.Courier) # si la police est indisponible
font.setPointSize(10)
self.edit.setFont(font)
# met en place la coloration syntaxique
self.colorSyntax = ColorSyntax(self.edit.document())
# affiche le script colorisé
self.setText(self.script)
# positionne le QTextEdit dans la fenêtre
layout = QHBoxLayout(self)
layout.setMargin(0)
layout.setSpacing(0)
self.number_bar = self.NumberBar()
self.number_bar.setTextEdit(self.edit)
layout.addWidget(self.number_bar)
layout.addWidget(self.edit)
self.setLayout(layout)
self.edit.installEventFilter(self)
self.edit.viewport().installEventFilter(self)
def eventFilter(self, object, event):
# Update the line numbers for all events on the text edit and the viewport.
# This is easier than connecting all necessary singals.
if object in (self.edit, self.edit.viewport()):
self.number_bar.update()
return False
return QFrame.eventFilter(object, event)
#========================================================================
def setText(self, chaineunicode):
"""Ecrire dans le widget edit (QTextEdit)"""
self.edit.append(chaineunicode)
# fait bouger le curseur à la fin du texte
self.edit.moveCursor(QtGui.QTextCursor.End, QtGui.QTextCursor.MoveAnchor)
# force le rafraichissement pour affichage en temps réel
QtCore.QCoreApplication.processEvents()
#############################################################################
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
scriptsql = u""""""
fen = QTextEditCS(scriptsql)
fen.show()
sys.exit(app.exec_())