Subversion Repositories Code-Repo

Rev

Rev 344 | Blame | Last modification | View Log | RSS feed

#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    // Set central widget
    centralWidget = new QWidget();
    setCentralWidget(centralWidget);

    // Serial connection UI
    groupSerialInit = new QGroupBox("Initialization");
    btnSerialConnect = new QPushButton("&Connect");
    btnSerialRefresh = new QPushButton("&Refresh");
    labelSerialPort = new QLabel("Port:");
    labelSerialSpeed = new QLabel("Baud Rate:");
    cboxSerialPort = new QComboBox();
    cboxSerialSpeed = new QComboBox();

    QHBoxLayout *serialInitLayout = new QHBoxLayout();
    serialInitLayout->addWidget(btnSerialConnect);
    serialInitLayout->addWidget(labelSerialPort);
    serialInitLayout->addWidget(cboxSerialPort);
    serialInitLayout->addWidget(labelSerialSpeed);
    serialInitLayout->addWidget(cboxSerialSpeed);
    serialInitLayout->addWidget(btnSerialRefresh);
    serialInitLayout->addStretch();
    groupSerialInit->setLayout(serialInitLayout);
    groupSerialInit->setFixedSize(groupSerialInit->sizeHint());


    // Serial data UI
    groupSerialData = new QGroupBox("Data");
    textSerialData = new QTextEdit();
    textSerialData->setCurrentFont(QFont("Consolas", 8));
    textSerialData->append("Waiting for serial connection...");
    textSerialData->setMinimumHeight(100);
    textSerialData->setReadOnly(true);
    textSerialTransmit = new QLineEdit();
    textSerialTransmit->setFont(QFont("Consolas", 8));
    btnSerialTransmit = new QPushButton("&Send");
    btnSerialClear = new QPushButton("Clear");

    QHBoxLayout *serialTransmitLayout = new QHBoxLayout();
    serialTransmitLayout->addWidget(textSerialTransmit);
    serialTransmitLayout->addWidget(btnSerialTransmit);
    serialTransmitLayout->addWidget(btnSerialClear);
    QVBoxLayout *serialDataLayout = new QVBoxLayout();
    serialDataLayout->addWidget(textSerialData);
    serialDataLayout->addLayout(serialTransmitLayout);
    groupSerialData->setLayout(serialDataLayout);


    // Macro controller
    groupMacro = new QGroupBox("Other");
    btnMacro = new QPushButton("&Macros");
    QHBoxLayout *macroLayout = new QHBoxLayout();
    macroLayout->addWidget(btnMacro);
    groupMacro->setLayout(macroLayout);
    groupMacro->setFixedSize(groupMacro->sizeHint());


    // Main layout
    QGridLayout *mainLayout = new QGridLayout();
    mainLayout->addWidget(groupSerialInit, 0, 0, 1, 1, Qt::AlignLeft);
    mainLayout->addWidget(groupMacro, 0, 1, 1, 1, Qt::AlignLeft);
    mainLayout->addWidget(groupSerialData, 1, 0, 1, 2);
    mainLayout->setColumnStretch(0, 0);
    mainLayout->setColumnStretch(1, 1);
    centralWidget->setLayout(mainLayout);

    // Initialize thread for connection to serial port
    serialThread = new QThread();
    serialController = new SerialController();
    serialController->moveToThread(serialThread);
    connect(this, SIGNAL(Serial_QueryParameters()), serialController, SLOT(Serial_QueryParameters()));
    connect(this, SIGNAL(Serial_Connect(QString,QString)), serialController, SLOT(Serial_Connect(QString,QString)));
    connect(this, SIGNAL(Serial_Disconnect()), serialController, SLOT(Serial_Disconnect()));
    connect(this, SIGNAL(Serial_TransmitString(QString)), serialController, SLOT(Serial_TransmitString(QString)));
    connect(serialController, SIGNAL(Serial_UpdateParameters(QStringList,QStringList)), this, SLOT(Serial_UpdateParameters(QStringList,QStringList)));
    connect(serialController, SIGNAL(Serial_ReceivedString(QString)), this, SLOT(Serial_ReceivedString(QString)));
    connect(serialController, SIGNAL(Serial_Connected()), this, SLOT(Serial_Connected()));
    connect(serialController, SIGNAL(Serial_Disconnected()), this, SLOT(Serial_Disconnected()));
    connect(serialThread, SIGNAL(finished()), serialController, SLOT(deleteLater()));
    serialThread->start();

    // Initialize macro widget
    macroController = new MacroController();
    connect(macroController, SIGNAL(Macro_TransmitText(QString)), this, SLOT(Serial_PrepareTransmit(QString)));
    connect(serialController, SIGNAL(Serial_Connected()), macroController, SLOT(Macro_EnableTransmit()));
    connect(serialController, SIGNAL(Serial_Disconnected()), macroController, SLOT(Macro_DisableTransmit()));

    // Connect local widgets
    connect(btnSerialConnect, SIGNAL(clicked()), this, SLOT(Serial_ConnectToggleBtn()));
    connect(btnSerialRefresh, SIGNAL(clicked()), this, SIGNAL(Serial_QueryParameters()));
    connect(btnSerialTransmit, SIGNAL(clicked()), this, SLOT(Serial_PrepareTransmit()));
    connect(textSerialTransmit, SIGNAL(returnPressed()), btnSerialTransmit, SIGNAL(clicked()));
    connect(btnSerialClear, SIGNAL(clicked()), this, SLOT(Serial_ClearBtn()));
    connect(btnMacro, SIGNAL(clicked()), this, SLOT(Macro_ShowWindowBtn()));

    emit Serial_QueryParameters();
    Serial_Disconnected();

    setWindowTitle("Marlin Controller");
}

MainWindow::~MainWindow()
{
    serialThread->quit();
}

void MainWindow::Serial_ConnectToggleBtn()
{
    if (serialController->connected) {
        emit Serial_Disconnect();
    } else {
        if (cboxSerialPort->currentText() != "" && cboxSerialSpeed->currentText() != "") {
            emit Serial_Connect(cboxSerialPort->currentText(), cboxSerialSpeed->currentText());
        }
    }
}

void MainWindow::Serial_PrepareTransmit(QString string)
{
    if (string == "") {
        string = textSerialTransmit->text();
        textSerialTransmit->setText("");
    }
    emit Serial_TransmitString(string);
    textSerialData->setTextColor(Qt::darkBlue);

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

void MainWindow::Serial_ClearBtn()
{
    textSerialData->clear();
}

void MainWindow::Macro_ShowWindowBtn()
{
    macroController->show();
}

void MainWindow::Serial_UpdateParameters(QStringList ports, QStringList speeds)
{
    cboxSerialPort->clear();
    cboxSerialPort->addItems(ports);

    cboxSerialSpeed->clear();
    cboxSerialSpeed->addItems(speeds);
}

void MainWindow::Serial_ReceivedString(QString string)
{
    textSerialData->setTextColor(Qt::darkRed);
    textSerialData->append("RX:  " + string);
}

void MainWindow::Serial_Connected()
{
    btnSerialConnect->setText("&Disconnect");
    cboxSerialPort->setEnabled(false);
    cboxSerialSpeed->setEnabled(false);
    btnSerialRefresh->setEnabled(false);
    textSerialTransmit->setEnabled(true);
    btnSerialTransmit->setEnabled(true);
}

void MainWindow::Serial_Disconnected()
{
    btnSerialConnect->setText("&Connect");
    cboxSerialPort->setEnabled(true);
    cboxSerialSpeed->setEnabled(true);
    btnSerialRefresh->setEnabled(true);
    textSerialTransmit->setEnabled(false);
    btnSerialTransmit->setEnabled(false);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    macroController->close();
    event->accept();
}