-
Notifications
You must be signed in to change notification settings - Fork 8
/
InstanceEditor.qml
99 lines (90 loc) · 3.36 KB
/
InstanceEditor.qml
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
import QtQuick 2.12
import QtQuick.Controls 2.5
// Component to edit/fixup FHIR instances as easily as possible
ScrollView {
property string instanceText
property string fontName
property string instancePlaceholder
/** Scroll the text to the specified line number
* @param lineNumber the line number, with counting started at 1
*/
function openError(lineNumber, linePosition) {
// We can't calculate the y position directly because a logical and
// displayed line might differ in height due to line wrapping. QML's
// positionToRectangle() function can tell us what we need, but for
// that we have to calculate the position in the text string of the
// line.
var position = 0
const lines = textArea.text.split("\n")
for (var currentLine = 0; currentLine < lineNumber - 1; currentLine++) {
position += lines[currentLine].length + 1 // include newline char
}
contentItem.contentY = textArea.positionToRectangle(position).y
textArea.forceActiveFocus()
textArea.cursorPosition = position + linePosition - 1 // don't include last newline char
}
clip: true
onImplicitHeightChanged: textArea.update()
TextArea {
id: textArea
text: instanceText
onTextChanged: appmodel.resourceText = text
font.family: fontName
font.preferShaping: false
selectByMouse: true
wrapMode: "WrapAtWordBoundaryOrAnywhere"
renderType: Text.NativeRendering
placeholderText: instancePlaceholder
background: Rectangle {
anchors.fill: parent
color: "#F7F9FA"
radius: 3
border.width: 1
border.color: "#E3E7FD"
}
// credit: https://stackoverflow.com/a/49875950/72944
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
hoverEnabled: true
propagateComposedEvents: true
cursorShape: containsMouse ? Qt.IBeamCursor : Qt.ArrowCursor
onClicked: openContextMenu(mouse)
onPressAndHold: if (mouse.source === Qt.MouseEventNotSynthesized) {
openContextMenu(mouse)
}
function openContextMenu(mouse) {
// keep selection when context menu is opened
const selectStart = textArea.selectionStart
const selectEnd = textArea.selectionEnd
const curPos = textArea.cursorPosition
contextMenu.x = mouse.x
contextMenu.y = mouse.y
contextMenu.open()
textArea.cursorPosition = curPos
textArea.select(selectStart, selectEnd)
}
Menu {
id: contextMenu
MenuItem {
text: qsTr("Cut")
onTriggered: {
textArea.cut()
}
}
MenuItem {
text: qsTr("Copy")
onTriggered: {
textArea.copy()
}
}
MenuItem {
text: qsTr("Paste")
onTriggered: {
textArea.paste()
}
}
}
}
}
}