Subversion Repositories Code-Repo

Rev

Rev 362 | Rev 372 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "IOWidget.h"

IOWidget::IOWidget(QWidget *parent) : QWidget(parent)
{
    // Serial data UI
    textData = new QTextEdit();
    textData->setCurrentFont(QFont("Consolas", 8));
    textData->append("Waiting for serial connection...");
    textData->setMinimumSize(400, 100);
    textData->setReadOnly(true);
    textTransmit = new QLineEdit();
    textTransmit->setEnabled(false);
    textTransmit->setFont(QFont("Consolas", 8));
    btnTransmit = new QPushButton("&Send");
    btnTransmit->setEnabled(false);
    btnClear = new QPushButton("Clear");

    QHBoxLayout *transmitLayout = new QHBoxLayout();
    transmitLayout->addWidget(textTransmit);
    transmitLayout->addWidget(btnTransmit);
    transmitLayout->addWidget(btnClear);
    QVBoxLayout *dataLayout = new QVBoxLayout();
    dataLayout->addWidget(textData);
    dataLayout->addLayout(transmitLayout);
    setLayout(dataLayout);

    connect(btnTransmit, SIGNAL(clicked()), this, SLOT(PreprocessTransmit()));
    connect(textTransmit, SIGNAL(returnPressed()), btnTransmit, SIGNAL(clicked()));
    connect(btnClear, SIGNAL(clicked()), this, SLOT(ClearBtn()));

    EnableTransmit(false);
    lastTransmit = true;
}

IOWidget::~IOWidget()
{

}

void IOWidget::PreprocessTransmit(QByteArray data)
{
    QString str;
    if (data.isEmpty()) {
        str = textTransmit->text();
        textTransmit->setText("");
    } else {
        str = QString(data);
    }

    emit TransmitByteArray(str.toUtf8());
    textData->setTextColor(Qt::darkBlue);

    QStringList cmds = str.split('\n');
    for (int i = 0; i < cmds.size(); i++) {
        textData->append("TX:  " + cmds[i]);
    }

    // Save a flag indicating last activity was a transmit
    lastTransmit = true;
}

void IOWidget::ProcessReceivedByte(QByteArray data)
{
    textData->setTextColor(Qt::darkRed);
    data.replace("\n", "\nRX:  ");
    if (lastTransmit) {
        textData->insertPlainText("\nRX:  " + QString(data));
    } else {
        textData->insertPlainText(QString(data));
    }
    textData->moveCursor(QTextCursor::End);
    lastTransmit = false;
}

void IOWidget::ClearBtn()
{
    textData->clear();
}

void IOWidget::EnableTransmit(bool enable)
{
    if (enable) {
        textTransmit->setEnabled(true);
        btnTransmit->setEnabled(true);
    } else {
        textTransmit->setEnabled(false);
        btnTransmit->setEnabled(false);
    }
}