-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConsole.qml
206 lines (189 loc) · 6.46 KB
/
Console.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import QtQuick 2.11
import QtQuick.Controls 2.2
ConsoleForm {
Shortcut {
sequence: "Alt+C"
onActivated: config.connectPort()
}
Shortcut {
sequence: "Ctrl+L"
onActivated: clearText()
}
Shortcut {
sequence: "Ctrl+E,Ctrl+W"
onActivated: edit.wrapMode = TextEdit.Wrap
}
clearButton.onClicked:{
clearText()
}
connectButton.icon.source: serial.openned ? "connect.png" : "disconnect.png"
connectButton.onPressed: {
config.connectPort()
}
ScrollView {
id: scrollViewConsole
anchors.topMargin: 10
anchors.right: parent.right
anchors.rightMargin: 10
anchors.left: parent.left
anchors.leftMargin: 10
anchors.bottom: flowConsole.top
anchors.bottomMargin: 10
anchors.top: parent.top
TextArea {
id: textAreaConsole
cursorVisible: true
selectByMouse: true
text: qsTr("")
wrapMode: Text.WordWrap
property int myCursorPosition: 0
Component.onCompleted: {
cursorPosition = 0
}
// TODO: solo scrollear cuando no se está al final del texto
onMyCursorPositionChanged: {
cursorPosition = myCursorPosition
}
Keys.onPressed: {
if(consmode){
switch (event.key) {
case Qt.Key_Up:
serial.writeString("[A")
break
case Qt.Key_Down:
serial.writeString("[B")
break
case Qt.Key_Left:
serial.writeString("[D")
break
case Qt.Key_Right:
serial.writeString("[C")
break
case Qt.Key_Tab:
serial.writeString("\t")
break
default:
if (textAreaConsole.activeFocus) {
if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return)
textAreaConsole.myCursorPosition = textAreaConsole.text.length
if(serial.isOpen){
serial.writeString(event.text)
}
if(localEcho){
var msg = [Number(event.text.charCodeAt(0))]
sendText(msg)
}
}
}
}
event.accepted = true
}
}
}
buttonEnviar.onClicked: {
var msg = textFieldConsole.text
if(!switchHex.checked){
if(serial.isOpen)
serial.writeString(msg+comboBoxAppend.model.get(comboBoxAppend.currentIndex).value)
}
else {
msg = msg.trim().split(" ")
var arr = []
for(var i = 0; i < msg.length; i++){
var n = parseInt(msg[i],16)
if (!isNaN(n)){
arr.push(n)
}
else{
arr = []
break
}
}
if(arr.length > 0){
msg = arr.map(
function(x){
return String.fromCharCode(x)
}
).join("")
if(serial.isOpen)
serial.writeString(msg+comboBoxAppend.model.get(comboBoxAppend.currentIndex).value)
}
else {
toast.show(qsTr("Error en caracteres Hexadecimales."),2000,'red')
}
}
//if(hex)
msg = msg.split("").map(function(x) {return Number(x.charCodeAt(0))})
if(echo)
sendText(msg)
}
function sendBytes(msg){
textAreaConsole.insert(textAreaConsole.text.length,msg+" ")
}
function sendText(msg){
if(!hex){
var msg2 = serial.bytes2String(msg)
var aux = 0
while (msg.length > 0){
switch(msg[0]){
case 0x08:
moveCursor(-1)
msg.shift()
break
case 0x1b:
msg.shift()
msg.shift()
var n = Number(String.fromCharCode(msg[0]))
while(!isNaN(n)){
aux = aux*10+n
msg.shift()
n = Number(String.fromCharCode(msg[0]))
}
switch(msg[0]){
case 0x4b:
removeAllFromCursor()
break
case 0x44:
moveCursor(-aux)
break
}
msg.shift()
break
default:
remove1Char()
print2Console(String.fromCharCode(msg[0]))
msg.shift()
}
}
}
else {
msg = msg.map(function (x) {return "0x"+("00"+x.toString(16)).slice(-2)}).toString().replace(/,/g , " ");
textAreaConsole.insert(textAreaConsole.text.length,msg+" ")
textAreaConsole.myCursorPosition += msg.length + 1
}
}
function remove1Char(){
textAreaConsole.remove(textAreaConsole.myCursorPosition,textAreaConsole.myCursorPosition+1)
}
function removeAllFromCursor(){
textAreaConsole.remove(textAreaConsole.myCursorPosition,textAreaConsole.text.length)
}
function moveCursor(n){
textAreaConsole.myCursorPosition = textAreaConsole.myCursorPosition + n
}
function print2Console(msg){
textAreaConsole.insert(textAreaConsole.myCursorPosition,msg)
textAreaConsole.myCursorPosition += msg.length
}
function clearText(){
if (consmode && !hex){
var currentText = textAreaConsole.text.split("\n")
textAreaConsole.clear()
textAreaConsole.text = currentText[currentText.length-1]
textAreaConsole.myCursorPosition = textAreaConsole.text.length
}else{
textAreaConsole.clear()
textAreaConsole.myCursorPosition = textAreaConsole.text.length
}
}
}