Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console improvements #53

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Plugins/System/Terminal/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Terminal::Terminal(Fact *parent)
qmlRegisterUncreatableType<Terminal>("APX.Terminal", 1, 0, "Terminal", "Reference only");

loadQml("qrc:/" PLUGIN_NAME "/TerminalPlugin.qml");

connect(clipboard, &QClipboard::dataChanged, this, &Terminal::clipboardContentChangedSignal);
}

void Terminal::exec(QString cmd)
Expand Down Expand Up @@ -240,6 +242,21 @@ QString Terminal::autocomplete(QString cmd)
return c;
}

bool Terminal::isClipboardEmpty() const
{
return clipboard->text().isEmpty();
}

void Terminal::copyConsoleHistoryToClipboard() const
{
AppNotify::instance()->copyTextToClipboardSignal();
}

void Terminal::copyTextToClipboard(const QString &text) const
{
clipboard->setText(text);
}

QMap<QString, QJSValue> Terminal::_get_js_properties(QString scope, QString flt)
{
QMap<QString, QJSValue> map;
Expand Down
10 changes: 10 additions & 0 deletions src/Plugins/System/Terminal/Terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#pragma once

#include <Fact/Fact.h>
#include <QClipboard>
#include <QGuiApplication>
#include <QQmlEngine>
#include <QtCore>

Expand All @@ -40,11 +42,18 @@ class Terminal : public Fact

Q_INVOKABLE QString autocomplete(QString cmd);

Q_INVOKABLE bool isClipboardEmpty() const;

Q_INVOKABLE void copyConsoleHistoryToClipboard() const;

Q_INVOKABLE void copyTextToClipboard(const QString &text) const;

private:
int _enterIndex;
QStringList _history;
int _historyIndex;
QString _replacedHistory;
QClipboard *clipboard{QGuiApplication::clipboard()};

QMap<QString, QJSValue> _get_js_properties(QString scope, QString flt);

Expand All @@ -54,4 +63,5 @@ public slots:

signals:
void newMessage(QtMsgType type, QString category, QString text);
void clipboardContentChangedSignal();
};
144 changes: 141 additions & 3 deletions src/Plugins/System/Terminal/qml/Terminal.qml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,63 @@ Rectangle {
Layout.alignment: Qt.AlignBottom
Layout.preferredWidth: 300
Layout.preferredHeight: 400
currentIndex: -1

property list<Item> selectedItems

spacing: lineSpace

Menu {
id: contextMenu
MenuItem {
text: "Cut"
enabled: listView.footerItem.isSomeSelection()
onTriggered: {
listView.footerItem.cut()
}
}
MenuItem {
text: "Copy"
enabled: listView.footerItem.isSomeSelection() || (listView.currentIndex !== -1)
onTriggered: {
if (listView.currentIndex !== -1)
listView.copySelectedLines()
else
listView.footerItem.copy()
}
}
MenuItem {
text: "Paste"
enabled: !listView.footerItem.isClipboardEmpty
onTriggered: {
listView.footerItem.paste()
listView.footerItem.setFocus()
}
}
MenuItem {
text: "Copy all"
onTriggered: {
listView.footerItem.copyAll()
}
}
}

MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
onClicked: contextMenu.popup()
}

MouseArea {
anchors.fill: parent
acceptedButtons: Qt.MiddleButton
onWheel: {
if (wheel.angleDelta.y > 0)
listView.focus=false
wheel.accepted=false
}
}

model: application.notifyModel
delegate: TerminalLine {
width: listView.width
Expand All @@ -75,13 +129,97 @@ Rectangle {
easing.type: Easing.OutCubic
}
}


onCountChanged: scrollTimer.start()
Timer {
id: scrollTimer
interval: 1
onTriggered: listView.scrollToEnd()
}

Keys.onPressed: {
if (event.key === Qt.Key_C && (event.modifiers & (Qt.ControlModifier | Qt.MetaModifier))) {
copySelectedLines()
}
if ((event.text.length == 1 && event.modifiers == Qt.NoModifier) || event.key == Qt.Key_Backspace) {
event.accepted=true
footerItem.setFocus();
if (event.key != Qt.Key_Backspace)
footerItem.appendCmd(event.text)
else
footerItem.doBackSpace()
}
}
Keys.onTabPressed: {
event.accepted=true
footerItem.hints()
footerItem.setFocus()
}
Keys.onEnterPressed: {
event.accepted = true
footerItem.exec()
}
Keys.onReturnPressed: {
event.accepted = true
footerItem.exec()
}
Keys.onUpPressed: {
footerItem.setFocus()
footerItem.upPressed(event)
}
Keys.onDownPressed: {
footerItem.setFocus()
footerItem.downPressed(event)
}

footer: TerminalExec {
width: parent.width
onFocused: listView.scrollToEnd()
onFocused: {
listView.scrollToEnd()
listView.currentIndex = -1

for (var i = 0; i < listView.selectedItems.length; i++) {
listView.selectedItems[i].selected=false
}
listView.selectedItems.length=0
}
}

function copySelectedLines()
{
var selectedText = ""
for (var i = 0; i < selectedItems.length; i++) {
selectedText += selectedItems[i].text + '\n'
}
footerItem.copyText(selectedText)
}
}
}

Item {
anchors {
top: parent.top
right: parent.right
rightMargin: 35
topMargin: 15
}

onClicked: listView.footerItem.focusRequested()
Rectangle {
width: 25
height: 20
color: "#80808080"

Text {
anchors.centerIn: parent
text: "☰"
color: "white"
font.pixelSize: 12
}

MouseArea {
anchors.fill: parent
onClicked: contextMenu.popup()
}
}
}
}
62 changes: 41 additions & 21 deletions src/Plugins/System/Terminal/qml/TerminalExec.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.7
import QtQuick 2.15
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.2

Expand All @@ -34,16 +34,17 @@ Rectangle{
readonly property string user: apx.vehicles.current.title
readonly property string pdel: "> "
readonly property string prefix: user+pdel
property bool isClipboardEmpty: terminal.isClipboardEmpty()

property var terminal: apx.tools.terminal

signal focused()

signal focusRequested() //fwd to text field

signal upPressed(var event)
signal downPressed(var event)
onUpPressed: (event) => cmdText.Keys.upPressed(event)
onDownPressed: (event) => cmdText.Keys.downPressed(event)

onPrefixChanged: setCmd(getCmd())
onFocusRequested: cmdText.forceActiveFocus()

TextInput {
id: cmdText
Expand All @@ -56,6 +57,7 @@ Rectangle{
readonly property int pos0: text.indexOf(pdel)+pdel.length
readonly property int pos: cursorPosition-pos0
text: prefix //+"</font><font color='#fff'>"
persistentSelection: true

onActiveFocusChanged: if(activeFocus)focused()

Expand All @@ -79,12 +81,12 @@ Rectangle{
onTriggered: if(cmdText.selectionStart<cmdText.pos0)cmdText.select(cmdText.pos0,cmdText.selectionEnd)
}
Keys.onPressed: {
//console.log("key: "+event.key+" mod: "+event.modifiers+" text: "+event.text)
// console.log("key: "+event.key+" mod: "+event.modifiers+" text: "+event.text)
consoleExec.focused()
forceActiveFocus()
if(pos<=0 && event.key===Qt.Key_Backspace){
event.accepted=true
}else if(event.key===Qt.Key_C && (event.modifiers&(Qt.ControlModifier|Qt.MetaModifier))){
}else if(event.key===Qt.Key_C && (event.modifiers&(Qt.ControlModifier|Qt.MetaModifier)) && selectedText == ""){
event.accepted=true
text+="^C"
reset()
Expand All @@ -95,13 +97,6 @@ Rectangle{
reset()
}*/
}
Keys.onTabPressed: {
//console.log("tabE")
event.accepted=true
hints()
}
Keys.onEnterPressed: enter(event)
Keys.onReturnPressed: enter(event)
Keys.onUpPressed: {
event.accepted=true
//var cpos=cursorPosition
Expand All @@ -114,11 +109,6 @@ Rectangle{
setCmd(terminal.historyPrev(getCmd()),true)
//cursorPosition=cpos
}
function enter(event)
{
event.accepted=true
exec()
}
}
Timer {
id: focusTimer
Expand All @@ -143,15 +133,15 @@ Rectangle{
}
function exec()
{
setFocus()
var cmd=getCmd()
reset()
if(cmd.length<=0)return
terminal.exec(cmd)
setFocus()
}
function reset()
{
consoleExec.focused()
focused()
terminal.enter(cmdText.text)
setCmd("")
terminal.historyReset()
Expand All @@ -164,6 +154,36 @@ Rectangle{
var c=terminal.autocomplete(cmd)
if(c===cmd)return
setCmd(c,true)
}

function appendCmd(cmd)
{
cmdText.text+=cmd
cmdText.cursorPosition=cmdText.text.length
cmdText.forceActiveFocus()
}

function doBackSpace()
{
if (cmdText.text.length != prefix.length)
cmdText.text = cmdText.text.slice(0, cmdText.text.length-1)
}

Connections {
target: terminal
function onClipboardContentChangedSignal() {
isClipboardEmpty = terminal.isClipboardEmpty()
}
}

function isSomeSelection()
{
return cmdText.selectedText != ""
}

function cut() {cmdText.cut()}
function paste() {cmdText.paste()}
function copy() {cmdText.copy()}
function copyAll() {terminal.copyConsoleHistoryToClipboard()}
function copyText(text) {terminal.copyTextToClipboard(text)}
}
Loading