diff --git a/CMakeLists.txt b/CMakeLists.txt index 13b0441..b476cd4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,12 @@ project(s20) add_definitions(-std=gnu++11) set(CMAKE_AUTOMOC TRUE) -find_package(Qt5 REQUIRED Core Network) +find_package(Qt5 REQUIRED Core Gui Widgets Network) + add_executable(s20 main.cpp socket.cpp consolereader.cpp server.cpp) target_link_libraries(s20 Qt5::Core Qt5::Network) + +include_directories( ${CMAKE_CURRENT_BINARY_DIR} ) +qt5_wrap_ui(UIS_HDRS dialog.ui) +add_executable(s20-gui main-gui.cpp dialog.cpp socket.cpp server.cpp ${UIS_HDRS}) +target_link_libraries(s20-gui Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Network) diff --git a/dialog.cpp b/dialog.cpp new file mode 100644 index 0000000..9a7d009 --- /dev/null +++ b/dialog.cpp @@ -0,0 +1,62 @@ +/************************************************************************* + * Copyright (C) 2015 by Andrius Štikonas * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#include + +#include "dialog.h" +#include "ui_dialog.h" + +Dialog::Dialog(std::vector *sockets_vector, QWidget *parent) : + QDialog(parent), + ui(new Ui::Dialog) +{ + sockets = sockets_vector; + ui->setupUi(this); + +} + +Dialog::~Dialog() +{ + delete ui; +} + +void Dialog::updateUi() +{ + for ( unsigned int i = 0; i < (*sockets).size(); ++i) + { + ui->toggleButton->setText((*sockets)[i]->powered ? QStringLiteral("Turn off") : QStringLiteral("Turn on")); + ui->comboBox->setItemText(i, (*sockets)[i]->socketName); + } +} + +void Dialog::discovered() +{ + ui->comboBox->clear(); + for ( std::vector::const_iterator i = sockets->begin() ; i != sockets->end(); ++i ) + { + connect(*i, &Socket::stateChanged, this, &Dialog::updateUi); + connect(ui->toggleButton, &QPushButton::clicked, this, &Dialog::togglePower); + ui->comboBox->addItem("Socket"); + } + + updateUi(); +} + +void Dialog::togglePower() +{ + (*sockets)[ui->comboBox->currentIndex()]->toggle(); +} diff --git a/dialog.h b/dialog.h new file mode 100644 index 0000000..89ecd74 --- /dev/null +++ b/dialog.h @@ -0,0 +1,46 @@ +/************************************************************************* + * Copyright (C) 2015 by Andrius Štikonas * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#include "socket.h" + +#ifndef DIALOG_H +#define DIALOG_H + +#include + +namespace Ui { +class Dialog; +} + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + explicit Dialog(std::vector *sockets_vector, QWidget *parent = 0); + ~Dialog(); + + void discovered(); + void updateUi(); + +private: + Ui::Dialog *ui; + std::vector *sockets; + void togglePower(); +}; + +#endif // DIALOG_H diff --git a/dialog.ui b/dialog.ui new file mode 100644 index 0000000..df90ce4 --- /dev/null +++ b/dialog.ui @@ -0,0 +1,62 @@ + + + Dialog + + + + 0 + 0 + 434 + 269 + + + + S20 socket manager + + + + + 300 + 200 + 84 + 33 + + + + PushButton + + + + + + 30 + 10 + 351 + 33 + + + + + + + Sockets: + + + + + + + + 250 + 0 + + + + + + + + + + + diff --git a/main-gui.cpp b/main-gui.cpp new file mode 100644 index 0000000..9d4992b --- /dev/null +++ b/main-gui.cpp @@ -0,0 +1,35 @@ +/************************************************************************* + * Copyright (C) 2015 by Andrius Štikonas * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#include +#include + +#include "dialog.h" +#include "server.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + std::vector *sockets = new std::vector; + Server *server = new Server(sockets); + Dialog *w = new Dialog(sockets); + QObject::connect(server, &Server::discovered, w, &Dialog::discovered); + w->show(); + + return app.exec(); +} diff --git a/s20-gui.pro b/s20-gui.pro new file mode 100644 index 0000000..eb7f562 --- /dev/null +++ b/s20-gui.pro @@ -0,0 +1,5 @@ +SOURCES = dialog.cpp main-gui.cpp server.cpp socket.cpp +HEADERS = dialog.h server.h socket.h +FORMS = dialog.ui +QT += network widgets gui +CONFIG += c++11