Subversion Repositories Code-Repo

Rev

Rev 372 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
362 Kevin 1
#include "IOWidget.h"
2
 
3
IOWidget::IOWidget(QWidget *parent) : QWidget(parent)
4
{
5
    // Serial data UI
6
    textData = new QTextEdit();
7
    textData->setCurrentFont(QFont("Consolas", 8));
372 Kevin 8
    textData->append("Waiting for serial connection... ");
362 Kevin 9
    textData->setMinimumSize(400, 100);
10
    textData->setReadOnly(true);
11
    textTransmit = new QLineEdit();
12
    textTransmit->setEnabled(false);
13
    textTransmit->setFont(QFont("Consolas", 8));
14
    btnTransmit = new QPushButton("&Send");
15
    btnTransmit->setEnabled(false);
16
    btnClear = new QPushButton("Clear");
17
 
18
    QHBoxLayout *transmitLayout = new QHBoxLayout();
19
    transmitLayout->addWidget(textTransmit);
20
    transmitLayout->addWidget(btnTransmit);
21
    transmitLayout->addWidget(btnClear);
22
    QVBoxLayout *dataLayout = new QVBoxLayout();
23
    dataLayout->addWidget(textData);
24
    dataLayout->addLayout(transmitLayout);
25
    setLayout(dataLayout);
26
 
27
    connect(btnTransmit, SIGNAL(clicked()), this, SLOT(PreprocessTransmit()));
28
    connect(textTransmit, SIGNAL(returnPressed()), btnTransmit, SIGNAL(clicked()));
29
    connect(btnClear, SIGNAL(clicked()), this, SLOT(ClearBtn()));
30
 
372 Kevin 31
//    EnableTransmit(false);
32
    textTransmit->setEnabled(false);
33
    btnTransmit->setEnabled(false);
362 Kevin 34
    lastTransmit = true;
35
}
36
 
37
IOWidget::~IOWidget()
38
{
39
 
40
}
41
 
42
void IOWidget::PreprocessTransmit(QByteArray data)
43
{
44
    QString str;
45
    if (data.isEmpty()) {
46
        str = textTransmit->text();
47
        textTransmit->setText("");
48
    } else {
49
        str = QString(data);
50
    }
51
 
372 Kevin 52
    QTime curTime = QTime::currentTime();
53
    QStringList cmds = str.split('\n', QString::SkipEmptyParts);
362 Kevin 54
    for (int i = 0; i < cmds.size(); i++) {
372 Kevin 55
        emit TransmitByteArray((cmds[i] + '\n').toUtf8());
56
        textData->setTextColor(Qt::darkBlue);
57
        textData->append(curTime.toString("[HH:mm:ss:zzz]: ") + cmds[i]);
362 Kevin 58
    }
372 Kevin 59
    textData->moveCursor(QTextCursor::End);
60
    textData->ensureCursorVisible();
362 Kevin 61
 
62
    // Save a flag indicating last activity was a transmit
63
    lastTransmit = true;
64
}
65
 
66
void IOWidget::ProcessReceivedByte(QByteArray data)
67
{
68
    textData->setTextColor(Qt::darkRed);
372 Kevin 69
    QTime curTime = QTime::currentTime();
70
    QString timeString = "\n" + curTime.toString("[HH:mm:ss:zzz]: ");
71
    if (data.endsWith('\n')) data.remove(data.size()-1, 1);
72
    data.replace('\n', timeString);
362 Kevin 73
    if (lastTransmit) {
372 Kevin 74
        textData->insertPlainText(timeString + QString(data));
362 Kevin 75
    } else {
76
        textData->insertPlainText(QString(data));
77
    }
78
    textData->moveCursor(QTextCursor::End);
372 Kevin 79
    textData->ensureCursorVisible();
362 Kevin 80
    lastTransmit = false;
81
}
82
 
83
void IOWidget::ClearBtn()
84
{
85
    textData->clear();
86
}
87
 
364 Kevin 88
void IOWidget::EnableTransmit(bool enable)
362 Kevin 89
{
372 Kevin 90
    textData->setTextColor(Qt::black);
364 Kevin 91
    if (enable) {
92
        textTransmit->setEnabled(true);
93
        btnTransmit->setEnabled(true);
372 Kevin 94
        textData->insertPlainText("connected!");
364 Kevin 95
    } else {
96
        textTransmit->setEnabled(false);
97
        btnTransmit->setEnabled(false);
372 Kevin 98
        textData->append("Serial disconnected.");
99
        textData->append("Waiting for serial connection... ");
364 Kevin 100
    }
372 Kevin 101
    textData->moveCursor(QTextCursor::End);
102
    textData->ensureCursorVisible();
362 Kevin 103
}