|
| 1 | +import Qb.Core 1.0 |
| 2 | +import QtQuick 2.10 |
| 3 | + |
| 4 | +Item{ |
| 5 | + property color textColor: "white" |
| 6 | + property color bgColor: "black" |
| 7 | + property color verticalScrollBarColor: "white" |
| 8 | + property color cursorColor: "white" |
| 9 | + property int cursorWidth: 5 |
| 10 | + property int leftMargin: 10 |
| 11 | + property int rightMargin: 10 |
| 12 | + property int topMargin: 10 |
| 13 | + property int bottomMargin: 10 |
| 14 | + property int bottomExtraSpace: 100 |
| 15 | + property string fontFamily: "vrinda" |
| 16 | + property int fontSize: 15 |
| 17 | + property int verticalScrollBarHeight: 18 |
| 18 | + |
| 19 | + property bool terminalEnabled: true |
| 20 | + |
| 21 | + signal command(string cmd); |
| 22 | + |
| 23 | + id: objQbTerminal |
| 24 | + width: 500 |
| 25 | + height: 500 |
| 26 | + clip: true |
| 27 | + enabled: true |
| 28 | + Keys.forwardTo: [objTerminalInput] |
| 29 | + |
| 30 | + onActiveFocusChanged: { |
| 31 | + console.log("Focus changed"); |
| 32 | + if(activeFocus){ |
| 33 | + properFocus(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + function setCursorToEnd(){ |
| 38 | + objTerminalContents.cursorPosition = objTerminalContents.length; |
| 39 | + objTerminalContents.cursorVisible = true; |
| 40 | + } |
| 41 | + |
| 42 | + function passwordMode(){ |
| 43 | + objTerminalInput.isPasswordMode = true; |
| 44 | + } |
| 45 | + |
| 46 | + function textMode(){ |
| 47 | + objTerminalInput.isPasswordMode = false; |
| 48 | + } |
| 49 | + |
| 50 | + function properFocus(){ |
| 51 | + if(!objTerminalInput.isPasswordMode){ |
| 52 | + objTerminalInputText.cursorVisible = true; |
| 53 | + objTerminalInputText.focus = true; |
| 54 | + objTerminalInputText.forceActiveFocus(); |
| 55 | + } |
| 56 | + else{ |
| 57 | + objTerminalInputPassword.cursorVisible = true; |
| 58 | + objTerminalInputPassword.focus = true; |
| 59 | + objTerminalInputPassword.forceActiveFocus(); |
| 60 | + } |
| 61 | + |
| 62 | + if(objQbTerminalFlickArea.contentHeight>objQbTerminalFlickArea.height){ |
| 63 | + objQbTerminalFlickArea.contentY = objQbTerminalFlickArea.contentHeight - objQbTerminalFlickArea.height; |
| 64 | + } |
| 65 | + |
| 66 | + objTerminalInput.y = objQbTerminalFlickArea.actualContentHeight - objTerminalInput.height; |
| 67 | + //console.log("Content Height:"+objQbTerminalFlickArea.contentHeight); |
| 68 | + //console.log("Terminal:"+objTerminalContents.height); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + function enableTerminal(){ |
| 74 | + objQbTerminal.terminalEnabled = true; |
| 75 | + } |
| 76 | + |
| 77 | + function disableTerminal(){ |
| 78 | + objQbTerminal.terminalEnabled = false; |
| 79 | + } |
| 80 | + |
| 81 | + function insertText(text){ |
| 82 | + objTerminalContents.insert(objTerminalContents.length,text); |
| 83 | + objTerminalContents.cursorPosition = objTerminalContents.length; |
| 84 | + properFocus(); |
| 85 | + } |
| 86 | + |
| 87 | + function clearTerminal(){ |
| 88 | + objTerminalContents.text = ""; |
| 89 | + objTerminalInputText.text = ""; |
| 90 | + objTerminalInputText.cursorVisible = false; |
| 91 | + objTerminalContents.cursorVisible = false; |
| 92 | + objTerminalInput.y = objQbTerminalFlickArea.actualContentHeight - objTerminalInput.height; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + Rectangle{ |
| 97 | + anchors.top: parent.top |
| 98 | + anchors.bottom: parent.bottom |
| 99 | + anchors.right: parent.right |
| 100 | + anchors.left: parent.left |
| 101 | + |
| 102 | + anchors.leftMargin: objQbTerminal.leftMargin |
| 103 | + anchors.rightMargin: objQbTerminal.rightMargin |
| 104 | + anchors.topMargin: objQbTerminal.topMargin |
| 105 | + anchors.bottomMargin: objQbTerminal.bottomMargin |
| 106 | + color: objQbTerminal.bgColor |
| 107 | + |
| 108 | + MouseArea{ |
| 109 | + preventStealing: true |
| 110 | + anchors.fill: parent |
| 111 | + onPressed: { |
| 112 | + if(objQbTerminal.terminalEnabled){ |
| 113 | + properFocus(); |
| 114 | + Qt.inputMethod.show(); |
| 115 | + } |
| 116 | + mouse.accepted = true; |
| 117 | + } |
| 118 | + onPositionChanged: { |
| 119 | + mouse.accepted = true; |
| 120 | + } |
| 121 | + onPressAndHold: { |
| 122 | + mouse.accepted = true; |
| 123 | + } |
| 124 | + onClicked: mouse.accepted = true; |
| 125 | + onReleased: mouse.accepted = true; |
| 126 | + onDoubleClicked: mouse.accepted = true; |
| 127 | + } |
| 128 | + |
| 129 | + Item { |
| 130 | + id: objVerticalScrollBar |
| 131 | + implicitWidth: objQbTerminal.verticalScrollBarHeight |
| 132 | + anchors.top: parent.top |
| 133 | + anchors.bottom: parent.bottom |
| 134 | + anchors.right: parent.right |
| 135 | + |
| 136 | + Connections { |
| 137 | + target: objQbTerminalFlickArea.visibleArea |
| 138 | + onHeightRatioChanged:{ |
| 139 | + objVerticalScrollBar.visible = (objQbTerminalFlickArea.visibleArea.heightRatio < 1); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + Rectangle { |
| 144 | + id: objHandler |
| 145 | + y: objQbTerminalFlickArea.visibleArea.yPosition * objQbTerminalFlickArea.height |
| 146 | + anchors.left: parent.left |
| 147 | + anchors.right: parent.right |
| 148 | + height: objQbTerminalFlickArea.visibleArea.heightRatio * objQbTerminalFlickArea.height |
| 149 | + color: objQbTerminal.verticalScrollBarColor |
| 150 | + } |
| 151 | + |
| 152 | + MouseArea { |
| 153 | + anchors.fill: parent |
| 154 | + preventStealing: true |
| 155 | + |
| 156 | + onMouseYChanged: |
| 157 | + if (mouseY - objHandler.height / 2 <= 0) { |
| 158 | + objQbTerminalFlickArea.contentY = 0; |
| 159 | + } |
| 160 | + else if ((mouseY - objHandler.height / 2) * objQbTerminalFlickArea.contentHeight / objVerticalScrollBar.height >= |
| 161 | + objQbTerminalFlickArea.contentHeight - objVerticalScrollBar.height) { |
| 162 | + objQbTerminalFlickArea.contentY = objQbTerminalFlickArea.contentHeight - objVerticalScrollBar.height; |
| 163 | + } |
| 164 | + else{ |
| 165 | + objQbTerminalFlickArea.contentY = (mouseY - objHandler.height / 2) * objQbTerminalFlickArea.contentHeight / objVerticalScrollBar.height |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + Flickable { |
| 171 | + property int actualContentHeight:1 |
| 172 | + id: objQbTerminalFlickArea |
| 173 | + clip: true |
| 174 | + interactive: false |
| 175 | + |
| 176 | + anchors.top: parent.top |
| 177 | + anchors.bottom: parent.bottom |
| 178 | + anchors.right: (objVerticalScrollBar.visible) ? objVerticalScrollBar.left : parent.right |
| 179 | + anchors.left: parent.left |
| 180 | + onActualContentHeightChanged: { |
| 181 | + objQbTerminalFlickArea.contentHeight = objQbTerminalFlickArea.actualContentHeight+objTerminalInput.height+objQbTerminal.bottomExtraSpace; |
| 182 | + } |
| 183 | + |
| 184 | + Item{ |
| 185 | + property bool isPasswordMode: false |
| 186 | + |
| 187 | + id: objTerminalInput |
| 188 | + anchors.left: parent.left |
| 189 | + anchors.leftMargin: objTerminalInputX.contentWidth-objTerminalInputX.cursorRectangle.width |
| 190 | + height: isPasswordMode?objTerminalInputPassword.height:objTerminalInputText.height |
| 191 | + anchors.right: parent.right |
| 192 | + |
| 193 | + TextInput{ |
| 194 | + clip: true |
| 195 | + id: objTerminalInputPassword |
| 196 | + focus: true |
| 197 | + enabled: objQbTerminal.terminalEnabled |
| 198 | + anchors.right: parent.right |
| 199 | + anchors.left: parent.left |
| 200 | + anchors.top: parent.top |
| 201 | + color: textColor |
| 202 | + text: "" |
| 203 | + wrapMode: TextEdit.Wrap |
| 204 | + echoMode: TextInput.Password |
| 205 | + font.family: objQbTerminal.fontFamily |
| 206 | + font.pixelSize: objQbTerminal.fontSize |
| 207 | + activeFocusOnPress: false |
| 208 | + verticalAlignment: TextEdit.AlignTop |
| 209 | + height: objTerminalContents.cursorRectangle.height*objTerminalInputPassword.lineCount |
| 210 | + readOnly: !objQbTerminal.terminalEnabled |
| 211 | + visible: objQbTerminal.terminalEnabled && objTerminalInput.isPasswordMode |
| 212 | + cursorDelegate: Rectangle{ |
| 213 | + width: objQbTerminal.cursorWidth |
| 214 | + color: objQbTerminal.cursorColor |
| 215 | + } |
| 216 | + onContentHeightChanged: { |
| 217 | + objQbTerminalFlickArea.contentHeight = objQbTerminalFlickArea.actualContentHeight+objTerminalInput.height+objQbTerminal.bottomExtraSpace; |
| 218 | + if(objQbTerminalFlickArea.contentHeight>objQbTerminalFlickArea.height){ |
| 219 | + objQbTerminalFlickArea.contentY = objQbTerminalFlickArea.contentHeight - objQbTerminalFlickArea.height; |
| 220 | + } |
| 221 | + } |
| 222 | + onAccepted: { |
| 223 | + objQbTerminal.command(objTerminalInputPassword.text); |
| 224 | + objTerminalInputPassword.text = ""; |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + TextEdit{ |
| 229 | + visible: false |
| 230 | + id: objTerminalInputX |
| 231 | + text: ">>>|" |
| 232 | + wrapMode: TextEdit.Wrap |
| 233 | + inputMethodHints: Qt.ImhNoPredictiveText |
| 234 | + textFormat: TextEdit.PlainText |
| 235 | + font.family: objQbTerminal.fontFamily |
| 236 | + font.pixelSize: objQbTerminal.fontSize |
| 237 | + activeFocusOnPress: false |
| 238 | + verticalAlignment: TextEdit.AlignTop |
| 239 | + height: objTerminalContents.cursorRectangle.height |
| 240 | + readOnly: false |
| 241 | + } |
| 242 | + |
| 243 | + TextEdit{ |
| 244 | + clip: true |
| 245 | + property string oldText: "" |
| 246 | + id: objTerminalInputText |
| 247 | + focus: true |
| 248 | + enabled: objQbTerminal.terminalEnabled |
| 249 | + anchors.right: parent.right |
| 250 | + anchors.left: parent.left |
| 251 | + anchors.top: parent.top |
| 252 | + color: textColor |
| 253 | + text: "" |
| 254 | + wrapMode: TextEdit.Wrap |
| 255 | + inputMethodHints: Qt.ImhNoPredictiveText|Qt.ImhPreferLowercase |
| 256 | + textFormat: TextEdit.PlainText |
| 257 | + font.family: objQbTerminal.fontFamily |
| 258 | + font.pixelSize: objQbTerminal.fontSize |
| 259 | + activeFocusOnPress: false |
| 260 | + verticalAlignment: TextEdit.AlignTop |
| 261 | + height: objTerminalContents.cursorRectangle.height*objTerminalInputText.lineCount |
| 262 | + readOnly: !objQbTerminal.terminalEnabled |
| 263 | + visible: objQbTerminal.terminalEnabled && !objTerminalInput.isPasswordMode |
| 264 | + cursorDelegate: Rectangle{ |
| 265 | + width: objQbTerminal.cursorWidth |
| 266 | + color: objQbTerminal.cursorColor |
| 267 | + } |
| 268 | + onContentHeightChanged: { |
| 269 | + objQbTerminalFlickArea.contentHeight = objQbTerminalFlickArea.actualContentHeight+objTerminalInput.height+objQbTerminal.bottomExtraSpace; |
| 270 | + if(objQbTerminalFlickArea.contentHeight>objQbTerminalFlickArea.height){ |
| 271 | + objQbTerminalFlickArea.contentY = objQbTerminalFlickArea.contentHeight - objQbTerminalFlickArea.height; |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + onLengthChanged: { |
| 276 | + if(objTerminalInputText.text!==objTerminalInputText.oldText){ |
| 277 | + objTerminalInputText.oldText = objTerminalInputText.text; |
| 278 | + if(objTerminalInputText.oldText.length>0){ |
| 279 | + var nline = objTerminalInputText.oldText.substring(objTerminalInputText.oldText.length-1,objTerminalInputText.oldText.length); |
| 280 | + if(nline === "\n"){ |
| 281 | + var cmd = objTerminalInputText.oldText.substring(0,objTerminalInputText.oldText.length-1); |
| 282 | + objTerminalInputText.oldText = ""; |
| 283 | + objTerminalInputText.text = ""; |
| 284 | + //insertText(cmd+"\n>>> "); |
| 285 | + objQbTerminal.command(cmd); |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + MouseArea{ |
| 292 | + preventStealing: true |
| 293 | + anchors.fill: parent |
| 294 | + onPressed: { |
| 295 | + if(objQbTerminal.terminalEnabled){ |
| 296 | + properFocus(); |
| 297 | + Qt.inputMethod.show(); |
| 298 | + } |
| 299 | + mouse.accepted = true; |
| 300 | + } |
| 301 | + onPositionChanged: { |
| 302 | + mouse.accepted = true; |
| 303 | + } |
| 304 | + onPressAndHold: { |
| 305 | + mouse.accepted = true; |
| 306 | + } |
| 307 | + onClicked: mouse.accepted = true; |
| 308 | + onReleased: mouse.accepted = true; |
| 309 | + onDoubleClicked: mouse.accepted = true; |
| 310 | + } |
| 311 | + |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + TextEdit{ |
| 316 | + id: objTerminalContents |
| 317 | + anchors.left: parent.left |
| 318 | + anchors.right: parent.right |
| 319 | + color: objQbTerminal.textColor |
| 320 | + wrapMode: TextEdit.Wrap |
| 321 | + inputMethodHints: Qt.ImhNoPredictiveText |
| 322 | + textFormat: TextEdit.PlainText |
| 323 | + font.family: objQbTerminal.fontFamily |
| 324 | + font.pixelSize: objQbTerminal.fontSize |
| 325 | + activeFocusOnPress: false |
| 326 | + readOnly: true |
| 327 | + onContentHeightChanged:{ |
| 328 | + objQbTerminalFlickArea.actualContentHeight = contentHeight; |
| 329 | + } |
| 330 | + |
| 331 | + |
| 332 | + MouseArea{ |
| 333 | + preventStealing: true |
| 334 | + anchors.fill: parent |
| 335 | + onPressed: { |
| 336 | + if(objQbTerminal.terminalEnabled){ |
| 337 | + properFocus(); |
| 338 | + Qt.inputMethod.show(); |
| 339 | + } |
| 340 | + mouse.accepted = true; |
| 341 | + } |
| 342 | + onPositionChanged: { |
| 343 | + mouse.accepted = true; |
| 344 | + } |
| 345 | + onPressAndHold: { |
| 346 | + mouse.accepted = true; |
| 347 | + } |
| 348 | + onClicked: mouse.accepted = true; |
| 349 | + onReleased: mouse.accepted = true; |
| 350 | + onDoubleClicked: mouse.accepted = true; |
| 351 | + } |
| 352 | + } |
| 353 | + } |
| 354 | + } |
| 355 | +} |
0 commit comments