#include "tools_Index3.h" #include "ui_tools_Index3.h" QString STR2HTML(QByteArray &str) { QString msg; msg.append(str); msg.replace("&","&"); msg.replace(">",">"); msg.replace("<","<"); msg.replace("\"","""); msg.replace("\'","'"); msg.replace(" "," "); //msg.replace("\n","
"); msg.replace("\t","    "); return msg; } void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QString msgstr; switch (type) { case QtDebugMsg: msgstr = QString::asprintf(">> Debug: %s \r\n", qPrintable(msg)); break; case QtInfoMsg: msgstr = QString::asprintf(">> Info: %s \r\n", qPrintable(msg)); break; case QtWarningMsg: msgstr = QString::asprintf(">> Warning: %s \r\n", qPrintable(msg)); break; case QtCriticalMsg: msgstr = QString::asprintf(">> Critical: %s \r\n", qPrintable(msg)); break; case QtFatalMsg: msgstr = QString::asprintf(">> Fatal: %s \r\n", qPrintable(msg)); break; } } Tools_Index3::Tools_Index3(QWidget *parent) : ToolsWidget(parent), ui(new Ui::Tools_Index3) { ui->setupUi(this); /* //load qss QFile file(":/qss/Setting.qss"); file.open(QFile::ReadOnly); QTextStream filetext(&file); QString stylesheet = filetext.readAll(); this->setStyleSheet(stylesheet); file.close(); */ timer = new QTimer(); timer->setInterval(1000); connect(timer,&QTimer::timeout, this,&Tools_Index3::timerout); //log_te = ui->plainTextEdit; //qInstallMessageHandler(myMessageOutput); ui->comboBox_EndType->addItem("CRLF","\r\n"); ui->comboBox_EndType->addItem("LF","\n"); ui->comboBox_EndType->addItem("CR","\r"); ui->comboBox_EndType->setCurrentIndex(1); int w = 150; int h = 50; ui->pushButton_clear->setFixedSize(w,h); ui->pushButton_ClearWindow->setFixedSize(w,h); ui->pushButton_Save->setFixedSize(w,h); ui->pushButton_send->setFixedSize(w,h); ui->comboBox_EndType->setFixedSize(w,h); ui->lineEdit->setFixedHeight(h); } Tools_Index3::~Tools_Index3() { delete ui; } void Tools_Index3::resizeEvent(QResizeEvent *event) { //Q_UNUSED(event) update(); } void Tools_Index3::keyPressEvent(QKeyEvent *event) //键盘按下事件 { switch(event->key()) { case Qt::Key_Return: { QString msg; msg = ui->lineEdit->text(); Transmit(msg); }break; case Qt::Key_Up: { if(History.size() > 0) { if(HistoryCount > 0) { HistoryCount --; } QString msg; msg = History.at(HistoryCount); ui->lineEdit->setText(msg); } }break; case Qt::Key_Down: { if(History.size() > 0) { if(HistoryCount < History.size()) { HistoryCount ++; } if(HistoryCount >= History.size()) { QString msg = ""; ui->lineEdit->setText(msg); return; } QString msg; msg = History.at(HistoryCount); ui->lineEdit->setText(msg); } }break; } } void Tools_Index3::timerout() { if(msgdata.size() > 0) { ui->textBrowser->append(msgdata); msgdata.clear(); timer->stop(); } } void Tools_Index3::wheelEvent(QWheelEvent *event) //键盘按下事件 { /* if(ui->lineEdit->) { if(event->angleDelta().y() > 0) { if(History.size() > 0) { if(HistoryCount > 0) { HistoryCount -= qAbs(event->angleDelta().y()/120); } QString msg; msg = History.at(HistoryCount); ui->lineEdit->setText(msg); } } else if(event->angleDelta().y() < 0) { if(History.size() > 0) { if(HistoryCount < History.size()) { HistoryCount += qAbs(event->angleDelta().y()/120); } if(HistoryCount >= History.size()) { QString msg = ""; ui->lineEdit->setText(msg); return; } QString msg; msg = History.at(HistoryCount); ui->lineEdit->setText(msg); } } } */ } void Tools_Index3::Recieve(QString msg) { msgdata.append(msg); while (!msgdata.isEmpty()) { if(!msgdata.contains("\n")) { return; } bool newline = false; int idx = msgdata.indexOf('\n'); if (idx == -1) { // Read the whole incoming buffer idx = msgdata.size(); } else { newline = true; } QByteArray fragment = msgdata.mid(0, idx); msgdata.remove(0, idx + (newline ? 1 : 0)); QByteArray str; if((QDateTime::currentMSecsSinceEpoch() - last) > 5000) { str.clear(); QString time = QDateTime::currentDateTime().toString("hh:mm:ss.zzz"); str.append(""); str.append(time); str.append(""); str.append(" << "); ui->textBrowser->append(str); str.clear(); } last = QDateTime::currentMSecsSinceEpoch(); str.append(""); str.append(STR2HTML(fragment)); //str.append(fragment); str.append(""); /* str.append(fragment); */ bool flag = false; QScrollBar *bar = ui->textBrowser->verticalScrollBar(); if(bar) { if(!bar->isHidden()) { if(bar->value() >= (bar->maximum() - 10)) { flag = true; } else { flag = false; } } else { flag = false; } } ui->textBrowser->append(str); if(flag) { QTextCursor cursor = ui->textBrowser->textCursor(); cursor.movePosition(QTextCursor::End); ui->textBrowser->setTextCursor(cursor); } } } void Tools_Index3::Transmit(QString msg) { if(ui->lineEdit->text().isEmpty()) { return; } History.append(msg); HistoryCount = History.size(); msg.append(EndChar); //清除时间间隔,保证每次发送出去返回肯定带时间 last = 0; QString time = QDateTime::currentDateTime().toString("hh:mm:ss.zzz"); QString str; str.append(""); str.append(time); str.append(""); str.append(" >> "); //ui->textBrowser->append(str); //str.clear(); str.append(""); str.append(msg); str.append(""); ui->textBrowser->append(str); QTextCursor cursor = ui->textBrowser->textCursor(); cursor.movePosition(QTextCursor::End); ui->textBrowser->setTextCursor(cursor); ui->lineEdit->clear(); emit Terminal_Transmit(msg); /* QString s; s.append("nihhh\tok\t\t\tuu\tooo\r\n"); s.append("nihhh\tok\tuu\tooo\n"); s.append("nihhh\tok\tuu\tooo\n"); s.append("nihhh ok uu ooo\n"); s.append("nihhh\tok\tuu\tooo"); s.append("nihhh\tok\tuu\tooo\n"); Recieve(s); */ } void Tools_Index3::on_pushButton_clear_clicked() { ui->lineEdit->clear(); } void Tools_Index3::on_pushButton_send_clicked() { QString msg; msg = ui->lineEdit->text(); Transmit(msg); } void Tools_Index3::on_comboBox_EndType_currentIndexChanged(int index) { EndChar.clear(); EndChar.append(ui->comboBox_EndType->currentData().toString()); } void Tools_Index3::on_pushButton_ClearWindow_clicked() { ui->textBrowser->clear(); } void Tools_Index3::on_pushButton_Save_clicked() { if(ui->textBrowser->toPlainText().size() > 0) { QFileDialog *dlg = new QFileDialog(); QString fileName = dlg->getSaveFileName(this, tr("Selete Path of File..."), "./Tlog/", tr("terminal file (*.terminal)")); if(!fileName.isEmpty()) { QFile *log_file = nullptr; log_file = new QFile(fileName); if(log_file->open(QIODevice::WriteOnly)) { log_file->write(ui->textBrowser->toPlainText().toUtf8()); log_file->close(); } } } else { //emit sho } }