Browse Source

Add some basic graphical user interface.

master
Andrius Štikonas 4 years ago
parent
commit
fb36cfaca2
6 changed files with 217 additions and 1 deletions
  1. 7
    1
      CMakeLists.txt
  2. 62
    0
      dialog.cpp
  3. 46
    0
      dialog.h
  4. 62
    0
      dialog.ui
  5. 35
    0
      main-gui.cpp
  6. 5
    0
      s20-gui.pro

+ 7
- 1
CMakeLists.txt View File

@@ -4,6 +4,12 @@ project(s20)
4 4
 add_definitions(-std=gnu++11)
5 5
 set(CMAKE_AUTOMOC TRUE)
6 6
 
7
-find_package(Qt5 REQUIRED Core Network)
7
+find_package(Qt5 REQUIRED Core Gui Widgets Network)
8
+
8 9
 add_executable(s20 main.cpp socket.cpp consolereader.cpp server.cpp)
9 10
 target_link_libraries(s20 Qt5::Core Qt5::Network)
11
+
12
+include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
13
+qt5_wrap_ui(UIS_HDRS dialog.ui)
14
+add_executable(s20-gui main-gui.cpp dialog.cpp socket.cpp server.cpp ${UIS_HDRS})
15
+target_link_libraries(s20-gui Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Network)

+ 62
- 0
dialog.cpp View File

@@ -0,0 +1,62 @@
1
+/*************************************************************************
2
+ *  Copyright (C) 2015 by Andrius Štikonas <andrius@stikonas.eu>         *
3
+ *                                                                       *
4
+ *  This program is free software; you can redistribute it and/or modify *
5
+ *  it under the terms of the GNU General Public License as published by *
6
+ *  the Free Software Foundation; either version 3 of the License, or    *
7
+ *  (at your option) any later version.                                  *
8
+ *                                                                       *
9
+ *  This program is distributed in the hope that it will be useful,      *
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of       *
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
12
+ *  GNU General Public License for more details.                         *
13
+ *                                                                       *
14
+ *  You should have received a copy of the GNU General Public License    *
15
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.*
16
+ *************************************************************************/
17
+
18
+#include <QComboBox>
19
+
20
+#include "dialog.h"
21
+#include "ui_dialog.h"
22
+
23
+Dialog::Dialog(std::vector<Socket*> *sockets_vector, QWidget *parent) :
24
+    QDialog(parent),
25
+    ui(new Ui::Dialog)
26
+{
27
+    sockets = sockets_vector;
28
+    ui->setupUi(this);
29
+
30
+}
31
+
32
+Dialog::~Dialog()
33
+{
34
+    delete ui;
35
+}
36
+
37
+void Dialog::updateUi()
38
+{
39
+    for ( unsigned int i = 0; i < (*sockets).size(); ++i)
40
+    {
41
+        ui->toggleButton->setText((*sockets)[i]->powered ? QStringLiteral("Turn off") : QStringLiteral("Turn on"));
42
+        ui->comboBox->setItemText(i, (*sockets)[i]->socketName);
43
+    }
44
+}
45
+
46
+void Dialog::discovered()
47
+{
48
+    ui->comboBox->clear();
49
+    for ( std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i )
50
+    {
51
+        connect(*i, &Socket::stateChanged, this, &Dialog::updateUi);
52
+        connect(ui->toggleButton, &QPushButton::clicked, this, &Dialog::togglePower);
53
+        ui->comboBox->addItem("Socket");
54
+    }
55
+    
56
+    updateUi();
57
+}
58
+
59
+void Dialog::togglePower()
60
+{
61
+    (*sockets)[ui->comboBox->currentIndex()]->toggle();
62
+}

+ 46
- 0
dialog.h View File

@@ -0,0 +1,46 @@
1
+/*************************************************************************
2
+ *  Copyright (C) 2015 by Andrius Štikonas <andrius@stikonas.eu>         *
3
+ *                                                                       *
4
+ *  This program is free software; you can redistribute it and/or modify *
5
+ *  it under the terms of the GNU General Public License as published by *
6
+ *  the Free Software Foundation; either version 3 of the License, or    *
7
+ *  (at your option) any later version.                                  *
8
+ *                                                                       *
9
+ *  This program is distributed in the hope that it will be useful,      *
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of       *
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
12
+ *  GNU General Public License for more details.                         *
13
+ *                                                                       *
14
+ *  You should have received a copy of the GNU General Public License    *
15
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.*
16
+ *************************************************************************/
17
+
18
+#include "socket.h"
19
+
20
+#ifndef DIALOG_H
21
+#define DIALOG_H
22
+
23
+#include <QDialog>
24
+
25
+namespace Ui {
26
+class Dialog;
27
+}
28
+
29
+class Dialog : public QDialog
30
+{
31
+    Q_OBJECT
32
+
33
+public:
34
+    explicit Dialog(std::vector<Socket*> *sockets_vector, QWidget *parent = 0);
35
+    ~Dialog();
36
+
37
+    void discovered();
38
+    void updateUi();
39
+
40
+private:
41
+    Ui::Dialog *ui;
42
+    std::vector<Socket*> *sockets;
43
+    void togglePower();
44
+};
45
+
46
+#endif // DIALOG_H

+ 62
- 0
dialog.ui View File

@@ -0,0 +1,62 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<ui version="4.0">
3
+ <class>Dialog</class>
4
+ <widget class="QDialog" name="Dialog">
5
+  <property name="geometry">
6
+   <rect>
7
+    <x>0</x>
8
+    <y>0</y>
9
+    <width>434</width>
10
+    <height>269</height>
11
+   </rect>
12
+  </property>
13
+  <property name="windowTitle">
14
+   <string>S20 socket manager</string>
15
+  </property>
16
+  <widget class="QPushButton" name="toggleButton">
17
+   <property name="geometry">
18
+    <rect>
19
+     <x>300</x>
20
+     <y>200</y>
21
+     <width>84</width>
22
+     <height>33</height>
23
+    </rect>
24
+   </property>
25
+   <property name="text">
26
+    <string>PushButton</string>
27
+   </property>
28
+  </widget>
29
+  <widget class="QWidget" name="">
30
+   <property name="geometry">
31
+    <rect>
32
+     <x>30</x>
33
+     <y>10</y>
34
+     <width>351</width>
35
+     <height>33</height>
36
+    </rect>
37
+   </property>
38
+   <layout class="QHBoxLayout" name="horizontalLayout">
39
+    <item>
40
+     <widget class="QLabel" name="label">
41
+      <property name="text">
42
+       <string>Sockets:</string>
43
+      </property>
44
+     </widget>
45
+    </item>
46
+    <item>
47
+     <widget class="QComboBox" name="comboBox">
48
+      <property name="minimumSize">
49
+       <size>
50
+        <width>250</width>
51
+        <height>0</height>
52
+       </size>
53
+      </property>
54
+     </widget>
55
+    </item>
56
+   </layout>
57
+  </widget>
58
+ </widget>
59
+ <layoutdefault spacing="6" margin="11"/>
60
+ <resources/>
61
+ <connections/>
62
+</ui>

+ 35
- 0
main-gui.cpp View File

@@ -0,0 +1,35 @@
1
+/*************************************************************************
2
+ *  Copyright (C) 2015 by Andrius Štikonas <andrius@stikonas.eu>         *
3
+ *                                                                       *
4
+ *  This program is free software; you can redistribute it and/or modify *
5
+ *  it under the terms of the GNU General Public License as published by *
6
+ *  the Free Software Foundation; either version 3 of the License, or    *
7
+ *  (at your option) any later version.                                  *
8
+ *                                                                       *
9
+ *  This program is distributed in the hope that it will be useful,      *
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of       *
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
12
+ *  GNU General Public License for more details.                         *
13
+ *                                                                       *
14
+ *  You should have received a copy of the GNU General Public License    *
15
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.*
16
+ *************************************************************************/
17
+
18
+#include <QApplication>
19
+#include <vector>
20
+
21
+#include "dialog.h"
22
+#include "server.h"
23
+
24
+int main(int argc, char *argv[])
25
+{
26
+    QApplication app(argc, argv);
27
+
28
+    std::vector<Socket*> *sockets = new std::vector<Socket*>;
29
+    Server *server = new Server(sockets);
30
+    Dialog *w = new Dialog(sockets);
31
+    QObject::connect(server, &Server::discovered, w, &Dialog::discovered);
32
+    w->show();
33
+
34
+    return app.exec();
35
+}

+ 5
- 0
s20-gui.pro View File

@@ -0,0 +1,5 @@
1
+SOURCES = dialog.cpp main-gui.cpp server.cpp socket.cpp
2
+HEADERS = dialog.h  server.h  socket.h
3
+FORMS = dialog.ui
4
+QT += network widgets gui
5
+CONFIG += c++11

Loading…
Cancel
Save