Subversion Repositories Code-Repo

Rev

Rev 347 | 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("Serial Port:");
    labelSerialSpeed = new QLabel("Baud Rate:");
    labelSerialDataBits = new QLabel("Data:");
    labelSerialStopBits = new QLabel("Stop:");
    labelSerialParity = new QLabel("Parity Bit:");
    labelSerialFlowControl = new QLabel("Flow Control:");
    cboxSerialPort = new QComboBox();
    cboxSerialPort->setMinimumWidth(80);
    cboxSerialSpeed = new QComboBox();
    cboxSerialSpeed->setEditable(true);
    cboxSerialSpeed->setValidator(new QIntValidator(0, 100000000, this));
    cboxSerialDataBits = new QComboBox();
    cboxSerialDataBits->setMaximumWidth(60);
    cboxSerialStopBits = new QComboBox();
    cboxSerialStopBits->setMaximumWidth(60);
    cboxSerialParity = new QComboBox();
    cboxSerialFlowControl = new QComboBox();
    cboxSerialFlowControl->setMinimumWidth(70);

    QGridLayout *serialSettingsLayout = new QGridLayout();
    serialSettingsLayout->addWidget(btnSerialConnect, 0, 0);
    serialSettingsLayout->addWidget(labelSerialPort, 0, 1);
    serialSettingsLayout->addWidget(cboxSerialPort, 0, 2);
    serialSettingsLayout->addWidget(labelSerialDataBits, 0, 3);
    serialSettingsLayout->addWidget(cboxSerialDataBits, 0, 4);
    serialSettingsLayout->addWidget(labelSerialParity, 0, 5);
    serialSettingsLayout->addWidget(cboxSerialParity, 0, 6);

    serialSettingsLayout->addWidget(btnSerialRefresh, 1, 0);
    serialSettingsLayout->addWidget(labelSerialSpeed, 1, 1);
    serialSettingsLayout->addWidget(cboxSerialSpeed, 1, 2);
    serialSettingsLayout->addWidget(labelSerialStopBits, 1, 3);
    serialSettingsLayout->addWidget(cboxSerialStopBits, 1, 4);
    serialSettingsLayout->addWidget(labelSerialFlowControl, 1, 5);
    serialSettingsLayout->addWidget(cboxSerialFlowControl, 1, 6);

    groupSerialInit->setLayout(serialSettingsLayout);
    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,QString,QString,QString,QString)),
            serialController, SLOT(Serial_Connect(QString,QString,QString,QString,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,QStringList,QStringList,QStringList,QStringList)),
            this, SLOT(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,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
    macroDockWidget = new QDockWidget("Macro Controller", this);
    macroController = new MacroController(macroDockWidget);
    macroDockWidget->setWidget(macroController);
    macroDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    macroDockWidget->hide();
    addDockWidget(Qt::RightDockWidgetArea, macroDockWidget);
    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()), macroDockWidget->toggleViewAction(), SLOT(trigger()));

    emit Serial_QueryParameters();
    Serial_Disconnected();

    setWindowTitle("Marlin Controller");
    setWindowIcon(QIcon(":/External/Resources/Icon_16.bmp"));
}

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(), cboxSerialDataBits->currentText(),
                                cboxSerialStopBits->currentText(), cboxSerialParity->currentText(), cboxSerialFlowControl->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::Serial_UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
                                     QStringList stopBits, QStringList parity, QStringList flowControl)
{
    QString currPort = cboxSerialPort->currentText();
    cboxSerialPort->clear();
    cboxSerialPort->addItems(ports);
    if (currPort != "" && ports.contains(currPort))
        cboxSerialPort->setCurrentText(currPort);

    QString currSpeed = cboxSerialSpeed->currentText();
    cboxSerialSpeed->clear();
    cboxSerialSpeed->addItems(speeds);
    if (currSpeed != "") cboxSerialSpeed->setCurrentText(currSpeed);

    int currData = cboxSerialDataBits->currentIndex();
    cboxSerialDataBits->clear();
    cboxSerialDataBits->addItems(dataBits);
    if (currData >= 0) cboxSerialDataBits->setCurrentIndex(currData);

    int currStop = cboxSerialStopBits->currentIndex();
    cboxSerialStopBits->clear();
    cboxSerialStopBits->addItems(stopBits);
    if (currStop >= 0) cboxSerialStopBits->setCurrentIndex(currStop);

    int currParity = cboxSerialParity->currentIndex();
    cboxSerialParity->clear();
    cboxSerialParity->addItems(parity);
    if (currParity >= 0) cboxSerialParity->setCurrentIndex(currParity);

    int currFlow = cboxSerialFlowControl->currentIndex();
    cboxSerialFlowControl->clear();
    cboxSerialFlowControl->addItems(flowControl);
    if (currFlow >= 0) cboxSerialFlowControl->setCurrentIndex(currFlow);
}

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);
}