From 8e28c11212664099810a290d67e5f6c0b16045b0 Mon Sep 17 00:00:00 2001 From: Atsushi Watanabe Date: Sun, 8 Jun 2014 21:01:48 +0900 Subject: [PATCH] Initial commit --- main.cpp | 11 ++++ ypspur_gui.cpp | 167 ++++++++++++++++++++++++++++++++++++++++++++++ ypspur_gui.h | 57 ++++++++++++++++ ypspur_gui.pro | 22 +++++++ ypspur_gui.ui | 175 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 432 insertions(+) create mode 100644 main.cpp create mode 100644 ypspur_gui.cpp create mode 100644 ypspur_gui.h create mode 100644 ypspur_gui.pro create mode 100644 ypspur_gui.ui diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..26f7e42 --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "ypspur_gui.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + YPSpur_gui gui; + gui.show(); + + return app.exec(); +} diff --git a/ypspur_gui.cpp b/ypspur_gui.cpp new file mode 100644 index 0000000..3587cc2 --- /dev/null +++ b/ypspur_gui.cpp @@ -0,0 +1,167 @@ +#include "ypspur_gui.h" +#include "ui_ypspur_gui.h" + +#include +#include +#include +#include + + +YPSpur_gui::YPSpur_gui(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::YPSpur_gui), + settings(QString("ypspur")) +{ + ui->setupUi(this); + + port = settings.value("coordinator/port", "/dev/ttyACM0").toString(); + if(!port.isEmpty()) ui->portList->addItem(port); + + paramFile = settings.value("coordinator/param", "").toString(); + QString paramName = paramFile; + paramName.replace(QRegExp("^.*([^/\\\\]*)$"),"\\1"); + if(!paramFile.isEmpty()) ui->parameterName->setText(paramName); +} + +YPSpur_gui::~YPSpur_gui() +{ + settings.sync(); + delete ui; +} + + +void YPSpur_gui::on_coordinatorStart_toggled(bool checked) +{ + if(checked) + { + ui->parameterBrowse->setDisabled(true); + ui->portList->setDisabled(true); + connect(&coordinator, SIGNAL(readyReadStandardError()), this, SLOT(updateCoordinatorError())); + connect(&coordinator, SIGNAL(readyReadStandardOutput()), this, SLOT(updateCoordinatorText())); + connect(&interpreter, SIGNAL(readyReadStandardError()), this, SLOT(updateInterpreterError())); + connect(&interpreter, SIGNAL(readyReadStandardOutput()), this, SLOT(updateInterpreterText())); + + QStringList args; + args.append("--device"); + args.append(port); + args.append("--param"); + args.append(paramFile); + args.append("--update-param"); + coordinator.start("/usr/local/bin/ypspur-coordinator", args); + } + else + { + interpreter.close(); + coordinator.close(); + ui->parameterBrowse->setDisabled(false); + ui->portList->setDisabled(false); + + mutexCoordinatorOutput.lock(); + ui->coordinatorOut->textCursor().movePosition(QTextCursor::End); + ui->coordinatorOut->insertHtml("

"); + ui->coordinatorOut->verticalScrollBar()->setValue( + ui->coordinatorOut->verticalScrollBar()->maximum()); + mutexCoordinatorOutput.unlock(); + } +} + +void YPSpur_gui::updateCoordinatorError() +{ + mutexCoordinatorOutput.lock(); + QString data(coordinator.readAllStandardError() ); + + data.replace(QRegExp(" ")," "); + data.replace(QRegExp("\n"),"
"); + ui->coordinatorOut->textCursor().movePosition(QTextCursor::End); + ui->coordinatorOut->insertHtml("" + data + ""); + ui->coordinatorOut->verticalScrollBar()->setValue( + ui->coordinatorOut->verticalScrollBar()->maximum()); + mutexCoordinatorOutput.unlock(); + if(data.contains("Command analyser started.")) + { + interpreter.start("/usr/local/bin/ypspur-interpreter"); + } +} + +void YPSpur_gui::updateCoordinatorText() +{ + mutexCoordinatorOutput.lock(); + QString data(coordinator.readAllStandardOutput()); + + data.replace(QRegExp(" ")," "); + data.replace(QRegExp("\n"),"
"); + ui->coordinatorOut->textCursor().movePosition(QTextCursor::End); + ui->coordinatorOut->insertHtml(data); + ui->coordinatorOut->verticalScrollBar()->setValue( + ui->coordinatorOut->verticalScrollBar()->maximum()); + mutexCoordinatorOutput.unlock(); +} + +void YPSpur_gui::updateInterpreterError() +{ + mutexInterpreterOutput.lock(); + QString data(interpreter.readAllStandardError() ); + + data.replace(QRegExp(" ")," "); + data.replace(QRegExp("\n"),"
"); + ui->interpreterOut->textCursor().movePosition(QTextCursor::End); + ui->interpreterOut->insertHtml("" + data + ""); + ui->interpreterOut->verticalScrollBar()->setValue( + ui->interpreterOut->verticalScrollBar()->maximum()); + mutexInterpreterOutput.unlock(); +} + +void YPSpur_gui::updateInterpreterText() +{ + mutexInterpreterOutput.lock(); + QString data(interpreter.readAllStandardOutput()); + + data.replace(QRegExp(" ")," "); + data.replace(QRegExp("\n"),"
"); + ui->interpreterOut->textCursor().movePosition(QTextCursor::End); + ui->interpreterOut->insertHtml(data); + ui->interpreterOut->verticalScrollBar()->setValue( + ui->interpreterOut->verticalScrollBar()->maximum()); + mutexInterpreterOutput.unlock(); +} + +void YPSpur_gui::on_parameterBrowse_clicked() +{ + QString defaultPath("./"); + if(!paramFile.isEmpty()) + { + defaultPath = paramFile; + } + QString fileName = QFileDialog::getOpenFileName( + this, + QString("Open Image"), + defaultPath, + QString("Parameter files (*.param)")); + if(!fileName.isEmpty()) + { + paramFile = fileName; + + QString paramName = fileName; + paramName.replace(QRegExp("^.*([^/\\\\]*)$"),"\\1"); + ui->parameterName->setText(paramName); + + settings.setValue("coordinator/param", paramFile); + } +} + +void YPSpur_gui::on_portList_textChanged(const QString &arg1) +{ + if(!arg1.isNull()) + { + port = arg1; + settings.setValue("coordinator/port", port); + } +} + +void YPSpur_gui::on_interpreterCommand_returnPressed() +{ + QString str = ui->interpreterCommand->text(); + str.append("\n"); + interpreter.write(str.toLocal8Bit()); + ui->interpreterCommand->setText(""); +} diff --git a/ypspur_gui.h b/ypspur_gui.h new file mode 100644 index 0000000..2d02b9f --- /dev/null +++ b/ypspur_gui.h @@ -0,0 +1,57 @@ +#ifndef YPSPUR_GUI_H +#define YPSPUR_GUI_H + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace Ui { +class YPSpur_gui; +} + + +class YPSpur_gui : public QMainWindow +{ + Q_OBJECT + +public: + explicit YPSpur_gui(QWidget *parent = 0); + ~YPSpur_gui(); + +private slots: + void on_coordinatorStart_toggled(bool checked); + void updateCoordinatorError(); + void updateCoordinatorText(); + void updateInterpreterError(); + void updateInterpreterText(); + + void on_parameterBrowse_clicked(); + + void on_portList_textChanged(const QString &arg1); + + void on_interpreterCommand_returnPressed(); + +private: + Ui::YPSpur_gui *ui; + + QProcess interpreter; + QProcess coordinator; + QMutex mutexInterpreterOutput; + QMutex mutexCoordinatorOutput; + + QString paramFile; + QString port; + + QSettings settings; +}; + +#endif // YPSPUR_GUI_H diff --git a/ypspur_gui.pro b/ypspur_gui.pro new file mode 100644 index 0000000..94bcb6a --- /dev/null +++ b/ypspur_gui.pro @@ -0,0 +1,22 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2014-06-08T15:22:02 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = ypspur_gui +TEMPLATE = app + + +SOURCES += main.cpp \ + ypspur_gui.cpp + +HEADERS += \ + ypspur_gui.h + +FORMS += \ + ypspur_gui.ui diff --git a/ypspur_gui.ui b/ypspur_gui.ui new file mode 100644 index 0000000..7555a88 --- /dev/null +++ b/ypspur_gui.ui @@ -0,0 +1,175 @@ + + + YPSpur_gui + + + + 0 + 0 + 640 + 320 + + + + + 65535 + 65535 + + + + YP-Spur coordinator + + + + + 65535 + 65535 + + + + false + + + + + + + 0 + 0 + + + + + 320 + 240 + + + + + 65535 + 65535 + + + + 0 + + + + &Coordinator + + + + + + + + Qt::LeftToRight + + + Parameter file + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + &Browse + + + + + + + Port + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + true + + + + + + + + + &Start / Stop + + + true + + + false + + + + + + + false + + + QFrame::WinPanel + + + Qt::ScrollBarAlwaysOn + + + true + + + + + + + + &Interpreter + + + + + + QFrame::WinPanel + + + QFrame::Sunken + + + Qt::ScrollBarAlwaysOn + + + true + + + + + + + Command + + + + + + + + + + + + &Quit + + + + + +