blob: 80f7c04c57242ac6a49ba3fc17753b46154170fa (
plain)
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
156
157
158
159
160
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import FileSystemModule
pragma ComponentBehavior: Bound
// This is the text editor that displays the currently open file, including
// their corresponding line numbers.
Rectangle {
id: root
required property string currentFilePath
required property bool showLineNumbers
property alias text: textArea
property int currentLineNumber: -1
property int rowHeight: Math.ceil(fontMetrics.lineSpacing)
color: Colors.background
onWidthChanged: textArea.update()
onHeightChanged: textArea.update()
RowLayout {
anchors.fill: parent
// We use a flickable to synchronize the position of the editor and
// the line numbers. This is necessary because the line numbers can
// extend the available height.
Flickable {
id: lineNumbers
// Calculate the width based on the logarithmic scale.
Layout.preferredWidth: fontMetrics.averageCharacterWidth
* (Math.floor(Math.log10(textArea.lineCount)) + 1) + 10
Layout.fillHeight: true
interactive: false
contentY: editorFlickable.contentY
visible: textArea.text !== "" && root.showLineNumbers
Column {
anchors.fill: parent
Repeater {
id: repeatedLineNumbers
model: LineNumberModel {
lineCount: textArea.text !== "" ? textArea.lineCount : 0
}
delegate: Item {
required property int index
width: parent.width
height: root.rowHeight
Label {
id: numbers
text: parent.index + 1
width: parent.width
height: parent.height
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
color: (root.currentLineNumber === parent.index)
? Colors.iconIndicator : Qt.darker(Colors.text, 2)
font: textArea.font
}
Rectangle {
id: indicator
anchors.left: numbers.right
width: 1
height: parent.height
color: Qt.darker(Colors.text, 3)
}
}
}
}
}
Flickable {
id: editorFlickable
property alias textArea: textArea
// We use an inline component to customize the horizontal and vertical
// scroll-bars. This is convenient when the component is only used in one file.
component MyScrollBar: ScrollBar {
id: scrollBar
background: Rectangle {
implicitWidth: scrollBar.interactive ? 8 : 4
implicitHeight: scrollBar.interactive ? 8 : 4
opacity: scrollBar.active && scrollBar.size < 1.0 ? 1.0 : 0.0
color: Colors.background
Behavior on opacity {
OpacityAnimator {
duration: 500
}
}
}
contentItem: Rectangle {
implicitWidth: scrollBar.interactive ? 8 : 4
implicitHeight: scrollBar.interactive ? 8 : 4
opacity: scrollBar.active && scrollBar.size < 1.0 ? 1.0 : 0.0
color: Colors.color1
Behavior on opacity {
OpacityAnimator {
duration: 1000
}
}
}
}
Layout.fillHeight: true
Layout.fillWidth: true
ScrollBar.horizontal: MyScrollBar {}
ScrollBar.vertical: MyScrollBar {}
boundsBehavior: Flickable.StopAtBounds
TextArea.flickable: TextArea {
id: textArea
anchors.fill: parent
focus: false
topPadding: 0
leftPadding: 10
text: FileSystemModel.readFile(root.currentFilePath)
tabStopDistance: fontMetrics.averageCharacterWidth * 4
// Grab the current line number from the C++ interface.
onCursorPositionChanged: {
root.currentLineNumber = FileSystemModel.currentLineNumber(
textArea.textDocument, textArea.cursorPosition)
}
color: Colors.textFile
selectedTextColor: Colors.textFile
selectionColor: Colors.selection
textFormat: TextEdit.PlainText
renderType: Text.QtRendering
selectByMouse: true
antialiasing: true
background: null
}
FontMetrics {
id: fontMetrics
font: textArea.font
}
}
}
}
|