-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.cpp
150 lines (116 loc) · 3.47 KB
/
terminal.cpp
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
#include "terminal.h"
#include "ui_terminal.h"
#include <QMessageBox>
#include <QTimer>
#include <QProcess>
#include <QKeyEvent>
#include <QTextBlock>
#define CMDTXT "user@Terminal# "
#define T_PrivPtr( o ) (( StationaryLampSet *) o )
Terminal::Terminal(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Terminal)
{
ui->setupUi(this);
//注册监视对象
ui->textEdit->installEventFilter(this);
//初始化QProcess
cmd=new QProcess(this);
connect(cmd , SIGNAL(readyReadStandardOutput()) , this , SLOT(on_readoutput()));
connect(cmd , SIGNAL(readyReadStandardError()) , this , SLOT(on_readerror()));
//注册启动事件
QTimer::singleShot(0, this, SLOT(init()));
}
Terminal::~Terminal()
{
//销毁外部程序
if(cmd)
{
cmd->close();
cmd->waitForFinished();
}
delete ui;
}
void Terminal::init()
{
cmd->start("bash"); //启动终端(Windows下改为cmd)
cmd->waitForStarted(); //等待启动完成
ui->textEdit->append(CMDTXT);
}
void Terminal::write(){
QString word=Terminal::textCursor();
QRegExp rxlen("user@Terminal#(.+)$");
if(rxlen.indexIn(word)!=-1 && Terminal::textEnd(15)!=CMDTXT)
{
QString ch = rxlen.cap(1).trimmed()+"\n";
char* shell; QByteArray ba = ch.toLatin1(); shell=ba.data();
cmd->write(shell);
//Terminal::checkshell();
}else{
QTextCursor cursor = ui->textEdit->textCursor();
cursor.movePosition(QTextCursor::End);
ui->textEdit->setTextCursor(cursor);
}
}
void Terminal::checkshell(){
QString word= ui->textEdit->toPlainText().trimmed();
QString w=word.mid(word.length()-1,word.length());
if(w!="#"){
ui->textEdit->append(CMDTXT);
}
}
//监视对象
bool Terminal::eventFilter(QObject *target, QEvent *event)
{
/*处理按键消息 */
if(event->type() == QEvent::KeyPress)
{
if (target == ui->textEdit){
/*强制类型转换 */
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
//处理回车消息
if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == 16777220){
Terminal::write();
return true;
//处理删除消息
}else if(keyEvent->key() == Qt::Key_Backspace ){
QString word=Terminal::textCursor();
//不允许删除前面的内容
if(Terminal::textEnd(15)==CMDTXT){return true;}
if(word.contains(CMDTXT)){
return (Terminal::textCursor()==CMDTXT);
}else{
return true;
}
//不允许修改前面的内容
}else if(Terminal::textEnd(15)==CMDTXT && Terminal::textCursor()!=CMDTXT){
return true;
}
}
}
return QWidget::eventFilter(target, event);
}
//取光标所在行内容
QString Terminal::textCursor()
{
QTextCursor tc;
tc = ui->textEdit->textCursor();
return tc.block().text();
}
//从后截取文本
QString Terminal::textEnd(int num)
{
QString word=ui->textEdit->toPlainText();
word=word.mid(word.size()-num,word.size());
return word;
}
void Terminal::on_readoutput()
{
ui->textEdit->append(cmd->readAllStandardOutput().data()); //将输出信息读取到编辑框
Terminal::checkshell();
}
void Terminal::on_readerror()
{
ui->textEdit->append(cmd->readAllStandardError().data());
Terminal::checkshell();
}