FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
CodingRobots/numberedtextedit.py at master · CodingRobots/CodingRobots · GitHub
CodingRobots
CodingRobots
Repository navigation
Code
Issues
1
(1)
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
CodingRobots
/
numberedtextedit.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
126 lines (97 loc) · 4.61 KB
Breadcrumbs
CodingRobots
/
numberedtextedit.py
Copy path
File metadata and controls
126 lines (97 loc) · 4.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# Copyright (c) 2009 John Schember
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE
from
PyQt4
.
Qt
import
QFrame
,
QWidget
,
QTextEdit
,
QHBoxLayout
,
QPainter
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
(),
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
)
class
LineTextWidget
(
QFrame
):
def
__init__
(
self
,
*
args
):
QFrame
.
__init__
(
self
,
*
args
)
self
.
setFrameStyle
(
QFrame
.
StyledPanel
|
QFrame
.
Sunken
)
self
.
edit
=
QTextEdit
()
self
.
edit
.
setFrameStyle
(
QFrame
.
NoFrame
)
self
.
edit
.
setAcceptRichText
(
False
)
self
.
number_bar
=
NumberBar
()
self
.
number_bar
.
setTextEdit
(
self
.
edit
)
hbox
=
QHBoxLayout
(
self
)
hbox
.
setSpacing
(
0
)
hbox
.
setMargin
(
0
)
hbox
.
addWidget
(
self
.
number_bar
)
hbox
.
addWidget
(
self
.
edit
)
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
getTextEdit
(
self
):
return
self
.
edit
Back
|
FazBrowse Home
|
New Git URL